Skip to content

Commit

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

Updates the token price and detection hooks 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/28504?quickstart=1)

## **Related issues**

## **Manual testing steps**

1. With `PORTFOLIO_VIEW=1`, requests should go to the price api across
all chains.
2. Without `PORTFOLIO_VIEW=1`, requests should go to the price 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 authored Nov 20, 2024
1 parent 62430fb commit 94a7b20
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ async function mockTokenPriceApi(
statusCode: 200,
json: {},
})),
// linea
await mockServer
.forGet('https://price.api.cx.metamask.io/v2/chains/59144/spot-prices')
.thenCallback(() => ({
statusCode: 200,
json: {},
})),
];
}

Expand Down
8 changes: 7 additions & 1 deletion ui/hooks/useTokenDetectionPolling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelector } from 'react-redux';
import {
getCurrentChainId,
getNetworkConfigurationsByChainId,
getUseTokenDetection,
} from '../selectors';
Expand All @@ -17,14 +18,19 @@ const useTokenDetectionPolling = () => {
const useTokenDetection = useSelector(getUseTokenDetection);
const completedOnboarding = useSelector(getCompletedOnboarding);
const isUnlocked = useSelector(getIsUnlocked);
const currentChainId = useSelector(getCurrentChainId);
const networkConfigurations = useSelector(getNetworkConfigurationsByChainId);

const enabled = completedOnboarding && isUnlocked && useTokenDetection;

const chainIds = process.env.PORTFOLIO_VIEW
? Object.keys(networkConfigurations)
: [currentChainId];

useMultiPolling({
startPolling: tokenDetectionStartPolling,
stopPollingByPollingToken: tokenDetectionStopPollingByPollingToken,
input: enabled ? [Object.keys(networkConfigurations)] : [],
input: enabled ? [chainIds] : [],
});

return {};
Expand Down
10 changes: 8 additions & 2 deletions ui/hooks/useTokenRatesPolling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelector } from 'react-redux';
import {
getCurrentChainId,
getMarketData,
getNetworkConfigurationsByChainId,
getTokenExchangeRates,
Expand All @@ -16,10 +17,11 @@ import {
} from '../ducks/metamask/metamask';
import useMultiPolling from './useMultiPolling';

const useTokenRatesPolling = ({ chainIds }: { chainIds?: string[] } = {}) => {
const useTokenRatesPolling = () => {
// Selectors to determine polling input
const completedOnboarding = useSelector(getCompletedOnboarding);
const isUnlocked = useSelector(getIsUnlocked);
const currentChainId = useSelector(getCurrentChainId);
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
const networkConfigurations = useSelector(getNetworkConfigurationsByChainId);

Expand All @@ -30,10 +32,14 @@ const useTokenRatesPolling = ({ chainIds }: { chainIds?: string[] } = {}) => {

const enabled = completedOnboarding && isUnlocked && useCurrencyRateCheck;

const chainIds = process.env.PORTFOLIO_VIEW
? Object.keys(networkConfigurations)
: [currentChainId];

useMultiPolling({
startPolling: tokenRatesStartPolling,
stopPollingByPollingToken: tokenRatesStopPollingByPollingToken,
input: enabled ? chainIds ?? Object.keys(networkConfigurations) : [],
input: enabled ? chainIds : [],
});

return {
Expand Down

0 comments on commit 94a7b20

Please sign in to comment.