-
-
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 v44 (#12344)
## **Description** Upgrades the assets controllers to v44. In this version, the token balances controller now stores erc20 balances across all chains and accounts, instead of just the current chain and account like before. This allows polling erc20 balances across chains. ## **Related issues** Depends on: #12340 ## **Manual testing steps** With PORTFOLIO_VIEW=false and PORTFOLIO_VIEW=true: 1. Onboard 2. Verify erc20 tokens have correct balances 3. Switch chains 4. Verify the erc20 tokens on the new chain have correct balances 5. Send a TX that changes an erc20 balance 6. Verify it gets updated after tx completes ## **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. --------- Co-authored-by: salimtb <[email protected]>
- Loading branch information
Showing
21 changed files
with
272 additions
and
203 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
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
27 changes: 27 additions & 0 deletions
27
app/components/hooks/AssetPolling/AssetPollingProvider.test.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,27 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { AssetPollingProvider } from './AssetPollingProvider'; | ||
|
||
jest.mock('./useCurrencyRatePolling', () => jest.fn()); | ||
jest.mock('./useTokenRatesPolling', () => jest.fn()); | ||
jest.mock('./useTokenDetectionPolling', () => jest.fn()); | ||
jest.mock('./useTokenListPolling', () => jest.fn()); | ||
jest.mock('./useTokenBalancesPolling', () => jest.fn()); | ||
|
||
describe('AssetPollingProvider', () => { | ||
it('should call all polling hooks', () => { | ||
|
||
Check warning on line 14 in app/components/hooks/AssetPolling/AssetPollingProvider.test.tsx GitHub Actions / scripts (lint)
|
||
render( | ||
<AssetPollingProvider> | ||
<div></div> | ||
</AssetPollingProvider> | ||
); | ||
|
||
expect(jest.requireMock('./useCurrencyRatePolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenRatesPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenDetectionPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenListPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenBalancesPolling')).toHaveBeenCalled(); | ||
}); | ||
}); |
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
58 changes: 58 additions & 0 deletions
58
app/components/hooks/AssetPolling/useTokenBalancesPolling.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,58 @@ | ||
import { renderHookWithProvider } from '../../../util/test/renderWithProvider'; | ||
import Engine from '../../../core/Engine'; | ||
import useTokenBalancesPolling from './useTokenBalancesPolling'; | ||
|
||
jest.mock('../../../core/Engine', () => ({ | ||
context: { | ||
TokenBalancesController: { | ||
startPolling: jest.fn(), | ||
stopPollingByPollingToken: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useTokenBalancesPolling', () => { | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const selectedChainId = '0x1' as const; | ||
const state = { | ||
engine: { | ||
backgroundState: { | ||
TokenBalancesController: { | ||
tokenBalances: {}, | ||
}, | ||
NetworkController: { | ||
selectedNetworkClientId: 'selectedNetworkClientId', | ||
networkConfigurationsByChainId: { | ||
[selectedChainId]: { | ||
chainId: selectedChainId, | ||
rpcEndpoints: [{ | ||
networkClientId: 'selectedNetworkClientId', | ||
}] | ||
}, | ||
'0x89': {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('Should poll by selected chain id, and stop polling on dismount', async () => { | ||
|
||
const { unmount } = renderHookWithProvider(() => useTokenBalancesPolling(), {state}); | ||
|
||
const mockedTokenBalancesController = jest.mocked(Engine.context.TokenBalancesController); | ||
|
||
expect(mockedTokenBalancesController.startPolling).toHaveBeenCalledTimes(1); | ||
expect( | ||
mockedTokenBalancesController.startPolling | ||
).toHaveBeenCalledWith({chainId: selectedChainId}); | ||
|
||
expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(0); | ||
unmount(); | ||
expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
app/components/hooks/AssetPolling/useTokenBalancesPolling.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,37 @@ | ||
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 { isPortfolioViewEnabled } from '../../../util/networks'; | ||
import { selectAllTokenBalances } from '../../../selectors/tokenBalancesController'; | ||
|
||
const useTokenBalancesPolling = ({ chainIds }: { chainIds?: Hex[] } = {}) => { | ||
|
||
// Selectors to determine polling input | ||
const networkConfigurations = useSelector(selectNetworkConfigurations); | ||
const currentChainId = useSelector(selectChainId); | ||
|
||
// Selectors returning state updated by the polling | ||
const tokenBalances = useSelector(selectAllTokenBalances); | ||
|
||
const chainIdsToPoll = isPortfolioViewEnabled | ||
? (chainIds ?? Object.keys(networkConfigurations)) | ||
: [currentChainId]; | ||
|
||
const { TokenBalancesController } = Engine.context; | ||
|
||
usePolling({ | ||
startPolling: | ||
TokenBalancesController.startPolling.bind(TokenBalancesController), | ||
stopPollingByPollingToken: | ||
TokenBalancesController.stopPollingByPollingToken.bind(TokenBalancesController), | ||
input: chainIdsToPoll.map((chainId) => ({chainId: chainId as Hex})), | ||
}); | ||
|
||
return { | ||
tokenBalances | ||
}; | ||
}; | ||
|
||
export default useTokenBalancesPolling; |
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.