diff --git a/packages/assets-controllers/src/TokenRatesController.test.ts b/packages/assets-controllers/src/TokenRatesController.test.ts index 2fcfdb46e68..dbfcffc0f39 100644 --- a/packages/assets-controllers/src/TokenRatesController.test.ts +++ b/packages/assets-controllers/src/TokenRatesController.test.ts @@ -985,7 +985,7 @@ describe('TokenRatesController', () => { }); describe('when polling is active', () => { - it('should update exchange rates when selected address changes', async () => { + it('should not update exchange rates when selected address changes', async () => { const alternateSelectedAddress = '0x0000000000000000000000000000000000000002'; const alternateSelectedAccount = createMockInternalAccount({ @@ -1024,7 +1024,7 @@ describe('TokenRatesController', () => { .mockResolvedValue(); triggerSelectedAccountChange(alternateSelectedAccount); - expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1); + expect(updateExchangeRatesSpy).not.toHaveBeenCalled(); }, ); }); @@ -1581,7 +1581,7 @@ describe('TokenRatesController', () => { ); }); - it('does not update state if there are no tokens for the given chain and address', async () => { + it('does not update state if there are no tokens for the given chain', async () => { await withController( async ({ controller, @@ -1589,23 +1589,10 @@ describe('TokenRatesController', () => { triggerNetworkStateChange, }) => { const tokenAddress = '0x0000000000000000000000000000000000000001'; - const differentAccount = - '0x1000000000000000000000000000000000000000'; controller.enable(); await callUpdateExchangeRatesMethod({ allTokens: { - // These tokens are for the right chain but wrong account - [ChainId.mainnet]: { - [differentAccount]: [ - { - address: tokenAddress, - decimals: 18, - symbol: 'TST', - aggregators: [], - }, - ], - }, - // These tokens are for the right account but wrong chain + // These tokens are on a different chain [toHex(2)]: { [defaultSelectedAddress]: [ { @@ -1713,7 +1700,10 @@ describe('TokenRatesController', () => { await callUpdateExchangeRatesMethod({ allTokens: { [chainId]: { - [defaultSelectedAddress]: tokens, + [defaultSelectedAddress]: tokens.slice(0, 100), + // Include tokens from non selected addresses + '0x0000000000000000000000000000000000000123': + tokens.slice(100), }, }, chainId, @@ -1748,6 +1738,7 @@ describe('TokenRatesController', () => { const tokenAddresses = [ '0x0000000000000000000000000000000000000001', '0x0000000000000000000000000000000000000002', + '0x0000000000000000000000000000000000000003', ]; const tokenPricesService = buildMockTokenPricesService({ fetchTokenPrices: jest.fn().mockResolvedValue({ @@ -1761,6 +1752,11 @@ describe('TokenRatesController', () => { tokenAddress: tokenAddresses[1], value: 0.002, }, + [tokenAddresses[2]]: { + currency: 'ETH', + tokenAddress: tokenAddresses[2], + value: 0.003, + }, }), }); await withController( @@ -1787,6 +1783,15 @@ describe('TokenRatesController', () => { aggregators: [], }, ], + // Include tokens from non selected addresses + '0x0000000000000000000000000000000000000123': [ + { + address: tokenAddresses[2], + decimals: 18, + symbol: 'TST1', + aggregators: [], + }, + ], }, }, chainId: ChainId.mainnet, @@ -1812,6 +1817,11 @@ describe('TokenRatesController', () => { "tokenAddress": "0x0000000000000000000000000000000000000002", "value": 0.002, }, + "0x0000000000000000000000000000000000000003": Object { + "currency": "ETH", + "tokenAddress": "0x0000000000000000000000000000000000000003", + "value": 0.003, + }, }, }, } diff --git a/packages/assets-controllers/src/TokenRatesController.ts b/packages/assets-controllers/src/TokenRatesController.ts index 102e52a9e24..aeddfbfcb0e 100644 --- a/packages/assets-controllers/src/TokenRatesController.ts +++ b/packages/assets-controllers/src/TokenRatesController.ts @@ -301,8 +301,6 @@ export class TokenRatesController extends StaticIntervalPollingController< this.#subscribeToTokensStateChange(); this.#subscribeToNetworkStateChange(); - - this.#subscribeToAccountChange(); } #subscribeToTokensStateChange() { @@ -356,45 +354,22 @@ export class TokenRatesController extends StaticIntervalPollingController< ); } - #subscribeToAccountChange() { - this.messagingSystem.subscribe( - 'AccountsController:selectedEvmAccountChange', - // TODO: Either fix this lint violation or explain why it's necessary to ignore. - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async (selectedAccount) => { - if (this.#selectedAccountId !== selectedAccount.id) { - this.#selectedAccountId = selectedAccount.id; - if (this.#pollState === PollState.Active) { - await this.updateExchangeRates(); - } - } - }, - ); - } - /** - * Get the user's tokens for the given chain. + * Get the tokens for the given chain. * * @param chainId - The chain ID. * @returns The list of tokens addresses for the current chain */ #getTokenAddresses(chainId: Hex): Hex[] { - const selectedAccount = this.messagingSystem.call( - 'AccountsController:getAccount', - this.#selectedAccountId, - ); - const selectedAddress = selectedAccount?.address ?? ''; - const tokens = this.#allTokens[chainId]?.[selectedAddress] || []; - const detectedTokens = - this.#allDetectedTokens[chainId]?.[selectedAddress] || []; - - return [ - ...new Set( - [...tokens, ...detectedTokens].map((token) => - toHex(toChecksumHexAddress(token.address)), - ), - ), - ].sort(); + const getTokens = (allTokens: Record) => + Object.values(allTokens ?? {}).flatMap((tokens) => + tokens.map(({ address }) => toHex(toChecksumHexAddress(address))), + ); + + const tokenAddresses = getTokens(this.#allTokens[chainId]); + const detectedTokenAddresses = getTokens(this.#allDetectedTokens[chainId]); + + return [...new Set([...tokenAddresses, ...detectedTokenAddresses])].sort(); } /**