add BCH to rateService
This commit is contained in:
parent
5175e7e2c4
commit
09d1d5d3ff
1 changed files with 64 additions and 20 deletions
|
|
@ -25,9 +25,10 @@ var RateService = function(opts) {
|
|||
self._isAvailable = false;
|
||||
self._rates = {};
|
||||
self._alternatives = [];
|
||||
self._ratesBCH = {};
|
||||
self._queued = [];
|
||||
|
||||
self._fetchCurrencies();
|
||||
self.updateRates();
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -39,14 +40,20 @@ RateService.singleton = function(opts) {
|
|||
return _instance;
|
||||
};
|
||||
|
||||
RateService.prototype._fetchCurrencies = function() {
|
||||
RateService.prototype.updateRates = function() {
|
||||
console.log('[rateService.js.44:updateRates:]'); //TODO
|
||||
var self = this;
|
||||
|
||||
var backoffSeconds = 5;
|
||||
var updateFrequencySeconds = 5 * 60;
|
||||
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
||||
var bchRateServiceUrl = 'https://api.kraken.com/0/public/Ticker?pair=BCHUSD,BCHEUR';
|
||||
|
||||
|
||||
function getBTC(cb, tries) {
|
||||
tries = tries || 0;
|
||||
if (tries > 5) return cb('could not get BTC rates');
|
||||
|
||||
var retrieve = function() {
|
||||
//log.info('Fetching exchange rates');
|
||||
self.httprequest.get(rateServiceUrl).success(function(res) {
|
||||
self.lodash.each(res, function(currency) {
|
||||
|
|
@ -57,27 +64,64 @@ RateService.prototype._fetchCurrencies = function() {
|
|||
rate: currency.rate
|
||||
});
|
||||
});
|
||||
|
||||
return cb();
|
||||
}).error(function() {
|
||||
//log.debug('Error fetching exchange rates', err);
|
||||
setTimeout(function() {
|
||||
backoffSeconds *= 1.5;
|
||||
getBTC(cb, tries++);
|
||||
}, backoffSeconds * 1000);
|
||||
return;
|
||||
})
|
||||
}
|
||||
|
||||
function getBCH(cb, tries) {
|
||||
tries = tries || 0;
|
||||
if (tries > 5) return cb('could not get BCH rates');
|
||||
|
||||
function retry(tries) {
|
||||
console.log('[rateService.js.84:retry:] BCH'); //TODO
|
||||
//log.debug('Error fetching exchange rates', err);
|
||||
setTimeout(function() {
|
||||
backoffSeconds *= 1.5;
|
||||
getBTC(cb, tries++);
|
||||
}, backoffSeconds * 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
self.httprequest.get(bchRateServiceUrl).success(function(res) {
|
||||
self.lodash.each(res.result, function(data, paircode) {
|
||||
var code = paircode.substr(3,3);
|
||||
var rate =data.c[0];
|
||||
self._ratesBCH[code] = rate;
|
||||
})
|
||||
return cb();
|
||||
}).error(function() {
|
||||
return retry(tries);
|
||||
})
|
||||
}
|
||||
|
||||
getBTC(function(err) {
|
||||
if (err) return;
|
||||
getBCH(function(err) {
|
||||
if (err) return;
|
||||
|
||||
self._isAvailable = true;
|
||||
self.lodash.each(self._queued, function(callback) {
|
||||
setTimeout(callback, 1);
|
||||
});
|
||||
setTimeout(retrieve, updateFrequencySeconds * 1000);
|
||||
}).error(function(err) {
|
||||
//log.debug('Error fetching exchange rates', err);
|
||||
setTimeout(function() {
|
||||
backoffSeconds *= 1.5;
|
||||
retrieve();
|
||||
}, backoffSeconds * 1000);
|
||||
return;
|
||||
});
|
||||
setTimeout( self.updateRates , updateFrequencySeconds * 1000);
|
||||
})
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
retrieve();
|
||||
};
|
||||
|
||||
RateService.prototype.getRate = function(code) {
|
||||
return this._rates[code];
|
||||
RateService.prototype.getRate = function(code, chain) {
|
||||
if (chain = 'bch')
|
||||
return this._ratesBCH[code];
|
||||
else
|
||||
return this._rates[code];
|
||||
};
|
||||
|
||||
RateService.prototype.getAlternatives = function() {
|
||||
|
|
@ -90,25 +134,25 @@ RateService.prototype.isAvailable = function() {
|
|||
|
||||
RateService.prototype.whenAvailable = function(callback) {
|
||||
if (this.isAvailable()) {
|
||||
setTimeout(callback, 1);
|
||||
setTimeout(callback, 10);
|
||||
} else {
|
||||
this._queued.push(callback);
|
||||
}
|
||||
};
|
||||
|
||||
RateService.prototype.toFiat = function(satoshis, code) {
|
||||
RateService.prototype.toFiat = function(satoshis, code, chain) {
|
||||
if (!this.isAvailable()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return satoshis * this.SAT_TO_BTC * this.getRate(code);
|
||||
return satoshis * this.SAT_TO_BTC * this.getRate(code, chain);
|
||||
};
|
||||
|
||||
RateService.prototype.fromFiat = function(amount, code) {
|
||||
RateService.prototype.fromFiat = function(amount, code, chain) {
|
||||
if (!this.isAvailable()) {
|
||||
return null;
|
||||
}
|
||||
return amount / this.getRate(code) * this.BTC_TO_SAT;
|
||||
return amount / this.getRate(code, chain) * this.BTC_TO_SAT;
|
||||
};
|
||||
|
||||
RateService.prototype.listAlternatives = function(sort) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue