-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multi chain polling for token prices (#28158)
<!-- 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** Multi chain polling for the token rates controller. This will fetch erc20 token prices across all evm chains. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28158?quickstart=1) ## **Related issues** ## **Manual testing steps** no visual changes, you should just see the network tab hitting price api across multiple chains, correct prices when switching chains, when adding new tokens, and `marketData` updated in state across chains ## **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. --------- Co-authored-by: MetaMask Bot <[email protected]> Co-authored-by: sahar-fehri <[email protected]>
- Loading branch information
1 parent
77b77a8
commit eab6233
Showing
11 changed files
with
183 additions
and
41 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { ReactNode } from 'react'; | ||
import useCurrencyRatePolling from '../hooks/useCurrencyRatePolling'; | ||
import useTokenRatesPolling from '../hooks/useTokenRatesPolling'; | ||
|
||
// This provider is a step towards making controller polling fully UI based. | ||
// Eventually, individual UI components will call the use*Polling hooks to | ||
// poll and return particular data. This polls globally in the meantime. | ||
export const AssetPollingProvider = ({ children }: { children: ReactNode }) => { | ||
useCurrencyRatePolling(); | ||
useTokenRatesPolling(); | ||
|
||
return <>{children}</>; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
type UseMultiPollingOptions<PollingInput> = { | ||
startPolling: (input: PollingInput) => Promise<string>; | ||
stopPollingByPollingToken: (pollingToken: string) => void; | ||
input: PollingInput[]; | ||
}; | ||
|
||
// A hook that manages multiple polling loops of a polling controller. | ||
// Callers provide an array of inputs, and the hook manages starting | ||
// and stopping polling loops for each input. | ||
const useMultiPolling = <PollingInput>( | ||
usePollingOptions: UseMultiPollingOptions<PollingInput>, | ||
) => { | ||
const [polls, setPolls] = useState(new Map()); | ||
|
||
useEffect(() => { | ||
// start new polls | ||
for (const input of usePollingOptions.input) { | ||
const key = JSON.stringify(input); | ||
if (!polls.has(key)) { | ||
usePollingOptions | ||
.startPolling(input) | ||
.then((token) => | ||
setPolls((prevPolls) => new Map(prevPolls).set(key, token)), | ||
); | ||
} | ||
} | ||
|
||
// stop existing polls | ||
for (const [inputKey, token] of polls.entries()) { | ||
const exists = usePollingOptions.input.some( | ||
(i) => inputKey === JSON.stringify(i), | ||
); | ||
|
||
if (!exists) { | ||
usePollingOptions.stopPollingByPollingToken(token); | ||
setPolls((prevPolls) => { | ||
const newPolls = new Map(prevPolls); | ||
newPolls.delete(inputKey); | ||
return newPolls; | ||
}); | ||
} | ||
} | ||
}, [usePollingOptions.input && JSON.stringify(usePollingOptions.input)]); | ||
|
||
// stop all polling on dismount | ||
useEffect(() => { | ||
return () => { | ||
for (const token of polls.values()) { | ||
usePollingOptions.stopPollingByPollingToken(token); | ||
} | ||
}; | ||
}, []); | ||
}; | ||
|
||
export default useMultiPolling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { | ||
getMarketData, | ||
getNetworkConfigurationsByChainId, | ||
getTokenExchangeRates, | ||
getTokensMarketData, | ||
getUseCurrencyRateCheck, | ||
} from '../selectors'; | ||
import { | ||
tokenRatesStartPolling, | ||
tokenRatesStopPollingByPollingToken, | ||
} from '../store/actions'; | ||
import useMultiPolling from './useMultiPolling'; | ||
|
||
const useTokenRatesPolling = ({ chainIds }: { chainIds?: string[] } = {}) => { | ||
// Selectors to determine polling input | ||
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck); | ||
const networkConfigurations = useSelector(getNetworkConfigurationsByChainId); | ||
|
||
// Selectors returning state updated by the polling | ||
const tokenExchangeRates = useSelector(getTokenExchangeRates); | ||
const tokensMarketData = useSelector(getTokensMarketData); | ||
const marketData = useSelector(getMarketData); | ||
|
||
useMultiPolling({ | ||
startPolling: tokenRatesStartPolling, | ||
stopPollingByPollingToken: tokenRatesStopPollingByPollingToken, | ||
input: useCurrencyRateCheck | ||
? chainIds ?? Object.keys(networkConfigurations) | ||
: [], | ||
}); | ||
|
||
return { | ||
tokenExchangeRates, | ||
tokensMarketData, | ||
marketData, | ||
}; | ||
}; | ||
|
||
export default useTokenRatesPolling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters