2014-11-05 12:57:21 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
//var util = require('util');
|
|
|
|
|
//var _ = require('lodash');
|
|
|
|
|
//var log = require('../util/log');
|
|
|
|
|
//var preconditions = require('preconditions').singleton();
|
|
|
|
|
//var request = require('request');
|
2014-11-05 12:57:21 -03:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
This class lets interfaces with BitPay's exchange rate API.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var RateService = function(opts) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2014-11-06 11:34:52 -03:00
|
|
|
opts = opts || {};
|
2015-03-06 12:00:10 -03:00
|
|
|
self.httprequest = opts.httprequest; // || request;
|
|
|
|
|
self.lodash = opts.lodash;
|
2014-11-06 11:34:52 -03:00
|
|
|
|
2014-11-05 12:57:21 -03:00
|
|
|
self.SAT_TO_BTC = 1 / 1e8;
|
|
|
|
|
self.BTC_TO_SAT = 1e8;
|
|
|
|
|
self.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
|
|
|
|
|
self.UNSUPPORTED_CURRENCY_ERROR = 'Currency not supported';
|
|
|
|
|
|
2014-11-28 11:23:08 -03:00
|
|
|
self._url = opts.url || 'https://insight.bitpay.com:443/api/rates';
|
|
|
|
|
|
2014-11-05 12:57:21 -03:00
|
|
|
self._isAvailable = false;
|
|
|
|
|
self._rates = {};
|
2014-11-05 22:05:09 -03:00
|
|
|
self._alternatives = [];
|
|
|
|
|
self._queued = [];
|
2014-11-05 12:57:21 -03:00
|
|
|
|
|
|
|
|
self._fetchCurrencies();
|
2014-11-05 22:05:09 -03:00
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2014-11-05 22:05:09 -03:00
|
|
|
var _instance;
|
|
|
|
|
RateService.singleton = function(opts) {
|
|
|
|
|
if (!_instance) {
|
|
|
|
|
_instance = new RateService(opts);
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-05 12:57:21 -03:00
|
|
|
RateService.prototype._fetchCurrencies = function() {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
var backoffSeconds = 5;
|
2015-07-01 19:10:57 -03:00
|
|
|
var updateFrequencySeconds = 5 * 60;
|
2014-11-21 17:02:08 -03:00
|
|
|
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
2014-11-21 12:11:35 -03:00
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
var retrieve = function() {
|
2015-03-06 12:00:10 -03:00
|
|
|
//log.info('Fetching exchange rates');
|
|
|
|
|
self.httprequest.get(rateServiceUrl).success(function(res) {
|
|
|
|
|
self.lodash.each(res, function(currency) {
|
2014-11-21 12:11:35 -03:00
|
|
|
self._rates[currency.code] = currency.rate;
|
|
|
|
|
self._alternatives.push({
|
|
|
|
|
name: currency.name,
|
|
|
|
|
isoCode: currency.code,
|
|
|
|
|
rate: currency.rate
|
|
|
|
|
});
|
2014-11-05 12:57:21 -03:00
|
|
|
});
|
2014-11-21 12:11:35 -03:00
|
|
|
self._isAvailable = true;
|
2015-03-06 12:00:10 -03:00
|
|
|
self.lodash.each(self._queued, function(callback) {
|
2014-11-21 12:11:35 -03:00
|
|
|
setTimeout(callback, 1);
|
|
|
|
|
});
|
|
|
|
|
setTimeout(retrieve, updateFrequencySeconds * 1000);
|
2015-03-06 12:00:10 -03:00
|
|
|
}).error(function(err) {
|
|
|
|
|
//log.debug('Error fetching exchange rates', err);
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
backoffSeconds *= 1.5;
|
|
|
|
|
retrieve();
|
|
|
|
|
}, backoffSeconds * 1000);
|
|
|
|
|
return;
|
2014-11-05 12:57:21 -03:00
|
|
|
});
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2014-11-21 12:11:35 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
retrieve();
|
2014-11-05 12:57:21 -03:00
|
|
|
};
|
|
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
RateService.prototype.getRate = function(code) {
|
2014-11-05 12:57:21 -03:00
|
|
|
return this._rates[code];
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
RateService.prototype.getHistoricRate = function(code, date, cb) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
self.httprequest.get(self._url + '/' + code + '?ts=' + date)
|
|
|
|
|
.success(function(body) {
|
|
|
|
|
return cb(null, body.rate)
|
|
|
|
|
})
|
|
|
|
|
.error(function(err) {
|
|
|
|
|
return cb(err)
|
|
|
|
|
});
|
|
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.getHistoricRates = function(code, dates, cb) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2014-11-28 11:23:08 -03:00
|
|
|
var tsList = dates.join(',');
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
self.httprequest.get(self._url + '/' + code + '?ts=' + tsList)
|
|
|
|
|
.success(function(body) {
|
|
|
|
|
if (!self.lodash.isArray(body)) {
|
|
|
|
|
body = [{
|
|
|
|
|
ts: dates[0],
|
|
|
|
|
rate: body.rate
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
return cb(null, body);
|
|
|
|
|
})
|
|
|
|
|
.error(function(err) {
|
|
|
|
|
return cb(err)
|
|
|
|
|
});
|
2014-11-05 12:57:21 -03:00
|
|
|
};
|
|
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
RateService.prototype.getAlternatives = function() {
|
2014-11-05 12:57:21 -03:00
|
|
|
return this._alternatives;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.isAvailable = function() {
|
|
|
|
|
return this._isAvailable;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.whenAvailable = function(callback) {
|
2014-11-05 22:21:32 -03:00
|
|
|
if (this.isAvailable()) {
|
2014-11-05 12:57:21 -03:00
|
|
|
setTimeout(callback, 1);
|
|
|
|
|
} else {
|
2014-11-05 22:05:09 -03:00
|
|
|
this._queued.push(callback);
|
2014-11-05 12:57:21 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.toFiat = function(satoshis, code) {
|
|
|
|
|
if (!this.isAvailable()) {
|
2015-03-06 12:00:10 -03:00
|
|
|
return null;
|
2014-11-05 12:57:21 -03:00
|
|
|
}
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
return satoshis * this.SAT_TO_BTC * this.getRate(code);
|
2014-11-05 12:57:21 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.toFiatHistoric = function(satoshis, code, date, cb) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2014-11-26 18:51:59 -03:00
|
|
|
self.getHistoricRate(code, date, function(err, rate) {
|
2014-11-05 12:57:21 -03:00
|
|
|
if (err) return cb(err);
|
|
|
|
|
return cb(null, satoshis * self.SAT_TO_BTC * rate);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.fromFiat = function(amount, code) {
|
|
|
|
|
if (!this.isAvailable()) {
|
2015-03-06 12:00:10 -03:00
|
|
|
return null;
|
2014-11-05 12:57:21 -03:00
|
|
|
}
|
2014-11-26 18:51:59 -03:00
|
|
|
return amount / this.getRate(code) * this.BTC_TO_SAT;
|
2014-11-05 12:57:21 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RateService.prototype.listAlternatives = function() {
|
2015-03-06 12:00:10 -03:00
|
|
|
var self = this;
|
2014-11-05 12:57:21 -03:00
|
|
|
if (!this.isAvailable()) {
|
2015-03-06 12:00:10 -03:00
|
|
|
return [];
|
2014-11-05 12:57:21 -03:00
|
|
|
}
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
return self.lodash.map(this.getAlternatives(), function(item) {
|
2014-11-05 12:57:21 -03:00
|
|
|
return {
|
|
|
|
|
name: item.name,
|
|
|
|
|
isoCode: item.isoCode
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
angular.module('copayApp.services').factory('rateService', function($http, lodash) {
|
|
|
|
|
// var cfg = _.extend(config.rates, {
|
|
|
|
|
// httprequest: $http
|
|
|
|
|
// });
|
2014-11-05 12:57:21 -03:00
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
var cfg = {
|
|
|
|
|
httprequest: $http,
|
|
|
|
|
lodash: lodash
|
|
|
|
|
};
|
|
|
|
|
return RateService.singleton(cfg);
|
|
|
|
|
});
|