From d909bde8ead2447f4746e1b435ad71179df4ec88 Mon Sep 17 00:00:00 2001 From: Brian Bergeron Date: Mon, 2 Dec 2024 16:12:59 -0800 Subject: [PATCH] fix(cherry-pick): use PORTFOLIO_VIEW flag to determine token list polling (#28585) Cherry picks https://github.com/MetaMask/metamask-extension/pull/28579 to 12.8.0 so chains aren't polled unnecessarily Co-authored-by: Dan J Miller --- ...rs-after-init-opt-in-background-state.json | 6 +----- .../errors-after-init-opt-in-ui-state.json | 6 +----- ui/hooks/useTokenListPolling.test.ts | 21 +++++++++++-------- ui/hooks/useTokenListPolling.ts | 8 ++++++- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-background-state.json b/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-background-state.json index 999dce99ca0c..9b0571e204d5 100644 --- a/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-background-state.json +++ b/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-background-state.json @@ -317,11 +317,7 @@ "TokenListController": { "tokenList": "object", "tokensChainsCache": { - "0x1": "object", - "0x539": "object", - "0xaa36a7": "object", - "0xe705": "object", - "0xe708": "object" + "0x539": "object" }, "preventPollingOnNetworkRestart": false }, diff --git a/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-ui-state.json b/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-ui-state.json index acd9d6f8d074..60125eaa8d5f 100644 --- a/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-ui-state.json +++ b/test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-ui-state.json @@ -175,11 +175,7 @@ "nonRPCGasFeeApisDisabled": "boolean", "tokenList": "object", "tokensChainsCache": { - "0x1": "object", - "0x539": "object", - "0xaa36a7": "object", - "0xe705": "object", - "0xe708": "object" + "0x539": "object" }, "preventPollingOnNetworkRestart": false, "tokens": "object", diff --git a/ui/hooks/useTokenListPolling.test.ts b/ui/hooks/useTokenListPolling.test.ts index 09a22fffea50..001dca71c80e 100644 --- a/ui/hooks/useTokenListPolling.test.ts +++ b/ui/hooks/useTokenListPolling.test.ts @@ -22,16 +22,23 @@ describe('useTokenListPolling', () => { jest.clearAllMocks(); }); - it('should poll for token lists on each chain when enabled, and stop on dismount', async () => { + it('should poll the selected network when enabled, and stop on dismount', async () => { const state = { metamask: { isUnlocked: true, completedOnboarding: true, useExternalServices: true, useTokenDetection: true, + selectedNetworkClientId: 'selectedNetworkClientId', networkConfigurationsByChainId: { - '0x1': {}, - '0x89': {}, + '0x1': { + chainId: '0x1', + rpcEndpoints: [ + { + networkClientId: 'selectedNetworkClientId', + }, + ], + }, }, }, }; @@ -43,19 +50,15 @@ describe('useTokenListPolling', () => { // Should poll each chain await Promise.all(mockPromises); - expect(tokenListStartPolling).toHaveBeenCalledTimes(2); + expect(tokenListStartPolling).toHaveBeenCalledTimes(1); expect(tokenListStartPolling).toHaveBeenCalledWith('0x1'); - expect(tokenListStartPolling).toHaveBeenCalledWith('0x89'); // Stop polling on dismount unmount(); - expect(tokenListStopPollingByPollingToken).toHaveBeenCalledTimes(2); + expect(tokenListStopPollingByPollingToken).toHaveBeenCalledTimes(1); expect(tokenListStopPollingByPollingToken).toHaveBeenCalledWith( '0x1_token', ); - expect(tokenListStopPollingByPollingToken).toHaveBeenCalledWith( - '0x89_token', - ); }); it('should not poll before onboarding is completed', async () => { diff --git a/ui/hooks/useTokenListPolling.ts b/ui/hooks/useTokenListPolling.ts index 9b43c3c6959a..7f7de517c304 100644 --- a/ui/hooks/useTokenListPolling.ts +++ b/ui/hooks/useTokenListPolling.ts @@ -1,5 +1,6 @@ import { useSelector } from 'react-redux'; import { + getCurrentChainId, getNetworkConfigurationsByChainId, getPetnamesEnabled, getUseExternalServices, @@ -17,6 +18,7 @@ import { import useMultiPolling from './useMultiPolling'; const useTokenListPolling = () => { + const currentChainId = useSelector(getCurrentChainId); const networkConfigurations = useSelector(getNetworkConfigurationsByChainId); const useTokenDetection = useSelector(getUseTokenDetection); const useTransactionSimulations = useSelector(getUseTransactionSimulations); @@ -31,10 +33,14 @@ const useTokenListPolling = () => { useExternalServices && (useTokenDetection || petnamesEnabled || useTransactionSimulations); + const chainIds = process.env.PORTFOLIO_VIEW + ? Object.keys(networkConfigurations) + : [currentChainId]; + useMultiPolling({ startPolling: tokenListStartPolling, stopPollingByPollingToken: tokenListStopPollingByPollingToken, - input: enabled ? Object.keys(networkConfigurations) : [], + input: enabled ? chainIds : [], }); return {};