From 3eb45e529f97ed863e96189d29f578453e26909f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 26 Nov 2014 18:51:59 -0300 Subject: [PATCH] extend rate services to retrieve historic rates --- js/controllers/history.js | 22 +++++++++++++++++--- js/models/RateService.js | 42 +++++++++++++++++++++++++++++---------- views/history.html | 1 + 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 3f47fb95c..fca38f2fa 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -140,11 +140,27 @@ angular.module('copayApp.controllers').controller('HistoryController', } var items = res.items; - - _.each(items, function(r) { - r.ts = r.minedTs || r.sentTs; + _.each(items, function(tx) { + tx.ts = tx.minedTs || tx.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.nbPages = res.nbPages; diff --git a/js/models/RateService.js b/js/models/RateService.js index 55995a567..c785166f3 100644 --- a/js/models/RateService.js +++ b/js/models/RateService.js @@ -45,7 +45,7 @@ RateService.prototype._fetchCurrencies = function() { var updateFrequencySeconds = 3600; var rateServiceUrl = 'https://bitpay.com/api/rates'; - var retrieve = function () { + var retrieve = function() { log.info('Fetching exchange rates'); self.request.get({ url: rateServiceUrl, @@ -53,7 +53,7 @@ RateService.prototype._fetchCurrencies = function() { }, function(err, res, body) { if (err || !body) { log.debug('Error fetching exchange rates', err); - setTimeout(function () { + setTimeout(function() { backoffSeconds *= 1.5; retrieve(); }, backoffSeconds * 1000); @@ -78,16 +78,36 @@ RateService.prototype._fetchCurrencies = function() { retrieve(); }; -RateService.prototype._getRate = function(code) { +RateService.prototype.getRate = function(code) { return this._rates[code]; }; -RateService.prototype._getHistoricRate = function(code, date, cb) { - // TODO (isocolsky): implement with a remote call - return cb(new Error('Not implemented')); +RateService.prototype.getHistoricRate = function(code, date, cb) { + var self = this; + + 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; }; @@ -107,13 +127,13 @@ RateService.prototype.toFiat = function(satoshis, code) { if (!this.isAvailable()) { 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) { var self = this; - self._getHistoricRate(code, date, function(err, rate) { + self.getHistoricRate(code, date, function(err, rate) { if (err) return cb(err); return cb(null, satoshis * self.SAT_TO_BTC * rate); }); @@ -123,7 +143,7 @@ RateService.prototype.fromFiat = function(amount, code) { if (!this.isAvailable()) { 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() { @@ -131,7 +151,7 @@ RateService.prototype.listAlternatives = function() { throw new Error(this.UNAVAILABLE_ERROR); } - return _.map(this._getAlternatives(), function(item) { + return _.map(this.getAlternatives(), function(item) { return { name: item.name, isoCode: item.isoCode diff --git a/views/history.html b/views/history.html index 682bf6de6..380fd9517 100644 --- a/views/history.html +++ b/views/history.html @@ -47,6 +47,7 @@ 'text-warning': btx.action == 'sent', 'text-gray': btx.action == 'moved'}"> {{btx.amount| noFractionNumber}} {{$root.wallet.settings.unitName}} + {{btx.alternativeAmount| noFractionNumber}} {{$root.wallet.settings.alternativeIsoCode}}