Skip to content

Commit

Permalink
fix(cherry-pick): use PORTFOLIO_VIEW flag to determine token list pol…
Browse files Browse the repository at this point in the history
…ling (#28585)

Cherry picks #28579
to 12.8.0 so chains aren't polled unnecessarily

Co-authored-by: Dan J Miller <[email protected]>
  • Loading branch information
bergeron and danjm authored Dec 3, 2024
1 parent 8e074a6 commit d909bde
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,7 @@
"TokenListController": {
"tokenList": "object",
"tokensChainsCache": {
"0x1": "object",
"0x539": "object",
"0xaa36a7": "object",
"0xe705": "object",
"0xe708": "object"
"0x539": "object"
},
"preventPollingOnNetworkRestart": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 12 additions & 9 deletions ui/hooks/useTokenListPolling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
},
},
};
Expand All @@ -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 () => {
Expand Down
8 changes: 7 additions & 1 deletion ui/hooks/useTokenListPolling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelector } from 'react-redux';
import {
getCurrentChainId,
getNetworkConfigurationsByChainId,
getPetnamesEnabled,
getUseExternalServices,
Expand All @@ -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);
Expand All @@ -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 {};
Expand Down

0 comments on commit d909bde

Please sign in to comment.