1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

fix contract exchange rate race condition (#10414)

This commit is contained in:
Brad Decker 2021-02-12 11:41:53 -06:00 committed by GitHub
parent b9a3d3442f
commit 4c5edea294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 24 deletions

View File

@ -19,10 +19,13 @@ export default class TokenRatesController {
* *
* @param {Object} [config] - Options to configure controller * @param {Object} [config] - Options to configure controller
*/ */
constructor({ currency, preferences } = {}) { constructor({ preferences, getNativeCurrency } = {}) {
this.store = new ObservableStore(); this.store = new ObservableStore();
this.currency = currency; this.getNativeCurrency = getNativeCurrency;
this.preferences = preferences; this.tokens = preferences.getState().tokens;
preferences.subscribe(({ tokens = [] }) => {
this.tokens = tokens;
});
} }
/** /**
@ -30,9 +33,7 @@ export default class TokenRatesController {
*/ */
async updateExchangeRates() { async updateExchangeRates() {
const contractExchangeRates = {}; const contractExchangeRates = {};
const nativeCurrency = this.currency const nativeCurrency = this.getNativeCurrency().toLowerCase();
? this.currency.state.nativeCurrency.toLowerCase()
: 'eth';
const pairs = this._tokens.map((token) => token.address).join(','); const pairs = this._tokens.map((token) => token.address).join(',');
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`; const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`;
if (this._tokens.length > 0) { if (this._tokens.length > 0) {
@ -60,21 +61,6 @@ export default class TokenRatesController {
} }
/* eslint-disable accessor-pairs */ /* 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} * @type {Array}
*/ */

View File

@ -169,8 +169,11 @@ export default class MetamaskController extends EventEmitter {
// token exchange rate tracker // token exchange rate tracker
this.tokenRatesController = new TokenRatesController({ this.tokenRatesController = new TokenRatesController({
currency: this.currencyRateController,
preferences: this.preferencesController.store, preferences: this.preferencesController.store,
getNativeCurrency: () => {
const { ticker } = this.networkController.getProviderConfig();
return ticker ?? 'ETH';
},
}); });
this.ensController = new EnsController({ this.ensController = new EnsController({

View File

@ -4,10 +4,19 @@ import { ObservableStore } from '@metamask/obs-store';
import TokenRatesController from '../../../../app/scripts/controllers/token-rates'; import TokenRatesController from '../../../../app/scripts/controllers/token-rates';
describe('TokenRatesController', function () { describe('TokenRatesController', function () {
let nativeCurrency;
let getNativeCurrency;
beforeEach(function () {
nativeCurrency = 'ETH';
getNativeCurrency = () => nativeCurrency;
});
it('should listen for preferences store updates', function () { it('should listen for preferences store updates', function () {
const preferences = new ObservableStore({ tokens: [] }); const preferences = new ObservableStore({ tokens: [] });
preferences.putState({ tokens: ['foo'] }); preferences.putState({ tokens: ['foo'] });
const controller = new TokenRatesController({ preferences }); const controller = new TokenRatesController({
preferences,
getNativeCurrency,
});
assert.deepEqual(controller._tokens, ['foo']); assert.deepEqual(controller._tokens, ['foo']);
}); });
@ -15,7 +24,10 @@ describe('TokenRatesController', function () {
const stub = sinon.stub(global, 'setInterval'); const stub = sinon.stub(global, 'setInterval');
const preferences = new ObservableStore({ tokens: [] }); const preferences = new ObservableStore({ tokens: [] });
preferences.putState({ tokens: ['foo'] }); preferences.putState({ tokens: ['foo'] });
const controller = new TokenRatesController({ preferences }); const controller = new TokenRatesController({
preferences,
getNativeCurrency,
});
controller.start(1337); controller.start(1337);
assert.strictEqual(stub.getCall(0).args[1], 1337); assert.strictEqual(stub.getCall(0).args[1], 1337);