-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multichain currency rate polling (#12268)
## **Description** This PR uses the `CurrencyRateController` to poll native currency prices across chains. This will keep prices updated in state across all networks. Because we're polling across all networks, it's no longer necessary to trigger updates when switching chains, so these places have been removed. Once we start showing tokens across all networks, "switching chains" will become a less relevant action. ## **Related issues** ## **Manual testing steps** 1. Verify fiat prices are correct for the native currency on a network 2. Switch to a network with a different native currency, 3. Verify fiat prices are correct for that network ## **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 Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.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-mobile/blob/main/.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
Showing
16 changed files
with
111 additions
and
68 deletions.
There are no files selected for viewing
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
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
11 changes: 11 additions & 0 deletions
11
app/components/hooks/AssetPolling/AssetPollingProvider.tsx
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,11 @@ | ||
import React, { ReactNode } from 'react'; | ||
import useCurrencyRatePolling from './useCurrencyRatePolling'; | ||
|
||
// 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(); | ||
|
||
return <>{children}</>; | ||
}; |
42 changes: 42 additions & 0 deletions
42
app/components/hooks/AssetPolling/useCurrencyRatePolling.test.ts
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,42 @@ | ||
import useCurrencyRatePolling from './useCurrencyRatePolling'; | ||
import { renderHookWithProvider } from '../../../util/test/renderWithProvider'; | ||
import Engine from '../../../core/Engine'; | ||
|
||
jest.mock('../../../core/Engine', () => ({ | ||
context: { | ||
CurrencyRateController: { | ||
startPolling: jest.fn(), | ||
stopPollingByPollingToken: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useCurrencyRatePolling', () => { | ||
|
||
it('Should poll by the native currencies in network state', async () => { | ||
|
||
const state = { | ||
engine: { | ||
backgroundState: { | ||
NetworkController: { | ||
networkConfigurationsByChainId: { | ||
'0x1': { | ||
nativeCurrency: 'ETH', | ||
}, | ||
'0x89': { | ||
nativeCurrency: 'POL', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
renderHookWithProvider(() => useCurrencyRatePolling(), {state}); | ||
|
||
expect( | ||
jest.mocked(Engine.context.CurrencyRateController.startPolling) | ||
).toHaveBeenCalledWith({nativeCurrencies: ['ETH', 'POL']}); | ||
|
||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
app/components/hooks/AssetPolling/useCurrencyRatePolling.ts
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,39 @@ | ||
import { useSelector } from 'react-redux'; | ||
import usePolling from '../usePolling'; | ||
import { selectNetworkConfigurations } from '../../../selectors/networkController'; | ||
import Engine from '../../../core/Engine'; | ||
import { selectConversionRate, selectCurrencyRates } from '../../../selectors/currencyRateController'; | ||
|
||
// Polls native currency prices across networks. | ||
const useCurrencyRatePolling = () => { | ||
|
||
// Selectors to determine polling input | ||
const networkConfigurations = useSelector(selectNetworkConfigurations); | ||
|
||
// Selectors returning state updated by the polling | ||
const conversionRate = useSelector(selectConversionRate); | ||
const currencyRates = useSelector(selectCurrencyRates); | ||
|
||
const nativeCurrencies = [ | ||
...new Set( | ||
Object.values(networkConfigurations).map((n) => n.nativeCurrency), | ||
), | ||
]; | ||
|
||
const { CurrencyRateController } = Engine.context; | ||
|
||
usePolling({ | ||
startPolling: | ||
CurrencyRateController.startPolling.bind(CurrencyRateController), | ||
stopPollingByPollingToken: | ||
CurrencyRateController.stopPollingByPollingToken.bind(CurrencyRateController), | ||
input: [{nativeCurrencies}], | ||
}); | ||
|
||
return { | ||
conversionRate, | ||
currencyRates, | ||
}; | ||
}; | ||
|
||
export default useCurrencyRatePolling; |
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
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
Oops, something went wrong.