Skip to content

Commit

Permalink
fix: use PORTFOLIO_VIEW flag to determine token list polling (#28579)
Browse files Browse the repository at this point in the history
## **Description**

Updates the token list hook to only poll across chains when
`PORTFOLIO_VIEW` is set.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28579?quickstart=1)

## **Related issues**

## **Manual testing steps**

1. With `PORTFOLIO_VIEW=1`, requests should go to the token api across
all chains.
2. Without `PORTFOLIO_VIEW=1`, requests should go to the token api on
the current chain.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
bergeron committed Nov 20, 2024
1 parent ec359ed commit 8de8e35
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 8de8e35

Please sign in to comment.