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);
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);
return cb(null, iden);
});

View file

@ -20,15 +20,26 @@ var RateService = function(opts) {
self._isAvailable = false;
self._rates = {};
self._alternatives = {};
self.queued = [];
self._alternatives = [];
self._queued = [];
self._fetchCurrencies();
}
};
var _instance;
RateService.singleton = function(opts) {
if (!_instance) {
_instance = new RateService(opts);
}
return _instance;
};
RateService.prototype._fetchCurrencies = function() {
var self = this;
log.info('Fetching exchange rates');
var backoffSeconds = 5;
var updateFrequencySeconds = 3600;
var rateServiceUrl = 'https://bitpay.com/api/rates';
@ -43,15 +54,15 @@ RateService.prototype._fetchCurrencies = function() {
return;
}
_.each(body, function(currency) {
self.rates[currency.code] = currency.rate;
self.alternatives.push({
self._rates[currency.code] = currency.rate;
self._alternatives.push({
name: currency.name,
isoCode: currency.code,
rate: currency.rate
});
});
self._isAvailable = true;
_.each(self.queued, function(callback) {
_.each(self._queued, function(callback) {
setTimeout(callback, 1);
});
setTimeout(function() {
@ -81,7 +92,7 @@ RateService.prototype.whenAvailable = function(callback) {
if (!this.isAvailable()) {
setTimeout(callback, 1);
} 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.send = sinon.stub();
Wallet._newRateService = sinon.stub().returns(null);
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() {
(function() {
new Wallet(walletConfig)
@ -102,8 +102,6 @@ describe('Wallet model', function() {
c.network.peerFromCopayer = sinon.stub().returns('xxxx');
c.network.send = sinon.stub();
c.addressBook = {
'2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': {
label: 'John',
@ -122,6 +120,8 @@ describe('Wallet model', function() {
c.networkName = walletConfig.networkName;
c.version = '0.0.1';
Wallet._newRateService = sinon.stub().returns(null);
return new Wallet(c);
}