Wallet/src/js/services/rateService.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-11-05 12:57:21 -03:00
'use strict';
2018-01-29 14:10:16 -04:00
angular.module('copayApp.services').factory('rateService', function($http, lodash) {
var SAT_TO_BTC = 1 / 1e8;
var BTC_TO_SAT = 1e8;
var UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
var UNSUPPORTED_CURRENCY_ERROR = 'Currency not supported';
var _isAvailable = false;
var _rates = {};
var _alternatives = [];
var _bchRate = null;
var _queued = [];
var root = {};
root.updateRates = function() {
var backoffSeconds = 5;
var rateServiceUrl = 'https://www.bitcoin.com/special/rates.json';
function getRates(cb, tries) {
tries = tries || 0;
if (tries > 5) return cb('could not get rates');
$http.get(rateServiceUrl).success(function(res) {
lodash.each(res, function(currency) {
_rates[currency.code] = currency.rate;
_alternatives.push({
name: currency.name,
isoCode: currency.code,
rate: currency.rate
});
if (currency.code == 'BCH') {
_bchRate = currency.rate;
}
});
2018-01-29 14:10:16 -04:00
return cb();
}).error(function() {
setTimeout(function() {
backoffSeconds *= 1.5;
getRates(cb, tries++)
}, backoffSeconds * 1000)
return;
2014-11-05 12:57:21 -03:00
});
2017-08-28 13:33:10 -03:00
}
2018-01-29 14:10:16 -04:00
getRates(function(err) {
2017-08-28 13:33:10 -03:00
if (err) return;
2018-01-29 14:10:16 -04:00
_isAvailable = true;
lodash.each(_queued, function(callback) {
2017-08-28 13:33:10 -03:00
setTimeout(callback, 1);
});
2018-01-29 14:10:16 -04:00
});
2014-11-05 12:57:21 -03:00
}
2018-01-29 14:10:16 -04:00
root.getRate = function(code, chain) {
var rate = _rates[code];
return chain == 'bch' ? _bchRate * rate : rate;
2014-11-05 12:57:21 -03:00
}
2015-03-06 12:00:10 -03:00
2018-01-29 14:10:16 -04:00
root.getAlternatives = function() {
return _alternatives;
}
2014-11-05 12:57:21 -03:00
2018-01-29 14:10:16 -04:00
root.isAvailable = function() {
return _isAvailable;
2014-11-05 12:57:21 -03:00
}
2018-01-29 14:10:16 -04:00
root.whenAvailable = function(callback) {
if (root.isAvailable()) {
setTimeout(callback, 10);
} else {
_queued.push(callback);
}
2014-11-05 12:57:21 -03:00
}
2018-01-29 14:10:16 -04:00
root.toFiat = function(satoshis, code, chain) {
if (!root.isAvailable()) {
return null;
2014-11-05 12:57:21 -03:00
}
2018-01-29 14:10:16 -04:00
return satoshis * SAT_TO_BTC * root.getRate(code, chain);
2016-12-15 15:49:02 -03:00
}
2014-11-05 12:57:21 -03:00
2018-01-29 14:10:16 -04:00
root.fromFiat = function(amount, code, chain) {
if (!root.isAvailable()) {
return null;
}
return amount / root.getRate(code, chain) * BTC_TO_SAT;
};
2014-11-05 12:57:21 -03:00
2018-01-29 14:10:16 -04:00
root.listAlternatives = function(sort) {
if (!root.isAvailable()) {
return [];
}
var alternatives = lodash.map(root.getAlternatives(), function(item) {
return {
name: item.name,
isoCode: item.isoCode
}
});
if (sort) {
alternatives.sort(function(a, b) {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
});
}
return lodash.uniq(alternatives, 'isoCode');
2015-03-06 12:00:10 -03:00
};
2018-01-29 14:10:16 -04:00
root.updateRates();
2018-01-29 14:10:36 -04:00
setInterval(root.updateRates, 5 * 60 * 1000);
2018-01-29 14:10:16 -04:00
return root;
2015-03-06 12:00:10 -03:00
});