made rate service angular factory
This commit is contained in:
parent
a53e236f9c
commit
b7652b511f
1 changed files with 90 additions and 158 deletions
|
|
@ -1,182 +1,114 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
//var util = require('util');
|
angular.module('copayApp.services').factory('rateService', function($http, lodash) {
|
||||||
//var _ = require('lodash');
|
var SAT_TO_BTC = 1 / 1e8;
|
||||||
//var log = require('../util/log');
|
var BTC_TO_SAT = 1e8;
|
||||||
//var preconditions = require('preconditions').singleton();
|
var UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
|
||||||
//var request = require('request');
|
var UNSUPPORTED_CURRENCY_ERROR = 'Currency not supported';
|
||||||
|
|
||||||
/*
|
var _isAvailable = false;
|
||||||
This class lets interfaces with BitPay's exchange rate API.
|
var _rates = {};
|
||||||
*/
|
var _alternatives = [];
|
||||||
|
var _bchRate = null;
|
||||||
|
var _queued = [];
|
||||||
|
|
||||||
var RateService = function(opts) {
|
var root = {};
|
||||||
var self = this;
|
|
||||||
|
|
||||||
opts = opts || {};
|
root.updateRates = function() {
|
||||||
self.httprequest = opts.httprequest; // || request;
|
var backoffSeconds = 5;
|
||||||
self.lodash = opts.lodash;
|
var rateServiceUrl = 'https://www.bitcoin.com/special/rates.json';
|
||||||
|
|
||||||
self.SAT_TO_BTC = 1 / 1e8;
|
function getRates(cb, tries) {
|
||||||
self.BTC_TO_SAT = 1e8;
|
tries = tries || 0;
|
||||||
self.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable() or use service.whenAvailable()';
|
if (tries > 5) return cb('could not get rates');
|
||||||
self.UNSUPPORTED_CURRENCY_ERROR = 'Currency not supported';
|
|
||||||
|
|
||||||
self._isAvailable = false;
|
$http.get(rateServiceUrl).success(function(res) {
|
||||||
self._rates = {};
|
lodash.each(res, function(currency) {
|
||||||
self._alternatives = [];
|
_rates[currency.code] = currency.rate;
|
||||||
self._bchRate = null;
|
_alternatives.push({
|
||||||
self._queued = [];
|
name: currency.name,
|
||||||
|
isoCode: currency.code,
|
||||||
self.updateRates();
|
rate: currency.rate
|
||||||
};
|
});
|
||||||
|
if (currency.code == 'BCH') {
|
||||||
|
_bchRate = currency.rate;
|
||||||
var _instance;
|
}
|
||||||
RateService.singleton = function(opts) {
|
|
||||||
if (!_instance) {
|
|
||||||
_instance = new RateService(opts);
|
|
||||||
}
|
|
||||||
return _instance;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.updateRates = function() {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
var backoffSeconds = 5;
|
|
||||||
var updateFrequencySeconds = 5 * 60;
|
|
||||||
var rateServiceUrl = 'https://bitpay.com/api/rates';
|
|
||||||
var bchRateServiceUrl = 'https://www.bitcoin.com/special/rates.json';
|
|
||||||
|
|
||||||
|
|
||||||
function getBTC(cb, tries) {
|
|
||||||
tries = tries || 0;
|
|
||||||
if (!self.httprequest) return;
|
|
||||||
if (tries > 5) return cb('could not get BTC rates');
|
|
||||||
|
|
||||||
//log.info('Fetching exchange rates');
|
|
||||||
self.httprequest.get(rateServiceUrl).success(function(res) {
|
|
||||||
self.lodash.each(res, function(currency) {
|
|
||||||
self._rates[currency.code] = currency.rate;
|
|
||||||
self._alternatives.push({
|
|
||||||
name: currency.name,
|
|
||||||
isoCode: currency.code,
|
|
||||||
rate: currency.rate
|
|
||||||
});
|
});
|
||||||
|
return cb();
|
||||||
|
}).error(function() {
|
||||||
|
setTimeout(function() {
|
||||||
|
backoffSeconds *= 1.5;
|
||||||
|
getRates(cb, tries++)
|
||||||
|
}, backoffSeconds * 1000)
|
||||||
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
return cb();
|
|
||||||
}).error(function() {
|
|
||||||
//log.debug('Error fetching exchange rates', err);
|
|
||||||
setTimeout(function() {
|
|
||||||
backoffSeconds *= 1.5;
|
|
||||||
getBTC(cb, tries++);
|
|
||||||
}, backoffSeconds * 1000);
|
|
||||||
return;
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBCH(cb, tries) {
|
|
||||||
tries = tries || 0;
|
|
||||||
if (!self.httprequest) return;
|
|
||||||
if (tries > 5) return cb('could not get BCH rates');
|
|
||||||
|
|
||||||
function retry(tries) {
|
|
||||||
//log.debug('Error fetching exchange rates', err);
|
|
||||||
setTimeout(function() {
|
|
||||||
backoffSeconds *= 1.5;
|
|
||||||
getBTC(cb, tries++);
|
|
||||||
}, backoffSeconds * 1000);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.httprequest.get(bchRateServiceUrl).success(function(res) {
|
getRates(function(err) {
|
||||||
self._bchRate = self.lodash.find(res, function(c) { return c.code == 'BCH'; }).rate;
|
|
||||||
return cb();
|
|
||||||
}).error(function() {
|
|
||||||
return retry(tries);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
getBTC(function(err) {
|
|
||||||
if (err) return;
|
|
||||||
getBCH(function(err) {
|
|
||||||
if (err) return;
|
if (err) return;
|
||||||
|
_isAvailable = true;
|
||||||
self._isAvailable = true;
|
lodash.each(_queued, function(callback) {
|
||||||
self.lodash.each(self._queued, function(callback) {
|
|
||||||
setTimeout(callback, 1);
|
setTimeout(callback, 1);
|
||||||
});
|
});
|
||||||
setTimeout( self.updateRates , updateFrequencySeconds * 1000);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.getRate = function(code, chain) {
|
|
||||||
var rate = this._rates[code];
|
|
||||||
return chain == 'bch' ? this._bchRate * rate : rate;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.getAlternatives = function() {
|
|
||||||
return this._alternatives;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.isAvailable = function() {
|
|
||||||
return this._isAvailable;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.whenAvailable = function(callback) {
|
|
||||||
if (this.isAvailable()) {
|
|
||||||
setTimeout(callback, 10);
|
|
||||||
} else {
|
|
||||||
this._queued.push(callback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.toFiat = function(satoshis, code, chain) {
|
|
||||||
if (!this.isAvailable()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return satoshis * this.SAT_TO_BTC * this.getRate(code, chain);
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.fromFiat = function(amount, code, chain) {
|
|
||||||
if (!this.isAvailable()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return amount / this.getRate(code, chain) * this.BTC_TO_SAT;
|
|
||||||
};
|
|
||||||
|
|
||||||
RateService.prototype.listAlternatives = function(sort) {
|
|
||||||
var self = this;
|
|
||||||
if (!this.isAvailable()) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
var alternatives = self.lodash.map(this.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 self.lodash.uniq(alternatives, 'isoCode');
|
|
||||||
};
|
|
||||||
|
|
||||||
angular.module('copayApp.services').factory('rateService', function($http, lodash) {
|
root.getRate = function(code, chain) {
|
||||||
// var cfg = _.extend(config.rates, {
|
var rate = _rates[code];
|
||||||
// httprequest: $http
|
return chain == 'bch' ? _bchRate * rate : rate;
|
||||||
// });
|
}
|
||||||
|
|
||||||
var cfg = {
|
root.getAlternatives = function() {
|
||||||
httprequest: $http,
|
return _alternatives;
|
||||||
lodash: lodash
|
}
|
||||||
|
|
||||||
|
root.isAvailable = function() {
|
||||||
|
return _isAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
|
root.whenAvailable = function(callback) {
|
||||||
|
if (root.isAvailable()) {
|
||||||
|
setTimeout(callback, 10);
|
||||||
|
} else {
|
||||||
|
_queued.push(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
root.toFiat = function(satoshis, code, chain) {
|
||||||
|
if (!root.isAvailable()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return satoshis * SAT_TO_BTC * root.getRate(code, chain);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.fromFiat = function(amount, code, chain) {
|
||||||
|
if (!root.isAvailable()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return amount / root.getRate(code, chain) * BTC_TO_SAT;
|
||||||
};
|
};
|
||||||
return RateService.singleton(cfg);
|
|
||||||
|
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');
|
||||||
|
};
|
||||||
|
|
||||||
|
root.updateRates();
|
||||||
|
setInterval(root.updateRates, 10000);
|
||||||
|
return root;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue