Address @matiu's comments
This commit is contained in:
parent
ac6dfc8035
commit
1c480d21a6
2 changed files with 19 additions and 7 deletions
|
|
@ -56,6 +56,11 @@ var defaultConfig = {
|
||||||
storageSalt: 'mjuBtGybi/4=',
|
storageSalt: 'mjuBtGybi/4=',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
rate: {
|
||||||
|
url: 'https://bitpay.com/api/rates',
|
||||||
|
updateFrequencySeconds: 60 * 60
|
||||||
|
},
|
||||||
|
|
||||||
disableVideo: true,
|
disableVideo: true,
|
||||||
verbose: 1,
|
verbose: 1,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,25 @@
|
||||||
|
|
||||||
var RateService = function(request) {
|
var RateService = function(request) {
|
||||||
this.isAvailable = false;
|
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.SAT_TO_BTC = 1 / 1e8;
|
||||||
|
var MINS_IN_HOUR = 60;
|
||||||
|
var MILLIS_IN_SECOND = 1000;
|
||||||
|
var rateServiceConfig = config.rate;
|
||||||
|
var updateFrequencySeconds = rateServiceConfig.updateFrequencySeconds || 60 * MINS_IN_HOUR;
|
||||||
|
var rateServiceUrl = rateServiceConfig.url || 'https://bitpay.com/api/rates';
|
||||||
this.queued = [];
|
this.queued = [];
|
||||||
this.alternatives = [];
|
this.alternatives = [];
|
||||||
var that = this;
|
var that = this;
|
||||||
var backoff = 5;
|
var backoffSeconds = 5;
|
||||||
var retrieve = function() {
|
var retrieve = function() {
|
||||||
request.get({
|
request.get({
|
||||||
url:'https://bitpay.com/api/rates',
|
url: rateServiceUrl,
|
||||||
json: true
|
json: true
|
||||||
}, function(err, response, listOfCurrencies) {
|
}, function(err, response, listOfCurrencies) {
|
||||||
if (err) {
|
if (err) {
|
||||||
backoff *= 1.5;
|
backoffSeconds *= 1.5;
|
||||||
setTimeout(retrieve, backoff * 1000);
|
setTimeout(retrieve, backoffSeconds * MILLIS_IN_SECOND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var rates = {};
|
var rates = {};
|
||||||
|
|
@ -31,6 +37,7 @@ var RateService = function(request) {
|
||||||
that.queued.forEach(function(callback) {
|
that.queued.forEach(function(callback) {
|
||||||
setTimeout(callback, 1);
|
setTimeout(callback, 1);
|
||||||
});
|
});
|
||||||
|
setTimeout(retrieve, updateFrequencySeconds * MILLIS_IN_SECOND);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
retrieve();
|
retrieve();
|
||||||
|
|
@ -46,21 +53,21 @@ RateService.prototype.whenAvailable = function(callback) {
|
||||||
|
|
||||||
RateService.prototype.toFiat = function(satoshis, code) {
|
RateService.prototype.toFiat = function(satoshis, code) {
|
||||||
if (!this.isAvailable) {
|
if (!this.isAvailable) {
|
||||||
return 0;
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
return satoshis * this.SAT_TO_BTC * this.rates[code];
|
return satoshis * this.SAT_TO_BTC * this.rates[code];
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype.fromFiat = function(amount, code) {
|
RateService.prototype.fromFiat = function(amount, code) {
|
||||||
if (!this.isAvailable) {
|
if (!this.isAvailable) {
|
||||||
return 0;
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
return amount / this.rates[code] / this.SAT_TO_BTC;
|
return amount / this.rates[code] / this.SAT_TO_BTC;
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype.listAlternatives = function() {
|
RateService.prototype.listAlternatives = function() {
|
||||||
if (!this.isAvailable) {
|
if (!this.isAvailable) {
|
||||||
return [];
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
var alts = [];
|
var alts = [];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue