-
-
Notifications
You must be signed in to change notification settings - Fork 308
fix(assets-controller): token datasource filtering #10172
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
base: main
Are you sure you want to change the base?
Changes from all commits
5be1186
c5d9fd5
b03222b
2ad2edc
7505cff
241cd49
7eee877
c72e2f2
67f1874
28e5735
1db1ce1
bfc8864
cbe2f52
8a5230c
422d5df
aef4a58
01e0cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also created an integration test on the AssetsController too 🎉 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import type { ApiPlatformClient } from '@metamask/core-backend'; | ||
| import { cleanAll } from 'nock'; | ||
|
|
||
| import { mockBscSpamApis } from './__fixtures__/bsc-spam-token/api-responses/index.js'; | ||
| import { | ||
| buildBscSpamAccount, | ||
| buildEmptyAssetsState, | ||
| getIgnoringCase, | ||
| } from './__fixtures__/bsc-spam-token/bscSpamWallet.js'; | ||
| import { registerBscSpamControllerActions } from './__fixtures__/bsc-spam-token/messenger.js'; | ||
| import { | ||
| BNB_ASSET_ID, | ||
| BSC_CHAIN_ID, | ||
| BSC_SPAM_ACCOUNT_ID, | ||
| CDOGE_ASSET_ID_CHECKSUM, | ||
| CDOGE_ASSET_ID_LOWERCASE, | ||
| } from './__fixtures__/bsc-spam-token/wallet.js'; | ||
| import { createMockMessengers } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
| import type { MockRootMessenger } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
| import { createTestApiClient } from './__fixtures__/mockTokenApi.js'; | ||
| import { waitFor, waitUntilStable } from './__fixtures__/test-utils.js'; | ||
| import { AssetsController } from './AssetsController.js'; | ||
| import type { AssetsControllerState } from './AssetsController.js'; | ||
|
|
||
| /** | ||
| * Integration coverage for `AssetsController` against the BNB Chain wallet | ||
| * from the `$$$DOGECHAIN` (`CDOGE`) spam-token report. | ||
| * | ||
| * Boots the real controller, answers the same captured APIs as | ||
| * `buildFastFetchSources.bsc-spam-token-filtering.integration.test.ts`, and | ||
| * asserts CDOGE never lands in persisted state. | ||
| * | ||
| * Integration Expectation - CDOGE is correctly filtered out of controller state. | ||
| */ | ||
|
|
||
| type StateSurface = { | ||
| surface: string; | ||
| lookUp: (state: AssetsControllerState, assetId: string) => unknown; | ||
| }; | ||
|
|
||
| const BALANCES: StateSurface = { | ||
| surface: 'balances', | ||
| lookUp: (state, assetId) => | ||
| getIgnoringCase(state.assetsBalance[BSC_SPAM_ACCOUNT_ID] ?? {}, assetId), | ||
| }; | ||
|
|
||
| const METADATA: StateSurface = { | ||
| surface: 'metadata', | ||
| lookUp: (state, assetId) => getIgnoringCase(state.assetsInfo, assetId), | ||
| }; | ||
|
|
||
| const PRICES: StateSurface = { | ||
| surface: 'prices', | ||
| lookUp: (state, assetId) => getIgnoringCase(state.assetsPrice, assetId), | ||
| }; | ||
|
|
||
| type WithControllerCallback<ReturnValue> = (args: { | ||
| controller: AssetsController; | ||
| messenger: MockRootMessenger; | ||
| }) => Promise<ReturnValue>; | ||
|
|
||
| async function withController<ReturnValue>( | ||
| { | ||
| state = buildEmptyAssetsState(), | ||
| queryApiClient = createTestApiClient(), | ||
| }: { | ||
| state?: Partial<AssetsControllerState>; | ||
| queryApiClient?: ApiPlatformClient; | ||
| }, | ||
| fn: WithControllerCallback<ReturnValue>, | ||
| ): Promise<ReturnValue> { | ||
| const { rootMessenger, assetsControllerMessenger } = createMockMessengers({ | ||
| registerCustomRootActions: registerBscSpamControllerActions, | ||
| }); | ||
|
|
||
| const controller = new AssetsController({ | ||
| messenger: assetsControllerMessenger, | ||
| state, | ||
| queryApiClient, | ||
| isBasicFunctionality: (): boolean => true, | ||
| }); | ||
|
|
||
| try { | ||
| return await fn({ controller, messenger: rootMessenger }); | ||
| } finally { | ||
| controller.destroy(); | ||
| queryApiClient.clear(); | ||
| } | ||
| } | ||
|
|
||
| async function fetchWallet( | ||
| state: Partial<AssetsControllerState> = buildEmptyAssetsState(), | ||
| ): Promise<AssetsControllerState> { | ||
| const { accountsSupportedNetworks } = mockBscSpamApis(); | ||
|
|
||
| return await withController({ state }, async ({ controller }) => { | ||
| // wait for `AccountsApiDataSource` to ask `/v2/supportedNetworks` to indicate the fast-lane is ready | ||
| await waitFor(() => expect(accountsSupportedNetworks.isDone()).toBe(true)); | ||
|
|
||
| await controller.getAssets([buildBscSpamAccount()], { | ||
| chainIds: [BSC_CHAIN_ID], | ||
| forceUpdate: true, | ||
| }); | ||
|
|
||
| // `getAssets` awaits the fast lane only; the slow lane is fire-and-forget | ||
| // and can still be writing. Let state settle so the assertions about CDOGE | ||
| // being absent cannot pass just because nothing has landed yet. | ||
| await waitUntilStable(() => controller.state); | ||
|
|
||
| return controller.state; | ||
| }); | ||
| } | ||
|
|
||
| const WALLET_PASSES = [ | ||
| { | ||
| pass: 'first pass over a fresh wallet', | ||
| run: (): Promise<AssetsControllerState> => fetchWallet(), | ||
| }, | ||
| { | ||
| pass: 'second pass over the wallet the first pass left behind', | ||
| run: async (): Promise<AssetsControllerState> => { | ||
| const firstPass = await fetchWallet(); | ||
| cleanAll(); | ||
|
|
||
| const secondPass = await fetchWallet( | ||
| buildEmptyAssetsState({ | ||
| assetsBalance: firstPass.assetsBalance, | ||
| assetsInfo: firstPass.assetsInfo, | ||
| assetsPrice: firstPass.assetsPrice, | ||
| }), | ||
| ); | ||
| return secondPass; | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| describe('AssetsController: BNB Chain spam token (CDOGE)', () => { | ||
| afterEach(() => { | ||
| cleanAll(); | ||
| }); | ||
|
|
||
| describe.each(WALLET_PASSES)('$pass', ({ run }) => { | ||
| let state: AssetsControllerState; | ||
|
|
||
| beforeAll(async () => { | ||
| state = await run(); | ||
| }); | ||
|
|
||
| it.each([BALANCES, METADATA])( | ||
| '$surface - filter out the spam token', | ||
| ({ lookUp }) => { | ||
| expect(lookUp(state, CDOGE_ASSET_ID_LOWERCASE)).toBeUndefined(); | ||
| expect(lookUp(state, CDOGE_ASSET_ID_CHECKSUM)).toBeUndefined(); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([BALANCES, METADATA])( | ||
| '$surface - keeps the native BNB asset despite low occurrences', | ||
| ({ lookUp }) => { | ||
| expect(lookUp(state, BNB_ASSET_ID)).toBeDefined(); | ||
| }, | ||
| ); | ||
|
|
||
| // Same gap as the pipeline suite: prices are not occurrence-filtered. | ||
| // Unlock cleanup eventually strips them; this flags the hole. | ||
| it.failing('keeps the spam token out of prices', () => { | ||
| expect(PRICES.lookUp(state, CDOGE_ASSET_ID_LOWERCASE)).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ import type { InternalAccount } from '@metamask/keyring-internal-api'; | |
| import type { FeatureFlags } from '@metamask/remote-feature-flag-controller'; | ||
|
|
||
| import { | ||
| createMockAssetControllerMessenger, | ||
| createMockInternalAccount, | ||
| createMockMessengers, | ||
| registerAssetsControllerActions, | ||
| } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
| import type { MockRootMessenger } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
|
|
@@ -67,9 +67,6 @@ async function withController<ReturnValue>( | |
| }: WithControllerOptions, | ||
| fn: WithControllerCallback<ReturnValue>, | ||
| ): Promise<ReturnValue> { | ||
| const { rootMessenger, assetsControllerMessenger } = | ||
| createMockAssetControllerMessenger({ delegateGetState: false }); | ||
|
|
||
| // Every account the wallet tracks balances for: the synthetic catch-all | ||
| // account plus the real custom-asset owner. | ||
| const accounts = [ | ||
|
|
@@ -87,11 +84,14 @@ async function withController<ReturnValue>( | |
| ), | ||
| ]; | ||
|
|
||
| registerAssetsControllerActions(rootMessenger, { | ||
| accounts, | ||
| enabledNetworkMap: { eip155: { '1': true, '10': true, '8453': true } }, | ||
| nativeAssetIdentifiers: SCAM_WALLET_NATIVE_ASSET_IDENTIFIERS, | ||
| remoteFeatureFlags, | ||
| const { rootMessenger, assetsControllerMessenger } = createMockMessengers({ | ||
| registerCustomRootActions: (messenger) => | ||
| registerAssetsControllerActions(messenger, { | ||
| accounts, | ||
| enabledNetworkMap: { eip155: { '1': true, '10': true, '8453': true } }, | ||
| nativeAssetIdentifiers: SCAM_WALLET_NATIVE_ASSET_IDENTIFIERS, | ||
| remoteFeatureFlags, | ||
| }), | ||
|
Comment on lines
+87
to
+94
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Messenger mock reformatting. |
||
| }); | ||
|
|
||
| const controller = new AssetsController({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ import type { InternalAccount } from '@metamask/keyring-internal-api'; | |
| import type { FeatureFlags } from '@metamask/remote-feature-flag-controller'; | ||
|
|
||
| import { | ||
| createMockAssetControllerMessenger, | ||
| createMockInternalAccount, | ||
| createMockMessengers, | ||
| registerAssetsControllerActions, | ||
| } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
| import type { MockRootMessenger } from './__fixtures__/MockAssetControllerMessenger.js'; | ||
|
|
@@ -89,8 +89,6 @@ async function withController<ReturnValue>( | |
| }: WithControllerOptions, | ||
| fn: WithControllerCallback<ReturnValue>, | ||
| ): Promise<ReturnValue> { | ||
| const { rootMessenger, assetsControllerMessenger } = | ||
| createMockAssetControllerMessenger({ delegateGetState: false }); | ||
| const accounts = [ | ||
| createMockInternalAccount({ | ||
| id: ACCOUNT_ONE_ID, | ||
|
|
@@ -104,11 +102,14 @@ async function withController<ReturnValue>( | |
| }), | ||
| ]; | ||
|
|
||
| registerAssetsControllerActions(rootMessenger, { | ||
| accounts, | ||
| enabledNetworkMap: { eip155: { '1': true, '10': true } }, | ||
| nativeAssetIdentifiers: { 'eip155:1': MAINNET_NATIVE }, | ||
| remoteFeatureFlags, | ||
| const { rootMessenger, assetsControllerMessenger } = createMockMessengers({ | ||
| registerCustomRootActions: (messenger) => | ||
| registerAssetsControllerActions(messenger, { | ||
| accounts, | ||
| enabledNetworkMap: { eip155: { '1': true, '10': true } }, | ||
| nativeAssetIdentifiers: { 'eip155:1': MAINNET_NATIVE }, | ||
| remoteFeatureFlags, | ||
| }), | ||
|
Comment on lines
+105
to
+112
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Messenger mock reformatting. |
||
| }); | ||
|
|
||
| const controller = new AssetsController({ | ||
|
|
||
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.
Dev dependency only, aligned to best practice on RPC mocking.