Add RateService to get info about fiat currencies
This commit is contained in:
parent
decc9e9dba
commit
3da033cb06
3 changed files with 86 additions and 31 deletions
40
js/services/rate.js
Normal file
40
js/services/rate.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
'use strict';
|
||||
|
||||
var RateService = function($http) {
|
||||
this.isAvailable = false;
|
||||
this.SAT_TO_BTC = 1 / 1e8;
|
||||
var that = this;
|
||||
var backoff = 5;
|
||||
var retrieve = function() {
|
||||
$http({method: 'GET', url: 'https://bitpay.com/api/rates'}).
|
||||
success(function(data, status, headers, config) {
|
||||
var rates = {};
|
||||
data.forEach(function(element) {
|
||||
rates[element.code] = element.rate;
|
||||
});
|
||||
that.isAvailable = true;
|
||||
that.rates = rates;
|
||||
}).
|
||||
error(function(data, status, headers, config) {
|
||||
backoff *= 1.5;
|
||||
setTimeout(retrieve, backoff * 1000);
|
||||
});
|
||||
};
|
||||
retrieve();
|
||||
};
|
||||
|
||||
RateService.prototype.toFiat = function(satoshis, code) {
|
||||
if (!this.isAvailable) {
|
||||
return 0;
|
||||
}
|
||||
return satoshis * this.SAT_TO_BTC * this.rates[code];
|
||||
};
|
||||
|
||||
RateService.prototype.fromFiat = function(amount, code) {
|
||||
if (!this.isAvailable) {
|
||||
return 0;
|
||||
}
|
||||
return amount / this.rates[code] / this.SAT_TO_BTC;
|
||||
};
|
||||
|
||||
angular.module('copayApp.services').service('rateService', RateService);
|
||||
Loading…
Add table
Add a link
Reference in a new issue