diff --git a/packages/assets-controllers/src/RatesController/RatesController.test.ts b/packages/assets-controllers/src/RatesController/RatesController.test.ts index 17e00f3326..12277764f5 100644 --- a/packages/assets-controllers/src/RatesController/RatesController.test.ts +++ b/packages/assets-controllers/src/RatesController/RatesController.test.ts @@ -100,8 +100,8 @@ describe('RatesController', () => { const { fiatCurrency, rates, cryptocurrencies } = ratesController.state; expect(ratesController).toBeDefined(); expect(fiatCurrency).toBe('usd'); - expect(Object.keys(rates)).toStrictEqual(['btc']); - expect(cryptocurrencies).toStrictEqual(['btc']); + expect(Object.keys(rates)).toStrictEqual([Cryptocurrency.Btc]); + expect(cryptocurrencies).toStrictEqual([Cryptocurrency.Btc]); }); }); @@ -315,7 +315,7 @@ describe('RatesController', () => { describe('setCryptocurrencyList', () => { it('updates the cryptocurrency list', async () => { const fetchExchangeRateStub = jest.fn().mockResolvedValue({}); - const mockCryptocurrencyList = [Cryptocurrency.Btc]; + const mockCryptocurrencyList: Cryptocurrency[] = []; // Different from default list const ratesController = setupRatesController({ interval: 150, initialState: {}, @@ -326,7 +326,11 @@ describe('RatesController', () => { const cryptocurrencyListPreUpdate = ratesController.getCryptocurrencyList(); - expect(cryptocurrencyListPreUpdate).toStrictEqual(['btc']); + expect(cryptocurrencyListPreUpdate).toStrictEqual([Cryptocurrency.Btc]); + // Just to make sure we're updating to something else than the default list + expect(cryptocurrencyListPreUpdate).not.toStrictEqual( + mockCryptocurrencyList, + ); await ratesController.setCryptocurrencyList(mockCryptocurrencyList); const cryptocurrencyListPostUpdate = diff --git a/packages/assets-controllers/src/RatesController/RatesController.ts b/packages/assets-controllers/src/RatesController/RatesController.ts index 70c56990a2..461e471d57 100644 --- a/packages/assets-controllers/src/RatesController/RatesController.ts +++ b/packages/assets-controllers/src/RatesController/RatesController.ts @@ -1,5 +1,6 @@ import { BaseController } from '@metamask/base-controller'; import { Mutex } from 'async-mutex'; +import type { Draft } from 'immer'; import { fetchMultiExchangeRate as defaultFetchExchangeRate } from '../crypto-compare-service'; import type { @@ -134,12 +135,14 @@ export class RatesController extends BaseController< }; } - this.update(() => { - return { - ...this.state, - rates: updatedRates, - }; - }); + this.update( + (state: Draft): RatesControllerState => { + return { + ...state, + rates: updatedRates, + }; + }, + ); }); } @@ -182,16 +185,20 @@ export class RatesController extends BaseController< /** * Sets the list of supported cryptocurrencies. - * @param list - The list of supported cryptocurrencies. + * @param cryptocurrencies - The list of supported cryptocurrencies. */ - async setCryptocurrencyList(list: Cryptocurrency[]): Promise { + async setCryptocurrencyList( + cryptocurrencies: Cryptocurrency[], + ): Promise { await this.#withLock(() => { - this.update(() => { - return { - ...this.state, - fromCurrencies: list, - }; - }); + this.update( + (state: Draft): RatesControllerState => { + return { + ...state, + cryptocurrencies, + }; + }, + ); }); } @@ -205,12 +212,14 @@ export class RatesController extends BaseController< } await this.#withLock(() => { - this.update(() => { - return { - ...defaultState, - fiatCurrency, - }; - }); + this.update( + (state: Draft): RatesControllerState => { + return { + ...state, + fiatCurrency, + }; + }, + ); }); await this.#updateRates(); }