Browse Source

establish a limit on look back time for a valid rate

activeAddress
Ivan Socolsky 9 years ago
parent
commit
8a6c5dfc13
  1. 2
      lib/common/defaults.js
  2. 5
      lib/fiatrateservice.js
  3. 29
      test/integration/fiatrateservice.js

2
lib/common/defaults.js

@ -42,6 +42,6 @@ Defaults.TWO_STEP_BALANCE_THRESHOLD = 100;
Defaults.FIAT_RATE_PROVIDER = 'BitPay';
Defaults.FIAT_RATE_FETCH_INTERVAL = 10; // In minutes
Defaults.FIAT_RATE_MAX_LOOK_BACK_TIME = 120; // In minutes
module.exports = Defaults;

5
lib/fiatrateservice.js

@ -112,12 +112,15 @@ FiatRateService.prototype.getRate = function(opts, cb) {
opts = opts || {};
var now = Date.now();
var provider = opts.provider || self.defaultProvider;
var ts = (_.isNumber(opts.ts) || _.isArray(opts.ts)) ? opts.ts : Date.now();
var ts = (_.isNumber(opts.ts) || _.isArray(opts.ts)) ? opts.ts : now;
async.map([].concat(ts), function(ts, cb) {
self.storage.fetchFiatRate(provider, opts.code, ts, function(err, rate) {
if (err) return cb(err);
if (rate && (now - rate.ts) > Defaults.FIAT_RATE_MAX_LOOK_BACK_TIME * 60 * 1000) rate = null;
return cb(null, {
ts: +ts,
rate: rate ? rate.value : undefined,

29
test/integration/fiatrateservice.js

@ -174,6 +174,35 @@ describe('Fiat rate service', function() {
});
});
});
it('should not get rate older than 2hs', function(done) {
var clock = sinon.useFakeTimers(0, 'Date');
service.storage.storeFiatRate('BitPay', [{
code: 'USD',
value: 123.45,
}], function(err) {
should.not.exist(err);
clock.tick((120 * 60 - 1) * 1000); // Almost 2 hours
service.getRate({
code: 'USD',
}, function(err, res) {
should.not.exist(err);
res.rate.should.equal(123.45);
res.fetchedOn.should.equal(0);
clock.restore();
clock.tick(2 * 1000); // 2 seconds later...
service.getRate({
code: 'USD',
}, function(err, res) {
should.not.exist(err);
should.not.exist(res.rate);
clock.restore();
done();
});
});
});
});
});
describe('#fetch', function() {

Loading…
Cancel
Save