Skip to content

Commit

Permalink
fix: PortfolioView: Selector to determine networks to poll (#28502)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

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

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **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
darkwing authored Nov 18, 2024
1 parent ee75939 commit ec8e5fb
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,58 @@ export const getAllEnabledNetworks = createDeepEqualSelector(
),
);

export const getChainIdsToPoll = createDeepEqualSelector(
getPreferences,
getNetworkConfigurationsByChainId,
getCurrentChainId,
(preferences, networkConfigurations, currentChainId) => {
const { pausedChainIds = [] } = preferences;

if (!process.env.PORTFOLIO_VIEW) {
return [currentChainId];
}

return Object.keys(networkConfigurations).filter(
(chainId) =>
!TEST_CHAINS.includes(chainId) && !pausedChainIds.includes(chainId),
);
},
);

export const getNetworkClientIdsToPoll = createDeepEqualSelector(
getPreferences,
getNetworkConfigurationsByChainId,
getCurrentChainId,
(preferences, networkConfigurations, currentChainId) => {
const { pausedChainIds = [] } = preferences;

if (!process.env.PORTFOLIO_VIEW) {
const networkConfiguration = networkConfigurations[currentChainId];
return [
networkConfiguration.rpcEndpoints[
networkConfiguration.defaultRpcEndpointIndex
].networkClientId,
];
}

return Object.entries(networkConfigurations).reduce(
(acc, [chainId, network]) => {
if (
!TEST_CHAINS.includes(chainId) &&
!pausedChainIds.includes(chainId)
) {
acc.push(
network.rpcEndpoints[network.defaultRpcEndpointIndex]
.networkClientId,
);
}
return acc;
},
[],
);
},
);

/**
* To retrieve the maxBaseFee and priorityFee the user has set as default
*
Expand Down
117 changes: 117 additions & 0 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,123 @@ describe('Selectors', () => {
});
});

describe('#getChainIdsToPoll', () => {
const networkConfigurationsByChainId = {
[CHAIN_IDS.MAINNET]: {
chainId: CHAIN_IDS.MAINNET,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'mainnet' }],
},
[CHAIN_IDS.LINEA_MAINNET]: {
chainId: CHAIN_IDS.LINEA_MAINNET,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'linea-mainnet' }],
},
[CHAIN_IDS.SEPOLIA]: {
chainId: CHAIN_IDS.SEPOLIA,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'sepolia' }],
},
[CHAIN_IDS.LINEA_SEPOLIA]: {
chainId: CHAIN_IDS.LINEA_SEPOLIA,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'linea-sepolia' }],
},
};

beforeEach(() => {
process.env.PORTFOLIO_VIEW = 'true';
});

afterEach(() => {
process.env.PORTFOLIO_VIEW = undefined;
});

it('returns only non-test chain IDs', () => {
const chainIds = selectors.getChainIdsToPoll({
metamask: {
preferences: { pausedChainIds: [] },
networkConfigurationsByChainId,
selectedNetworkClientId: 'mainnet',
},
});
expect(Object.values(chainIds)).toHaveLength(2);
expect(chainIds).toStrictEqual([
CHAIN_IDS.MAINNET,
CHAIN_IDS.LINEA_MAINNET,
]);
});

it('does not return paused chain IDs', () => {
const chainIds = selectors.getChainIdsToPoll({
metamask: {
preferences: { pausedChainIds: [CHAIN_IDS.LINEA_MAINNET] },
networkConfigurationsByChainId,
selectedNetworkClientId: 'mainnet',
},
});
expect(Object.values(chainIds)).toHaveLength(1);
expect(chainIds).toStrictEqual([CHAIN_IDS.MAINNET]);
});
});

describe('#getNetworkClientIdsToPoll', () => {
const networkConfigurationsByChainId = {
[CHAIN_IDS.MAINNET]: {
chainId: CHAIN_IDS.MAINNET,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'mainnet' }],
},
[CHAIN_IDS.LINEA_MAINNET]: {
chainId: CHAIN_IDS.LINEA_MAINNET,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'linea-mainnet' }],
},
[CHAIN_IDS.SEPOLIA]: {
chainId: CHAIN_IDS.SEPOLIA,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'sepolia' }],
},
[CHAIN_IDS.LINEA_SEPOLIA]: {
chainId: CHAIN_IDS.LINEA_SEPOLIA,
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 'linea-sepolia' }],
},
};

beforeEach(() => {
process.env.PORTFOLIO_VIEW = 'true';
});

afterEach(() => {
process.env.PORTFOLIO_VIEW = undefined;
});

it('returns only non-test chain IDs', () => {
const chainIds = selectors.getNetworkClientIdsToPoll({
metamask: {
preferences: { pausedChainIds: [] },
networkConfigurationsByChainId,
selectedNetworkClientId: 'mainnet',
},
});
expect(Object.values(chainIds)).toHaveLength(2);
expect(chainIds).toStrictEqual(['mainnet', 'linea-mainnet']);
});

it('does not return paused chain IDs', () => {
const chainIds = selectors.getNetworkClientIdsToPoll({
metamask: {
preferences: { pausedChainIds: [CHAIN_IDS.LINEA_MAINNET] },
networkConfigurationsByChainId,
selectedNetworkClientId: 'mainnet',
},
});
expect(Object.values(chainIds)).toHaveLength(1);
expect(chainIds).toStrictEqual(['mainnet']);
});
});

describe('#isHardwareWallet', () => {
it('returns false if it is not a HW wallet', () => {
const mockStateWithImported = modifyStateWithHWKeyring(
Expand Down

0 comments on commit ec8e5fb

Please sign in to comment.