Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 300
[token-detection-controller] Apply recent DetectTokensController updates#3923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MajorLift
merged 18 commits into
main
from
240215-TokenDetectionController-sync-with-extensionFeb 27, 2024
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0fa46b3
Initialize `#selectedAddress` using `AccountsController:getSelectedAc…
MajorLift e40ae46
[detectTokens] Align variable names with extension `detectNewTokens` …
MajorLift a10e4e0
[detectTokens] Apply extension-side fix for getting correct `networkC…
MajorLift a26ea0f
[detectTokens] Apply and refactor fixes for detected tokens being add…
MajorLift 99e01da
Replace `#getCorrectChainId` with `#getCorrectChainIdAndNetworkClientId`
MajorLift 904e4b7
test: register action handlers for `getSelectedAccount`, `findNetwork…
MajorLift 7324749
Fix erroneous reference to `tokenList`
MajorLift e2be829
Fix typo
MajorLift a3b5c55
Update changelog
MajorLift f452964
test: adjust coverage thresholds
MajorLift 366e56c
test: Fix unmocked `ProviderConfig` for changed `chainId`
MajorLift 857142b
Update packages/assets-controllers/CHANGELOG.md
MajorLift 2b88d05
Rename `accoutAddress` option of `detectTokens` to `selectedAddress` …
MajorLift 1379987
Update `this.#chainId` on `networkDidChange` event
MajorLift e9bbf1d
Remove reference to `this.#chainId` from `#getCorrectChainIdAndNetwor…
MajorLift 3f9de16
Merge branch 'main' into 240215-TokenDetectionController-sync-with-ex…
MajorLift 7d15225
Merge branch 'main' into 240215-TokenDetectionController-sync-with-ex…
MajorLift 2b4a6f7
Merge branch 'main' into 240215-TokenDetectionController-sync-with-ex…
MajorLift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
96 changes: 75 additions & 21 deletions
96 packages/assets-controllers/src/TokenDetectionController.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -8,12 +8,14 @@ import { | ||
| } from '@metamask/controller-utils'; | ||
| import type { InternalAccount } from '@metamask/keyring-api'; | ||
| import type { KeyringControllerState } from '@metamask/keyring-controller'; | ||
| import { | ||
| defaultState as defaultNetworkState, | ||
| type NetworkState, | ||
| type NetworkConfiguration, | ||
| type NetworkController, | ||
| import type { | ||
| NetworkState, | ||
| NetworkConfiguration, | ||
| NetworkController, | ||
| ProviderConfig, | ||
| NetworkClientId, | ||
| } from '@metamask/network-controller'; | ||
| import { defaultState as defaultNetworkState } from '@metamask/network-controller'; | ||
| import { | ||
| getDefaultPreferencesState, | ||
| type PreferencesState, | ||
| @@ -138,8 +140,11 @@ function buildTokenDetectionControllerMessenger( | ||
| return controllerMessenger.getRestricted({ | ||
| name: controllerName, | ||
| allowedActions: [ | ||
| 'AccountsController:getSelectedAccount', | ||
| 'KeyringController:getState', | ||
| 'NetworkController:findNetworkClientIdByChainId', | ||
| 'NetworkController:getNetworkConfigurationByNetworkClientId', | ||
| 'NetworkController:getProviderConfig', | ||
| 'TokensController:getState', | ||
| 'TokensController:addDetectedTokens', | ||
| 'TokenListController:getState', | ||
| @@ -338,7 +343,16 @@ describe('TokenDetectionController', () => { | ||
| selectedAddress, | ||
| }, | ||
| }, | ||
| async ({ controller, mockTokenListGetState, callActionSpy }) => { | ||
| async ({ | ||
| controller, | ||
| mockGetProviderConfig, | ||
| mockTokenListGetState, | ||
| callActionSpy, | ||
| }) => { | ||
| mockGetProviderConfig({ | ||
| chainId: '0x89', | ||
| } as unknown as ProviderConfig); | ||
| mockTokenListGetState({ | ||
| ...getDefaultTokenListState(), | ||
| tokensChainsCache: { | ||
| @@ -1748,19 +1762,19 @@ describe('TokenDetectionController', () => { | ||
| await advanceTime({ clock, duration: 0 }); | ||
| expect(spy.mock.calls).toMatchObject([ | ||
| [{ networkClientId: 'mainnet', accountAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', accountAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', accountAddress: '0x3' }], | ||
| [{ networkClientId: 'mainnet', selectedAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', selectedAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', selectedAddress: '0x3' }], | ||
| ]); | ||
| await advanceTime({ clock, duration: DEFAULT_INTERVAL }); | ||
| expect(spy.mock.calls).toMatchObject([ | ||
| [{ networkClientId: 'mainnet', accountAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', accountAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', accountAddress: '0x3' }], | ||
| [{ networkClientId: 'mainnet', accountAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', accountAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', accountAddress: '0x3' }], | ||
| [{ networkClientId: 'mainnet', selectedAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', selectedAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', selectedAddress: '0x3' }], | ||
| [{ networkClientId: 'mainnet', selectedAddress: '0x1' }], | ||
| [{ networkClientId: 'sepolia', selectedAddress: '0xdeadbeef' }], | ||
| [{ networkClientId: 'goerli', selectedAddress: '0x3' }], | ||
| ]); | ||
| }, | ||
| ); | ||
| @@ -1793,7 +1807,7 @@ describe('TokenDetectionController', () => { | ||
| }); | ||
| await controller.detectTokens({ | ||
| networkClientId: NetworkType.goerli, | ||
| accountAddress: selectedAddress, | ||
| selectedAddress, | ||
| }); | ||
| expect(callActionSpy).not.toHaveBeenCalledWith( | ||
| 'TokensController:addDetectedTokens', | ||
| @@ -1833,7 +1847,7 @@ describe('TokenDetectionController', () => { | ||
| }); | ||
| await controller.detectTokens({ | ||
| networkClientId: NetworkType.mainnet, | ||
| accountAddress: selectedAddress, | ||
| selectedAddress, | ||
| }); | ||
| expect(callActionSpy).toHaveBeenLastCalledWith( | ||
| 'TokensController:addDetectedTokens', | ||
| @@ -1896,7 +1910,7 @@ describe('TokenDetectionController', () => { | ||
| await controller.detectTokens({ | ||
| networkClientId: NetworkType.mainnet, | ||
| accountAddress: selectedAddress, | ||
| selectedAddress, | ||
| }); | ||
| expect(callActionSpy).toHaveBeenCalledWith( | ||
| @@ -1951,7 +1965,7 @@ describe('TokenDetectionController', () => { | ||
| await controller.detectTokens({ | ||
| networkClientId: NetworkType.mainnet, | ||
| accountAddress: selectedAddress, | ||
| selectedAddress, | ||
| }); | ||
| expect(mockTrackMetaMetricsEvent).toHaveBeenCalledWith({ | ||
| @@ -1983,10 +1997,14 @@ function getTokensPath(chainId: Hex) { | ||
| type WithControllerCallback<ReturnValue> = ({ | ||
| controller, | ||
| mockGetSelectedAccount, | ||
| mockKeyringGetState, | ||
| mockTokensGetState, | ||
| mockTokenListGetState, | ||
| mockPreferencesGetState, | ||
| mockFindNetworkClientIdByChainId, | ||
| mockGetNetworkConfigurationByNetworkClientId, | ||
| mockGetProviderConfig, | ||
| callActionSpy, | ||
| triggerKeyringUnlock, | ||
| triggerKeyringLock, | ||
| @@ -1996,13 +2014,18 @@ type WithControllerCallback<ReturnValue> = ({ | ||
| triggerNetworkDidChange, | ||
| }: { | ||
| controller: TokenDetectionController; | ||
| mockGetSelectedAccount: (address: string) => void; | ||
| mockKeyringGetState: (state: KeyringControllerState) => void; | ||
| mockTokensGetState: (state: TokensState) => void; | ||
| mockTokenListGetState: (state: TokenListState) => void; | ||
| mockPreferencesGetState: (state: PreferencesState) => void; | ||
| mockFindNetworkClientIdByChainId: ( | ||
| handler: (chainId: Hex) => NetworkClientId, | ||
| ) => void; | ||
| mockGetNetworkConfigurationByNetworkClientId: ( | ||
| handler: (networkClientId: string) => NetworkConfiguration, | ||
| ) => void; | ||
| mockGetProviderConfig: (config: ProviderConfig) => void; | ||
| callActionSpy: jest.SpyInstance; | ||
| triggerKeyringUnlock: () => void; | ||
| triggerKeyringLock: () => void; | ||
| @@ -2039,25 +2062,45 @@ async function withController<ReturnValue>( | ||
| const controllerMessenger = | ||
| messenger ?? new ControllerMessenger<AllowedActions, AllowedEvents>(); | ||
| const mockGetSelectedAccount = jest.fn<InternalAccount, []>(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'AccountsController:getSelectedAccount', | ||
| mockGetSelectedAccount.mockReturnValue({ | ||
| address: '0x1', | ||
| } as InternalAccount), | ||
mcmire marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| ); | ||
| const mockKeyringState = jest.fn<KeyringControllerState, []>(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'KeyringController:getState', | ||
| mockKeyringState.mockReturnValue({ | ||
| isUnlocked: isKeyringUnlocked ?? true, | ||
| } as KeyringControllerState), | ||
| ); | ||
| const mockFindNetworkClientIdByChainId = jest.fn<NetworkClientId, [Hex]>(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'NetworkController:findNetworkClientIdByChainId', | ||
| mockFindNetworkClientIdByChainId.mockReturnValue(NetworkType.mainnet), | ||
| ); | ||
| const mockGetNetworkConfigurationByNetworkClientId = jest.fn< | ||
| ReturnType<NetworkController['getNetworkConfigurationByNetworkClientId']>, | ||
| Parameters<NetworkController['getNetworkConfigurationByNetworkClientId']> | ||
| >(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'NetworkController:getNetworkConfigurationByNetworkClientId', | ||
| mockGetNetworkConfigurationByNetworkClientId.mockImplementation( | ||
| (networkClientId: string) => { | ||
| (networkClientId: NetworkClientId) => { | ||
| return mockNetworkConfigurations[networkClientId]; | ||
| }, | ||
| ), | ||
| ); | ||
| const mockGetProviderConfig = jest.fn<ProviderConfig, []>(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'NetworkController:getProviderConfig', | ||
| mockGetProviderConfig.mockReturnValue({ | ||
| type: NetworkType.mainnet, | ||
| chainId: '0x1', | ||
| } as unknown as ProviderConfig), | ||
| ); | ||
| const mockTokensState = jest.fn<TokensState, []>(); | ||
| controllerMessenger.registerActionHandler( | ||
| 'TokensController:getState', | ||
| @@ -2096,6 +2139,9 @@ async function withController<ReturnValue>( | ||
| try { | ||
| return await fn({ | ||
| controller, | ||
| mockGetSelectedAccount: (address: string) => { | ||
| mockGetSelectedAccount.mockReturnValue({ address } as InternalAccount); | ||
| }, | ||
| mockKeyringGetState: (state: KeyringControllerState) => { | ||
| mockKeyringState.mockReturnValue(state); | ||
| }, | ||
| @@ -2108,13 +2154,21 @@ async function withController<ReturnValue>( | ||
| mockTokenListGetState: (state: TokenListState) => { | ||
| mockTokenListState.mockReturnValue(state); | ||
| }, | ||
| mockFindNetworkClientIdByChainId: ( | ||
| handler: (chainId: Hex) => NetworkClientId, | ||
| ) => { | ||
| mockFindNetworkClientIdByChainId.mockImplementation(handler); | ||
| }, | ||
| mockGetNetworkConfigurationByNetworkClientId: ( | ||
| handler: (networkClientId: string) => NetworkConfiguration, | ||
| handler: (networkClientId: NetworkClientId) => NetworkConfiguration, | ||
| ) => { | ||
| mockGetNetworkConfigurationByNetworkClientId.mockImplementation( | ||
| handler, | ||
| ); | ||
| }, | ||
| mockGetProviderConfig: (config: ProviderConfig) => { | ||
| mockGetProviderConfig.mockReturnValue(config); | ||
| }, | ||
| callActionSpy, | ||
| triggerKeyringUnlock: () => { | ||
| controllerMessenger.publish('KeyringController:unlock'); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻