added rate services to wallet

This commit is contained in:
Ivan Socolsky 2014-11-05 22:05:09 -03:00
commit 015af22638
4 changed files with 25 additions and 11 deletions

View file

@ -86,7 +86,9 @@ Identity.create = function(opts, cb) {
opts = _.extend({}, opts); opts = _.extend({}, opts);
var iden = new Identity(opts); var iden = new Identity(opts);
iden.store(_.extend(opts, {failIfExists: true}), function(err) { iden.store(_.extend(opts, {
failIfExists: true
}), function(err) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, iden); return cb(null, iden);
}); });

View file

@ -20,15 +20,26 @@ var RateService = function(opts) {
self._isAvailable = false; self._isAvailable = false;
self._rates = {}; self._rates = {};
self._alternatives = {}; self._alternatives = [];
self.queued = []; self._queued = [];
self._fetchCurrencies(); self._fetchCurrencies();
} };
var _instance;
RateService.singleton = function(opts) {
if (!_instance) {
_instance = new RateService(opts);
}
return _instance;
};
RateService.prototype._fetchCurrencies = function() { RateService.prototype._fetchCurrencies = function() {
var self = this; var self = this;
log.info('Fetching exchange rates');
var backoffSeconds = 5; var backoffSeconds = 5;
var updateFrequencySeconds = 3600; var updateFrequencySeconds = 3600;
var rateServiceUrl = 'https://bitpay.com/api/rates'; var rateServiceUrl = 'https://bitpay.com/api/rates';
@ -43,15 +54,15 @@ RateService.prototype._fetchCurrencies = function() {
return; return;
} }
_.each(body, function(currency) { _.each(body, function(currency) {
self.rates[currency.code] = currency.rate; self._rates[currency.code] = currency.rate;
self.alternatives.push({ self._alternatives.push({
name: currency.name, name: currency.name,
isoCode: currency.code, isoCode: currency.code,
rate: currency.rate rate: currency.rate
}); });
}); });
self._isAvailable = true; self._isAvailable = true;
_.each(self.queued, function(callback) { _.each(self._queued, function(callback) {
setTimeout(callback, 1); setTimeout(callback, 1);
}); });
setTimeout(function() { setTimeout(function() {
@ -81,7 +92,7 @@ RateService.prototype.whenAvailable = function(callback) {
if (!this.isAvailable()) { if (!this.isAvailable()) {
setTimeout(callback, 1); setTimeout(callback, 1);
} else { } else {
this.queued.push(callback); this._queued.push(callback);
} }
}; };

View file

@ -83,6 +83,7 @@ describe('PayPro (in Wallet) model', function() {
c.network.getHexNonces = sinon.stub(); c.network.getHexNonces = sinon.stub();
c.network.send = sinon.stub(); c.network.send = sinon.stub();
Wallet._newRateService = sinon.stub().returns(null);
return new Wallet(c); return new Wallet(c);
} }

View file

@ -56,8 +56,8 @@ var addCopayers = function(w) {
} }
}; };
describe('Wallet model', function() {
describe('Wallet model', function() {
it('should fail to create an instance', function() { it('should fail to create an instance', function() {
(function() { (function() {
new Wallet(walletConfig) new Wallet(walletConfig)
@ -102,8 +102,6 @@ describe('Wallet model', function() {
c.network.peerFromCopayer = sinon.stub().returns('xxxx'); c.network.peerFromCopayer = sinon.stub().returns('xxxx');
c.network.send = sinon.stub(); c.network.send = sinon.stub();
c.addressBook = { c.addressBook = {
'2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': { '2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': {
label: 'John', label: 'John',
@ -122,6 +120,8 @@ describe('Wallet model', function() {
c.networkName = walletConfig.networkName; c.networkName = walletConfig.networkName;
c.version = '0.0.1'; c.version = '0.0.1';
Wallet._newRateService = sinon.stub().returns(null);
return new Wallet(c); return new Wallet(c);
} }