Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit d91d7c9

Browse files
authored
Merge pull request #499 from lightninglabs/update-exchange-rate
Update exchange rate every 15 minutes
2 parents 56a5c9c + c81cec5 commit d91d7c9

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/action/wallet.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { observe } from 'mobx';
77
import { toBuffer, parseSat, checkHttpStatus, nap } from '../helper';
8-
import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY } from '../config';
8+
import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY, RATE_DELAY } from '../config';
99
import * as log from './log';
1010

1111
class WalletAction {
@@ -113,7 +113,7 @@ class WalletAction {
113113
this.getBalance(),
114114
this.getChannelBalance(),
115115
this.getNewAddress(),
116-
this.getExchangeRate(),
116+
this.pollExchangeRate(),
117117
]);
118118
}
119119

@@ -279,6 +279,17 @@ class WalletAction {
279279
}
280280
}
281281

282+
/**
283+
* Poll for the current btc/fiat exchange rate based on the currently selected
284+
* fiat currency every 15 minutes.
285+
* @return {Promise<undefined>}
286+
*/
287+
async pollExchangeRate() {
288+
await this.getExchangeRate();
289+
clearTimeout(this.tPollRate);
290+
this.tPollRate = setTimeout(() => this.pollExchangeRate(), RATE_DELAY);
291+
}
292+
282293
/**
283294
* Fetch a current btc/fiat exchange rate based on the currently selected
284295
* fiat currency and persist the value on disk for the next time the app

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports.RETRY_DELAY = 3000;
22
module.exports.LND_INIT_DELAY = 5000;
33
module.exports.NOTIFICATION_DELAY = 5000;
4+
module.exports.RATE_DELAY = 15 * 60 * 1000;
45

56
module.exports.LND_PORT = 10009;
67
module.exports.LND_PEER_PORT = 10019;

test/unit/action/wallet.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import WalletAction from '../../../src/action/wallet';
55
import NavAction from '../../../src/action/nav';
66
import NotificationAction from '../../../src/action/notification';
77
import * as logger from '../../../src/action/log';
8+
import { nap } from '../../../src/helper';
89
import nock from 'nock';
910
import 'isomorphic-fetch';
1011

@@ -23,6 +24,7 @@ describe('Action Wallet Unit Tests', () => {
2324
store = new Store();
2425
require('../../../src/config').RETRY_DELAY = 1;
2526
require('../../../src/config').NOTIFICATION_DELAY = 1;
27+
require('../../../src/config').RATE_DELAY = 1;
2628
grpc = sinon.createStubInstance(GrpcAction);
2729
db = sinon.createStubInstance(AppStorage);
2830
notification = sinon.createStubInstance(NotificationAction);
@@ -31,6 +33,7 @@ describe('Action Wallet Unit Tests', () => {
3133
});
3234

3335
afterEach(() => {
36+
clearTimeout(wallet.tPollRate);
3437
sandbox.restore();
3538
});
3639

@@ -106,10 +109,10 @@ describe('Action Wallet Unit Tests', () => {
106109

107110
describe('update()', () => {
108111
it('should refresh balances, exchange rate and address', async () => {
109-
sandbox.stub(wallet, 'getExchangeRate');
112+
sandbox.stub(wallet, 'pollExchangeRate');
110113
await wallet.update();
111114
expect(grpc.sendCommand, 'was called thrice');
112-
expect(wallet.getExchangeRate, 'was called once');
115+
expect(wallet.pollExchangeRate, 'was called once');
113116
});
114117
});
115118

@@ -324,6 +327,15 @@ describe('Action Wallet Unit Tests', () => {
324327
});
325328
});
326329

330+
describe('pollExchangeRate()', () => {
331+
it('should call getExchangeRate', async () => {
332+
sandbox.stub(wallet, 'getExchangeRate');
333+
await wallet.pollExchangeRate();
334+
await nap(30);
335+
expect(wallet.getExchangeRate.callCount, 'to be greater than', 1);
336+
});
337+
});
338+
327339
describe('getExchangeRate()', () => {
328340
it('should get exchange rate', async () => {
329341
nock('https://blockchain.info')

0 commit comments

Comments
 (0)