mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
fix contract exchange rate race condition (#10414)
This commit is contained in:
parent
b9a3d3442f
commit
4c5edea294
@ -19,10 +19,13 @@ 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;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -30,9 +33,7 @@ export default class TokenRatesController {
|
||||
*/
|
||||
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) {
|
||||
@ -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}
|
||||
*/
|
||||
|
@ -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({
|
||||
|
@ -4,10 +4,19 @@ 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']);
|
||||
});
|
||||
|
||||
@ -15,7 +24,10 @@ describe('TokenRatesController', 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);
|
||||
|
Loading…
Reference in New Issue
Block a user