replaced old rate service as angular controller
This commit is contained in:
parent
e1eef196a3
commit
505951a0b1
2 changed files with 8 additions and 83 deletions
|
|
@ -13,6 +13,9 @@ var request = require('request');
|
||||||
var RateService = function(opts) {
|
var RateService = function(opts) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
opts = opts || {};
|
||||||
|
self.request = opts.request || request;
|
||||||
|
|
||||||
self.SAT_TO_BTC = 1 / 1e8;
|
self.SAT_TO_BTC = 1 / 1e8;
|
||||||
self.BTC_TO_SAT = 1e8;
|
self.BTC_TO_SAT = 1e8;
|
||||||
self.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
|
self.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
|
||||||
|
|
@ -44,7 +47,7 @@ RateService.prototype._fetchCurrencies = function() {
|
||||||
var updateFrequencySeconds = 3600;
|
var updateFrequencySeconds = 3600;
|
||||||
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
||||||
|
|
||||||
request.get({
|
self.request.get({
|
||||||
url: rateServiceUrl,
|
url: rateServiceUrl,
|
||||||
json: true
|
json: true
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
|
|
|
||||||
|
|
@ -1,85 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var MINS_IN_HOUR = 60;
|
angular.module('copayApp.services').factory('rateService', function(request) {
|
||||||
var MILLIS_IN_SECOND = 1000;
|
return copay.RateService.singleton({
|
||||||
|
request: request
|
||||||
var RateService = function(request) {
|
|
||||||
this.isAvailable = false;
|
|
||||||
this.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable or use service.whenAvailable';
|
|
||||||
this.SAT_TO_BTC = 1 / 1e8;
|
|
||||||
this.BTC_TO_SAT = 1e8;
|
|
||||||
var rateServiceConfig = config.rate;
|
|
||||||
var updateFrequencySeconds = rateServiceConfig.updateFrequencySeconds || 60 * MINS_IN_HOUR;
|
|
||||||
var rateServiceUrl = rateServiceConfig.url || 'https://bitpay.com/api/rates';
|
|
||||||
this.queued = [];
|
|
||||||
this.alternatives = [];
|
|
||||||
var self = this;
|
|
||||||
var backoffSeconds = 5;
|
|
||||||
var retrieve = function() {
|
|
||||||
request.get({
|
|
||||||
url: rateServiceUrl,
|
|
||||||
json: true
|
|
||||||
}, function(err, response, listOfCurrencies) {
|
|
||||||
if (err) {
|
|
||||||
backoffSeconds *= 1.5;
|
|
||||||
setTimeout(retrieve, backoffSeconds * MILLIS_IN_SECOND);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var rates = {};
|
|
||||||
listOfCurrencies.forEach(function(element) {
|
|
||||||
rates[element.code] = element.rate;
|
|
||||||
self.alternatives.push({
|
|
||||||
name: element.name,
|
|
||||||
isoCode: element.code,
|
|
||||||
rate: element.rate
|
|
||||||
});
|
|
||||||
});
|
|
||||||
self.isAvailable = true;
|
|
||||||
self.rates = rates;
|
|
||||||
self.queued.forEach(function(callback) {
|
|
||||||
setTimeout(callback, 1);
|
|
||||||
});
|
|
||||||
setTimeout(retrieve, updateFrequencySeconds * MILLIS_IN_SECOND);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
retrieve();
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.whenAvailable = function(callback) {
|
|
||||||
if (this.isAvailable) {
|
|
||||||
setTimeout(callback, 1);
|
|
||||||
} else {
|
|
||||||
this.queued.push(callback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.toFiat = function(satoshis, code) {
|
|
||||||
if (!this.isAvailable) {
|
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
|
||||||
}
|
|
||||||
return satoshis * this.SAT_TO_BTC * this.rates[code];
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.fromFiat = function(amount, code) {
|
|
||||||
if (!this.isAvailable) {
|
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
|
||||||
}
|
|
||||||
return amount / this.rates[code] * this.BTC_TO_SAT;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.listAlternatives = function() {
|
|
||||||
if (!this.isAvailable) {
|
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
var alts = [];
|
|
||||||
this.alternatives.forEach(function(element) {
|
|
||||||
alts.push({
|
|
||||||
name: element.name,
|
|
||||||
isoCode: element.isoCode
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return alts;
|
});
|
||||||
};
|
|
||||||
|
|
||||||
angular.module('copayApp.services').service('rateService', RateService);
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue