diff --git a/src/action/wallet.js b/src/action/wallet.js index 11f108a4c..3104f9332 100644 --- a/src/action/wallet.js +++ b/src/action/wallet.js @@ -5,7 +5,7 @@ import { observe } from 'mobx'; import { toBuffer, parseSat, checkHttpStatus, nap } from '../helper'; -import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY } from '../config'; +import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY, RATE_DELAY } from '../config'; import * as log from './log'; class WalletAction { @@ -113,7 +113,7 @@ class WalletAction { this.getBalance(), this.getChannelBalance(), this.getNewAddress(), - this.getExchangeRate(), + this.pollExchangeRate(), ]); } @@ -279,6 +279,17 @@ class WalletAction { } } + /** + * Poll for the current btc/fiat exchange rate based on the currently selected + * fiat currency every 15 minutes. + * @return {Promise} + */ + async pollExchangeRate() { + await this.getExchangeRate(); + clearTimeout(this.tPollRate); + this.tPollRate = setTimeout(() => this.pollExchangeRate(), RATE_DELAY); + } + /** * Fetch a current btc/fiat exchange rate based on the currently selected * fiat currency and persist the value on disk for the next time the app diff --git a/src/config.js b/src/config.js index 0ffeb3cde..db8c60f56 100644 --- a/src/config.js +++ b/src/config.js @@ -1,6 +1,7 @@ module.exports.RETRY_DELAY = 3000; module.exports.LND_INIT_DELAY = 5000; module.exports.NOTIFICATION_DELAY = 5000; +module.exports.RATE_DELAY = 15 * 60 * 1000; module.exports.LND_PORT = 10009; module.exports.LND_PEER_PORT = 10019; diff --git a/test/unit/action/wallet.spec.js b/test/unit/action/wallet.spec.js index c9851aa64..a8c82b47f 100644 --- a/test/unit/action/wallet.spec.js +++ b/test/unit/action/wallet.spec.js @@ -5,6 +5,7 @@ import WalletAction from '../../../src/action/wallet'; import NavAction from '../../../src/action/nav'; import NotificationAction from '../../../src/action/notification'; import * as logger from '../../../src/action/log'; +import { nap } from '../../../src/helper'; import nock from 'nock'; import 'isomorphic-fetch'; @@ -23,6 +24,7 @@ describe('Action Wallet Unit Tests', () => { store = new Store(); require('../../../src/config').RETRY_DELAY = 1; require('../../../src/config').NOTIFICATION_DELAY = 1; + require('../../../src/config').RATE_DELAY = 1; grpc = sinon.createStubInstance(GrpcAction); db = sinon.createStubInstance(AppStorage); notification = sinon.createStubInstance(NotificationAction); @@ -31,6 +33,7 @@ describe('Action Wallet Unit Tests', () => { }); afterEach(() => { + clearTimeout(wallet.tPollRate); sandbox.restore(); }); @@ -106,10 +109,10 @@ describe('Action Wallet Unit Tests', () => { describe('update()', () => { it('should refresh balances, exchange rate and address', async () => { - sandbox.stub(wallet, 'getExchangeRate'); + sandbox.stub(wallet, 'pollExchangeRate'); await wallet.update(); expect(grpc.sendCommand, 'was called thrice'); - expect(wallet.getExchangeRate, 'was called once'); + expect(wallet.pollExchangeRate, 'was called once'); }); }); @@ -324,6 +327,15 @@ describe('Action Wallet Unit Tests', () => { }); }); + describe('pollExchangeRate()', () => { + it('should call getExchangeRate', async () => { + sandbox.stub(wallet, 'getExchangeRate'); + await wallet.pollExchangeRate(); + await nap(30); + expect(wallet.getExchangeRate.callCount, 'to be greater than', 1); + }); + }); + describe('getExchangeRate()', () => { it('should get exchange rate', async () => { nock('https://blockchain.info')