Skip to content

Commit

Permalink
fix contract exchange rate race condition (#10414)
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker authored Feb 12, 2021
1 parent b9a3d34 commit 4c5edea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
28 changes: 7 additions & 21 deletions app/scripts/controllers/token-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ export default class TokenRatesController {
*
* @param {Object} [config] - Options to configure controller
*/
constructor({ currency, preferences } = {}) {
constructor({ preferences, getNativeCurrency } = {}) {
this.store = new ObservableStore();
this.currency = currency;
this.preferences = preferences;
this.getNativeCurrency = getNativeCurrency;
this.tokens = preferences.getState().tokens;
preferences.subscribe(({ tokens = [] }) => {
this.tokens = tokens;
});
}

/**
* Updates exchange rates for all tokens
*/
async updateExchangeRates() {
const contractExchangeRates = {};
const nativeCurrency = this.currency
? this.currency.state.nativeCurrency.toLowerCase()
: 'eth';
const nativeCurrency = this.getNativeCurrency().toLowerCase();
const pairs = this._tokens.map((token) => token.address).join(',');
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`;
if (this._tokens.length > 0) {
Expand Down Expand Up @@ -60,21 +61,6 @@ export default class TokenRatesController {
}

/* eslint-disable accessor-pairs */
/**
* @type {Object}
*/
set preferences(preferences) {
this._preferences && this._preferences.unsubscribe();
if (!preferences) {
return;
}
this._preferences = preferences;
this.tokens = preferences.getState().tokens;
preferences.subscribe(({ tokens = [] }) => {
this.tokens = tokens;
});
}

/**
* @type {Array}
*/
Expand Down
5 changes: 4 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ export default class MetamaskController extends EventEmitter {

// token exchange rate tracker
this.tokenRatesController = new TokenRatesController({
currency: this.currencyRateController,
preferences: this.preferencesController.store,
getNativeCurrency: () => {
const { ticker } = this.networkController.getProviderConfig();
return ticker ?? 'ETH';
},
});

this.ensController = new EnsController({
Expand Down
16 changes: 14 additions & 2 deletions test/unit/app/controllers/token-rates-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import { ObservableStore } from '@metamask/obs-store';
import TokenRatesController from '../../../../app/scripts/controllers/token-rates';

describe('TokenRatesController', function () {
let nativeCurrency;
let getNativeCurrency;
beforeEach(function () {
nativeCurrency = 'ETH';
getNativeCurrency = () => nativeCurrency;
});
it('should listen for preferences store updates', function () {
const preferences = new ObservableStore({ tokens: [] });
preferences.putState({ tokens: ['foo'] });
const controller = new TokenRatesController({ preferences });
const controller = new TokenRatesController({
preferences,
getNativeCurrency,
});
assert.deepEqual(controller._tokens, ['foo']);
});

it('should poll on correct interval', async function () {
const stub = sinon.stub(global, 'setInterval');
const preferences = new ObservableStore({ tokens: [] });
preferences.putState({ tokens: ['foo'] });
const controller = new TokenRatesController({ preferences });
const controller = new TokenRatesController({
preferences,
getNativeCurrency,
});
controller.start(1337);

assert.strictEqual(stub.getCall(0).args[1], 1337);
Expand Down

0 comments on commit 4c5edea

Please sign in to comment.