add BCH to rateService

This commit is contained in:
Matias Alejo Garcia 2017-08-28 13:33:10 -03:00 committed by Gustavo Maximiliano Cortez
commit 09d1d5d3ff
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF

View file

@ -25,9 +25,10 @@ var RateService = function(opts) {
self._isAvailable = false; self._isAvailable = false;
self._rates = {}; self._rates = {};
self._alternatives = []; self._alternatives = [];
self._ratesBCH = {};
self._queued = []; self._queued = [];
self._fetchCurrencies(); self.updateRates();
}; };
@ -39,14 +40,20 @@ RateService.singleton = function(opts) {
return _instance; return _instance;
}; };
RateService.prototype._fetchCurrencies = function() { RateService.prototype.updateRates = function() {
console.log('[rateService.js.44:updateRates:]'); //TODO
var self = this; var self = this;
var backoffSeconds = 5; var backoffSeconds = 5;
var updateFrequencySeconds = 5 * 60; var updateFrequencySeconds = 5 * 60;
var rateServiceUrl = 'https://bitpay.com/api/rates'; 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'); //log.info('Fetching exchange rates');
self.httprequest.get(rateServiceUrl).success(function(res) { self.httprequest.get(rateServiceUrl).success(function(res) {
self.lodash.each(res, function(currency) { self.lodash.each(res, function(currency) {
@ -57,27 +64,64 @@ RateService.prototype._fetchCurrencies = function() {
rate: currency.rate 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._isAvailable = true;
self.lodash.each(self._queued, function(callback) { self.lodash.each(self._queued, function(callback) {
setTimeout(callback, 1); setTimeout(callback, 1);
}); });
setTimeout(retrieve, updateFrequencySeconds * 1000); setTimeout( self.updateRates , updateFrequencySeconds * 1000);
}).error(function(err) { })
//log.debug('Error fetching exchange rates', err); })
setTimeout(function() {
backoffSeconds *= 1.5;
retrieve();
}, backoffSeconds * 1000);
return;
});
};
retrieve();
}; };
RateService.prototype.getRate = function(code) { RateService.prototype.getRate = function(code, chain) {
return this._rates[code]; if (chain = 'bch')
return this._ratesBCH[code];
else
return this._rates[code];
}; };
RateService.prototype.getAlternatives = function() { RateService.prototype.getAlternatives = function() {
@ -90,25 +134,25 @@ RateService.prototype.isAvailable = function() {
RateService.prototype.whenAvailable = function(callback) { RateService.prototype.whenAvailable = function(callback) {
if (this.isAvailable()) { if (this.isAvailable()) {
setTimeout(callback, 1); setTimeout(callback, 10);
} else { } else {
this._queued.push(callback); this._queued.push(callback);
} }
}; };
RateService.prototype.toFiat = function(satoshis, code) { RateService.prototype.toFiat = function(satoshis, code, chain) {
if (!this.isAvailable()) { if (!this.isAvailable()) {
return null; 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()) { if (!this.isAvailable()) {
return null; 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) { RateService.prototype.listAlternatives = function(sort) {