-
-
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: upgrade assets controllers to 42 with multichain token rates (#…
…12270) ## **Description** This PR upgrades asset controllers to 42. This version contains `resetState` functions in controllers, so it no longer needs to be patched. It also allows us to poll erc20 token rates across chains. ## **Related issues** ## **Manual testing steps** With PORTFOLIO_VIEW=false and PORTFOLIO_VIEW=true: 1. Import some erc20 tokens 2. Verify erc20 token prices are correct 3. Switch chains 4. Verify erc20 token prices are correct 5. In settings, change the fiat currency 6. Verify erc20 token prices are correct ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** No visual changes. erc20 prices should be correct when adding tokens, switching networks, and switching currencies. https://github.com/user-attachments/assets/29a733ae-630f-4d1f-952c-d5ac54bef14a <!-- [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
11 changed files
with
144 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import React, { ReactNode } from 'react'; | ||
import useCurrencyRatePolling from './useCurrencyRatePolling'; | ||
import useTokenRatesPolling from './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}</>; | ||
}; |
51 changes: 51 additions & 0 deletions
51
app/components/hooks/AssetPolling/useTokenRatesPolling.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,51 @@ | ||
import useTokenRatesPolling from './useTokenRatesPolling'; | ||
import { renderHookWithProvider } from '../../../util/test/renderWithProvider'; | ||
import Engine from '../../../core/Engine'; | ||
|
||
jest.mock('../../../core/Engine', () => ({ | ||
context: { | ||
TokenRatesController: { | ||
startPolling: jest.fn(), | ||
stopPollingByPollingToken: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useTokenRatesPolling', () => { | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const state = { | ||
engine: { | ||
backgroundState: { | ||
TokenRatesController: { | ||
marketData: {}, | ||
}, | ||
NetworkController: { | ||
networkConfigurationsByChainId: { | ||
'0x1': {}, | ||
'0x89': {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('Should poll by provided chain ids, and stop polling on dismount', async () => { | ||
|
||
const { unmount } = renderHookWithProvider(() => useTokenRatesPolling({chainIds: ['0x1']}), {state}); | ||
|
||
const mockedTokenRatesController = jest.mocked(Engine.context.TokenRatesController); | ||
|
||
expect(mockedTokenRatesController.startPolling).toHaveBeenCalledTimes(1); | ||
expect( | ||
mockedTokenRatesController.startPolling | ||
).toHaveBeenCalledWith({chainId: '0x1'}); | ||
|
||
expect(mockedTokenRatesController.stopPollingByPollingToken).toHaveBeenCalledTimes(0); | ||
unmount(); | ||
expect(mockedTokenRatesController.stopPollingByPollingToken).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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 Engine from '../../../core/Engine'; | ||
import { selectChainId, selectNetworkConfigurations } from '../../../selectors/networkController'; | ||
import { Hex } from '@metamask/utils'; | ||
import { selectContractExchangeRates, selectTokenMarketData } from '../../../selectors/tokenRatesController'; | ||
import { isPortfolioViewEnabled } from '../../../util/networks'; | ||
|
||
const useTokenRatesPolling = ({ chainIds }: { chainIds?: Hex[] } = {}) => { | ||
|
||
// Selectors to determine polling input | ||
const networkConfigurations = useSelector(selectNetworkConfigurations); | ||
const currentChainId = useSelector(selectChainId); | ||
|
||
// Selectors returning state updated by the polling | ||
const contractExchangeRates = useSelector(selectContractExchangeRates); | ||
const tokenMarketData = useSelector(selectTokenMarketData); | ||
|
||
const chainIdsToPoll = isPortfolioViewEnabled | ||
? (chainIds ?? Object.keys(networkConfigurations)) | ||
: [currentChainId]; | ||
|
||
const { TokenRatesController } = Engine.context; | ||
|
||
usePolling({ | ||
startPolling: | ||
TokenRatesController.startPolling.bind(TokenRatesController), | ||
stopPollingByPollingToken: | ||
TokenRatesController.stopPollingByPollingToken.bind(TokenRatesController), | ||
input: chainIdsToPoll.map((chainId) => ({chainId: chainId as Hex})), | ||
}); | ||
|
||
return { | ||
contractExchangeRates, | ||
tokenMarketData | ||
}; | ||
}; | ||
|
||
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
Oops, something went wrong.