extend rate services to retrieve historic rates
This commit is contained in:
parent
ec4d774bae
commit
3eb45e529f
3 changed files with 50 additions and 13 deletions
|
|
@ -140,11 +140,27 @@ angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = res.items;
|
var items = res.items;
|
||||||
|
_.each(items, function(tx) {
|
||||||
_.each(items, function(r) {
|
tx.ts = tx.minedTs || tx.sentTs;
|
||||||
r.ts = r.minedTs || r.sentTs;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var index = _.indexBy(res.items, function(tx) {
|
||||||
|
return Math.floor(tx.ts / 1000);
|
||||||
|
});
|
||||||
|
rateService.getHistoricRates(w.settings.alternativeIsoCode, _.keys(index), function(err, res) {
|
||||||
|
console.log(res);
|
||||||
|
if (!err && res) {
|
||||||
|
_.each(res, function(r) {
|
||||||
|
var tx = index[r.ts];
|
||||||
|
tx.alternativeAmount = tx.amountSat * rateService.SAT_TO_BTC * r.rate;
|
||||||
|
});
|
||||||
|
setTimeout(function() {
|
||||||
|
$scope.$digest();
|
||||||
|
}, 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$scope.blockchain_txs = w.cached_txs = items;
|
$scope.blockchain_txs = w.cached_txs = items;
|
||||||
$scope.nbPages = res.nbPages;
|
$scope.nbPages = res.nbPages;
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,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';
|
||||||
|
|
||||||
var retrieve = function () {
|
var retrieve = function() {
|
||||||
log.info('Fetching exchange rates');
|
log.info('Fetching exchange rates');
|
||||||
self.request.get({
|
self.request.get({
|
||||||
url: rateServiceUrl,
|
url: rateServiceUrl,
|
||||||
|
|
@ -53,7 +53,7 @@ RateService.prototype._fetchCurrencies = function() {
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
if (err || !body) {
|
if (err || !body) {
|
||||||
log.debug('Error fetching exchange rates', err);
|
log.debug('Error fetching exchange rates', err);
|
||||||
setTimeout(function () {
|
setTimeout(function() {
|
||||||
backoffSeconds *= 1.5;
|
backoffSeconds *= 1.5;
|
||||||
retrieve();
|
retrieve();
|
||||||
}, backoffSeconds * 1000);
|
}, backoffSeconds * 1000);
|
||||||
|
|
@ -78,16 +78,36 @@ RateService.prototype._fetchCurrencies = function() {
|
||||||
retrieve();
|
retrieve();
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype._getRate = function(code) {
|
RateService.prototype.getRate = function(code) {
|
||||||
return this._rates[code];
|
return this._rates[code];
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype._getHistoricRate = function(code, date, cb) {
|
RateService.prototype.getHistoricRate = function(code, date, cb) {
|
||||||
// TODO (isocolsky): implement with a remote call
|
var self = this;
|
||||||
return cb(new Error('Not implemented'));
|
|
||||||
|
self.request.get({
|
||||||
|
url: 'http://localhost:3001/api/rates/' + code + '?ts=' + date,
|
||||||
|
json: true
|
||||||
|
}, function(err, res, body) {
|
||||||
|
if (err || res.statusCode != 200 || !body) return cb(err || res);
|
||||||
|
return cb(null, body.rate);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype._getAlternatives = function() {
|
RateService.prototype.getHistoricRates = function(code, dates, cb) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
dates = [].concat(dates).join(',');
|
||||||
|
self.request.get({
|
||||||
|
url: 'http://localhost:3001/api/rates/' + code + '?ts=' + dates,
|
||||||
|
json: true
|
||||||
|
}, function(err, res, body) {
|
||||||
|
if (err || res.statusCode != 200 || !body) return cb(err || res);
|
||||||
|
return cb(null, body);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
RateService.prototype.getAlternatives = function() {
|
||||||
return this._alternatives;
|
return this._alternatives;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -107,13 +127,13 @@ RateService.prototype.toFiat = function(satoshis, code) {
|
||||||
if (!this.isAvailable()) {
|
if (!this.isAvailable()) {
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
return satoshis * this.SAT_TO_BTC * this._getRate(code);
|
return satoshis * this.SAT_TO_BTC * this.getRate(code);
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype.toFiatHistoric = function(satoshis, code, date, cb) {
|
RateService.prototype.toFiatHistoric = function(satoshis, code, date, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
self._getHistoricRate(code, date, function(err, rate) {
|
self.getHistoricRate(code, date, function(err, rate) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
return cb(null, satoshis * self.SAT_TO_BTC * rate);
|
return cb(null, satoshis * self.SAT_TO_BTC * rate);
|
||||||
});
|
});
|
||||||
|
|
@ -123,7 +143,7 @@ RateService.prototype.fromFiat = function(amount, code) {
|
||||||
if (!this.isAvailable()) {
|
if (!this.isAvailable()) {
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
return amount / this._getRate(code) * this.BTC_TO_SAT;
|
return amount / this.getRate(code) * this.BTC_TO_SAT;
|
||||||
};
|
};
|
||||||
|
|
||||||
RateService.prototype.listAlternatives = function() {
|
RateService.prototype.listAlternatives = function() {
|
||||||
|
|
@ -131,7 +151,7 @@ RateService.prototype.listAlternatives = function() {
|
||||||
throw new Error(this.UNAVAILABLE_ERROR);
|
throw new Error(this.UNAVAILABLE_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _.map(this._getAlternatives(), function(item) {
|
return _.map(this.getAlternatives(), function(item) {
|
||||||
return {
|
return {
|
||||||
name: item.name,
|
name: item.name,
|
||||||
isoCode: item.isoCode
|
isoCode: item.isoCode
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@
|
||||||
'text-warning': btx.action == 'sent',
|
'text-warning': btx.action == 'sent',
|
||||||
'text-gray': btx.action == 'moved'}">
|
'text-gray': btx.action == 'moved'}">
|
||||||
<b>{{btx.amount| noFractionNumber}} {{$root.wallet.settings.unitName}}</b>
|
<b>{{btx.amount| noFractionNumber}} {{$root.wallet.settings.unitName}}</b>
|
||||||
|
<b ng-show="btx.alternativeAmount >= 0">{{btx.alternativeAmount| noFractionNumber}} {{$root.wallet.settings.alternativeIsoCode}}</b>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue