Skip to content

Commit

Permalink
fix: CurrencyController (dashpay#12)
Browse files Browse the repository at this point in the history
* update CurrencyController to use DashCentral API

* update package dependencies
  • Loading branch information
snogcel authored Feb 1, 2017
1 parent adad308 commit 19a7c6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
22 changes: 12 additions & 10 deletions lib/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ function CurrencyController(options) {
this.node = options.node;
var refresh = options.currencyRefresh || CurrencyController.DEFAULT_CURRENCY_DELAY;
this.currencyDelay = refresh * 60000;
this.bitstampRate = 0;
this.exchange_rates = {
dash_usd: 0.00,
btc_usd: 0.00,
btc_dash: 0.00
};
this.timestamp = Date.now();
}

Expand All @@ -15,28 +19,26 @@ CurrencyController.DEFAULT_CURRENCY_DELAY = 10;
CurrencyController.prototype.index = function(req, res) {
var self = this;
var currentTime = Date.now();
if (self.bitstampRate === 0 || currentTime >= (self.timestamp + self.currencyDelay)) {
if (self.exchange_rates.dash_usd === 0.00 || currentTime >= (self.timestamp + self.currencyDelay)) {
self.timestamp = currentTime;
request('https://www.bitstamp.net/api/ticker/', function(err, response, body) {
request('https://www.dashcentral.org/api/v1/public', function(err, response, body) {
if (err) {
self.node.log.error(err);
}
if (!err && response.statusCode === 200) {
self.bitstampRate = parseFloat(JSON.parse(body).last);
var response = JSON.parse(body);
self.exchange_rates = response.exchange_rates;
self.exchange_rates.bitstamp = response.exchange_rates.dash_usd; // backwards compatibility
}
res.jsonp({
status: 200,
data: {
bitstamp: self.bitstampRate
}
data: self.exchange_rates
});
});
} else {
res.jsonp({
status: 200,
data: {
bitstamp: self.bitstampRate
}
data: self.exchange_rates
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"bitcoreNode": "lib",
"dependencies": {
"async": "*",
"bitcore-lib-dash": "^0.13.19",
"bitcore-lib-dash": "^0.13.20",
"bitcore-message-dash": "^1.0.5",
"body-parser": "^1.13.3",
"compression": "^1.6.1",
Expand Down
62 changes: 40 additions & 22 deletions test/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ var CurrencyController = require('../lib/currency');

describe('Currency', function() {

var bitstampData = {
high: 239.44,
last: 237.90,
timestamp: 1443798711,
bid: 237.61,
vwap: 237.88,
volume: 21463.27736401,
low: 235.00,
ask: 237.90
var dashCentralData = {
general: {
consensus_blockheight: 561311,
consensus_version: 120058,
consensus_protocolversion: 70103,
all_user: 687,
active_user: 372,
registered_masternodes: 1583,
registered_masternodes_verified: 770
},
exchange_rates: {
dash_usd: 9.4858840414,
btc_usd: 682.93,
btc_dash: 0.01388998
}
};

it.skip('will make live request to bitstamp', function(done) {
it.skip('will make live request to dash central', function(done) {
var currency = new CurrencyController({});
var req = {};
var res = {
jsonp: function(response) {
response.status.should.equal(200);
should.exist(response.data.bitstamp);
(typeof response.data.bitstamp).should.equal('number');
should.exist(response.data.dash_usd);
(typeof response.data.dash_usd).should.equal('number');
done();
}
};
Expand All @@ -34,22 +40,26 @@ describe('Currency', function() {

it('will retrieve a fresh value', function(done) {
var TestCurrencyController = proxyquire('../lib/currency', {
request: sinon.stub().callsArgWith(1, null, {statusCode: 200}, JSON.stringify(bitstampData))
request: sinon.stub().callsArgWith(1, null, {statusCode: 200}, JSON.stringify(dashCentralData))
});
var node = {
log: {
error: sinon.stub()
}
};
var currency = new TestCurrencyController({node: node});
currency.bitstampRate = 220.20;
currency.exchange_rates = {
dash_usd: 9.4858840414,
btc_usd: 682.93,
btc_dash: 0.01388998
};
currency.timestamp = Date.now() - 61000 * CurrencyController.DEFAULT_CURRENCY_DELAY;
var req = {};
var res = {
jsonp: function(response) {
response.status.should.equal(200);
should.exist(response.data.bitstamp);
response.data.bitstamp.should.equal(237.90);
should.exist(response.data.dash_usd);
response.data.dash_usd.should.equal(9.4858840414);
done();
}
};
Expand All @@ -66,14 +76,18 @@ describe('Currency', function() {
}
};
var currency = new TestCurrencyController({node: node});
currency.bitstampRate = 237.90;
currency.exchange_rates = {
dash_usd: 9.4858840414,
btc_usd: 682.93,
btc_dash: 0.01388998
};
currency.timestamp = Date.now() - 65000 * CurrencyController.DEFAULT_CURRENCY_DELAY;
var req = {};
var res = {
jsonp: function(response) {
response.status.should.equal(200);
should.exist(response.data.bitstamp);
response.data.bitstamp.should.equal(237.90);
should.exist(response.data);
response.data.dash_usd.should.equal(9.4858840414);
node.log.error.callCount.should.equal(1);
done();
}
Expand All @@ -92,14 +106,18 @@ describe('Currency', function() {
}
};
var currency = new TestCurrencyController({node: node});
currency.bitstampRate = 237.90;
currency.exchange_rates = {
dash_usd: 9.4858840414,
btc_usd: 682.93,
btc_dash: 0.01388998
};
currency.timestamp = Date.now();
var req = {};
var res = {
jsonp: function(response) {
response.status.should.equal(200);
should.exist(response.data.bitstamp);
response.data.bitstamp.should.equal(237.90);
should.exist(response.data.dash_usd);
response.data.dash_usd.should.equal(9.4858840414);
request.callCount.should.equal(0);
done();
}
Expand Down

0 comments on commit 19a7c6e

Please sign in to comment.