diff --git a/lib/currency.js b/lib/currency.js index 0c4a4e319..68e55df77 100644 --- a/lib/currency.js +++ b/lib/currency.js @@ -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(); } @@ -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 }); } diff --git a/package.json b/package.json index bf25e4e1d..76bf9d6bf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/currency.js b/test/currency.js index 6437e2cbf..227938081 100644 --- a/test/currency.js +++ b/test/currency.js @@ -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(); } }; @@ -34,7 +40,7 @@ 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: { @@ -42,14 +48,18 @@ describe('Currency', function() { } }; 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(); } }; @@ -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(); } @@ -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(); }