diff --git a/app/scripts/controllers/token-rates.js b/app/scripts/controllers/token-rates.js index a3f0f6710..9d597ce1b 100644 --- a/app/scripts/controllers/token-rates.js +++ b/app/scripts/controllers/token-rates.js @@ -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} */ diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 7d85e903c..eea666f3c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -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({ diff --git a/test/unit/app/controllers/token-rates-controller.js b/test/unit/app/controllers/token-rates-controller.js index c1f69d54f..6c23abbaf 100644 --- a/test/unit/app/controllers/token-rates-controller.js +++ b/test/unit/app/controllers/token-rates-controller.js @@ -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);