diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index a6aab0b20d2..322c19d277b 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -503,7 +503,7 @@ "count": 9 }, "vitest/no-conditional-in-test": { - "count": 13 + "count": 14 } }, "packages/assets-controller/src/AssetsController.ts": { @@ -564,7 +564,7 @@ }, "packages/assets-controller/src/data-sources/PriceDataSource.test.ts": { "vitest/no-conditional-in-test": { - "count": 2 + "count": 5 } }, "packages/assets-controller/src/data-sources/RpcDataSource.test.ts": { @@ -679,11 +679,6 @@ "count": 1 } }, - "packages/assets-controller/src/pipeline/buildWsUpdateSources.price-updates.integration.test.ts": { - "vitest/no-conditional-in-test": { - "count": 3 - } - }, "packages/assets-controller/src/pipeline/executeAssetsPipeline.ts": { "no-unused-vars": { "count": 1 diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 3058c7c675d..d14b37da4d8 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -9,14 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Add `syncAssetsPrices(assetIds)`, exposed as the `AssetsController:syncAssetsPrices` messenger action. It force-fetches spot prices for an explicit CAIP-19 asset list through `PriceDataSource` and merges them into state ([#10892](https://github.com/MetaMask/core/pull/10892)) - Add support for Node.js 26 ([#10981](https://github.com/MetaMask/core/pull/10981)) ### Changed +- Fetch token metadata again when stored metadata is missing a field that controller logic or a client reads (`symbol`, `name`, `decimals`, `image`, `aggregators`, or `occurrences`), instead of treating an `image` as complete +- **BREAKING:** Remove `DetectionMiddleware`. A balance refresh prices balances on the current response only when they have no stored price, and excludes hidden assets. The price poll still refreshes stored prices for held balances and custom assets, and does not price metadata that has no balance. `TokenDataSource` fetches metadata for state balances that have none and for response balances, and excludes hidden assets ([#10892](https://github.com/MetaMask/core/pull/10892)) +- **BREAKING:** Remove `assetsForPriceUpdate` from `getAssets` options. Refresh an explicit asset list with `syncAssetsPrices` instead ([#10892](https://github.com/MetaMask/core/pull/10892)) - Bump `@metamask/account-tree-controller` from `^11.0.1` to `^11.1.1` ([#10891](https://github.com/MetaMask/core/pull/10891), [#10925](https://github.com/MetaMask/core/pull/10925)) - Bump `@metamask/assets-controllers` from `^112.1.2` to `^112.1.3` ([#10925](https://github.com/MetaMask/core/pull/10925)) - Bump `@metamask/network-controller` from `^37.1.0` to `^37.1.1` ([#10982](https://github.com/MetaMask/core/pull/10982)) +### Removed + +- **BREAKING:** Remove the `AssetsController:assetsDetected` event and the `AssetsControllerAssetsDetectedEvent` type. No client subscribed to it ([#10892](https://github.com/MetaMask/core/pull/10892)) +- **BREAKING:** Remove `detectedAssets` from `DataResponse` and the unused `AssetsDetectedEvent` type. Newly detected tokens still arrive as balances and metadata ([#10892](https://github.com/MetaMask/core/pull/10892)) + ## [19.1.0] ### Changed diff --git a/packages/assets-controller/docs/decisions/2026-10-20-remove-detection-middleware.md b/packages/assets-controller/docs/decisions/2026-10-20-remove-detection-middleware.md new file mode 100644 index 00000000000..88e37490cd5 --- /dev/null +++ b/packages/assets-controller/docs/decisions/2026-10-20-remove-detection-middleware.md @@ -0,0 +1,207 @@ +# How the wallet fetch and picks token metadata and prices + +- Date: 2026-10-20 +- Status: Accepted +- Package: `@metamask/assets-controller` + +For each token, the wallet shows three things: how much the user holds, the +token's name and icon, and a fiat price. Different steps fetch those three +things. This note is about the name/icon step and the price step. + +There used to be a step in between, called detection. It built one shared list +of "new" tokens, and both later steps could only work from that list. That +step is gone. The name/icon step and the price step now each decide which +tokens they need. + +This is not the "token detection" setting. That setting still controls which +Ethereum tokens the wallet scans for. This note is only about what happens +after a balance has already been found. + +## Words used below + +| Word | Meaning | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------- | +| Balance | How much of a token an account holds. | +| Token metadata | Symbol, name, decimals, image, aggregator list, and occurrence count. The controller does not ask the token service for `type`. | +| Price | The token's value in the user's currency, such as USD. | +| Custom token | A token the user added by hand. We keep it even if it looks unpopular. | +| Hidden token | A token the user chose to hide. We do not fetch it. | +| Native token | The chain's own coin, such as ETH on Ethereum or BNB on BNB Chain. | +| Spam | A token that showed up in the wallet without the user asking for it, often an airdrop. We try not to show those. | + +## What happens on a refresh + +Balances are fetched first. Names, icons, and prices are filled in after that, +then saved together. + +### Before + +Detection ran after balances and built one list of new tokens. Names and prices +could only use that list. A token the user already held was left off, even when +it had no name or no price. Names and prices waited for the list, then ran +together. + +```mermaid +flowchart LR + Bal["Fetch balances"] --> Det["Detection
one list of new tokens"] + Det --> Names["Token metadata
only tokens on that list"] + Det --> Prices["Prices
only tokens on that list
that have no price yet"] + Names --> Save["Save"] + Prices --> Save +``` + +### After + +Detection is gone. Names and prices start as soon as balances are back, and +each one chooses its own tokens. Hidden tokens are skipped by both. + +```mermaid +flowchart LR + Bal["Fetch balances"] --> Names["Token metadata
tokens that still need them"] + Bal --> Prices["Prices
response tokens that
still have no price"] + Names --> Save["Save"] + Prices --> Save +``` + +Changing the display currency does not fetch balances again. Saved prices are +cleared, then every token the user holds is priced in the new currency. + +```mermaid +flowchart LR + Change["User changes currency"] --> Clear["Clear saved prices"] + Clear --> Price["Price every token the user holds"] + Price --> Save["Save"] +``` + +## Prices + +A balance refresh and the price poll retrieve different tokens. + +**On a balance refresh**, prices come only from tokens on this response. A +token is retrieved when all of these are true: + +- It is on this response's balances. The balance step already limited that + list to the accounts and chains this refresh asked about. +- The user has not hidden it. +- It can have a market price. A staking placeholder (a `slip44` id with a + suffix after the coin type) and Tron bandwidth or energy are skipped. +- Its chain is one the price service supports. If that service cannot be + reached, this check is skipped and the remaining tokens are still asked for. +- The wallet does not already have a price stored for it. + +A token that already has a stored price is left alone on a refresh. + +**On the price poll**, the list is built from saved state, not from the +response. It includes every one of these, then drops the same hidden, +non-priceable, and unsupported-chain tokens as a refresh: + +- Every token in saved balances, on the accounts and chains this poll asked + about. A balance fetch leaves this as the visible set: holdings, plus + native, pins, and default tracked assets at zero. +- Every custom token on those accounts, including one not yet written as a + balance row. + +An empty account list means every account. An empty chain list means every +chain. Tokens that already have a price stay on this list, so the poll can +refresh them. + +A price fetched in the last minute is not fetched again. Two requests that +need the same price share one request. A request that forces an update clears +that one-minute cache for the tokens it is about to ask for. + +A caller that names specific tokens uses `syncAssetsPrices`, which fetches +them again even when a price is already stored. + +Changing the display currency is separate from both of the lists above. Those +prices are in the wrong currency, so the cache is cleared and every token the +user holds is priced again. + +## Token metadata + +This step runs after balances, and only when the refresh asked for metadata. +It asks the token service for tokens whose details are still incomplete, writes +the answer onto this refresh, and drops a new token that fails the spam check. +Then the rest of the refresh continues. + +A record is complete when it has a symbol, a name, decimals, an image, an +aggregator list, and an occurrence count. + +The tokens to fetch from the Tokens API come from two places. + +**Saved balances.** `state.assetsBalance` entries whose metadata is incomplete, +limited to the accounts and chains this refresh asked about. An empty account +or chain list means all of them. These are heals: the missing fields are +filled in, and they are not spam-filtered. + +**The response.** Every token in the response's `assetsBalance`. One is a heal +when it is already a saved balance or already has any saved metadata. +Otherwise it is new, and the spam check below can drop it. + +A token is left off both lists when any of these is true: + +- The user hid it. +- This response already has a complete record for it. +- Saved metadata already has a complete record for it. +- It is a staking-contract placeholder. + +Of what remains, only chains the Tokens API lists in `fullSupport` or +`partialSupport` are fetched. If that list cannot be loaded, or none of the +tokens are on it, nothing is fetched. + +After the token service answers, each returned token is kept or dropped: + +| Token | What we do | +| -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | +| The chain's own coin | Always keep it | +| A token the user added | Always keep it | +| mUSD | Always keep it | +| A heal (a saved balance, or a response token we already track) | Keep it, and write the fields the service returned | +| A new Ethereum-style token | Keep it only if enough other sources list it. The bar is that chain's suggested floor, usually 3. No count is treated as zero and dropped | +| A new token on another chain | Ask Blockaid, and drop it if that scan says it is malicious | +| Any other token the service returned | Keep it | + +If the token-service request fails, balances already fetched are left as they +are. + +## Trade-offs + +What we gain: + +- A simpler pipeline. One step and one shared list are gone, so there are + fewer places for a token to be dropped by mistake. +- The existing metadata-healing behavior is preserved. Tokens in balances + that are missing one of the fields above remain candidates, without relying + on a detection list. +- Each step can be tested on its own, because it picks its own tokens from + state and the current balances. + +What it costs: + +- More calls to the token service. Held tokens that are missing one of those + fields are asked for on every refresh. A token the service still does not + return a complete record for is asked for again each time. Prices are + fetched only for tokens on this response that have no price yet. The + one-minute price cache and the shared in-flight request limit repeat price + calls, but do not remove them. +- The two steps each decide which accounts, chains, and hidden tokens count. + That logic now lives in two places and has to be kept in line. + +## If you call this package + +| You used to | Do this now | +| ----------------------------------------------------- | ------------------------------------------------------------- | +| Pass `assetsForPriceUpdate` into `getAssets` | Call `AssetsController:syncAssetsPrices` with those token ids | +| Import `DetectionMiddleware` | Delete it. Names and prices are chosen on their own | +| Pass `detectionMiddleware` when building a fetch lane | Stop passing it. The lane types no longer have that field | + +## Where to read the code + +| Question | Start here | +| ----------------------------------- | --------------------------------------------------------------------- | +| Refresh prices for a list of tokens | `AssetsController:syncAssetsPrices` | +| User changed currency | `AssetsController.setSelectedCurrency` | +| Which tokens a refresh prices | `getUnpricedResponseAssetIds` | +| Which tokens the price poll prices | `getAssetIdsForPriceFetch` | +| Which tokens get token metadata | `getAssetIdsForMetadataFetch` | +| How those lists are fetched | `PriceDataSource`, `TokenDataSource` | +| Order of a refresh | `buildFastFetchSources`, `buildUpdateSources`, `buildWsUpdateSources` | diff --git a/packages/assets-controller/src/AssetsController-method-action-types.ts b/packages/assets-controller/src/AssetsController-method-action-types.ts index 68590b2ce8a..f456418eb3b 100644 --- a/packages/assets-controller/src/AssetsController-method-action-types.ts +++ b/packages/assets-controller/src/AssetsController-method-action-types.ts @@ -10,6 +10,24 @@ export type AssetsControllerGetAssetsAction = { handler: AssetsController['getAssets']; }; +/** + * Force-fetch spot prices for an explicit asset list. + * + * Calls `PriceDataSource` directly and merges the result into `assetsPrice`. + * Prices are shared across accounts, so this does not take an account or + * chain scope and does not run the balance pipeline. + * + * Returns without calling the Price API when basic functionality is off. + * That matches the asset pipeline, which drops the price source in RPC-only + * mode, and `setSelectedCurrency`, which skips its price refresh. + * + * @param assetIds - CAIP-19 asset IDs to sync. + */ +export type AssetsControllerSyncAssetsPricesAction = { + type: `AssetsController:syncAssetsPrices`; + handler: AssetsController['syncAssetsPrices']; +}; + export type AssetsControllerGetAssetsBalanceAction = { type: `AssetsController:getAssetsBalance`; handler: AssetsController['getAssetsBalance']; @@ -200,6 +218,7 @@ export type AssetsControllerSetSelectedCurrencyAction = { */ export type AssetsControllerMethodActions = | AssetsControllerGetAssetsAction + | AssetsControllerSyncAssetsPricesAction | AssetsControllerGetAssetsBalanceAction | AssetsControllerGetAssetMetadataAction | AssetsControllerGetAccountAssetByIDAction diff --git a/packages/assets-controller/src/AssetsController.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts b/packages/assets-controller/src/AssetsController.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts index 249a351734c..ee4ea5201a5 100644 --- a/packages/assets-controller/src/AssetsController.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts +++ b/packages/assets-controller/src/AssetsController.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts @@ -175,7 +175,7 @@ describe('AssetsController (Accounts API v6): BNB Chain spam token (CDOGE)', () // Unlike the v5 suite, this is not `it.failing`! // On v6 the backend omits the Malicious row, so the token is - // never in `assetsBalance` or `detectedAssets` at any pipeline stage and + // never in `assetsBalance` at any pipeline stage and // no price is ever fetched for it. // // Note we still need the unlock cleanup (`cleanSpamAssets`) to diff --git a/packages/assets-controller/src/AssetsController.test.ts b/packages/assets-controller/src/AssetsController.test.ts index bd3aa8da0a2..4843c19f298 100644 --- a/packages/assets-controller/src/AssetsController.test.ts +++ b/packages/assets-controller/src/AssetsController.test.ts @@ -4121,6 +4121,62 @@ describe('AssetsController', () => { }); }); + describe('syncAssetsPrices', () => { + it('is exposed as the AssetsController:syncAssetsPrices messenger action', async () => { + const fetchedPrice = { + assetPriceType: 'fungible' as const, + price: 1.25, + usdPrice: 1.25, + lastUpdated: 123, + }; + const fetchSpy = vi + .spyOn(PriceDataSource.prototype, 'fetchAssetPrices') + .mockResolvedValue({ + [MOCK_ASSET_ID]: fetchedPrice, + }); + + await withController(async ({ controller, messenger }) => { + await messenger.call('AssetsController:syncAssetsPrices', [ + MOCK_ASSET_ID, + ]); + + expect(fetchSpy).toHaveBeenCalledWith([MOCK_ASSET_ID]); + expect(controller.state.assetsPrice[MOCK_ASSET_ID]).toStrictEqual( + fetchedPrice, + ); + }); + + fetchSpy.mockRestore(); + }); + + it('returns without fetching prices when basic functionality is off', async () => { + const fetchSpy = vi + .spyOn(PriceDataSource.prototype, 'fetchAssetPrices') + .mockResolvedValue({ + [MOCK_ASSET_ID]: { + assetPriceType: 'fungible', + price: 1.25, + usdPrice: 1.25, + lastUpdated: 123, + }, + }); + + await withController( + { isBasicFunctionality: () => false }, + async ({ controller, messenger }) => { + await messenger.call('AssetsController:syncAssetsPrices', [ + MOCK_ASSET_ID, + ]); + + expect(fetchSpy).not.toHaveBeenCalled(); + expect(controller.state.assetsPrice[MOCK_ASSET_ID]).toBeUndefined(); + }, + ); + + fetchSpy.mockRestore(); + }); + }); + describe('setSelectedCurrency', () => { it('updates selectedCurrency in state', async () => { await withController(({ controller }) => { @@ -4138,18 +4194,18 @@ describe('AssetsController', () => { await withController(({ controller }) => { expect(controller.state.selectedCurrency).toBe('usd'); - const getAssetsSpy = vi.spyOn(controller, 'getAssets'); + const refreshSpy = vi.spyOn(controller, 'syncAssetsPrices'); controller.setSelectedCurrency('usd'); expect(controller.state.selectedCurrency).toBe('usd'); - expect(getAssetsSpy).not.toHaveBeenCalled(); + expect(refreshSpy).not.toHaveBeenCalled(); - getAssetsSpy.mockRestore(); + refreshSpy.mockRestore(); }); }); - it('calls getAssets with forceUpdate, price dataType, and assetsForPriceUpdate to refresh prices', async () => { + it('refreshes prices for held assets when the currency changes', async () => { const mockAssetId2 = 'eip155:1/erc20:0x6B175474E89094C44Da98b954EedeAC495271d0F' as Caip19AssetId; const initialState: Partial = { @@ -4162,17 +4218,15 @@ describe('AssetsController', () => { }; await withController({ state: initialState }, ({ controller }) => { - const getAssetsSpy = vi.spyOn(controller, 'getAssets'); + const refreshSpy = vi + .spyOn(controller, 'syncAssetsPrices') + .mockResolvedValue(undefined); controller.setSelectedCurrency('eur'); - expect(getAssetsSpy).toHaveBeenCalledWith(expect.any(Array), { - forceUpdate: true, - dataTypes: ['price'], - assetsForPriceUpdate: [MOCK_ASSET_ID, mockAssetId2], - }); + expect(refreshSpy).toHaveBeenCalledWith([MOCK_ASSET_ID, mockAssetId2]); - getAssetsSpy.mockRestore(); + refreshSpy.mockRestore(); }); }); @@ -4188,14 +4242,14 @@ describe('AssetsController', () => { await withController( { state: initialState, isBasicFunctionality: () => false }, ({ controller }) => { - const getAssetsSpy = vi.spyOn(controller, 'getAssets'); + const refreshSpy = vi.spyOn(controller, 'syncAssetsPrices'); controller.setSelectedCurrency('eur'); expect(controller.state.selectedCurrency).toBe('eur'); - expect(getAssetsSpy).not.toHaveBeenCalled(); + expect(refreshSpy).not.toHaveBeenCalled(); - getAssetsSpy.mockRestore(); + refreshSpy.mockRestore(); }, ); }); @@ -5021,7 +5075,6 @@ describe('AssetsController', () => { resolveGetAssets = (): void => resolve({}); }), ); - // First trigger: unlock + account tree ready. #start() kicks off // #runStartupRefresh(), whose forced getAssets() call is still // pending (it never resolves until we call resolveGetAssets below). @@ -5050,21 +5103,13 @@ describe('AssetsController', () => { resolveGetAssets?.(); await flushPromises(); - // Once the single in-flight startup refresh settles, it also - // triggers one follow-up price-only getAssets() call — but a - // duplicate #runStartupRefresh() would double both of these (4 - // total), not just add one. Two calls confirms no duplicate ran. - expect(getAssetsSpy).toHaveBeenCalledTimes(2); - expect(getAssetsSpy).toHaveBeenNthCalledWith( - 1, + // The startup refresh remains a single getAssets call. Prices are + // fetched inside that pipeline pass. + expect(getAssetsSpy).toHaveBeenCalledTimes(1); + expect(getAssetsSpy).toHaveBeenCalledWith( expect.anything(), expect.objectContaining({ forceUpdate: true }), ); - expect(getAssetsSpy).toHaveBeenNthCalledWith( - 2, - expect.anything(), - expect.objectContaining({ dataTypes: ['price'] }), - ); getAssetsSpy.mockRestore(); }, ); @@ -5157,6 +5202,57 @@ describe('AssetsController', () => { }); }); + it('prices a default tracked asset seeded when a chain is enabled', async () => { + const [lineaMusd] = getDefaultTrackedAssetsForChain('eip155:59144'); + + await withController(async ({ controller, messenger }) => { + await activateTracking(messenger); + + let musdPresentAtPriceFetch: boolean | undefined; + const fetchSpy = vi + .spyOn(PriceDataSource.prototype, 'fetch') + .mockImplementation(async () => { + musdPresentAtPriceFetch ??= Boolean( + controller.state.assetsBalance[MOCK_ACCOUNT_ID]?.[lineaMusd], + ); + return {}; + }); + + try { + ( + messenger as unknown as { + publish: ( + topic: string, + payload?: unknown, + patches?: unknown, + ) => void; + } + ).publish( + 'NetworkEnablementController:stateChange', + { + enabledNetworkMap: { + eip155: { '1': true, '59144': true }, + }, + nativeAssetIdentifiers: { + 'eip155:1': 'eip155:1/slip44:60', + }, + }, + [], + ); + + // The price poll snapshots state synchronously on an existing + // subscription, before this handler yields. + expect(fetchSpy).toHaveBeenCalled(); + expect(musdPresentAtPriceFetch).toBe(true); + expect( + controller.state.assetsBalance[MOCK_ACCOUNT_ID]?.[lineaMusd], + ).toStrictEqual({ amount: '0' }); + } finally { + fetchSpy.mockRestore(); + } + }); + }); + it('seeds a zero native balance for a Solana account with no assets', async () => { // The Accounts API returns nothing at all for an account that holds no // assets — not even a zero native balance — so the controller has to @@ -5444,6 +5540,156 @@ describe('AssetsController', () => { controller.destroy(); } }); + + it('prices a native seeded when the selected EVM network switches', async () => { + const sepoliaHex = '0xaa36a7'; + const mainnetHex = '0x1'; + let selectedNetworkClientId = 'sepolia'; + const switchedAccountId = 'switched-account-id'; + let selectedAccounts = [createMockInternalAccount()]; + + const getNetworkState = (): NetworkState => ({ + networkConfigurationsByChainId: {}, + networksMetadata: {}, + selectedNetworkClientId, + }); + + const messenger: RootMessenger = new Messenger({ + namespace: MOCK_ANY_NAMESPACE, + }); + + registerKeyringUnlockMock(messenger); + + messenger.registerActionHandler( + 'AccountTreeController:getAccountsFromSelectedAccountGroup', + () => selectedAccounts, + ); + let isAccountTreeInitialized = false; + messenger.registerActionHandler( + 'AccountTreeController:isInitialized', + () => isAccountTreeInitialized, + ); + messenger.subscribe('AccountTreeController:initialized', () => { + isAccountTreeInitialized = true; + }); + messenger.subscribe('AccountTreeController:uninitialized', () => { + isAccountTreeInitialized = false; + }); + messenger.registerActionHandler( + 'NetworkEnablementController:getState', + () => ({ + enabledNetworkMap: { eip155: { '1': true, '11155111': true } }, + nativeAssetIdentifiers: { + 'eip155:1': 'eip155:1/slip44:60', + 'eip155:11155111': 'eip155:11155111/slip44:60', + }, + }), + ); + messenger.registerActionHandler( + 'NetworkController:getState', + getNetworkState, + ); + ( + messenger as { + registerActionHandler: ( + actionName: string, + handler: (id: string) => unknown, + ) => void; + } + ).registerActionHandler( + 'NetworkController:getNetworkClientById', + (networkClientId: string) => ({ + provider: {}, + configuration: { + chainId: networkClientId === 'mainnet' ? mainnetHex : sepoliaHex, + }, + }), + ); + ( + messenger as { + registerActionHandler: ( + actionName: string, + handler: () => unknown, + ) => void; + } + ).registerActionHandler('ClientController:getState', () => ({ + isUiOpen: true, + })); + + const queryApiClient = { + ...createMockQueryApiClient(), + accounts: { + fetchV2SupportedNetworks: vi.fn().mockResolvedValue({ + fullSupport: [1, 11155111], + partialSupport: [], + }), + fetchV5MultiAccountBalances: vi.fn().mockResolvedValue({ + balances: [], + unprocessedNetworks: [], + }), + }, + } as unknown as ApiPlatformClient; + + const controller = new AssetsController({ + messenger: messenger as unknown as AssetsControllerMessenger, + queryApiClient, + subscribeToBasicFunctionalityChange: (): void => { + /* no-op */ + }, + }); + + let nativePresentAtPriceFetch: boolean | undefined; + const fetchSpy = vi + .spyOn(PriceDataSource.prototype, 'fetch') + .mockImplementation(async () => { + nativePresentAtPriceFetch ??= Boolean( + controller.state.assetsBalance[switchedAccountId]?.[ + MOCK_NATIVE_ASSET_ID + ], + ); + return {}; + }); + + try { + ( + messenger as unknown as { + publish: (topic: string, payload?: unknown) => void; + } + ).publish('ClientController:stateChanged', { isUiOpen: true }); + messenger.publish('KeyringController:unlock'); + (messenger as unknown as { publish: LifecyclePublish }).publish( + 'AccountTreeController:initialized', + {}, + ); + await flushPromises(); + + // A new account has no native row yet. The switch must seed it + // before the price subscription snapshots state. + fetchSpy.mockClear(); + nativePresentAtPriceFetch = undefined; + selectedAccounts = [ + createMockInternalAccount({ id: switchedAccountId }), + ]; + selectedNetworkClientId = 'mainnet'; + (messenger as unknown as { publish: LifecyclePublish }).publish( + 'NetworkController:networkDidChange', + getNetworkState(), + ); + await flushPromises(); + + expect(fetchSpy).toHaveBeenCalled(); + expect(nativePresentAtPriceFetch).toBe(true); + expect( + controller.state.assetsBalance[switchedAccountId]?.[ + MOCK_NATIVE_ASSET_ID + ], + ).toStrictEqual({ amount: '0' }); + } finally { + fetchSpy.mockRestore(); + await flushPromises(); + controller.destroy(); + } + }); }); describe('account group changes', () => { @@ -5940,21 +6186,11 @@ describe('AssetsController', () => { ); await new Promise((resolve) => setTimeout(resolve, 100)); - expect(getAssetsSpy).toHaveBeenCalledTimes(2); - expect(getAssetsSpy).toHaveBeenNthCalledWith( - 1, + expect(getAssetsSpy).toHaveBeenCalledTimes(1); + expect(getAssetsSpy).toHaveBeenCalledWith( [expect.objectContaining({ id: MOCK_ACCOUNT_ID })], expect.objectContaining({ forceUpdate: true }), ); - expect(getAssetsSpy).toHaveBeenNthCalledWith( - 2, - [expect.objectContaining({ id: MOCK_ACCOUNT_ID })], - expect.objectContaining({ - forceUpdate: true, - dataTypes: ['price'], - assetsForPriceUpdate: expect.arrayContaining(['eip155:1/slip44:60']), - }), - ); controller.destroy(); }); diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index e1873113d3f..7a046a342be 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -92,10 +92,7 @@ import { AccountsApiDataSource } from './data-sources/AccountsApiDataSource.js'; import { isStakingContractAssetId } from './data-sources/evm-rpc-services/index.js'; import { shouldSkipNativeForCaipChainId } from './data-sources/evm-rpc-services/utils/assets.js'; import type { PriceDataSourceConfig } from './data-sources/PriceDataSource.js'; -import { - isPriceableAsset, - PriceDataSource, -} from './data-sources/PriceDataSource.js'; +import { PriceDataSource } from './data-sources/PriceDataSource.js'; import type { RpcDataSourceConfig } from './data-sources/RpcDataSource.js'; import { RpcDataSource } from './data-sources/RpcDataSource.js'; import type { AccountsControllerAccountBalancesUpdatedEvent } from './data-sources/SnapDataSource.js'; @@ -114,7 +111,6 @@ import { } from './defaults.js'; import { projectLogger, createModuleLogger } from './logger.js'; import { CustomAssetGraduationMiddleware } from './middlewares/CustomAssetGraduationMiddleware.js'; -import { DetectionMiddleware } from './middlewares/DetectionMiddleware.js'; import { createParallelBalanceMiddleware, createParallelMiddleware, @@ -218,6 +214,7 @@ const MESSENGER_EXPOSED_METHODS = [ 'getAccountAssetsByIDs', 'getAccountAssetsByScope', 'getAssetsPrice', + 'syncAssetsPrices', 'getExchangeRatesForBridge', 'getStateForTransactionPay', 'addCustomAsset', @@ -311,16 +308,10 @@ export type AssetsControllerPriceChangedEvent = { payload: [{ prices: Record }]; }; -export type AssetsControllerAssetsDetectedEvent = { - type: `${typeof CONTROLLER_NAME}:assetsDetected`; - payload: [{ accountId: AccountId; assetIds: Caip19AssetId[] }]; -}; - export type AssetsControllerEvents = | AssetsControllerStateChangeEvent | AssetsControllerBalanceChangedEvent - | AssetsControllerPriceChangedEvent - | AssetsControllerAssetsDetectedEvent; + | AssetsControllerPriceChangedEvent; type AllowedActions = // AssetsController @@ -553,18 +544,6 @@ function normalizeResponse(response: DataResponse): DataResponse { } } - // Preserve detectedAssets with normalized asset IDs - if (response.detectedAssets) { - normalized.detectedAssets = {}; - for (const [accountId, assetIds] of Object.entries( - response.detectedAssets, - )) { - normalized.detectedAssets[accountId] = assetIds.map((assetId) => - normalizeAssetId(assetId), - ); - } - } - // Preserve errors (chain IDs don't need normalization) if (response.errors) { normalized.errors = { ...response.errors }; @@ -916,8 +895,6 @@ export class AssetsController extends BaseController< readonly #priceDataSource: PriceDataSource; - readonly #detectionMiddleware: DetectionMiddleware; - readonly #customAssetGraduationMiddleware: CustomAssetGraduationMiddleware; readonly #rpcFallbackMiddleware: RpcFallbackMiddleware; @@ -1060,9 +1037,6 @@ export class AssetsController extends BaseController< getAssetsState: (): AssetsControllerState => this.state, ...priceDataSourceConfig, }); - this.#detectionMiddleware = new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => this.state, - }); this.#customAssetGraduationMiddleware = new CustomAssetGraduationMiddleware( { getSelectedAccountId: (): AccountId | undefined => { @@ -1503,13 +1477,11 @@ export class AssetsController extends BaseController< this.#ensureDefaultTrackedAssetsSeeded(); // Balances were just force-fetched — skip AccountsApi's subscribe-time poll. this.#subscribeAssets({ skipInitialFetch: true }); - this.#fetchMissingPricesWithoutCache(accounts, [...this.#enabledChains]); } catch (error) { log('Failed to fetch assets on startup', error); this.#ensureNativeBalancesDefaultZero(); this.#ensureDefaultTrackedAssetsSeeded(); this.#subscribeAssets({ skipInitialFetch: true }); - this.#fetchMissingPricesWithoutCache(accounts, [...this.#enabledChains]); } finally { releaseLock(); } @@ -1634,8 +1606,6 @@ export class AssetsController extends BaseController< */ bypassServerCache?: boolean; dataTypes?: DataType[]; - /** Specific CAIP-19 asset IDs whose prices should be refreshed. */ - assetsForPriceUpdate?: Caip19AssetId[]; }, ): Promise>> { const chainIds = options?.chainIds ?? [...this.#enabledChains]; @@ -1647,42 +1617,99 @@ export class AssetsController extends BaseController< } if (options?.forceUpdate) { - const pipelineTrace = this.#firstInitFetchReported - ? undefined - : this.#trace; - const requestOptions = { - assetTypes, - dataTypes, - forceUpdate: true as const, - bypassServerCache: options?.bypassServerCache, - assetsForPriceUpdate: options?.assetsForPriceUpdate, - }; - - if (this.#isBalanceV6Enabled()) { - await this.#forceUpdateAssetsV6({ - accounts, - chainIds, - request: this.#buildDataRequest(accounts, chainIds, requestOptions), - pipelineTrace, - }); - } else { - await this.#forceUpdateAssetsV5({ - accounts, - chainIds, - request: this.#buildForceUpdateRequestV5( - accounts, - chainIds, - requestOptions, - ), - pipelineTrace, - }); - } + await this.#forceUpdateAssets({ + accounts, + chainIds, + requestOptions: { + assetTypes, + dataTypes, + forceUpdate: true, + bypassServerCache: options.bypassServerCache, + }, + }); } const result = this.#getAssetsFromState(accounts, chainIds, assetTypes); return result; } + /** + * Force-fetch spot prices for an explicit asset list. + * + * Calls `PriceDataSource` directly and merges the result into `assetsPrice`. + * Prices are shared across accounts, so this does not take an account or + * chain scope and does not run the balance pipeline. + * + * Returns without calling the Price API when basic functionality is off. + * That matches the asset pipeline, which drops the price source in RPC-only + * mode, and `setSelectedCurrency`, which skips its price refresh. + * + * @param assetIds - CAIP-19 asset IDs to sync. + */ + async syncAssetsPrices(assetIds: Caip19AssetId[]): Promise { + if (!this.#isBasicFunctionality() || assetIds.length === 0) { + return; + } + + const assetsPrice = await this.#priceDataSource.fetchAssetPrices(assetIds); + if (Object.keys(assetsPrice).length === 0) { + return; + } + + await this.#updateState({ + assetsPrice, + updateMode: 'merge', + }); + } + + /** + * Run a forced fetch on the v5 or v6 pipeline. + * + * @param params - Accounts, chains, and request fields for this fetch. + * @param params.accounts - Accounts in this fetch. + * @param params.chainIds - Chains in this fetch. + * @param params.requestOptions - Shared force-update request fields. + */ + async #forceUpdateAssets({ + accounts, + chainIds, + requestOptions, + }: { + accounts: InternalAccount[]; + chainIds: ChainId[]; + requestOptions: { + assetTypes: AssetType[]; + dataTypes: DataType[]; + forceUpdate: true; + bypassServerCache?: boolean; + }; + }): Promise { + const pipelineTrace = this.#firstInitFetchReported + ? undefined + : this.#trace; + + if (this.#isBalanceV6Enabled()) { + await this.#forceUpdateAssetsV6({ + accounts, + chainIds, + request: this.#buildDataRequest(accounts, chainIds, requestOptions), + pipelineTrace, + }); + return; + } + + await this.#forceUpdateAssetsV5({ + accounts, + chainIds, + request: this.#buildForceUpdateRequestV5( + accounts, + chainIds, + requestOptions, + ), + pipelineTrace, + }); + } + /** * v5 force-update request: every pin of the requested accounts, unscoped. * Delete with the rest of the v5 path when `assetsAccountsApiV6` is the default. @@ -1694,7 +1721,6 @@ export class AssetsController extends BaseController< * @param requestOptions.dataTypes - Data types to fetch. * @param requestOptions.forceUpdate - Always `true` to bypass caches. * @param requestOptions.bypassServerCache - Also bypass server-side HTTP caches. - * @param requestOptions.assetsForPriceUpdate - Assets to refresh prices for. * @returns The v5 data request. */ #buildForceUpdateRequestV5( @@ -1705,7 +1731,6 @@ export class AssetsController extends BaseController< dataTypes: DataType[]; forceUpdate: true; bypassServerCache?: boolean; - assetsForPriceUpdate?: Caip19AssetId[]; }, ): DataRequest { const customAssets: Caip19AssetId[] = []; @@ -1736,7 +1761,6 @@ export class AssetsController extends BaseController< accountsApiDataSource: this.#accountsApiDataSource, customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -1779,7 +1803,6 @@ export class AssetsController extends BaseController< accountsApiDataSource: this.#accountsApiDataSource, customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -1938,7 +1961,6 @@ export class AssetsController extends BaseController< const { response } = await this.#executeMiddlewares({ sources: [ createParallelBalanceMiddleware(slowSources), - this.#detectionMiddleware, ...(isBasicFunctionality ? [ createParallelMiddleware([ @@ -2537,74 +2559,15 @@ export class AssetsController extends BaseController< // Currency changed — old cached prices are in the wrong currency. this.#priceDataSource.invalidatePriceCache(); - this.getAssets(this.#getSelectedAccounts(), { - forceUpdate: true, - dataTypes: ['price'], - assetsForPriceUpdate: Object.values(this.state.assetsBalance).flatMap( + this.syncAssetsPrices( + Object.values(this.state.assetsBalance).flatMap( (balances) => Object.keys(balances) as Caip19AssetId[], ), - }).catch((error) => { + ).catch((error) => { log('Failed to fetch asset prices after current currency change', error); }); } - /** - * Fetch spot prices (bypassing deduper cache) for held assets that have no - * entry in `assetsPrice` yet. Used when switching or adding a network. - * - * @param accounts - Accounts whose held assets should be priced. - * @param chainIds - Chains to scope the check and fetch. - */ - #fetchMissingPricesWithoutCache( - accounts: InternalAccount[], - chainIds: ChainId[], - ): void { - if (!this.#isBasicFunctionality() || accounts.length === 0) { - return; - } - - const accountIds = new Set(accounts.map((account) => account.id)); - const chainFilter = new Set(chainIds); - const assetsForPriceUpdate: Caip19AssetId[] = []; - const prices = this.state.assetsPrice as Record; - - for (const [accountId, accountBalances] of Object.entries( - this.state.assetsBalance, - )) { - if (!accountIds.has(accountId)) { - continue; - } - for (const assetId of Object.keys(accountBalances)) { - if (!chainFilter.has(assetId.split('/')[0] as ChainId)) { - continue; - } - const normalizedAssetId = normalizeAssetId(assetId as Caip19AssetId); - if (prices[normalizedAssetId] ?? prices[assetId]) { - continue; - } - if (!isPriceableAsset(normalizedAssetId)) { - continue; - } - assetsForPriceUpdate.push(normalizedAssetId); - } - } - - if (assetsForPriceUpdate.length === 0) { - return; - } - - this.#priceDataSource.invalidatePriceCache(); - - this.getAssets(accounts, { - forceUpdate: true, - dataTypes: ['price'], - chainIds, - assetsForPriceUpdate, - }).catch((error: unknown) => { - log('Failed to fetch missing prices', { error }); - }); - } - // ============================================================================ // SUBSCRIPTIONS // ============================================================================ @@ -3006,9 +2969,6 @@ export class AssetsController extends BaseController< try { const previousState = this.state; const previousPrices = { ...this.state.assetsPrice }; - // Use detectedAssets from response (assets without metadata) - const detectedAssets: Record = - normalizedResponse.detectedAssets ?? {}; // Track actual changes for logging const changedBalances: { @@ -3294,13 +3254,6 @@ export class AssetsController extends BaseController< changedMetadata.length > 0 ? changedMetadata : undefined, changedPrices: changedPriceAssets.length > 0 ? changedPriceAssets : undefined, - newAssets: - Object.keys(detectedAssets).length > 0 - ? Object.entries(detectedAssets).map(([accountId, assets]) => ({ - accountId, - assets, - })) - : undefined, }); } @@ -3329,16 +3282,6 @@ export class AssetsController extends BaseController< prices: changedPrices, }); } - - // Publish assets detected events - for (const [accountId, assetIds] of Object.entries(detectedAssets)) { - if (assetIds.length > 0) { - this.messenger.publish('AssetsController:assetsDetected', { - accountId, - assetIds, - }); - } - } } finally { releaseLock(); } @@ -4042,6 +3985,8 @@ export class AssetsController extends BaseController< const releaseLock = await this.#accountRefreshMutex.acquire(); try { + this.#ensureNativeBalancesDefaultZero(); + this.#ensureDefaultTrackedAssetsSeeded(); if (accounts.length > 0) { await this.getAssets(accounts, { chainIds: [...this.#enabledChains], @@ -4049,10 +3994,7 @@ export class AssetsController extends BaseController< }); } - this.#ensureNativeBalancesDefaultZero(); - this.#ensureDefaultTrackedAssetsSeeded(); this.#subscribeAssets({ skipInitialFetch: true }); - this.#fetchMissingPricesWithoutCache(accounts, [...this.#enabledChains]); } finally { releaseLock(); } @@ -4091,27 +4033,23 @@ export class AssetsController extends BaseController< // Users may want to see historical balances even if the network is currently disabled. // The data will simply not be updated until the network is re-enabled. + const accounts = this.#getSelectedAccounts(); + this.#ensureNativeBalancesDefaultZero(); + if (addedChains.length > 0) { + this.#ensureDefaultTrackedAssetsSeeded(addedChains); + } + // Refresh subscriptions for new chain set this.#subscribeAssets(); // One-time fetch for newly enabled chains. v6 `full` replace is scoped to // those chains; existing balances on other chains are left untouched. - const accounts = this.#getSelectedAccounts(); if (addedChains.length > 0 && accounts.length > 0) { await this.getAssets(accounts, { chainIds: addedChains, forceUpdate: true, }); } - - this.#ensureNativeBalancesDefaultZero(); - // Seed default tracked assets (mUSD) for any chain the user has - // *just* enabled. This is what makes mUSD appear on Monad after - // the user finally adds it to NetworkEnablementController. - if (addedChains.length > 0) { - this.#ensureDefaultTrackedAssetsSeeded(addedChains); - } - this.#fetchMissingPricesWithoutCache(accounts, [...this.#enabledChains]); } /** @@ -4143,8 +4081,6 @@ export class AssetsController extends BaseController< }); this.#ensureDefaultTrackedAssetsSeeded([caipChainId]); - const accounts = this.#getSelectedAccounts(); - this.#fetchMissingPricesWithoutCache(accounts, [caipChainId]); } /** @@ -4211,6 +4147,7 @@ export class AssetsController extends BaseController< try { await this.#refreshActiveChainsOnNetworkSwitch(); + this.#ensureNativeBalancesDefaultZero(); this.#subscribeAssets(); await this.getAssets(accounts, { @@ -4218,9 +4155,6 @@ export class AssetsController extends BaseController< forceUpdate: true, dataTypes: ['balance', 'metadata', 'price'], }); - - this.#ensureNativeBalancesDefaultZero(); - this.#fetchMissingPricesWithoutCache(accounts, [selectedChainId]); } finally { releaseLock(); } @@ -4261,13 +4195,12 @@ export class AssetsController extends BaseController< return; } + this.#ensureNativeBalancesDefaultZero(); await this.getAssets(accounts, { chainIds: [caipChainId], forceUpdate: true, dataTypes: ['balance', 'metadata', 'price'], }); - this.#ensureNativeBalancesDefaultZero(); - this.#fetchMissingPricesWithoutCache(accounts, [caipChainId]); } /** @@ -4322,7 +4255,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4333,7 +4265,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4391,7 +4322,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4402,7 +4332,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4533,6 +4462,7 @@ export class AssetsController extends BaseController< 'AssetsController:getAccountAssetsByScope', ); this.messenger.unregisterActionHandler('AssetsController:getAssetsPrice'); + this.messenger.unregisterActionHandler('AssetsController:syncAssetsPrices'); this.messenger.unregisterActionHandler( 'AssetsController:getExchangeRatesForBridge', ); diff --git a/packages/assets-controller/src/AssetsController.ws-price-updates.integration.test.ts b/packages/assets-controller/src/AssetsController.ws-price-updates.integration.test.ts index 22133ed8997..abded4fe5ac 100644 --- a/packages/assets-controller/src/AssetsController.ws-price-updates.integration.test.ts +++ b/packages/assets-controller/src/AssetsController.ws-price-updates.integration.test.ts @@ -318,7 +318,7 @@ describe('AssetsController: websocket price updates', () => { it('did not refetch metadata for the enriched token', () => { expect(askedAbout(result.mocks.tokenAPI.assetBatches)).toStrictEqual( - new Set([ETH_ASSET_ID]), + new Set(), ); }); }); diff --git a/packages/assets-controller/src/README.md b/packages/assets-controller/src/README.md index 33535f73a5f..8cd99a71f61 100644 --- a/packages/assets-controller/src/README.md +++ b/packages/assets-controller/src/README.md @@ -21,11 +21,8 @@ The `AssetsController` is a unified asset management system that provides real-t │ ├───────────────────┤ │ │ │ │ │ RPC │ │ │ │ │ ├───────────────────┤ ├───────────────────┤ │ -│ │ Detection │ │ Detection │ │ -│ ├───────────────────┤ ├───────────────────┤ │ -│ │ Token │ │ Token │ │ -│ ├───────────────────┤ ├───────────────────┤ │ -│ │ Price │ │ Price │ │ +│ │ Token + Price │ │ Token + Price │ │ +│ │ (parallel) │ │ (parallel) │ │ │ └───────────────────┘ └───────────────────┘ │ │ │ │ On-demand data fetch Process incoming updates │ @@ -105,9 +102,8 @@ executeMiddlewares([ AccountsApiDataSource:getAssetsMiddleware, // Balance from API SnapDataSource:getAssetsMiddleware, // Balance from Snaps RpcDataSource:getAssetsMiddleware, // Balance from RPC - DetectionMiddleware:getAssetsMiddleware, // Marks assets without metadata - TokenDataSource:getAssetsMiddleware, // Enriches with token metadata - PriceDataSource:getAssetsMiddleware, // Fetches USD prices + TokenDataSource:getAssetsMiddleware, // Metadata for balances missing it + PriceDataSource:getAssetsMiddleware, // Prices for state + response balances ]) ``` @@ -115,13 +111,12 @@ executeMiddlewares([ ```typescript executeMiddlewares([ - DetectionMiddleware:getAssetsMiddleware, // Marks assets without metadata - TokenDataSource:getAssetsMiddleware, // Fetches missing metadata - PriceDataSource:getAssetsMiddleware, // Fetches prices for detected assets + TokenDataSource:getAssetsMiddleware, // Metadata for balances missing it + PriceDataSource:getAssetsMiddleware, // Prices for state + response balances ]) ``` -**Order matters**: Middlewares are stacked with `reduceRight`, so Detection runs first (populates `detectedAssets`), then Token enriches metadata, then Price fetches prices. +**Order matters**: Middlewares are stacked with `reduceRight`. Balance sources run first. Token and price then choose their own asset sets from the request, controller state, and the balance response. Hidden assets are excluded. --- @@ -152,9 +147,8 @@ start() // Called by KeyringController:unlock ├── AccountsApiDataSource fetches balances from API ├── SnapDataSource handles solana:*, bip122:*... ├── RpcDataSource handles any remaining EVM chains - ├── DetectionMiddleware marks assets without metadata - ├── TokenDataSource enriches metadata - └── PriceDataSource fetches prices for discovered assets + ├── TokenDataSource enriches metadata for balances that need it + └── PriceDataSource fetches prices for state and response balances ``` --- @@ -190,9 +184,8 @@ DataSource calls: messenger.call('AssetsController:assetsUpdate', response, sour │ └── assetsPrice - Price updates │ ├── executeMiddlewares(Event Stack, request, response) - │ ├── DetectionMiddleware - Marks assets without metadata - │ ├── TokenDataSource - Fetches missing metadata - │ └── PriceDataSource - Fetches prices for detected assets + │ ├── TokenDataSource - Fetches metadata for balances that need it + │ └── PriceDataSource - Fetches prices for state and response balances │ └── updateState(enrichedResponse) ├── Normalize asset IDs (checksum EVM addresses) @@ -203,7 +196,8 @@ DataSource calls: messenger.call('AssetsController:assetsUpdate', response, sour │ └── Publish events: ├── AssetsController:stateChange - └── AssetsController:assetsDetected (if assets without metadata) + ├── AssetsController:balanceChanged (when a balance changes) + └── AssetsController:priceChanged (when a price changes) ``` #### 3.3 Chain Assignment Algorithm @@ -622,27 +616,6 @@ messenger.subscribe('AssetsController:priceChanged', (event) => { --- -#### `AssetsController:assetsDetected` - -Emitted when assets without metadata are detected for an account. - -```typescript -messenger.subscribe('AssetsController:assetsDetected', (event) => { - console.log(`New assets detected for ${event.accountId}:`, event.assetIds); -}); -``` - -**Payload:** - -```typescript -{ - accountId: AccountId; - assetIds: Caip19AssetId[]; -} -``` - ---- - ### Type Definitions #### Identifier Types @@ -810,11 +783,10 @@ const multiChainAssets = await messenger.call( These middlewares process responses after primary data sources fetch balance data: -| Middleware | Purpose | When It Runs | -| ------------------- | -------------------------------------------------- | ------------------------- | -| DetectionMiddleware | Identifies assets without metadata | After balance fetch | -| TokenDataSource | Enriches with token metadata (symbol, name, image) | After detection | -| PriceDataSource | Fetches USD prices for assets | After metadata enrichment | +| Middleware | Purpose | When It Runs | +| --------------- | ------------------------------------------------------- | ------------------- | +| TokenDataSource | Enriches balances that are missing metadata | After balance fetch | +| PriceDataSource | Prices state assets and response balances, minus hidden | After balance fetch | All middlewares are part of the unified `subscribeAssets` flow—there are no separate subscription mechanisms. @@ -833,7 +805,7 @@ flowchart TB subgraph AssetsControllerInit["AssetsController (with queryApiClient)"] AC[AssetsController] - AC --> AA & API & SNAP & RPC & TOK & PRICE & DET + AC --> AA & API & SNAP & RPC & TOK & PRICE end subgraph DataSources["Data Source Instances - Order 1-4"] @@ -843,7 +815,6 @@ flowchart TB RPC[RpcDataSource - Order 4] TOK[TokenDataSource] PRICE[PriceDataSource] - DET[DetectionMiddleware] end subgraph ExternalDeps["External Dependencies"] @@ -947,11 +918,6 @@ messenger.subscribe('AssetsController:balanceChanged', (event) => { console.log(`${event.assetId}: ${event.previousAmount} → ${event.newAmount}`); }); -// New asset detection -messenger.subscribe('AssetsController:assetsDetected', (event) => { - showNotification(`Found ${event.assetIds.length} new tokens!`); -}); - // Price updates (includes full price data) messenger.subscribe('AssetsController:priceChanged', ({ prices }) => { refreshPriceDisplay(prices); diff --git a/packages/assets-controller/src/__fixtures__/assetSurfaces.ts b/packages/assets-controller/src/__fixtures__/assetSurfaces.ts index 56e37290c65..8f9dd51e5ae 100644 --- a/packages/assets-controller/src/__fixtures__/assetSurfaces.ts +++ b/packages/assets-controller/src/__fixtures__/assetSurfaces.ts @@ -77,24 +77,6 @@ export const buildPricesSurface = ( getIgnoringCase(selectPrices(source) ?? {}, assetId), }); -/** - * Build the `detected assets` surface over a detected-assets record. - * - * @param selectDetectedAssets - Reads the detected assets from the source. - * @returns The detected-assets surface. - */ -export const buildDetectedAssetsSurface = ( - selectDetectedAssets: ( - source: Source, - ) => Record | undefined, -): AssetSurface => ({ - surface: 'detected assets', - lookUp: (source, assetId) => - Object.values(selectDetectedAssets(source) ?? {}) - .flat() - .find((detectedId) => detectedId.toLowerCase() === assetId.toLowerCase()), -}); - /** * The `balances` surface over `AssetsController` state for one account. * @@ -140,9 +122,3 @@ export const RESPONSE_METADATA_SURFACE: ResponseSurface = buildMetadataSurface( export const RESPONSE_PRICES_SURFACE: ResponseSurface = buildPricesSurface( (response: DataResponse) => response.assetsPrice, ); - -/** The `detected assets` surface over a pipeline `DataResponse`. */ -export const RESPONSE_DETECTED_ASSETS_SURFACE: ResponseSurface = - buildDetectedAssetsSurface( - (response: DataResponse) => response.detectedAssets, - ); diff --git a/packages/assets-controller/src/__fixtures__/ws-price-updates/wsWallet.ts b/packages/assets-controller/src/__fixtures__/ws-price-updates/wsWallet.ts index 27a1cccd200..8420a4e9a3a 100644 --- a/packages/assets-controller/src/__fixtures__/ws-price-updates/wsWallet.ts +++ b/packages/assets-controller/src/__fixtures__/ws-price-updates/wsWallet.ts @@ -106,6 +106,8 @@ export function buildUsdcHeldAndPricedState(): AssetsControllerState { decimals: 6, image: 'https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png', + aggregators: ['metamask'], + occurrences: 5, }, }, assetsPrice: { diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index a698938c19a..4b176d2d68a 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -3,6 +3,7 @@ import type { InternalAccount } from '@metamask/keyring-internal-api'; import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'; import type { Mock } from 'vitest'; +import { getDefaultAssetsControllerState } from '../AssetsController.js'; import type { ChainId, DataRequest, @@ -108,6 +109,25 @@ type MockPriceData = { totalVolume: number; }; +/** + * Build controller state from the default fixture. + * + * Seeded default-token metadata is cleared unless the case supplies its own + * `assetsInfo`, so price requests stay limited to the balances under test. + * + * @param overrides - State fields this case sets. + * @returns A complete assets controller state. + */ +function createTestAssetsState( + overrides: Partial = {}, +): AssetsControllerState { + return { + ...getDefaultAssetsControllerState(), + assetsInfo: {}, + ...overrides, + }; +} + function createMockPriceData(price: number = 100): MockPriceData { return { price, @@ -240,6 +260,134 @@ describe('PriceDataSource', () => { controller.destroy(); }); + it('middleware prices response balances and leaves state-only balances to the poll', async () => { + const { controller, apiClient } = setupController({ + balanceState: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, + }, + }, + priceResponse: { + [MOCK_TOKEN_ASSET]: createMockPriceData(1), + }, + }); + const context = createMiddlewareContext({ + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '10' }, + }, + }, + }, + }); + + await controller.assetsMiddleware(context, vi.fn()); + + const assetsCalled = apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]; + expect(assetsCalled).toStrictEqual([normalizeAssetId(MOCK_TOKEN_ASSET)]); + + controller.destroy(); + }); + + it('middleware excludes hidden assets on the response', async () => { + const getAssetsState = (): AssetsControllerState => + createTestAssetsState({ + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, + }, + }, + assetPreferences: { + [MOCK_TOKEN_ASSET]: { hidden: true }, + }, + }); + const { controller, apiClient } = setupController({ + getAssetsState, + priceResponse: { + [MOCK_NATIVE_ASSET]: createMockPriceData(2500), + }, + }); + const context = createMiddlewareContext({ + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, + [MOCK_TOKEN_ASSET]: { amount: '10' }, + }, + }, + }, + }); + + await controller.assetsMiddleware(context, vi.fn()); + + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + [MOCK_NATIVE_ASSET], + expect.anything(), + ); + + controller.destroy(); + }); + + it.each([ + { + source: 'balance', + assetId: MOCK_NATIVE_ASSET, + }, + { + source: 'metadata', + assetId: + 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId, + }, + { + source: 'custom', + assetId: + 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId, + }, + ])( + 'middleware does not price a state-only $source asset', + async ({ source, assetId }) => { + const { controller, apiClient } = setupController({ + getAssetsState: () => + createTestAssetsState({ + ...(source === 'balance' + ? { + assetsBalance: { + 'mock-account-id': { [assetId]: { amount: '1' } }, + }, + } + : {}), + ...(source === 'metadata' + ? { + assetsInfo: { + [assetId]: { + type: 'erc20', + symbol: 'TOK', + name: 'Token', + decimals: 18, + }, + }, + } + : {}), + ...(source === 'custom' + ? { customAssets: { 'mock-account-id': [assetId] } } + : {}), + }), + priceResponse: { + [assetId]: createMockPriceData(1), + }, + }); + + await controller.assetsMiddleware( + createMiddlewareContext({ response: {} }), + vi.fn(), + ); + + expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); + + controller.destroy(); + }, + ); + it('fetch skips malformed asset IDs in balance state and still fetches prices for valid assets', async () => { const { controller, apiClient } = setupController({ balanceState: { @@ -841,7 +989,7 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware passes to next when no detected assets', async () => { + it('middleware passes to next when the response has no balances', async () => { const { controller } = setupController(); const next = vi.fn().mockResolvedValue(undefined); @@ -856,39 +1004,28 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware passes to next when detected assets is empty', async () => { - const { controller } = setupController(); - - const next = vi.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - response: { detectedAssets: {} }, + it('fetchAssetPrices requests spot prices for the given assets', async () => { + const { controller, apiClient } = setupController({ + priceResponse: { + [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), + }, }); - await controller.assetsMiddleware(context, next); - - expect(next).toHaveBeenCalledWith(context); + const prices = await controller.fetchAssetPrices([MOCK_TOKEN_ASSET]); - controller.destroy(); - }); - - it('middleware passes to next when assetsForPriceUpdate is empty and no detected assets', async () => { - const { controller, apiClient } = setupController(); - - const next = vi.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [] }), - response: {}, + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + [MOCK_TOKEN_ASSET], + { currency: 'usd', includeMarketData: true }, + ); + expect(prices[MOCK_TOKEN_ASSET]).toMatchObject({ + assetPriceType: 'fungible', + price: 1, }); - await controller.assetsMiddleware(context, next); - - expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); - expect(next).toHaveBeenCalledWith(context); - controller.destroy(); }); - it('middleware fetches prices when assetsForPriceUpdate has values', async () => { + it('middleware fetches prices for assets in the balance response', async () => { const { controller, apiClient } = setupController({ priceResponse: { [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), @@ -897,14 +1034,19 @@ describe('PriceDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + }, + }, }); await controller.assetsMiddleware(context, next); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( - [MOCK_TOKEN_ASSET], + [normalizeAssetId(MOCK_TOKEN_ASSET)], { currency: 'usd', includeMarketData: true }, ); expect(context.response.assetsPrice?.[MOCK_TOKEN_ASSET]).toStrictEqual({ @@ -921,43 +1063,87 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware fetches prices for detected assets', async () => { + it('middleware does not fetch a token that already has a price', async () => { const { controller, apiClient } = setupController({ + getAssetsState: () => + createTestAssetsState({ + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + [MOCK_NATIVE_ASSET]: { amount: '2' }, + }, + }, + assetsPrice: { + [normalizeAssetId(MOCK_TOKEN_ASSET)]: { + assetPriceType: 'fungible', + price: 1, + usdPrice: 1, + lastUpdated: 1, + }, + }, + }), priceResponse: { - [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), + [MOCK_NATIVE_ASSET]: createMockPriceData(2500), }, }); const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + [MOCK_NATIVE_ASSET]: { amount: '2' }, + }, }, }, }); await controller.assetsMiddleware(context, next); + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledTimes(1); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( - [normalizeAssetId(MOCK_TOKEN_ASSET)], + [MOCK_NATIVE_ASSET], { currency: 'usd', includeMarketData: true }, ); - expect(context.response.assetsPrice?.[MOCK_TOKEN_ASSET]).toStrictEqual({ - assetPriceType: 'fungible', - price: 1.0, - usdPrice: 1.0, - pricePercentChange1d: 2.5, - lastUpdated: expect.any(Number), - marketCap: 1000000000, - totalVolume: 50000000, + + controller.destroy(); + }); + + it('fetch still requests a token that already has a price', async () => { + const { controller, apiClient } = setupController({ + getAssetsState: () => + createTestAssetsState({ + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + }, + assetsPrice: { + [MOCK_TOKEN_ASSET]: { + assetPriceType: 'fungible', + price: 1, + usdPrice: 1, + lastUpdated: 1, + }, + }, + }), + priceResponse: { + [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), + }, }); - expect(next).toHaveBeenCalledWith(context); + + await controller.fetch(createDataRequest()); + + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + [normalizeAssetId(MOCK_TOKEN_ASSET)], + { currency: 'usd', includeMarketData: true }, + ); controller.destroy(); }); - it('middleware filters out non-priceable detected assets', async () => { + it('middleware filters out non-priceable balance assets', async () => { const tronBandwidthAsset = 'tron:0x2b6653dc/slip44:bandwidth' as Caip19AssetId; @@ -968,8 +1154,10 @@ describe('PriceDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [tronBandwidthAsset], + assetsBalance: { + 'mock-account-id': { + [tronBandwidthAsset]: { amount: '1' }, + }, }, }, }); @@ -992,8 +1180,10 @@ describe('PriceDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1026,8 +1216,10 @@ describe('PriceDataSource', () => { lastUpdated: Date.now(), }, }, - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1140,19 +1332,24 @@ describe('PriceDataSource', () => { }); const next = vi.fn().mockResolvedValue(undefined); + const balanceResponse = { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + }, + }; // First call — populates freshness cache const context1 = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: balanceResponse, }); await controller.assetsMiddleware(context1, next); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledTimes(1); // Second call — skipped (fresh) const context2 = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: balanceResponse, }); await controller.assetsMiddleware(context2, next); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledTimes(1); @@ -1160,8 +1357,7 @@ describe('PriceDataSource', () => { // Invalidate cache, then fetch again — API is called controller.invalidatePriceCache(); const context3 = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: balanceResponse, }); await controller.assetsMiddleware(context3, next); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledTimes(2); @@ -1185,15 +1381,20 @@ describe('PriceDataSource', () => { apiClient.prices.fetchV3SpotPrices.mockReturnValue(apiPromise); const next = vi.fn().mockResolvedValue(undefined); + const balanceResponse = { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + }, + }; // Fire two parallel middleware calls for the same asset const context1 = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: balanceResponse, }); const context2 = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, + response: balanceResponse, }); const promise1 = controller.assetsMiddleware(context1, next); @@ -1241,20 +1442,23 @@ describe('PriceDataSource', () => { expect.anything(), ); - // Now middleware requests MOCK_TOKEN_ASSET (not yet fetched) and MOCK_NATIVE_ASSET (fresh) + // The response adds MOCK_TOKEN_ASSET while state still contains the fresh native. const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ - request: createDataRequest({ - assetsForPriceUpdate: [MOCK_TOKEN_ASSET, MOCK_NATIVE_ASSET], - }), - response: {}, + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + }, + }, }); await controller.assetsMiddleware(context, next); // Only MOCK_TOKEN_ASSET should be sent to the API (MOCK_NATIVE_ASSET is fresh) expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledTimes(2); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenLastCalledWith( - [MOCK_TOKEN_ASSET], + [normalizeAssetId(MOCK_TOKEN_ASSET)], expect.anything(), ); @@ -1285,13 +1489,13 @@ describe('PriceDataSource', () => { apiClient as unknown as PriceDataSourceOptions['queryApiClient'], getSelectedCurrency: (): SupportedCurrency => 'usd', getAssetsState: (): AssetsControllerState => - ({ + createTestAssetsState({ assetsBalance: { 'mock-account-id': { [MOCK_NATIVE_ASSET]: { amount: '1.5' }, }, }, - }) as AssetsControllerState, + }), }); await controller2.fetch(createDataRequest()); @@ -1391,17 +1595,15 @@ describe('PriceDataSource', () => { describe('supported networks filtering', () => { const SUPPORTED_SOLANA_CHAIN = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'; const SUPPORTED_SOLANA_ASSET = `${SUPPORTED_SOLANA_CHAIN}/slip44:501`; - const UNSUPPORTED_SOLANA_ASSET = - 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1/slip44:501'; + const UNSUPPORTED_SOLANA_CHAIN = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1'; + const UNSUPPORTED_SOLANA_ASSET = `${UNSUPPORTED_SOLANA_CHAIN}/slip44:501`; const MALFORMED_ASSET_ID = 'not-a-valid-caip19' as Caip19AssetId; const arrangeMiddleware = ({ - assetsForPriceUpdate = [], - detectedAssets = [], + assets = [], supportedNetworks = ['eip155:1'], }: { - assetsForPriceUpdate?: Caip19AssetId[]; - detectedAssets?: Caip19AssetId[]; + assets?: Caip19AssetId[]; supportedNetworks?: string[]; }): { controller: PriceDataSource; @@ -1417,8 +1619,16 @@ describe('PriceDataSource', () => { }); const context = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate }), - response: { detectedAssets: { 'mock-account-id': detectedAssets } }, + request: createDataRequest({ + chainIds: [SUPPORTED_SOLANA_CHAIN, UNSUPPORTED_SOLANA_CHAIN], + }), + response: { + assetsBalance: { + 'mock-account-id': Object.fromEntries( + assets.map((assetId) => [assetId, { amount: '1' }]), + ), + }, + }, }); return { @@ -1443,16 +1653,7 @@ describe('PriceDataSource', () => { }, }, { - name: 'filters out detected assets from unsupported networks', - assets: [UNSUPPORTED_SOLANA_ASSET], - expectAPI: ({ - apiClient, - }: ReturnType): void => { - expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); - }, - }, - { - name: 'does not call API when all assetsForPriceUpdate are from unsupported networks', + name: 'filters out balance assets from unsupported networks', assets: [UNSUPPORTED_SOLANA_ASSET], expectAPI: ({ apiClient, @@ -1472,26 +1673,11 @@ describe('PriceDataSource', () => { ]; it.each(testCases)( - 'request.assetsForPriceUpdate price updates: $name', - async ({ assets, expectAPI }) => { - expect.hasAssertions(); - const setup = arrangeMiddleware({ - assetsForPriceUpdate: assets as Caip19AssetId[], - supportedNetworks: [SUPPORTED_SOLANA_CHAIN], - }); - - await setup.act(); - expectAPI(setup); - setup.controller.destroy(); - }, - ); - - it.each(testCases)( - 'request.detectedAssets price updates: $name', + 'balance price updates: $name', async ({ assets, expectAPI }) => { expect.hasAssertions(); const setup = arrangeMiddleware({ - detectedAssets: assets as Caip19AssetId[], + assets: assets as Caip19AssetId[], supportedNetworks: [SUPPORTED_SOLANA_CHAIN], }); diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index bd00a6eef92..3a72188c988 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -4,7 +4,6 @@ import type { V3SpotPricesResponse, } from '@metamask/core-backend'; import { ApiPlatformClient } from '@metamask/core-backend'; -import { parseCaipAssetType } from '@metamask/utils'; import { projectLogger, createModuleLogger } from '../logger.js'; import { forDataTypes } from '../types.js'; @@ -17,7 +16,13 @@ import type { AssetsControllerState, } from '../types.js'; import { DedupingBatchFetcher } from '../utils/dedupingBatchFetcher.js'; -import { fetchWithTimeout, safeNormalizeAssetId } from '../utils/index.js'; +import { + filterAssetsBySupportedNetworks, + getAssetIdsForPriceFetch, + getUnpricedResponseAssetIds, + isPriceableAsset, +} from '../utils/getAssetIdsForPriceFetch.js'; +import { fetchWithTimeout } from '../utils/index.js'; import type { SubscriptionRequest } from './AbstractDataSource.js'; import { reduceInBatchesSerially } from './evm-rpc-services/index.js'; @@ -113,30 +118,7 @@ export type PriceDataSourceOptions = PriceDataSourceConfig & { * Asset reference patterns that should NOT be sent to the Price API. * These are internal resource tracking values without market prices. */ -const NON_PRICEABLE_ASSET_PATTERNS = [ - // Synthetic slip44 staking-position assets: the Price API only knows about - // pure numeric coin-type references (e.g. slip44:195). Any suffix after the - // number (e.g. slip44:195-ready-for-withdrawal, slip44:195-in-lock-period, - // slip44:195-staking-rewards, slip44:195-staked-for-…) is a MetaMask-internal - // synthetic asset that has no market price. - /\/slip44:\d+-/u, - // Tron non-price resource assets (bandwidth, energy) - /\/slip44:bandwidth$/u, - /\/slip44:energy$/u, - /\/slip44:maximum-bandwidth$/u, - /\/slip44:maximum-energy$/u, -]; - -/** - * Check if an asset ID represents a priceable asset. - * Filters out internal resource tracking values that don't have market prices. - * - * @param assetId - The CAIP-19 asset ID to check. - * @returns True if the asset has market price data. - */ -export function isPriceableAsset(assetId: Caip19AssetId): boolean { - return !NON_PRICEABLE_ASSET_PATTERNS.some((pattern) => pattern.test(assetId)); -} +export { isPriceableAsset } from '../utils/getAssetIdsForPriceFetch.js'; /** Market data item from spot prices response (same as FungibleAssetPrice without lastUpdated) */ type SpotPriceMarketData = Omit< @@ -231,81 +213,34 @@ export class PriceDataSource { * Get the middleware for enriching responses with price data. * * This middleware: - * 1. Extracts the response from context - * 2. Fetches prices for detected assets (assets without metadata) - * 3. Enriches the response with fetched prices - * 4. Calls next() at the end to continue the middleware chain - * - * Note: This middleware ONLY fetches prices for detected assets. - * For fetching prices for all assets, use the subscription mechanism - * which polls prices for all assets in the balance state. + * 1. Selects response balances that still need a price. + * {@link getUnpricedResponseAssetIds} skips hidden, non-priceable, and + * already-priced assets, and assets on chains the Price API does not support + * 2. Fetches those prices. A price asked for within the freshness window is + * skipped, unless the request forces an update + * 3. Merges fetched prices into the response and continues the chain. A + * failed fetch leaves the response balances unchanged * * @returns The middleware function for the assets pipeline. */ get assetsMiddleware(): Middleware { return forDataTypes(['price'], async (ctx, next) => { - // Extract response from context const { response, request } = ctx; - - const statePrices = (this.#getAssetsState()?.assetsPrice ?? {}) as Record< - string, - FungibleAssetPrice - >; - - const assetIds = new Set(); - - for (const assetId of request.assetsForPriceUpdate ?? []) { - assetIds.add(assetId); - } - - // Detected assets only need a price fetch when state has none yet. - // Explicit assetsForPriceUpdate (e.g. currency change) are always fetched. - for (const detectedAccountAssets of Object.values( - response.detectedAssets ?? {}, - )) { - for (const assetId of detectedAccountAssets) { - const normalizedAssetId = safeNormalizeAssetId(assetId); - const alreadyQueued = request.assetsForPriceUpdate?.some( - (queuedId) => - queuedId === assetId || queuedId === normalizedAssetId, - ); - if ( - statePrices[assetId] === undefined && - statePrices[normalizedAssetId] === undefined && - !alreadyQueued - ) { - assetIds.add(normalizedAssetId); - } - } - } - - if (assetIds.size === 0) { - return next(ctx); - } - - // Filter to only priceable assets - const priceableAssetIds = [...assetIds].filter(isPriceableAsset); - - if (priceableAssetIds.length === 0) { - return next(ctx); - } - - const supportedNetworks = await this.#getSupportedNetworks(); - const supportedAssetIds = this.#filterAssetsByNetwork( - priceableAssetIds, - supportedNetworks, - ); - - if (supportedAssetIds.length === 0) { + const unpricedAssetIds = await getUnpricedResponseAssetIds({ + state: this.#getAssetsState(), + response, + getSupportedNetworks: () => this.#getSupportedNetworks(), + }); + if (unpricedAssetIds.length === 0) { return next(ctx); } if (request.forceUpdate) { - this.#deduper.invalidateKeys(supportedAssetIds); + this.#deduper.invalidateKeys(unpricedAssetIds); } try { - const spotPrices = await this.#fetchSpotPrices(supportedAssetIds); + const spotPrices = await this.#fetchSpotPrices(unpricedAssetIds); response.assetsPrice = { ...(response.assetsPrice ?? {}), ...spotPrices, @@ -319,6 +254,25 @@ export class PriceDataSource { }); } + /** + * Fetch spot prices for an explicit asset list, bypassing the freshness cache. + * + * @param assetIds - CAIP-19 asset IDs to price. + * @returns Prices keyed by asset ID. Empty when none are priceable or supported. + */ + async fetchAssetPrices( + assetIds: Caip19AssetId[], + ): Promise> { + const supportedAssetIds = + await this.#filterToSupportedPriceableAssets(assetIds); + if (supportedAssetIds.length === 0) { + return {}; + } + + this.#deduper.invalidateKeys(supportedAssetIds); + return this.#fetchSpotPrices(supportedAssetIds); + } + // ============================================================================ // HELPERS // ============================================================================ @@ -454,56 +408,37 @@ export class PriceDataSource { */ #getAssetIdsFromBalanceState(request: DataRequest): Caip19AssetId[] { try { - const state = this.#getAssetsState(); - const assetIds = new Set(); - - const accountIds = request.accountsWithSupportedChains.map( - (a) => a.account.id, - ); - const accountFilter = - accountIds.length > 0 ? new Set(accountIds) : undefined; - const chainFilter = - request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; - - if (state?.assetsBalance) { - for (const [accountId, accountBalances] of Object.entries( - state.assetsBalance, - )) { - // Filter by account if specified - if (accountFilter && !accountFilter.has(accountId)) { - continue; - } - - for (const assetId of Object.keys(accountBalances)) { - // Filter by chain if specified; skip malformed asset IDs for this entry only - if (chainFilter) { - try { - const { chainId } = parseCaipAssetType( - assetId as Caip19AssetId, - ); - if (!chainFilter.has(chainId)) { - continue; - } - } catch (error) { - log('Skipping malformed asset ID in balance state', { - assetId, - error, - }); - continue; - } - } - assetIds.add(assetId as Caip19AssetId); - } - } - } - - return [...assetIds]; + return [ + ...getAssetIdsForPriceFetch({ + state: this.#getAssetsState(), + request, + }), + ]; } catch (error) { log('Failed to get asset IDs from balance state', { error }); return []; } } + /** + * Drop non-priceable assets, then assets on chains the price API does not + * support. An empty supported-network set keeps every priceable asset. + * + * @param assetIds - Candidate CAIP-19 asset IDs. + * @returns Priceable IDs on a supported chain. + */ + async #filterToSupportedPriceableAssets( + assetIds: Caip19AssetId[], + ): Promise { + const priceableAssetIds = assetIds.filter(isPriceableAsset); + if (priceableAssetIds.length === 0) { + return []; + } + + const supportedNetworks = await this.#getSupportedNetworks(); + return this.#filterAssetsByNetwork(priceableAssetIds, supportedNetworks); + } + /** * Price API networks supported by the v3 spot-prices endpoint * (`fullSupport` plus `partialSupport.spotPricesV3`). @@ -536,18 +471,7 @@ export class PriceDataSource { assetIds: Caip19AssetId[], supportedNetworks: Set, ): Caip19AssetId[] { - if (supportedNetworks.size === 0) { - return assetIds; - } - return assetIds.filter((assetId) => { - try { - const parsed = parseCaipAssetType(assetId); - const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`; - return supportedNetworks.has(chainId); - } catch { - return false; - } - }); + return filterAssetsBySupportedNetworks(assetIds, supportedNetworks); } // ============================================================================ @@ -555,8 +479,10 @@ export class PriceDataSource { // ============================================================================ /** - * Fetch prices for assets held by the accounts and chains in the request. - * Reads asset IDs from balance state via the constructor `getAssetsState`. + * Fetch prices for visible assets on the accounts and chains in the request: + * saved balances and custom assets. Metadata with no balance is skipped. + * Unlike the middleware, this includes assets that already have a stored + * price so the poll can refresh them. The freshness cache skips recent ones. * * @param request - The data request specifying accounts and chains. * @returns DataResponse containing asset prices. @@ -564,28 +490,15 @@ export class PriceDataSource { async fetch(request: DataRequest): Promise { const response: DataResponse = {}; - // Get asset IDs from balance state - const rawAssetIds = this.#getAssetIdsFromBalanceState(request); - - // Filter out non-priceable assets (e.g., Tron bandwidth/energy resources) - const priceableAssetIds = rawAssetIds.filter(isPriceableAsset); - - if (priceableAssetIds.length === 0) { - return response; - } - - const supportedNetworks = await this.#getSupportedNetworks(); - const assetIds = this.#filterAssetsByNetwork( - priceableAssetIds, - supportedNetworks, + const assetIds = await this.#filterToSupportedPriceableAssets( + this.#getAssetIdsFromBalanceState(request), ); - if (assetIds.length === 0) { return response; } try { - const spotPrices = await this.#fetchSpotPrices([...assetIds]); + const spotPrices = await this.#fetchSpotPrices(assetIds); response.assetsPrice = { ...(response.assetsPrice ?? {}), diff --git a/packages/assets-controller/src/data-sources/RpcDataSource.test.ts b/packages/assets-controller/src/data-sources/RpcDataSource.test.ts index e060d373336..94f1aab41cc 100644 --- a/packages/assets-controller/src/data-sources/RpcDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/RpcDataSource.test.ts @@ -1565,14 +1565,13 @@ describe('RpcDataSource', () => { MOCK_CHAIN_ID_CAIP, createMockInternalAccount(), ); - expect(result.detectedAssets).toBeDefined(); expect(result.assetsBalance).toBeDefined(); expect(Object.keys(result.assetsInfo ?? {})).toHaveLength(1); }); detectTokensSpy.mockRestore(); }); - it('returns detected asset ids but omits assetsBalance when detected token has no decimals', async () => { + it('omits balance and metadata when detected token has no decimals', async () => { const assetId = 'eip155:1/erc20:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd' as Caip19AssetId; const detectTokensSpy = vi @@ -1604,9 +1603,6 @@ describe('RpcDataSource', () => { MOCK_CHAIN_ID_CAIP, createMockInternalAccount(), ); - expect(result.detectedAssets?.[MOCK_ACCOUNT_ID]).toStrictEqual([ - assetId, - ]); expect( result.assetsBalance?.[MOCK_ACCOUNT_ID]?.[assetId], ).toBeUndefined(); diff --git a/packages/assets-controller/src/data-sources/RpcDataSource.ts b/packages/assets-controller/src/data-sources/RpcDataSource.ts index baceaa24158..36a7aa13578 100644 --- a/packages/assets-controller/src/data-sources/RpcDataSource.ts +++ b/packages/assets-controller/src/data-sources/RpcDataSource.ts @@ -731,9 +731,6 @@ export class RpcDataSource extends AbstractDataSource< // Only send new data to AssetsController - it handles merging atomically // to avoid race conditions when concurrent updates occur for the same account const response: DataResponse = { - detectedAssets: { - [result.accountId]: result.detectedAssets.map((asset) => asset.assetId), - }, assetsInfo: newMetadata, assetsBalance: { [result.accountId]: newBalances, @@ -1704,9 +1701,6 @@ export class RpcDataSource extends AbstractDataSource< } const response: DataResponse = { - detectedAssets: { - [accountId]: result.detectedAssets.map((asset) => asset.assetId), - }, assetsBalance: { [accountId]: balances, }, diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts index f67c4d0607a..e98c9f4d19d 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts @@ -138,8 +138,8 @@ function createDataRequest( function createMiddlewareContext(overrides?: Partial): Context { return { request: createDataRequest(), - response: {}, ...overrides, + response: overrides?.response ?? {}, }; } @@ -217,7 +217,7 @@ describe('TokenDataSource', () => { expect(typeof middleware).toBe('function'); }); - it('middleware passes to next when no detected assets', async () => { + it('middleware passes to next when the response has no balances', async () => { const { controller } = setupController({ messenger: createTestMessenger(), }); @@ -232,22 +232,7 @@ describe('TokenDataSource', () => { expect(next).toHaveBeenCalledWith(context); }); - it('middleware passes to next when detected assets is empty', async () => { - const { controller } = setupController({ - messenger: createTestMessenger(), - }); - - const next = vi.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - response: { detectedAssets: {} }, - }); - - await controller.assetsMiddleware(context, next); - - expect(next).toHaveBeenCalledWith(context); - }); - - it('middleware fetches metadata for detected assets', async () => { + it('middleware fetches metadata for response balances', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -257,8 +242,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -326,9 +313,8 @@ describe('TokenDataSource', () => { }); it('middleware heals low-occurrence balance-only assets without spam-filtering them out', async () => { - // Already-tracked balances missing assetsInfo are healed via assetsBalance - // (not detectedAssets). Spam filtering must not strip those holdings or - // withhold metadata — DetectionMiddleware intentionally skips them. + // Balances already tracked in state, but missing metadata, are enriched + // without spam filtering. const lowOccurrenceAsset = 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId; @@ -339,6 +325,14 @@ describe('TokenDataSource', () => { createMockAssetResponse(lowOccurrenceAsset, { occurrences: 1 }), ], suggestedOccurrenceFloors: { '1': 3 }, + getAssetsState: (): Partial => ({ + assetsInfo: {}, + assetsBalance: { + 'mock-account-id': { + [lowOccurrenceAsset]: { amount: '50' }, + }, + }, + }), }); const next = vi.fn().mockResolvedValue(undefined); @@ -365,7 +359,7 @@ describe('TokenDataSource', () => { expect(next).toHaveBeenCalledWith(context); }); - it('middleware still spam-filters newly detected low-occurrence assets even when also in assetsBalance', async () => { + it('middleware spam-filters new low-occurrence balances', async () => { const spamAsset = 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId; @@ -379,9 +373,6 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [spamAsset], - }, assetsBalance: { 'mock-account-id': { [spamAsset]: { amount: '50' }, @@ -400,12 +391,9 @@ describe('TokenDataSource', () => { | undefined )?.[spamAsset], ).toBeUndefined(); - expect(context.response.detectedAssets?.['mock-account-id']).not.toContain( - spamAsset, - ); }); - it('middleware skips assets with existing metadata containing image in response', async () => { + it('middleware skips a response token whose metadata is already complete', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -414,8 +402,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, assetsInfo: { [MOCK_TOKEN_ASSET]: { @@ -424,6 +414,8 @@ describe('TokenDataSource', () => { symbol: 'EXT', decimals: 18, image: 'https://existing.com/icon.png', + aggregators: ['metamask'], + occurrences: 5, }, }, }, @@ -435,7 +427,7 @@ describe('TokenDataSource', () => { expect(next).toHaveBeenCalledWith(context); }); - it('middleware skips assets with existing metadata containing image in state', async () => { + it('middleware skips a state token whose metadata is already complete', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -447,6 +439,8 @@ describe('TokenDataSource', () => { symbol: 'STT', decimals: 18, image: 'https://state.com/icon.png', + aggregators: ['metamask'], + occurrences: 5, }, }, }), @@ -455,8 +449,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -477,8 +473,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, assetsInfo: { [MOCK_TOKEN_ASSET]: { @@ -520,8 +518,11 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET, unsupportedAsset], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + [unsupportedAsset]: { amount: '1' }, + }, }, }, }); @@ -554,8 +555,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [unsupportedAsset], + assetsBalance: { + 'mock-account-id': { + [unsupportedAsset]: { amount: '1' }, + }, }, }, }); @@ -578,8 +581,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -603,8 +608,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -630,8 +637,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_NATIVE_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1' }, + }, }, }, }); @@ -659,8 +668,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_SPL_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_SPL_ASSET]: { amount: '1' }, + }, }, }, }); @@ -709,8 +720,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [assetId], + assetsBalance: { + 'mock-account-id': { + [assetId]: { amount: '1' }, + }, }, }, }); @@ -743,8 +756,10 @@ describe('TokenDataSource', () => { image: 'https://dai.com/icon.png', }, }, - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -755,7 +770,7 @@ describe('TokenDataSource', () => { expect(context.response.assetsInfo?.[MOCK_TOKEN_ASSET]).toBeDefined(); }); - it('middleware handles multiple detected assets from multiple accounts', async () => { + it('middleware fetches metadata for balances from multiple accounts', async () => { const secondAsset = 'eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f' as Caip19AssetId; @@ -774,9 +789,13 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'account-1': [MOCK_TOKEN_ASSET], - 'account-2': [secondAsset], + assetsBalance: { + 'account-1': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + 'account-2': { + [secondAsset]: { amount: '1' }, + }, }, }, }); @@ -809,9 +828,13 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'account-1': [MOCK_TOKEN_ASSET], - 'account-2': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'account-1': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, + 'account-2': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -849,8 +872,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': assetIds, + assetsBalance: { + 'mock-account-id': Object.fromEntries( + assetIds.map((assetId) => [assetId, { amount: '1' }]), + ), }, }, }); @@ -880,8 +905,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -912,11 +939,11 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [ - MOCK_TOKEN_ASSET, - 'invalid-asset-id' as Caip19AssetId, - ], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + ['invalid-asset-id' as Caip19AssetId]: { amount: '1' }, + }, }, }, }); @@ -954,9 +981,6 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET, spamAsset], - }, assetsBalance: { 'mock-account-id': { [MOCK_TOKEN_ASSET]: { amount: '100' }, @@ -977,13 +1001,6 @@ describe('TokenDataSource', () => { ] as Record | undefined; expect(accountBalances?.[MOCK_TOKEN_ASSET]).toBeDefined(); expect(accountBalances?.[spamAsset]).toBeUndefined(); - - expect(context.response.detectedAssets?.['mock-account-id']).toContain( - MOCK_TOKEN_ASSET, - ); - expect(context.response.detectedAssets?.['mock-account-id']).not.toContain( - spamAsset, - ); }); it('middleware strips stub metadata of filtered-out assets so it does not persist', async () => { @@ -1003,9 +1020,6 @@ describe('TokenDataSource', () => { // persist to state and mask re-detection of the spam token. const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [spamAsset], - }, assetsBalance: { 'mock-account-id': { [spamAsset]: { amount: '50' }, @@ -1055,7 +1069,7 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); // Websocket-shaped update: brand-new tokens (absent from state) with - // seeded metadata stubs. Runs BEFORE DetectionMiddleware. + // seeded metadata stubs. const context = createMiddlewareContext({ request: createDataRequest({ dataTypes: ['balance'] }), response: { @@ -1283,8 +1297,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest({ chainIds: ['eip155:143'] }), response: { - detectedAssets: { - 'mock-account-id': [monadToken], + assetsBalance: { + 'mock-account-id': { + [monadToken]: { amount: '1' }, + }, }, }, }); @@ -1313,8 +1329,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest({ chainIds: ['eip155:137'] }), response: { - detectedAssets: { - 'mock-account-id': [polygonToken], + assetsBalance: { + 'mock-account-id': { + [polygonToken]: { amount: '1' }, + }, }, }, }); @@ -1341,8 +1359,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [spamAsset], + assetsBalance: { + 'mock-account-id': { + [spamAsset]: { amount: '1' }, + }, }, }, }); @@ -1381,8 +1401,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest(), response: { - detectedAssets: { - 'mock-account-id': [checksummedCustomAsset], + assetsBalance: { + 'mock-account-id': { + [checksummedCustomAsset]: { amount: '1' }, + }, }, }, }); @@ -1416,8 +1438,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [musdAsset], + assetsBalance: { + 'mock-account-id': { + [musdAsset]: { amount: '1' }, + }, }, }, }); @@ -1442,8 +1466,10 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [noOccurrenceAsset], + assetsBalance: { + 'mock-account-id': { + [noOccurrenceAsset]: { amount: '1' }, + }, }, }, }); @@ -1489,8 +1515,11 @@ describe('TokenDataSource', () => { const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [maliciousSolanaToken, benignSolanaToken], + assetsBalance: { + 'mock-account-id': { + [maliciousSolanaToken]: { amount: '1' }, + [benignSolanaToken]: { amount: '1' }, + }, }, }, }); @@ -1515,101 +1544,40 @@ describe('TokenDataSource', () => { ], }); - const next = vi.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - response: {}, - }); - - await controller.assetsMiddleware(context, next); - - expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); - expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]?.type).toBe( - 'native', - ); - }); - - it('middleware always includes native asset IDs in the fetch', async () => { - const { controller, apiClient } = setupController({ - messenger: createTestMessenger(), - supportedNetworks: ['eip155:1'], - nativeAssetIds: [MOCK_NATIVE_ASSET], - assetsResponse: [ - createMockAssetResponse(MOCK_TOKEN_ASSET), - createMockAssetResponse(MOCK_NATIVE_ASSET, { - name: 'Ethereum', - symbol: 'ETH', - }), - ], - }); - const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '0' }, + }, }, }, }); await controller.assetsMiddleware(context, next); - expect(apiClient.tokens.fetchV3Assets).toHaveBeenCalledWith( - expect.arrayContaining([MOCK_TOKEN_ASSET, MOCK_NATIVE_ASSET]), - expect.objectContaining({ includeIconUrl: true }), - ); expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]?.type).toBe( 'native', ); }); - it('middleware fetches native asset IDs even when detectedAssets is undefined', async () => { - const { controller, apiClient } = setupController({ - messenger: createTestMessenger(), - supportedNetworks: ['eip155:1'], - nativeAssetIds: [MOCK_NATIVE_ASSET], - assetsResponse: [ - createMockAssetResponse(MOCK_NATIVE_ASSET, { - name: 'Ethereum', - symbol: 'ETH', - }), - ], - }); - - const next = vi.fn().mockResolvedValue(undefined); - // detectedAssets is intentionally omitted (undefined) to mirror the real-world - // case where DetectionMiddleware finds zero balances and zero custom assets - const context = createMiddlewareContext({ - response: {}, - }); - - await controller.assetsMiddleware(context, next); - - expect(apiClient.tokens.fetchV3Assets).toHaveBeenCalledWith( - [MOCK_NATIVE_ASSET], - expect.objectContaining({ includeIconUrl: true }), - ); - expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); - }); - - it('middleware fetches native asset IDs when detectedAssets is an empty object', async () => { + it('middleware does not fetch a native that is not a balance', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], nativeAssetIds: [MOCK_NATIVE_ASSET], - assetsResponse: [ - createMockAssetResponse(MOCK_NATIVE_ASSET, { - name: 'Ethereum', - symbol: 'ETH', - }), - ], + assetsResponse: [createMockAssetResponse(MOCK_TOKEN_ASSET)], }); const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1617,30 +1585,43 @@ describe('TokenDataSource', () => { await controller.assetsMiddleware(context, next); expect(apiClient.tokens.fetchV3Assets).toHaveBeenCalledWith( - [MOCK_NATIVE_ASSET], + [MOCK_TOKEN_ASSET], expect.objectContaining({ includeIconUrl: true }), ); - expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); }); - it('middleware deduplicates native asset IDs with detected assets', async () => { + it('middleware skips a native balance whose metadata is already complete', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], nativeAssetIds: [MOCK_NATIVE_ASSET], - assetsResponse: [ - createMockAssetResponse(MOCK_NATIVE_ASSET, { - name: 'Ethereum', - symbol: 'ETH', - }), - ], + getAssetsState: () => ({ + assetsInfo: { + [MOCK_NATIVE_ASSET]: { + type: 'native', + symbol: 'ETH', + name: 'Ethereum', + decimals: 18, + image: 'https://example.com/eth.png', + aggregators: [], + occurrences: 1, + }, + }, + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '0' }, + }, + }, + }), }); const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_NATIVE_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1648,35 +1629,31 @@ describe('TokenDataSource', () => { await controller.assetsMiddleware(context, next); expect(apiClient.tokens.fetchV3Assets).toHaveBeenCalledWith( - [MOCK_NATIVE_ASSET], + [MOCK_TOKEN_ASSET], expect.objectContaining({ includeIconUrl: true }), ); }); - it('middleware includes multiple native asset IDs across chains', async () => { - const polygonNativeAsset = 'eip155:137/slip44:966' as Caip19AssetId; - + it('middleware deduplicates native asset IDs with response balances', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), - supportedNetworks: ['eip155:1', 'eip155:137'], - nativeAssetIds: [MOCK_NATIVE_ASSET, polygonNativeAsset], + supportedNetworks: ['eip155:1'], + nativeAssetIds: [MOCK_NATIVE_ASSET], assetsResponse: [ createMockAssetResponse(MOCK_NATIVE_ASSET, { name: 'Ethereum', symbol: 'ETH', }), - createMockAssetResponse(polygonNativeAsset, { - name: 'Polygon', - symbol: 'POL', - }), ], }); const next = vi.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1684,12 +1661,9 @@ describe('TokenDataSource', () => { await controller.assetsMiddleware(context, next); expect(apiClient.tokens.fetchV3Assets).toHaveBeenCalledWith( - expect.arrayContaining([ - MOCK_TOKEN_ASSET, - MOCK_NATIVE_ASSET, - polygonNativeAsset, - ]), + [MOCK_NATIVE_ASSET], expect.objectContaining({ includeIconUrl: true }), ); + expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); }); }); diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index 224dd6eae7f..b7460820d0b 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -19,11 +19,9 @@ import type { FungibleAssetMetadata, DataResponse, } from '../types.js'; +import { getAssetIdsForMetadataFetch } from '../utils/getAssetIdsForMetadataFetch.js'; import { fetchWithTimeout } from '../utils/index.js'; -import { - isStakingContractAssetId, - reduceInBatchesSerially, -} from './evm-rpc-services/index.js'; +import { reduceInBatchesSerially } from './evm-rpc-services/index.js'; // ============================================================================ // CONSTANTS @@ -174,17 +172,6 @@ function cleanResponseSpam( } } } - - // Correctly clean detectedAssets by its own Ids - if (response.detectedAssets) { - for (const [accountId, assetIds] of Object.entries( - response.detectedAssets, - )) { - response.detectedAssets[accountId] = assetIds.filter( - (id) => !spamLowerIds.has(id.toLowerCase()), - ); - } - } } // ============================================================================ @@ -195,12 +182,14 @@ function cleanResponseSpam( * TokenDataSource enriches responses with token metadata from the Tokens API. * * This middleware-based data source: - * - Checks detected assets for missing metadata/images - * - Also checks `assetsBalance` entries missing metadata/images (heal path) + * - Collects metadata for state balances that have no metadata yet, and for + * every balance on the current response + * - Excludes hidden assets * - Fetches metadata from Tokens API v3 for assets needing enrichment * - Filters EVM ERC-20 spam using per-chain floors from Token API - * `/v1/suggestedOccurrenceFloors` (default floor 3) for newly detected - * assets only; balance-only heals are enriched without spam-filtering + * `/v1/suggestedOccurrenceFloors` (default floor 3) for balances that are + * new to state; balances already tracked in state are enriched without + * spam-filtering * - Merges fetched metadata into the response * * Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid @@ -394,14 +383,14 @@ export class TokenDataSource { } /** - * Middleware that runs BEFORE DetectionMiddleware for account-activity - * (websocket) updates. New-to-state EVM ERC-20 balances are enriched with - * their Token API occurrence counts first, and tokens below the per-chain - * suggested occurrence floor are dropped from the response (balances and - * stub metadata) so spam airdrops never reach detection, metadata - * enrichment, pricing, or state. Custom assets and mUSD are exempt, and - * assets unknown to the Token API are kept (fail open), mirroring - * {@link TokenDataSource.assetsMiddleware} filtering semantics. + * Middleware that runs for account-activity (websocket) updates. New-to-state + * EVM ERC-20 balances are enriched with their Token API occurrence counts + * first, and tokens below the per-chain suggested occurrence floor are + * dropped from the response (balances and stub metadata) so spam airdrops + * never reach metadata enrichment, pricing, or state. Custom assets and + * mUSD are exempt, and assets unknown to the Token API are kept (fail + * open), mirroring {@link TokenDataSource.assetsMiddleware} filtering + * semantics. * * @returns The middleware function for the assets pipeline. */ @@ -429,8 +418,7 @@ export class TokenDataSource { ); // Candidates: EVM ERC-20s that are genuinely new (absent from state - // balances and metadata) — the same assets DetectionMiddleware would - // mark as newly detected right after this middleware. + // balances and metadata). const candidateByLowerId = new Map(); for (const [accountId, accountBalances] of Object.entries( response.assetsBalance ?? {}, @@ -527,31 +515,28 @@ export class TokenDataSource { * Get the middleware for enriching responses with token metadata. * * This middleware: - * 1. Extracts the response from context - * 2. Fetches metadata for detected assets and balances missing metadata + * 1. Fetches metadata for state balances that are missing a field clients + * or controller logic read, and for every balance on the current + * response, excluding hidden assets + * 2. Spam-filters balances that are new to state; balances already tracked + * are enriched without being removed * 3. Enriches the response with fetched metadata * 4. Calls next() at the end to continue the middleware chain * - * Spam filtering (EVM occurrence floors / non-EVM Blockaid) applies only to - * newly `detectedAssets`. Balance-only heals — assets already present in - * `assetsBalance` but missing `assetsInfo`, which DetectionMiddleware skips — - * are enriched without being filtered out of balances. - * * @returns The middleware function for the assets pipeline. */ get assetsMiddleware(): Middleware { return forDataTypes(['metadata'], async (ctx, next) => { - // Extract response from context - const { response } = ctx; - - const { assetsInfo: stateMetadata, customAssets } = - this.#getAssetsState(); - const assetIdsNeedingMetadata = new Set(); - // Newly detected asset IDs (lowercase) — subject to spam filtering. - const detectedAssetIds = new Set(); - // Balance-only heals (in assetsBalance, not newly detected) — enrich - // metadata but do not spam-filter / delete holdings. - const balanceHealAssetIds = new Set(); + const { request, response } = ctx; + const assetsState = this.#getAssetsState(); + const { customAssets } = assetsState; + const nativeAssetIdsList = this.#getNativeAssetIds(); + const { assetIdsNeedingMetadata, balanceHealAssetIds } = + getAssetIdsForMetadataFetch({ + state: assetsState, + request, + response, + }); // Custom assets are user-imported — exempt from spam filtering. // State stores asset IDs in their normalized (checksummed) form, but the @@ -563,66 +548,10 @@ export class TokenDataSource { .map((id) => id.toLowerCase()), ); - // Always include native asset IDs from NetworkEnablementController - const nativeAssetIdsList = this.#getNativeAssetIds(); + // Natives in the metadata response are kept even at zero occurrences. const nativeAssetIds = new Set( nativeAssetIdsList.map((id) => id.toLowerCase()), ); - for (const nativeAssetId of nativeAssetIdsList) { - assetIdsNeedingMetadata.add(nativeAssetId); - } - - // Also fetch metadata for detected assets that are missing it - if (response.detectedAssets) { - for (const detectedIds of Object.values(response.detectedAssets)) { - for (const assetId of detectedIds) { - // Skip if response already has metadata with image - const responseMetadata = response.assetsInfo?.[assetId]; - if (responseMetadata?.image) { - continue; - } - - // Skip if state already has metadata with image - const existingMetadata = stateMetadata[assetId]; - if (existingMetadata?.image) { - continue; - } - - // Skip staking contracts; we use built-in metadata and do not fetch from the tokens API - if (isStakingContractAssetId(assetId)) { - continue; - } - - detectedAssetIds.add(assetId.toLowerCase()); - assetIdsNeedingMetadata.add(assetId); - } - } - } - - // Also fetch metadata for balances that are missing it - if (response.assetsBalance) { - for (const accountBalances of Object.values(response.assetsBalance)) { - for (const assetId of Object.keys( - accountBalances, - ) as Caip19AssetId[]) { - if (response.assetsInfo?.[assetId]?.image) { - continue; - } - if (stateMetadata[assetId]?.image) { - continue; - } - if (isStakingContractAssetId(assetId)) { - continue; - } - assetIdsNeedingMetadata.add(assetId); - // Only treat as a heal when DetectionMiddleware did not queue it - // as newly detected — those remain subject to spam filtering. - if (!detectedAssetIds.has(assetId.toLowerCase())) { - balanceHealAssetIds.add(assetId.toLowerCase()); - } - } - } - } if (assetIdsNeedingMetadata.size === 0) { return next(ctx); diff --git a/packages/assets-controller/src/index.ts b/packages/assets-controller/src/index.ts index 3c328054c1c..dd56b5ee3c7 100644 --- a/packages/assets-controller/src/index.ts +++ b/packages/assets-controller/src/index.ts @@ -36,7 +36,6 @@ export type { AssetsControllerStateChangeEvent, AssetsControllerBalanceChangedEvent, AssetsControllerPriceChangedEvent, - AssetsControllerAssetsDetectedEvent, AssetsControllerEvents, } from './AssetsController.js'; export type { @@ -47,6 +46,7 @@ export type { AssetsControllerGetAccountAssetsByIDsAction, AssetsControllerGetAccountAssetsByScopeAction, AssetsControllerGetAssetsPriceAction, + AssetsControllerSyncAssetsPricesAction, AssetsControllerAddCustomAssetAction, AssetsControllerRemoveCustomAssetAction, AssetsControllerGetCustomAssetsAction, @@ -109,7 +109,6 @@ export type { BalanceChangeEvent, PriceChangeEvent, MetadataChangeEvent, - AssetsDetectedEvent, } from './types.js'; // Data sources - base class and types @@ -168,12 +167,10 @@ export type { // Middlewares export { CustomAssetGraduationMiddleware, - DetectionMiddleware, RpcFallbackMiddleware, } from './middlewares/index.js'; export type { CustomAssetGraduationMiddlewareOptions, - DetectionMiddlewareOptions, RpcFallbackMiddlewareOptions, } from './middlewares/index.js'; diff --git a/packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts b/packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts deleted file mode 100644 index 3aa2ff60e64..00000000000 --- a/packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts +++ /dev/null @@ -1,430 +0,0 @@ -import type { InternalAccount } from '@metamask/keyring-internal-api'; -import { Messenger } from '@metamask/messenger'; -import { afterEach, describe, expect, it, vi } from 'vitest'; - -import type { - Context, - DataRequest, - Caip19AssetId, - AssetsControllerState, -} from '../types.js'; -import { normalizeAssetId } from '../utils/index.js'; -import { DetectionMiddleware } from './DetectionMiddleware.js'; - -const MOCK_ADDRESS = '0x1234567890123456789012345678901234567890'; -const MOCK_ACCOUNT_ID = 'mock-account-id'; -const MOCK_ASSET_1 = - 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; -const MOCK_ASSET_2 = - 'eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7' as Caip19AssetId; -const MOCK_NATIVE_ASSET = 'eip155:1/slip44:60' as Caip19AssetId; - -function createMockAccount( - overrides?: Partial, -): InternalAccount { - return { - id: MOCK_ACCOUNT_ID, - address: MOCK_ADDRESS, - options: {}, - methods: [], - type: 'eip155:eoa', - scopes: ['eip155:0'], - metadata: { - name: 'Test Account', - keyring: { type: 'HD Key Tree' }, - importTime: Date.now(), - lastSelected: Date.now(), - }, - ...overrides, - }; -} - -function createDataRequest( - overrides?: Partial & { accounts?: InternalAccount[] }, -): DataRequest { - const chainIds = overrides?.chainIds ?? ['eip155:1']; - const accounts = overrides?.accounts ?? [createMockAccount()]; - const { accounts: _a, ...rest } = overrides ?? {}; - return { - chainIds, - accountsWithSupportedChains: accounts.map((a) => ({ - account: a, - supportedChains: chainIds, - })), - dataTypes: ['balance'], - ...rest, - }; -} - -function createAssetsState( - metadataAssets: Caip19AssetId[] = [], - assetsPrice: Caip19AssetId[] = [], -): AssetsControllerState { - const assetsInfo: Record = {}; - for (const assetId of metadataAssets) { - assetsInfo[assetId] = { name: `Asset ${assetId}` }; - } - const priceState: Record = {}; - for (const assetId of assetsPrice) { - priceState[assetId] = { price: 1 }; - } - return { - assetsInfo, - assetsBalance: {}, - customAssets: {}, - assetsPrice: priceState, - } as AssetsControllerState; -} - -function createMiddlewareContext(overrides?: Partial): Context { - return { - request: createDataRequest(), - response: {}, - ...overrides, - }; -} - -function setupController( - stateMetadata: Caip19AssetId[] = [], - stateAssetsPrice: Caip19AssetId[] = [], -): { - middleware: DetectionMiddleware; - messenger: Messenger<'DetectionMiddleware', never, never>; -} { - const messenger = new Messenger<'DetectionMiddleware', never, never>({ - namespace: 'DetectionMiddleware', - }); - - const middlewareInstance = new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => - createAssetsState(stateMetadata, stateAssetsPrice), - }); - - return { - middleware: middlewareInstance, - messenger, - }; -} - -describe('DetectionMiddleware', () => { - afterEach(() => { - vi.clearAllMocks(); - }); - - it('initializes with correct name', () => { - const { middleware } = setupController(); - expect(middleware.name).toBe('DetectionMiddleware'); - }); - - it('exposes getAssetsMiddleware on instance', () => { - const { middleware } = setupController(); - - const middlewareFn = middleware.assetsMiddleware; - expect(typeof middlewareFn).toBe('function'); - }); - - it('passes through when no balances in response', async () => { - const { middleware } = setupController(); - const context = createMiddlewareContext({ - response: {}, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(next).toHaveBeenCalledWith(context); - expect(context.response.detectedAssets).toBeUndefined(); - }); - - it('detects assets without metadata', async () => { - const { middleware } = setupController([]); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - [MOCK_ASSET_2]: { amount: '2000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1, MOCK_ASSET_2], - }); - expect(context.request.assetsForPriceUpdate).toStrictEqual([ - normalizeAssetId(MOCK_ASSET_1), - normalizeAssetId(MOCK_ASSET_2), - ]); - expect(next).toHaveBeenCalledWith(context); - }); - - it('skips balance assets that already have metadata in state', async () => { - const { middleware } = setupController([MOCK_ASSET_1, MOCK_NATIVE_ASSET]); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - [MOCK_NATIVE_ASSET]: { amount: '2000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - // Both assets are already in state.assetsInfo → nothing new to detect - expect(context.response.detectedAssets).toBeUndefined(); - expect(next).toHaveBeenCalledWith(context); - }); - - it('only detects assets not already in state (mixed scenario)', async () => { - const { middleware } = setupController([MOCK_ASSET_1]); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - [MOCK_ASSET_2]: { amount: '2000' }, - [MOCK_NATIVE_ASSET]: { amount: '3000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - // MOCK_ASSET_1 is already in state.assetsInfo → skipped; the other two are new - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_2, MOCK_NATIVE_ASSET], - }); - expect(next).toHaveBeenCalledWith(context); - }); - - it('handles multiple accounts, skipping assets already in state per account', async () => { - const { middleware } = setupController([MOCK_NATIVE_ASSET]); - const account2Id = 'account-2-id'; - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - [account2Id]: { - [MOCK_ASSET_2]: { amount: '2000' }, - [MOCK_NATIVE_ASSET]: { amount: '3000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - // MOCK_NATIVE_ASSET is in state.assetsInfo → skipped for account2; MOCK_ASSET_1 and MOCK_ASSET_2 are new - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1], - [account2Id]: [MOCK_ASSET_2], - }); - expect(next).toHaveBeenCalledWith(context); - }); - - it('skips an account entirely when all its balance assets are already in state', async () => { - const { middleware } = setupController([MOCK_ASSET_1]); - const account2Id = 'account-2-id'; - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - [account2Id]: { - [MOCK_ASSET_2]: { amount: '2000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - // MOCK_ASSET_1 is already in state.assetsInfo → MOCK_ACCOUNT_ID produces no new assets; - // MOCK_ASSET_2 is new → account2 is included - expect(context.response.detectedAssets).toStrictEqual({ - [account2Id]: [MOCK_ASSET_2], - }); - expect(next).toHaveBeenCalledWith(context); - }); - - it('only runs for balance dataType', async () => { - const { middleware } = setupController(); - const context = createMiddlewareContext({ - request: createDataRequest({ dataTypes: ['metadata'] }), - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toBeUndefined(); - expect(next).toHaveBeenCalledWith(context); - }); - - it('runs when dataTypes includes balance among others', async () => { - const { middleware } = setupController([]); - const context = createMiddlewareContext({ - request: createDataRequest({ dataTypes: ['balance', 'metadata'] }), - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1], - }); - expect(next).toHaveBeenCalledWith(context); - }); - - it('handles empty assetsBalance object', async () => { - const { middleware } = setupController(); - const context = createMiddlewareContext({ - response: { - assetsBalance: {}, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toBeUndefined(); - expect(next).toHaveBeenCalledWith(context); - }); - - it('handles account with empty balances', async () => { - const { middleware } = setupController(); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: {}, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toBeUndefined(); - expect(next).toHaveBeenCalledWith(context); - }); - - it('queues assetsForPriceUpdate for detected assets missing a price', async () => { - const { middleware } = setupController([], [MOCK_ASSET_1]); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - [MOCK_ASSET_2]: { amount: '2000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1, MOCK_ASSET_2], - }); - expect(context.request.assetsForPriceUpdate).toStrictEqual([ - normalizeAssetId(MOCK_ASSET_2), - ]); - expect(next).toHaveBeenCalledWith(context); - }); - - it('does not queue assetsForPriceUpdate when all detected assets have prices', async () => { - const { middleware } = setupController([], [MOCK_ASSET_1]); - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1], - }); - expect(context.request.assetsForPriceUpdate).toBeUndefined(); - expect(next).toHaveBeenCalledWith(context); - }); - - it('queues assetsForPriceUpdate for known balance assets that still lack a price', async () => { - const { middleware } = setupController([MOCK_NATIVE_ASSET]); - // Asset already has metadata (known / seeded) but no price yet. - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_NATIVE_ASSET]: { amount: '0' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middleware.assetsMiddleware(context, next); - - expect(context.response.detectedAssets).toBeUndefined(); - expect(context.request.assetsForPriceUpdate).toStrictEqual([ - normalizeAssetId(MOCK_NATIVE_ASSET), - ]); - expect(next).toHaveBeenCalledWith(context); - }); - - it('retrieves middleware from instance', async () => { - const { middleware } = setupController(); - const middlewareFn = middleware.assetsMiddleware; - - const context = createMiddlewareContext({ - response: { - assetsBalance: { - [MOCK_ACCOUNT_ID]: { - [MOCK_ASSET_1]: { amount: '1000' }, - }, - }, - }, - }); - const next = vi.fn().mockImplementation((ctx) => Promise.resolve(ctx)); - - await middlewareFn(context, next); - - expect(context.response.detectedAssets).toStrictEqual({ - [MOCK_ACCOUNT_ID]: [MOCK_ASSET_1], - }); - }); -}); diff --git a/packages/assets-controller/src/middlewares/DetectionMiddleware.ts b/packages/assets-controller/src/middlewares/DetectionMiddleware.ts deleted file mode 100644 index 7f6221c59ba..00000000000 --- a/packages/assets-controller/src/middlewares/DetectionMiddleware.ts +++ /dev/null @@ -1,201 +0,0 @@ -import { projectLogger, createModuleLogger } from '../logger.js'; -import { forDataTypes } from '../types.js'; -import type { - AccountId, - AssetsControllerState, - Caip19AssetId, - Middleware, -} from '../types.js'; -import { normalizeAssetId } from '../utils/index.js'; - -// ============================================================================ -// CONSTANTS -// ============================================================================ - -const CONTROLLER_NAME = 'DetectionMiddleware'; - -// Logger for debugging -createModuleLogger(projectLogger, CONTROLLER_NAME); - -// ============================================================================ -// DETECTION MIDDLEWARE -// ============================================================================ - -/** - * DetectionMiddleware builds the set of assets that downstream sources use for - * metadata and price fetching. - * - * An asset is included in `detectedAssets` only when it is genuinely new: - * - Assets from `response.assetsBalance` are included only if they are absent - * from BOTH `state.assetsBalance` (never tracked before) AND `state.assetsInfo` - * (no metadata yet). Assets already present in either collection are considered - * known and are intentionally excluded — PriceDataSource's own subscription - * handles periodic refreshes for those. - * - Each account's custom assets from state are always included because they - * may have no balance yet and are explicitly managed by the user. - * - * Usage: - * ```typescript - * const detectionMiddleware = new DetectionMiddleware({ - * getAssetsState: () => this.state, - * }); - * const middleware = detectionMiddleware.assetsMiddleware; - * ``` - */ -export type DetectionMiddlewareOptions = { - /** Current AssetsController state. Used to distinguish newly detected assets. */ - getAssetsState: () => AssetsControllerState; -}; - -export class DetectionMiddleware { - readonly name = CONTROLLER_NAME; - - readonly #getAssetsState: () => AssetsControllerState; - - constructor(options: DetectionMiddlewareOptions) { - this.#getAssetsState = options.getAssetsState; - } - - getName(): string { - return this.name; - } - - /** - * Get the middleware that builds detectedAssets for metadata and price fetching. - * - * This middleware: - * 1. Includes assets from response.assetsBalance that are absent from both - * state.assetsBalance and state.assetsInfo (brand-new assets only) - * 2. Always includes each account's custom assets from state - * 3. Fills response.detectedAssets with the resulting asset IDs per account - * 4. Queues detected assets that lack a price in state on - * request.assetsForPriceUpdate so PriceDataSource fetches them in the same - * pipeline pass (including the background RPC detection path) - * - * @returns The middleware function for the assets pipeline. - */ - get assetsMiddleware(): Middleware { - return forDataTypes(['balance'], async (ctx, next) => { - const { request, response } = ctx; - - // Get state for custom assets, existing balances, and existing metadata - const state = this.#getAssetsState(); - const { - customAssets: stateCustomAssets, - assetsBalance: stateAssetsBalance, - assetsInfo: stateAssetsInfo, - assetsPrice: stateAssetsPrice, - } = state; - - const detectedAssets: Record = {}; - - // 1. From balance response: only include assets that are genuinely new — - // not already present in state.assetsBalance or state.assetsInfo. - if (response.assetsBalance) { - for (const [accountId, accountBalances] of Object.entries( - response.assetsBalance, - )) { - const detected: Caip19AssetId[] = []; - - const stateAccountBalances = stateAssetsBalance[accountId] ?? {}; - - for (const assetId of Object.keys(accountBalances)) { - const caipAssetId = assetId as Caip19AssetId; - // Skip if already tracked in state balances or already has metadata - if ( - stateAccountBalances[caipAssetId] !== undefined || - stateAssetsInfo[caipAssetId] !== undefined - ) { - continue; - } - detected.push(caipAssetId); - } - - // Merge custom assets for this account, applying the same filter: - // skip if already in state balance or already has metadata. - const customForAccount = stateCustomAssets?.[accountId] ?? []; - for (const assetId of customForAccount) { - if (detected.includes(assetId)) { - continue; - } - if ( - stateAccountBalances[assetId] !== undefined || - stateAssetsInfo[assetId] !== undefined - ) { - continue; - } - detected.push(assetId); - } - - if (detected.length > 0) { - detectedAssets[accountId] = detected; - } - } - } - - // 2. Accounts in request that weren't in balance response: include their - // custom assets that are not yet in state. - for (const { account } of request.accountsWithSupportedChains) { - const accountId = account.id; - if (detectedAssets[accountId]) { - continue; - } - const stateAccountBalances = stateAssetsBalance[accountId] ?? {}; - const customForAccount = stateCustomAssets?.[accountId] ?? []; - const newCustomAssets = customForAccount.filter((assetId) => { - const inBalance = stateAccountBalances[assetId] !== undefined; - const inInfo = stateAssetsInfo[assetId] !== undefined; - return !inBalance && !inInfo; - }); - if (newCustomAssets.length > 0) { - detectedAssets[accountId] = newCustomAssets; - } - } - - if (Object.keys(detectedAssets).length > 0) { - response.detectedAssets = detectedAssets; - } - - // Queue prices for: - // 1) newly detected assets missing a price, and - // 2) assets in this balance response that already exist in state but still - // lack a price (e.g. natives seeded before the first price poll). - // PriceDataSource filters non-priceable IDs before calling the API. - const prices = stateAssetsPrice as Record; - const missingPriceAssets = new Set(); - - const maybeQueue = (assetId: Caip19AssetId): void => { - const normalizedAssetId = normalizeAssetId(assetId); - if ( - prices[normalizedAssetId] === undefined && - prices[assetId] === undefined - ) { - missingPriceAssets.add(normalizedAssetId); - } - }; - - for (const accountAssetIds of Object.values(detectedAssets)) { - for (const assetId of accountAssetIds) { - maybeQueue(assetId); - } - } - - if (response.assetsBalance) { - for (const accountBalances of Object.values(response.assetsBalance)) { - for (const assetId of Object.keys(accountBalances)) { - maybeQueue(assetId as Caip19AssetId); - } - } - } - - if (missingPriceAssets.size > 0) { - request.assetsForPriceUpdate = [ - ...(request.assetsForPriceUpdate ?? []), - ...missingPriceAssets, - ]; - } - - return next(ctx); - }); - } -} diff --git a/packages/assets-controller/src/middlewares/ParallelMiddleware.ts b/packages/assets-controller/src/middlewares/ParallelMiddleware.ts index b35a4d5e0e8..d53f4833cd4 100644 --- a/packages/assets-controller/src/middlewares/ParallelMiddleware.ts +++ b/packages/assets-controller/src/middlewares/ParallelMiddleware.ts @@ -53,12 +53,6 @@ export function mergeDataResponses(responses: DataResponse[]): DataResponse { ...response.errors, }; } - if (response.detectedAssets) { - merged.detectedAssets = { - ...(merged.detectedAssets ?? {}), - ...response.detectedAssets, - }; - } if (response.updateMode === 'full') { merged.updateMode = 'full'; } else if ( diff --git a/packages/assets-controller/src/middlewares/index.ts b/packages/assets-controller/src/middlewares/index.ts index aa509e625ff..4a1d4fd281a 100644 --- a/packages/assets-controller/src/middlewares/index.ts +++ b/packages/assets-controller/src/middlewares/index.ts @@ -1,7 +1,5 @@ export { CustomAssetGraduationMiddleware } from './CustomAssetGraduationMiddleware.js'; export type { CustomAssetGraduationMiddlewareOptions } from './CustomAssetGraduationMiddleware.js'; -export { DetectionMiddleware } from './DetectionMiddleware.js'; -export type { DetectionMiddlewareOptions } from './DetectionMiddleware.js'; export { RpcFallbackMiddleware } from './RpcFallbackMiddleware.js'; export type { RpcFallbackMiddlewareOptions } from './RpcFallbackMiddleware.js'; export { diff --git a/packages/assets-controller/src/pipeline/__snapshots__/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts.snap b/packages/assets-controller/src/pipeline/__snapshots__/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts.snap index d2d211c5112..a81662b1a86 100644 --- a/packages/assets-controller/src/pipeline/__snapshots__/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts.snap +++ b/packages/assets-controller/src/pipeline/__snapshots__/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts.snap @@ -1038,26 +1038,6 @@ exports[`assets pipeline (Accounts API v6): BNB Chain spam token (CDOGE) > gener "usdPrice": 716.8635016621672, }, }, - "detectedAssets": { - "b1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d": [ - "eip155:56/slip44:714", - "eip155:56/erc20:0x003d87d02A2A01E9E8a20f507C83E15DD83A33d1", - "eip155:56/erc20:0x55d398326f99059fF775485246999027B3197955", - "eip155:56/erc20:0x8899eC96ed8C96b5C86C23c3f069c3dEf75b6d97", - "eip155:56/erc20:0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d", - "eip155:56/erc20:0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56", - "eip155:56/erc20:0x0cCD575bf9378c06f6DCa82f8122F570769F00C2", - "eip155:56/erc20:0x2B72867c32CF673F7b02d208B26889fEd353B1f8", - "eip155:56/erc20:0x4f0ed527e8A95ecAA132Af214dFd41F30b361600", - "eip155:56/erc20:0x69b14e8D3CEBfDD8196Bfe530954A0C226E5008E", - "eip155:56/erc20:0x77087aB5Df23cFB52449A188e80e9096201c2097", - "eip155:56/erc20:0x94a8b4EE5CD64C79D0Ee816f467EA73009f51aA0", - "eip155:56/erc20:0xBb7D61D2511fD2e63F02178ca9B663458Af9FC63", - "eip155:56/erc20:0xBbcF57177D8752B21d080bf30a06CE20aD6333F8", - "eip155:56/erc20:0xDdC0dBd7dC799ae53A98a60b54999cb6eBb3Abf0", - "eip155:56/erc20:0xe57F73EB27Da9D17f90C994744D842e95700C100", - ], - }, "updateMode": "full", } `; diff --git a/packages/assets-controller/src/pipeline/__snapshots__/buildWsUpdateSources.price-updates.integration.test.ts.snap b/packages/assets-controller/src/pipeline/__snapshots__/buildWsUpdateSources.price-updates.integration.test.ts.snap index ddd920cbd50..50e4453ffce 100644 --- a/packages/assets-controller/src/pipeline/__snapshots__/buildWsUpdateSources.price-updates.integration.test.ts.snap +++ b/packages/assets-controller/src/pipeline/__snapshots__/buildWsUpdateSources.price-updates.integration.test.ts.snap @@ -130,12 +130,6 @@ exports[`websocket update pipeline: prices for surfaced holdings > generates sna "usdPrice": 2688.8502994319642, }, }, - "detectedAssets": { - "c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7f": [ - "eip155:1/slip44:60", - "eip155:1/erc20:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", - ], - }, "updateMode": "merge", } `; diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts index 5d4c22d59f0..266579de07e 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.accounts-api-v6.integration.test.ts @@ -3,7 +3,6 @@ import { cleanAll } from 'nock'; import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -30,7 +29,6 @@ import { PriceDataSource } from '../data-sources/PriceDataSource.js'; import { RpcDataSource } from '../data-sources/RpcDataSource.js'; import { TokenDataSource } from '../data-sources/TokenDataSource.js'; import { CustomAssetGraduationMiddleware } from '../middlewares/CustomAssetGraduationMiddleware.js'; -import { DetectionMiddleware } from '../middlewares/DetectionMiddleware.js'; import { RpcFallbackMiddleware } from '../middlewares/RpcFallbackMiddleware.js'; import type { AccountId, @@ -66,7 +64,6 @@ import { buildFastFetchSources, executeAssetsPipeline } from './index.js'; const BALANCES = responseBalancesSurface(BSC_SPAM_ACCOUNT_ID); const METADATA = RESPONSE_METADATA_SURFACE; const PRICES = RESPONSE_PRICES_SURFACE; -const DETECTED_ASSETS = RESPONSE_DETECTED_ASSETS_SURFACE; async function runPipeline( state: AssetsControllerState, @@ -183,9 +180,6 @@ async function runPipeline( getAssetsState: (): AssetsControllerState => state, isBalanceV6Enabled: (): boolean => true, }), - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, @@ -237,30 +231,40 @@ describe('assets pipeline (Accounts API v6): BNB Chain spam token (CDOGE)', () = cleanAll(); }); - describe.each(WALLET_PASSES)('$pass', ({ run }) => { + describe.each(WALLET_PASSES)('$pass', ({ pass, run }) => { let response: DataResponse; beforeAll(async () => { response = await run(); }); - it.each([BALANCES, METADATA, DETECTED_ASSETS])( + it.each([BALANCES, METADATA])( '$surface - filter out the spam token', ({ lookUp }) => { expect(lookUp(response, CDOGE_ASSET_ID_LOWERCASE)).toBeUndefined(); }, ); - it.each([BALANCES, METADATA])( - '$surface - keeps the native BNB asset despite low occurrences', - ({ lookUp }) => { - expect(lookUp(response, BNB_ASSET_ID)).toBeDefined(); - }, - ); + if (pass.startsWith('second')) { + it('balances - keeps the stored native BNB balance', () => { + expect(BALANCES.lookUp(response, BNB_ASSET_ID)).toBeDefined(); + }); + + it('metadata - does not refetch the stored native BNB record', () => { + expect(METADATA.lookUp(response, BNB_ASSET_ID)).toBeUndefined(); + }); + } else { + it.each([BALANCES, METADATA])( + '$surface - keeps the native BNB asset despite low occurrences', + ({ lookUp }) => { + expect(lookUp(response, BNB_ASSET_ID)).toBeDefined(); + }, + ); + } // Unlike the v5 suite, this is not `it.failing`! // On v6 the backend omits the Malicious row, so the token is - // never in `assetsBalance` or `detectedAssets` at any pipeline stage and + // never in `assetsBalance` at any pipeline stage and // no price is ever fetched for it. // // Note we still need the unlock cleanup (`cleanSpamAssets`) to @@ -307,7 +311,7 @@ describe('assets pipeline (Accounts API v6): BNB Chain spam token (CDOGE) import return { source, requests }; }; - it.each([BALANCES, METADATA, DETECTED_ASSETS])( + it.each([BALANCES, METADATA])( '$surface - keeps the imported token the backend flags as Malicious', async ({ lookUp }) => { const { response, v6Balances } = await runPipeline( diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.integration.test.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.integration.test.ts index 1876bf0f58a..6ad581c7555 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.integration.test.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.bsc-spam-token-filtering.integration.test.ts @@ -3,7 +3,6 @@ import { cleanAll } from 'nock'; import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -28,7 +27,6 @@ import { PriceDataSource } from '../data-sources/PriceDataSource.js'; import { RpcDataSource } from '../data-sources/RpcDataSource.js'; import { TokenDataSource } from '../data-sources/TokenDataSource.js'; import { CustomAssetGraduationMiddleware } from '../middlewares/CustomAssetGraduationMiddleware.js'; -import { DetectionMiddleware } from '../middlewares/DetectionMiddleware.js'; import { RpcFallbackMiddleware } from '../middlewares/RpcFallbackMiddleware.js'; import type { AccountId, @@ -55,7 +53,6 @@ import { buildFastFetchSources, executeAssetsPipeline } from './index.js'; const BALANCES = responseBalancesSurface(BSC_SPAM_ACCOUNT_ID); const METADATA = RESPONSE_METADATA_SURFACE; const PRICES = RESPONSE_PRICES_SURFACE; -const DETECTED_ASSETS = RESPONSE_DETECTED_ASSETS_SURFACE; async function runPipeline( state: AssetsControllerState, @@ -168,9 +165,6 @@ async function runPipeline( rpcDataSource: rpcOverride ?? rpcDataSource, getAssetsState: (): AssetsControllerState => state, }), - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, @@ -217,26 +211,36 @@ describe('assets pipeline: BNB Chain spam token (CDOGE)', () => { cleanAll(); }); - describe.each(WALLET_PASSES)('$pass', ({ run }) => { + describe.each(WALLET_PASSES)('$pass', ({ pass, run }) => { let response: DataResponse; beforeAll(async () => { response = await run(); }); - it.each([BALANCES, METADATA, DETECTED_ASSETS])( + it.each([BALANCES, METADATA])( '$surface - filter out the spam token', ({ lookUp }) => { expect(lookUp(response, CDOGE_ASSET_ID_LOWERCASE)).toBeUndefined(); }, ); - it.each([BALANCES, METADATA])( - '$surface - keeps the native BNB asset despite low occurrences', - ({ lookUp }) => { - expect(lookUp(response, BNB_ASSET_ID)).toBeDefined(); - }, - ); + if (pass.startsWith('second')) { + it('balances - keeps the stored native BNB balance', () => { + expect(BALANCES.lookUp(response, BNB_ASSET_ID)).toBeDefined(); + }); + + it('metadata - does not refetch the stored native BNB record', () => { + expect(METADATA.lookUp(response, BNB_ASSET_ID)).toBeUndefined(); + }); + } else { + it.each([BALANCES, METADATA])( + '$surface - keeps the native BNB asset despite low occurrences', + ({ lookUp }) => { + expect(lookUp(response, BNB_ASSET_ID)).toBeDefined(); + }, + ); + } // Legitimate failing test, our middleware stack does not filter out spam // asset prices! This does eventually get cleaned up during unlock cleanup, @@ -280,7 +284,7 @@ describe('assets pipeline (without graduation middleware only on v6 integration) return { source, requests }; }; - it.each([BALANCES, METADATA, DETECTED_ASSETS])( + it.each([BALANCES, METADATA])( '$surface - keeps the imported token despite a positive API balance and low occurrences', async ({ lookUp }) => { const response = await runPipeline( diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts index a3ffc06a296..75fd2d4a5d5 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts @@ -18,7 +18,6 @@ function buildSources(): FastFetchSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -28,14 +27,13 @@ describe('buildFastFetchSources', () => { it.each([ { title: - 'orders the lane balances → graduation → rpc fallback → detection → enrichment', + 'orders the lane balances → graduation → rpc fallback → enrichment', isBasicFunctionality: true, includeCustomAssetGraduation: true, expected: [ 'AccountsApiDataSource', 'CustomAssetGraduationMiddleware', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, @@ -47,7 +45,6 @@ describe('buildFastFetchSources', () => { expected: [ 'AccountsApiDataSource', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts index dee8259266a..7a23c81de8b 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts @@ -12,31 +12,28 @@ export type FastFetchSources = { accountsApiDataSource: AssetsDataSource; customAssetGraduationMiddleware: AssetsDataSource; rpcFallbackMiddleware: AssetsDataSource; - detectionMiddleware: AssetsDataSource; tokenDataSource: AssetsDataSource; priceDataSource: AssetsDataSource; }; /** - * Compose the fast fetch lane: Accounts API balances → custom-asset graduation → - * RPC fallback → detection → token metadata and prices in parallel. + * Compose the fast fetch lane: balances in parallel → custom-asset graduation → + * RPC fallback → token metadata and prices in parallel. * * Snap and RPC balance sources are deliberately absent — the controller runs * those in a background lane because of their latency. * - * Ordering carries two invariants: - * - Graduation runs BEFORE the RPC fallback so it only ever sees Accounts - * API / websocket balances. RPC intentionally carries custom assets and must - * never trigger graduation. - * - Detection runs before token and price enrichment, which both read - * `response.detectedAssets`. + * Ordering carries one invariant: graduation runs BEFORE the RPC fallback so + * it only ever sees Accounts API / websocket balances. RPC intentionally + * carries custom assets and must never trigger graduation. Token and price + * enrichment each choose their own asset sets from the request and controller + * state. * * @param sources - The sources to place into the lane. * @param options - Lane options. * @param options.isBasicFunctionality - When false, the lane is empty. No - * balance source runs here, so detection would have nothing to scan and no - * token or price source to feed. The RPC background lane fetches balances and - * runs detection after them. + * balance source runs here, so there is no token or price source to feed. + * The RPC background lane fetches balances and then runs token and price. * @param options.includeCustomAssetGraduation - `true` on the Accounts API v5 * lane. The v6 lane never graduates custom assets: it sends the pins to the * endpoint as `includeAssetIds`, and a chain that comes back without all of @@ -54,7 +51,6 @@ export function buildFastFetchSources( accountsApiDataSource, customAssetGraduationMiddleware, rpcFallbackMiddleware, - detectionMiddleware, tokenDataSource, priceDataSource, } = sources; @@ -69,7 +65,6 @@ export function buildFastFetchSources( ? [customAssetGraduationMiddleware] : []), rpcFallbackMiddleware, - detectionMiddleware, createParallelMiddleware([tokenDataSource, priceDataSource]), ]; } diff --git a/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts b/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts index 5f82b1224a5..824a35faf9e 100644 --- a/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts @@ -17,7 +17,6 @@ function buildSources(): UpdateSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -26,53 +25,43 @@ function buildSources(): UpdateSources { describe('buildUpdateSources', () => { it.each([ { - title: - 'v5 AccountsApi lane: graduation → RPC fallback → detection → enrichment', + title: 'v5 AccountsApi lane: graduation → RPC fallback → enrichment', includeCustomAssetGraduation: true, includeRpcFallback: true, isBasicFunctionality: true, expected: [ 'CustomAssetGraduationMiddleware', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, { - title: 'v5 AccountsApi lane: graduation → RPC fallback → detection', + title: 'v5 AccountsApi lane: graduation → RPC fallback', includeCustomAssetGraduation: true, includeRpcFallback: true, isBasicFunctionality: false, - expected: [ - 'CustomAssetGraduationMiddleware', - 'RpcFallbackMiddleware', - 'DetectionMiddleware', - ], + expected: ['CustomAssetGraduationMiddleware', 'RpcFallbackMiddleware'], }, { - title: 'v5 Snap/RPC lane: detection → enrichment', + title: 'v5 Snap/RPC lane: enrichment', includeCustomAssetGraduation: false, includeRpcFallback: false, isBasicFunctionality: true, - expected: ['DetectionMiddleware', 'ParallelMiddleware'], + expected: ['ParallelMiddleware'], }, { - title: 'v6 lane: RPC fallback → detection → enrichment', + title: 'v6 lane: RPC fallback → enrichment', includeCustomAssetGraduation: false, includeRpcFallback: true, isBasicFunctionality: true, - expected: [ - 'RpcFallbackMiddleware', - 'DetectionMiddleware', - 'ParallelMiddleware', - ], + expected: ['RpcFallbackMiddleware', 'ParallelMiddleware'], }, { - title: 'v6 lane: detection only', + title: 'v6 lane: no enrichment when basic functionality is off', includeCustomAssetGraduation: false, includeRpcFallback: false, isBasicFunctionality: false, - expected: ['DetectionMiddleware'], + expected: [], }, ])( '$title', diff --git a/packages/assets-controller/src/pipeline/buildUpdateSources.ts b/packages/assets-controller/src/pipeline/buildUpdateSources.ts index c7ef1d9cf3d..9df1abe6439 100644 --- a/packages/assets-controller/src/pipeline/buildUpdateSources.ts +++ b/packages/assets-controller/src/pipeline/buildUpdateSources.ts @@ -5,7 +5,6 @@ import type { AssetsDataSource } from '../types.js'; export type UpdateSources = { customAssetGraduationMiddleware: AssetsDataSource; rpcFallbackMiddleware: AssetsDataSource; - detectionMiddleware: AssetsDataSource; tokenDataSource: AssetsDataSource; priceDataSource: AssetsDataSource; }; @@ -31,7 +30,6 @@ export function buildUpdateSources( const { customAssetGraduationMiddleware, rpcFallbackMiddleware, - detectionMiddleware, tokenDataSource, priceDataSource, } = sources; @@ -41,7 +39,6 @@ export function buildUpdateSources( ? [customAssetGraduationMiddleware] : []), ...(options.includeRpcFallback ? [rpcFallbackMiddleware] : []), - detectionMiddleware, ...(options.isBasicFunctionality ? [createParallelMiddleware([tokenDataSource, priceDataSource])] : []), diff --git a/packages/assets-controller/src/pipeline/buildWsUpdateSources.price-updates.integration.test.ts b/packages/assets-controller/src/pipeline/buildWsUpdateSources.price-updates.integration.test.ts index 5387ba205ca..855a6b3f940 100644 --- a/packages/assets-controller/src/pipeline/buildWsUpdateSources.price-updates.integration.test.ts +++ b/packages/assets-controller/src/pipeline/buildWsUpdateSources.price-updates.integration.test.ts @@ -3,7 +3,6 @@ import { cleanAll } from 'nock'; import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -41,7 +40,6 @@ import { AccountActivityDataSource } from '../data-sources/AccountActivityDataSo import { PriceDataSource } from '../data-sources/PriceDataSource.js'; import { TokenDataSource } from '../data-sources/TokenDataSource.js'; import { CustomAssetGraduationMiddleware } from '../middlewares/CustomAssetGraduationMiddleware.js'; -import { DetectionMiddleware } from '../middlewares/DetectionMiddleware.js'; import type { AccountId, AssetsControllerState, @@ -68,7 +66,6 @@ type UpdateLane = 'v5' | 'v6'; const BALANCES = responseBalancesSurface(WS_ACCOUNT_ID); const METADATA = RESPONSE_METADATA_SURFACE; const PRICES = RESPONSE_PRICES_SURFACE; -const DETECTED_ASSETS = RESPONSE_DETECTED_ASSETS_SURFACE; /** A completed websocket update pass. */ type WsUpdatePassResult = { @@ -180,9 +177,6 @@ async function runWsUpdatePass({ getAssetsState: (): AssetsControllerState => state, }), rpcFallbackMiddleware, - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, @@ -249,7 +243,7 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { }); }); - it.each([BALANCES, METADATA, PRICES, DETECTED_ASSETS])( + it.each([BALANCES, METADATA, PRICES])( '$surface - present for the surfaced holdings after one pass', ({ lookUp }) => { expect(lookUp(result.response, ETH_ASSET_ID)).toBeDefined(); @@ -294,15 +288,6 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { }); }); - it('queues both surfaced holdings for a price update', () => { - const queued = - result.request.assetsForPriceUpdate?.map((id) => id.toLowerCase()) ?? - []; - expect(queued).toStrictEqual( - expect.arrayContaining([ETH_ASSET_ID, USDC_ASSET_ID_LOWERCASE]), - ); - }); - it('invokes the Price API for both surfaced holdings in the same pass', () => { expect(askedAbout(result.mocks.priceAPI.priceBatches)).toStrictEqual( new Set([ETH_ASSET_ID, USDC_ASSET_ID_LOWERCASE]), @@ -357,17 +342,7 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { }); }); - it('does not re-detect the holding', () => { - expect( - DETECTED_ASSETS.lookUp(result.response, ETH_ASSET_ID), - ).toBeUndefined(); - }); - it('prices the held-but-unpriced asset in the same pass', () => { - const queued = - result.request.assetsForPriceUpdate?.map((id) => id.toLowerCase()) ?? - []; - expect(queued).toStrictEqual([ETH_ASSET_ID]); expect(askedAbout(result.mocks.priceAPI.priceBatches)).toStrictEqual( new Set([ETH_ASSET_ID]), ); @@ -395,20 +370,13 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { }); }); - it('does not re-detect the token', () => { - expect( - DETECTED_ASSETS.lookUp(result.response, USDC_ASSET_ID_LOWERCASE), - ).toBeUndefined(); - }); - it('does not re-price the already-priced token', () => { - expect(result.request.assetsForPriceUpdate ?? []).toStrictEqual([]); expect(result.mocks.priceAPI.priceBatches).toStrictEqual([]); }); - it('does not refetch metadata for the enriched token, only the native asset', () => { + it('does not refetch metadata for the enriched token', () => { expect(askedAbout(result.mocks.tokenAPI.assetBatches)).toStrictEqual( - new Set([ETH_ASSET_ID]), + new Set(), ); }); diff --git a/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts b/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts index 4bff77d3cdb..0d0533c0a65 100644 --- a/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts @@ -24,7 +24,6 @@ function buildSources(): WsUpdateSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubTokenSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -33,39 +32,36 @@ function buildSources(): WsUpdateSources { describe('buildWsUpdateSources', () => { it.each([ { - title: 'v5 lane: graduation → occurrence filter → detection → enrichment', + title: 'v5 lane: graduation → occurrence filter → enrichment', includeCustomAssetGraduation: true, isBasicFunctionality: true, expected: [ 'CustomAssetGraduationMiddleware', 'OccurrenceFloorFilter', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, { - title: 'v5 lane: runs only the graduation and detection', + title: 'v5 lane: runs only graduation', includeCustomAssetGraduation: true, isBasicFunctionality: false, - expected: ['CustomAssetGraduationMiddleware', 'DetectionMiddleware'], + expected: ['CustomAssetGraduationMiddleware'], }, { - title: - 'v6 lane: occurrence filter → RPC fallback → detection → enrichment', + title: 'v6 lane: occurrence filter → RPC fallback → enrichment', includeCustomAssetGraduation: false, isBasicFunctionality: true, expected: [ 'OccurrenceFloorFilter', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, { - title: 'v6 lane: runs only the detection', + title: 'v6 lane: runs nothing when basic functionality is off', includeCustomAssetGraduation: false, isBasicFunctionality: false, - expected: ['DetectionMiddleware'], + expected: [], }, ])( '$title', diff --git a/packages/assets-controller/src/pipeline/buildWsUpdateSources.ts b/packages/assets-controller/src/pipeline/buildWsUpdateSources.ts index 4e620ff2c2e..5fc6f17e447 100644 --- a/packages/assets-controller/src/pipeline/buildWsUpdateSources.ts +++ b/packages/assets-controller/src/pipeline/buildWsUpdateSources.ts @@ -5,7 +5,6 @@ import type { AssetsDataSource, Middleware } from '../types.js'; export type WsUpdateSources = { customAssetGraduationMiddleware: AssetsDataSource; rpcFallbackMiddleware: AssetsDataSource; - detectionMiddleware: AssetsDataSource; /** Also supplies the occurrence-filter middleware used for websocket airdrops. */ tokenDataSource: AssetsDataSource & { readonly occurrenceFilterMiddleware: Middleware; @@ -34,7 +33,6 @@ export function buildWsUpdateSources( const { customAssetGraduationMiddleware, rpcFallbackMiddleware, - detectionMiddleware, tokenDataSource, priceDataSource, } = sources; @@ -52,7 +50,6 @@ export function buildWsUpdateSources( ...(!options.includeCustomAssetGraduation && options.isBasicFunctionality ? [rpcFallbackMiddleware] : []), - detectionMiddleware, ...(options.isBasicFunctionality ? [createParallelMiddleware([tokenDataSource, priceDataSource])] : []), diff --git a/packages/assets-controller/src/types.ts b/packages/assets-controller/src/types.ts index 37e7663eb37..512bda93a71 100644 --- a/packages/assets-controller/src/types.ts +++ b/packages/assets-controller/src/types.ts @@ -370,8 +370,6 @@ export type DataRequest = { bypassServerCache?: boolean; /** Hint for polling interval (ms) - used by data sources that implement polling */ updateInterval?: number; - /** Specific CAIP-19 asset IDs for price update */ - assetsForPriceUpdate?: Caip19AssetId[]; }; /** @@ -386,8 +384,6 @@ export type DataResponse = { assetsBalance?: Record>; /** Errors encountered, keyed by chain ID (chain-axis fallback + telemetry) */ errors?: Record; - /** Detected assets (assets that do not have metadata) */ - detectedAssets?: Record; /** * How to apply this response to state. See {@link AssetsUpdateMode}. * Defaults to `'merge'` if omitted. @@ -667,11 +663,3 @@ export type MetadataChangeEvent = { assetId: Caip19AssetId; changes: Partial; }; - -/** - * Event emitted when assets without metadata are detected - */ -export type AssetsDetectedEvent = { - accountId: AccountId; - assetIds: Caip19AssetId[]; -}; diff --git a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts new file mode 100644 index 00000000000..dfb66c95f53 --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts @@ -0,0 +1,417 @@ +import type { InternalAccount } from '@metamask/keyring-internal-api'; +import { describe, expect, it } from 'vitest'; + +import type { + AssetBalance, + AssetsControllerState, + Caip19AssetId, + ChainId, + DataRequest, + FungibleAssetMetadata, +} from '../types.js'; +import { getAssetIdsForMetadataFetch } from './getAssetIdsForMetadataFetch.js'; + +const CHAIN_MAINNET = 'eip155:1' as ChainId; +const CHAIN_POLYGON = 'eip155:137' as ChainId; +const ACCOUNT_A = 'account-a'; +const ACCOUNT_B = 'account-b'; + +const NATIVE_MAINNET = 'eip155:1/slip44:60' as Caip19AssetId; +const TOKEN_MAINNET = + 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; +const TOKEN_POLYGON = + 'eip155:137/erc20:0x3c499c542cef5e3811e1192ce70d8cc03d5c3359' as Caip19AssetId; +const STAKING_MAINNET = + 'eip155:1/erc20:0x4fef9d741011476750a243ac70b9789a63dd47df' as Caip19AssetId; + +const ZERO_BALANCE: AssetBalance = { amount: '0' }; + +const COMPLETE_METADATA: FungibleAssetMetadata = { + type: 'erc20', + symbol: 'TOK', + name: 'Token', + decimals: 18, + image: 'https://example.com/token.png', + aggregators: ['metamask'], + occurrences: 5, +}; + +const METADATA_WITH_IMAGE: FungibleAssetMetadata = { + type: 'erc20', + symbol: 'TOK', + name: 'Token', + decimals: 18, + image: 'https://example.com/token.png', +}; + +const METADATA_WITHOUT_IMAGE: FungibleAssetMetadata = { + type: 'erc20', + symbol: 'TOK', + name: 'Token', + decimals: 18, +}; + +function createAccount(id: string): InternalAccount { + return { + id, + address: '0x1234567890123456789012345678901234567890', + options: {}, + methods: [], + type: 'eip155:eoa', + scopes: ['eip155:0'], + metadata: { + name: 'Test Account', + keyring: { type: 'HD Key Tree' }, + importTime: Date.now(), + lastSelected: Date.now(), + }, + }; +} + +function createRequest( + overrides: { + accountIds?: string[]; + chainIds?: ChainId[]; + } = {}, +): Pick { + const chainIds = overrides.chainIds ?? [CHAIN_MAINNET]; + const accountIds = overrides.accountIds ?? [ACCOUNT_A]; + return { + chainIds, + accountsWithSupportedChains: accountIds.map((id) => ({ + account: createAccount(id), + supportedChains: chainIds, + })), + }; +} + +function emptyState( + overrides: Partial< + Pick< + AssetsControllerState, + 'assetsBalance' | 'assetsInfo' | 'assetPreferences' + > + > = {}, +): Pick< + AssetsControllerState, + 'assetsBalance' | 'assetsInfo' | 'assetPreferences' +> { + return { + assetsBalance: {}, + assetsInfo: {}, + assetPreferences: {}, + ...overrides, + }; +} + +function sortedIds(ids: Set): string[] { + return [...ids].sort(); +} + +describe('getAssetIdsForMetadataFetch', () => { + it('includes state balances without metadata as heals', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }), + request: createRequest(), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + TOKEN_MAINNET.toLowerCase(), + ]); + }); + + it('includes new response balances without marking them as heals', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState(), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('marks response balances already in state as heals', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + TOKEN_MAINNET.toLowerCase(), + ]); + }); + + it('marks response balances with known state metadata as heals', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsInfo: { [TOKEN_MAINNET]: METADATA_WITHOUT_IMAGE }, + }), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + TOKEN_MAINNET.toLowerCase(), + ]); + }); + + it('excludes assets whose state metadata has every client-used field', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + assetsInfo: { [TOKEN_MAINNET]: COMPLETE_METADATA }, + }), + request: createRequest(), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('excludes assets whose response metadata has every client-used field', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState(), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + assetsInfo: { [TOKEN_MAINNET]: COMPLETE_METADATA }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it.each([ + ['image', METADATA_WITH_IMAGE], + ['aggregators', { ...COMPLETE_METADATA, aggregators: undefined }], + ['occurrences', { ...COMPLETE_METADATA, occurrences: undefined }], + ['symbol', { ...COMPLETE_METADATA, symbol: '' }], + ['name', { ...COMPLETE_METADATA, name: '' }], + ['decimals', { ...COMPLETE_METADATA, decimals: Number.NaN }], + ] as const)( + 'includes assets whose metadata is missing %s', + (_missingField, metadata) => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + assetsInfo: { + [TOKEN_MAINNET]: metadata as unknown as FungibleAssetMetadata, + }, + }), + request: createRequest(), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + TOKEN_MAINNET.toLowerCase(), + ]); + }, + ); + + it('excludes hidden assets', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_MAINNET]: ZERO_BALANCE, + }, + }, + assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, + }), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + NATIVE_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + NATIVE_MAINNET.toLowerCase(), + ]); + }); + + it('excludes staking contract asset IDs', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [STAKING_MAINNET]: ZERO_BALANCE }, + }, + }), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [STAKING_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('filters state balances by account ID', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + [ACCOUNT_B]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }), + request: createRequest({ accountIds: [ACCOUNT_A] }), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + NATIVE_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + NATIVE_MAINNET.toLowerCase(), + ]); + }); + + it('filters state balances by chain ID', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + }, + }, + }), + request: createRequest({ chainIds: [CHAIN_MAINNET] }), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + NATIVE_MAINNET, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([ + NATIVE_MAINNET.toLowerCase(), + ]); + }); + + it('does not filter response balances by account or chain ID', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState(), + request: createRequest({ + accountIds: [ACCOUNT_A], + chainIds: [CHAIN_MAINNET], + }), + response: { + assetsBalance: { + [ACCOUNT_B]: { [TOKEN_POLYGON]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + TOKEN_POLYGON, + ]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('skips malformed state balance asset IDs when chain filtering', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + 'not-a-valid-caip19': ZERO_BALANCE, + [NATIVE_MAINNET]: ZERO_BALANCE, + } as Record, + }, + }), + request: createRequest({ chainIds: [CHAIN_MAINNET] }), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([ + NATIVE_MAINNET, + ]); + }); + + it('returns empty sets when no candidates need metadata', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState(), + request: createRequest(), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('includes chain filter when request has both mainnet and polygon', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + }, + }, + }), + request: createRequest({ + chainIds: [CHAIN_MAINNET, CHAIN_POLYGON], + }), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual( + [NATIVE_MAINNET, TOKEN_POLYGON].sort(), + ); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual( + [NATIVE_MAINNET.toLowerCase(), TOKEN_POLYGON.toLowerCase()].sort(), + ); + }); +}); diff --git a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts new file mode 100644 index 00000000000..907ddffafc4 --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts @@ -0,0 +1,212 @@ +import { parseCaipAssetType } from '@metamask/utils'; + +import { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js'; +import type { + AssetMetadata, + AssetsControllerState, + Caip19AssetId, + DataRequest, + DataResponse, +} from '../types.js'; + +export type GetAssetIdsForMetadataFetchParams = { + /** Current AssetsController state slices used for candidate collection. */ + state: Pick< + AssetsControllerState, + 'assetsBalance' | 'assetsInfo' | 'assetPreferences' + >; + /** Account and chain scope for the fetch. */ + request: Pick; + /** Current pipeline response balances and metadata. */ + response: Pick; +}; + +export type GetAssetIdsForMetadataFetchResult = { + /** Asset IDs that still need metadata fetched. */ + assetIdsNeedingMetadata: Set; + /** + * Lower-case asset IDs that must skip spam filtering (state balance heals + * and response balances already known in state). + */ + balanceHealAssetIds: Set; +}; + +/** + * Metadata to fetch for this request. + * + * State balances whose metadata is missing a field that controller logic or a + * client reads are heals: they are enriched and not spam-filtered. Those + * fields are `symbol`, `name`, `decimals`, `image`, `aggregators`, and + * `occurrences`. Response balances are included too; a response balance + * already present in state is a heal, and one that is new to state is + * spam-filtered. Hidden assets are excluded. + * + * @param params - State, request scope, and response balances/metadata. + * @param params.state - Controller state slices used for candidate collection. + * @param params.request - Account and chain scope for the fetch. + * @param params.response - Current pipeline response balances and metadata. + * @returns Asset IDs to fetch, and the subset that must skip spam filtering. + */ +export function getAssetIdsForMetadataFetch({ + state, + request, + response, +}: GetAssetIdsForMetadataFetchParams): GetAssetIdsForMetadataFetchResult { + const stateMetadata = state.assetsInfo ?? {}; + const accountFilter = buildAccountFilter(request); + const chainFilter = buildChainFilter(request); + const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); + const knownMetadataIds = new Set( + Object.keys(stateMetadata).map((assetId) => assetId.toLowerCase()), + ); + const assetIdsNeedingMetadata = new Set(); + const balanceHealAssetIds = new Set(); + const stateBalanceIds = new Set(); + + const needsMetadata = (assetId: string): boolean => { + if (hiddenAssetIds.has(assetId.toLowerCase())) { + return false; + } + if ( + hasMetadataUsedByClients(response.assetsInfo?.[assetId as Caip19AssetId]) + ) { + return false; + } + if (hasMetadataUsedByClients(stateMetadata[assetId as Caip19AssetId])) { + return false; + } + return !isStakingContractAssetId(assetId); + }; + + const inRequestChains = (assetId: string): boolean => { + if (!chainFilter) { + return true; + } + try { + const { chainId } = parseCaipAssetType(assetId as Caip19AssetId); + return chainFilter.has(chainId); + } catch { + return false; + } + }; + + for (const [accountId, accountBalances] of Object.entries( + state.assetsBalance ?? {}, + )) { + if (accountFilter && !accountFilter.has(accountId)) { + continue; + } + for (const assetId of Object.keys(accountBalances)) { + stateBalanceIds.add(assetId.toLowerCase()); + if (!inRequestChains(assetId) || !needsMetadata(assetId)) { + continue; + } + assetIdsNeedingMetadata.add(assetId); + balanceHealAssetIds.add(assetId.toLowerCase()); + } + } + + for (const accountBalances of Object.values(response.assetsBalance ?? {})) { + for (const assetId of Object.keys(accountBalances)) { + if (!needsMetadata(assetId)) { + continue; + } + assetIdsNeedingMetadata.add(assetId); + const lowerId = assetId.toLowerCase(); + if (stateBalanceIds.has(lowerId) || knownMetadataIds.has(lowerId)) { + balanceHealAssetIds.add(lowerId); + } + } + } + + return { assetIdsNeedingMetadata, balanceHealAssetIds }; +} + +/** + * True when metadata already has every field that controller logic or a + * client reads from `assetsInfo`. + * + * @param metadata - Stored or in-flight asset metadata. + * @returns Whether another metadata fetch can be skipped. + */ +function hasMetadataUsedByClients( + metadata: AssetMetadata | undefined, +): boolean { + if ( + !metadata || + !('aggregators' in metadata) || + !('occurrences' in metadata) + ) { + return false; + } + + return ( + hasText(metadata.symbol) && + hasText(metadata.name) && + isFiniteNonNegative(metadata.decimals) && + hasText(metadata.image) && + Array.isArray(metadata.aggregators) && + isFiniteNonNegative(metadata.occurrences) + ); +} + +/** + * @param value - Candidate string field. + * @returns Whether the field is a non-empty string. + */ +function hasText(value: string | undefined): boolean { + return typeof value === 'string' && value.length > 0; +} + +/** + * @param value - Candidate numeric field. + * @returns Whether the field is a finite number greater than or equal to zero. + */ +function isFiniteNonNegative(value: unknown): boolean { + return typeof value === 'number' && Number.isFinite(value) && value >= 0; +} + +/** + * Account IDs from the request, or `undefined` when the request has none + * (no account filtering). + * + * @param request - Request with accountsWithSupportedChains. + * @returns Account ID set, or undefined when empty. + */ +function buildAccountFilter( + request: Pick, +): Set | undefined { + const accountIds = request.accountsWithSupportedChains.map( + ({ account }) => account.id, + ); + return accountIds.length > 0 ? new Set(accountIds) : undefined; +} + +/** + * Chain IDs from the request, or `undefined` when the request has none + * (no chain filtering). + * + * @param request - Request with chainIds. + * @returns Chain ID set, or undefined when empty. + */ +function buildChainFilter( + request: Pick, +): Set | undefined { + return request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; +} + +/** + * Lower-case keys of assets the user has marked hidden. + * + * @param assetPreferences - Preference map from controller state. + * @returns Set of lower-case asset ID keys. + */ +function collectHiddenAssetIdKeys( + assetPreferences: AssetsControllerState['assetPreferences'] | undefined, +): Set { + return new Set( + Object.entries(assetPreferences ?? {}) + .filter(([, preferences]) => preferences.hidden) + .map(([assetId]) => assetId.toLowerCase()), + ); +} diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts new file mode 100644 index 00000000000..3bcc84c1b4a --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts @@ -0,0 +1,346 @@ +import type { InternalAccount } from '@metamask/keyring-internal-api'; +import { describe, expect, it, vi } from 'vitest'; + +import type { + AssetBalance, + AssetsControllerState, + Caip19AssetId, + ChainId, + DataRequest, + FungibleAssetMetadata, +} from '../types.js'; +import { + getAssetIdsForPriceFetch, + getResponseAssetIdsForPriceFetch, + getUnpricedResponseAssetIds, +} from './getAssetIdsForPriceFetch.js'; +import { normalizeAssetId } from './normalizeAssetId.js'; + +const CHAIN_MAINNET = 'eip155:1' as ChainId; +const ACCOUNT_A = 'account-a'; +const ACCOUNT_B = 'account-b'; + +const NATIVE_MAINNET = 'eip155:1/slip44:60' as Caip19AssetId; +const TOKEN_MAINNET = + 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; +const TOKEN_POLYGON = + 'eip155:137/erc20:0x3c499c542cef5e3811e1192ce70d8cc03d5c3359' as Caip19AssetId; +const CUSTOM_MAINNET = + 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId; +const METADATA_MAINNET = + 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId; + +const ZERO_BALANCE: AssetBalance = { amount: '0' }; + +const TOKEN_METADATA: FungibleAssetMetadata = { + type: 'erc20', + symbol: 'TOK', + name: 'Token', + decimals: 18, +}; + +function createAccount(id: string): InternalAccount { + return { + id, + address: '0x1234567890123456789012345678901234567890', + options: {}, + methods: [], + type: 'eip155:eoa', + scopes: ['eip155:0'], + metadata: { + name: 'Test Account', + keyring: { type: 'HD Key Tree' }, + importTime: Date.now(), + lastSelected: Date.now(), + }, + }; +} + +function createRequest( + overrides: { + accountIds?: string[]; + chainIds?: ChainId[]; + } = {}, +): Pick { + const chainIds = overrides.chainIds ?? [CHAIN_MAINNET]; + const accountIds = overrides.accountIds ?? [ACCOUNT_A]; + return { + chainIds, + accountsWithSupportedChains: accountIds.map((id) => ({ + account: createAccount(id), + supportedChains: chainIds, + })), + }; +} + +function emptyState( + overrides: Partial< + Pick< + AssetsControllerState, + 'assetsBalance' | 'customAssets' | 'assetsInfo' | 'assetPreferences' + > + > = {}, +): Pick< + AssetsControllerState, + 'assetsBalance' | 'customAssets' | 'assetsInfo' | 'assetPreferences' +> { + return { + assetsBalance: {}, + customAssets: {}, + assetsInfo: {}, + assetPreferences: {}, + ...overrides, + }; +} + +function sortedIds(ids: Set): Caip19AssetId[] { + return [...ids].sort(); +} + +describe('getAssetIdsForPriceFetch', () => { + it.each([ + { + name: 'state balances in account/chain scope', + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + }, + }), + expected: [NATIVE_MAINNET], + }, + { + name: 'custom assets in account/chain scope', + state: emptyState({ + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + }), + expected: [normalizeAssetId(CUSTOM_MAINNET)], + }, + { + name: 'union of state balance and custom assets', + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + }, + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, + }), + expected: [NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET)].sort(), + }, + ])('includes $name', ({ state, expected }) => { + const result = getAssetIdsForPriceFetch({ + state, + request: createRequest(), + }); + + expect(sortedIds(result)).toStrictEqual([...expected].sort()); + }); + + it.each([ + { + name: 'hidden state balance', + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_MAINNET]: ZERO_BALANCE, + }, + }, + assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, + }), + expected: [NATIVE_MAINNET], + }, + { + name: 'hidden custom asset', + state: emptyState({ + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + assetPreferences: { [CUSTOM_MAINNET]: { hidden: true } }, + }), + expected: [], + }, + { + name: 'metadata with no balance', + state: emptyState({ + assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, + }), + expected: [], + }, + ])('excludes $name', ({ state, expected }) => { + const result = getAssetIdsForPriceFetch({ + state, + request: createRequest(), + }); + + expect(sortedIds(result)).toStrictEqual(expected); + }); + + it('filters balances and custom assets by account ID', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + [ACCOUNT_B]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + customAssets: { + [ACCOUNT_A]: [CUSTOM_MAINNET], + [ACCOUNT_B]: [METADATA_MAINNET], + }, + }), + request: createRequest({ accountIds: [ACCOUNT_A] }), + }); + + expect(sortedIds(result)).toStrictEqual( + [NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET)].sort(), + ); + }); + + it('filters all sources by chain ID', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + }, + }, + customAssets: { + [ACCOUNT_A]: [CUSTOM_MAINNET, TOKEN_POLYGON], + }, + assetsInfo: { + [METADATA_MAINNET]: TOKEN_METADATA, + [TOKEN_POLYGON]: TOKEN_METADATA, + }, + }), + request: createRequest({ chainIds: [CHAIN_MAINNET] }), + }); + + expect(sortedIds(result)).toStrictEqual( + [NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET)].sort(), + ); + }); + + it('deduplicates the same asset across a state balance and a custom asset', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + customAssets: { [ACCOUNT_A]: [TOKEN_MAINNET] }, + assetsInfo: { [TOKEN_MAINNET]: TOKEN_METADATA }, + }), + request: createRequest(), + }); + + expect([...result]).toStrictEqual([normalizeAssetId(TOKEN_MAINNET)]); + }); + + it('skips malformed asset IDs and keeps valid ones', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { + 'not-a-valid-caip19': ZERO_BALANCE, + [NATIVE_MAINNET]: ZERO_BALANCE, + } as Record, + }, + }), + request: createRequest(), + }); + + expect([...result]).toStrictEqual([NATIVE_MAINNET]); + }); + + it('returns every balance on the response', () => { + const result = getResponseAssetIdsForPriceFetch({ + response: { + assetsBalance: { + [ACCOUNT_A]: { + [TOKEN_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + }, + [ACCOUNT_B]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + expect(sortedIds(result)).toStrictEqual( + [ + NATIVE_MAINNET, + normalizeAssetId(TOKEN_MAINNET), + normalizeAssetId(TOKEN_POLYGON), + ].sort(), + ); + }); + + it('returns unpriced priceable response balances on a supported chain', async () => { + const result = await getUnpricedResponseAssetIds({ + state: { + ...emptyState(), + assetsPrice: { + [TOKEN_POLYGON]: { price: 1 }, + } as AssetsControllerState['assetsPrice'], + }, + response: { + assetsBalance: { + [ACCOUNT_A]: { + [TOKEN_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + 'tron:728126428/slip44:bandwidth': ZERO_BALANCE, + }, + }, + }, + getSupportedNetworks: async () => new Set([CHAIN_MAINNET]), + }); + + expect(result).toStrictEqual([normalizeAssetId(TOKEN_MAINNET)]); + }); + + it('skips hidden response balances', async () => { + const getSupportedNetworks = vi.fn(async () => new Set([CHAIN_MAINNET])); + + const result = await getUnpricedResponseAssetIds({ + state: { + ...emptyState({ + assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, + }), + assetsPrice: {}, + }, + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + getSupportedNetworks, + }); + + expect(result).toStrictEqual([]); + expect(getSupportedNetworks).not.toHaveBeenCalled(); + }); + + it('does not load supported networks when nothing priceable remains', async () => { + const getSupportedNetworks = vi.fn(); + + const result = await getUnpricedResponseAssetIds({ + state: { ...emptyState(), assetsPrice: {} }, + response: { + assetsBalance: { + [ACCOUNT_A]: { + 'tron:728126428/slip44:bandwidth': ZERO_BALANCE, + }, + }, + }, + getSupportedNetworks, + }); + + expect(result).toStrictEqual([]); + expect(getSupportedNetworks).not.toHaveBeenCalled(); + }); + + it('returns an empty set when no candidates are in scope', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState(), + request: createRequest(), + }); + + expect([...result]).toStrictEqual([]); + }); +}); diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts new file mode 100644 index 00000000000..38846f30f94 --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts @@ -0,0 +1,315 @@ +import { parseCaipAssetType } from '@metamask/utils'; + +import { projectLogger, createModuleLogger } from '../logger.js'; +import type { + AssetsControllerState, + Caip19AssetId, + DataRequest, + DataResponse, +} from '../types.js'; +import { normalizeAssetId } from './normalizeAssetId.js'; + +const log = createModuleLogger(projectLogger, 'getAssetIdsForPriceFetch'); + +const NON_PRICEABLE_ASSET_PATTERNS = [ + // Synthetic slip44 staking-position assets: the Price API only knows about + // pure numeric coin-type references (e.g. slip44:195). Any suffix after the + // number is a MetaMask-internal synthetic asset that has no market price. + /\/slip44:\d+-/u, + // Tron non-price resource assets (bandwidth, energy) + /\/slip44:bandwidth$/u, + /\/slip44:energy$/u, + /\/slip44:maximum-bandwidth$/u, + /\/slip44:maximum-energy$/u, +]; + +/** + * Check if an asset ID represents a priceable asset. + * Filters out internal resource tracking values that don't have market prices. + * + * @param assetId - The CAIP-19 asset ID to check. + * @returns True if the asset has market price data. + */ +export function isPriceableAsset(assetId: Caip19AssetId): boolean { + return !NON_PRICEABLE_ASSET_PATTERNS.some((pattern) => pattern.test(assetId)); +} + +export type GetAssetIdsForPriceFetchParams = { + /** Current AssetsController state slices used for candidate collection. */ + state: Pick< + AssetsControllerState, + 'assetsBalance' | 'customAssets' | 'assetPreferences' + >; + /** Account and chain scope for the fetch. */ + request: Pick; +}; + +/** + * Assets the price poll should price: saved balances and custom assets, + * excluding hidden assets. Scoped to the request's accounts and chains. + * Balance rows are what a fetch leaves visible (holdings, plus native, pins, + * and default tracked assets at zero). Metadata with no balance is not priced. + * + * The balance-refresh middleware does not use this. It prices response + * balances through {@link getUnpricedResponseAssetIds}. + * + * @param params - State and request scope. + * @param params.state - Controller state slices used for candidate collection. + * @param params.request - Account and chain scope for the fetch. + * @returns Deduplicated, checksummed CAIP-19 asset IDs. + */ +export function getAssetIdsForPriceFetch({ + state, + request, +}: GetAssetIdsForPriceFetchParams): Set { + const assetIds = new Set(); + const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); + const accountFilter = buildAccountFilter(request); + const chainFilter = buildChainFilter(request); + const addAssetId = (assetId: string): void => { + addStoredAssetId(assetId, assetIds, chainFilter, hiddenAssetIds); + }; + + addBalancesFromAccounts(state.assetsBalance, accountFilter, addAssetId); + addCustomAssets(state.customAssets, accountFilter, addAssetId); + + return assetIds; +} + +export type GetResponseAssetIdsForPriceFetchParams = { + /** Current pipeline response balances. Already scoped to the request. */ + response: Pick; +}; + +/** + * Asset IDs on the current response balances. Balance sources already limit + * the response to the request and write ids in the form stored prices use. + * + * @param params - Response balances. + * @param params.response - Current pipeline response balances. + * @returns Deduplicated, checksummed CAIP-19 asset IDs. + */ +export function getResponseAssetIdsForPriceFetch({ + response, +}: GetResponseAssetIdsForPriceFetchParams): Set { + const assetIds = new Set(); + for (const accountBalances of Object.values(response.assetsBalance ?? {})) { + for (const assetId of Object.keys(accountBalances)) { + try { + assetIds.add(normalizeAssetId(assetId as Caip19AssetId)); + } catch (error) { + log('Skipping malformed asset ID for price fetch', { assetId, error }); + } + } + } + return assetIds; +} + +export type GetUnpricedResponseAssetIdsParams = + GetResponseAssetIdsForPriceFetchParams & { + /** Hidden preferences and stored prices. */ + state: Pick; + /** + * Loads chains the price API can price. Called only when a priceable + * response balance remains. An empty set keeps every remaining asset. + */ + getSupportedNetworks: () => Promise>; + }; + +/** + * Response balances the balance-refresh middleware should price. + * + * Hidden assets are excluded, then non-priceable assets, assets on chains the + * price API does not support, and assets that already have a stored price. + * + * @param params - State, response balances, and network lookup. + * @param params.state - Hidden preferences and stored prices. + * @param params.response - Current pipeline response balances. + * @param params.getSupportedNetworks - Loads chains the price API can price. + * @returns Asset IDs that still need a price. + */ +export async function getUnpricedResponseAssetIds({ + state, + response, + getSupportedNetworks, +}: GetUnpricedResponseAssetIdsParams): Promise { + const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); + const priceableAssetIds = [ + ...getResponseAssetIdsForPriceFetch({ response }), + ].filter( + (assetId) => !hiddenAssetIds.has(assetId) && isPriceableAsset(assetId), + ); + if (priceableAssetIds.length === 0) { + return []; + } + + const supportedNetworks = await getSupportedNetworks(); + return filterAssetsMissingPrice( + filterAssetsBySupportedNetworks(priceableAssetIds, supportedNetworks), + state.assetsPrice, + ); +} + +/** + * Keeps asset IDs whose chain is in `supportedNetworks`. + * + * @param assetIds - CAIP-19 asset IDs. + * @param supportedNetworks - Supported CAIP-2 chain IDs. + * @returns Matching IDs, or all IDs if the set is empty. + */ +export function filterAssetsBySupportedNetworks( + assetIds: Caip19AssetId[], + supportedNetworks: Set, +): Caip19AssetId[] { + if (supportedNetworks.size === 0) { + return assetIds; + } + return assetIds.filter((assetId) => { + try { + const parsed = parseCaipAssetType(assetId); + const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`; + return supportedNetworks.has(chainId); + } catch { + return false; + } + }); +} + +/** + * Keep asset IDs that have no stored price. + * + * @param assetIds - CAIP-19 asset IDs from the response. + * @param storedPrices - Prices already in controller state. + * @returns IDs with no entry under that id. + */ +function filterAssetsMissingPrice( + assetIds: Caip19AssetId[], + storedPrices: AssetsControllerState['assetsPrice'] | undefined, +): Caip19AssetId[] { + const prices = storedPrices ?? {}; + return assetIds.filter((assetId) => prices[assetId] === undefined); +} + +/** + * Account IDs from the request, or `undefined` when the request has none + * (no account filtering). + * + * @param request - Request with accountsWithSupportedChains. + * @returns Account ID set, or undefined when empty. + */ +function buildAccountFilter( + request: Pick, +): Set | undefined { + const accountIds = request.accountsWithSupportedChains.map( + ({ account }) => account.id, + ); + return accountIds.length > 0 ? new Set(accountIds) : undefined; +} + +/** + * Chain IDs from the request, or `undefined` when the request has none + * (no chain filtering). + * + * @param request - Request with chainIds. + * @returns Chain ID set, or undefined when empty. + */ +function buildChainFilter( + request: Pick, +): Set | undefined { + return request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; +} + +/** + * Ids of assets the user has marked hidden. Preferences in state are stored + * checksummed, same as balances and prices. + * + * @param assetPreferences - Preference map from controller state. + * @returns Set of hidden CAIP-19 asset IDs. + */ +function collectHiddenAssetIdKeys( + assetPreferences: AssetsControllerState['assetPreferences'] | undefined, +): Set { + return new Set( + Object.entries(assetPreferences ?? {}) + .filter(([, preferences]) => preferences.hidden) + .map(([assetId]) => normalizeAssetId(assetId as Caip19AssetId)), + ); +} + +/** + * Checksum `assetId`, then keep it when it is in chain scope and not hidden. + * An id that fails checksumming is skipped. {@link normalizeAssetId} is + * memoized, so an id that is already checksummed is cheap. + * + * @param assetId - Candidate CAIP-19 asset ID. + * @param assetIds - Collected ids. + * @param chainFilter - Optional allowed chain IDs. + * @param hiddenAssetIds - Hidden asset IDs from state. + */ +function addStoredAssetId( + assetId: string, + assetIds: Set, + chainFilter: Set | undefined, + hiddenAssetIds: Set, +): void { + try { + const id = normalizeAssetId(assetId as Caip19AssetId); + if (hiddenAssetIds.has(id)) { + return; + } + const { chainId } = parseCaipAssetType(id); + if (chainFilter && !chainFilter.has(chainId)) { + return; + } + assetIds.add(id); + } catch (error) { + log('Skipping malformed asset ID for price fetch', { assetId, error }); + } +} + +/** + * Add every asset ID under each account's balance map, optionally filtering + * by account. + * + * @param balances - Account → asset balance map. + * @param accountFilter - Optional allowed account IDs. + * @param addAssetId - Callback that records a candidate asset ID. + */ +function addBalancesFromAccounts( + balances: DataResponse['assetsBalance'] | undefined, + accountFilter: Set | undefined, + addAssetId: (assetId: string) => void, +): void { + for (const [accountId, accountBalances] of Object.entries(balances ?? {})) { + if (accountFilter && !accountFilter.has(accountId)) { + continue; + } + for (const assetId of Object.keys(accountBalances)) { + addAssetId(assetId); + } + } +} + +/** + * Add custom (imported) asset IDs for accounts in scope. + * + * @param customAssets - Account → custom asset ID list. + * @param accountFilter - Optional allowed account IDs. + * @param addAssetId - Callback that records a candidate asset ID. + */ +function addCustomAssets( + customAssets: AssetsControllerState['customAssets'] | undefined, + accountFilter: Set | undefined, + addAssetId: (assetId: string) => void, +): void { + for (const [accountId, customAssetIds] of Object.entries( + customAssets ?? {}, + )) { + if (accountFilter && !accountFilter.has(accountId)) { + continue; + } + for (const assetId of customAssetIds) { + addAssetId(assetId); + } + } +} diff --git a/tsc-suppressions.json b/tsc-suppressions.json index 9468d122fae..422a1a195c9 100644 --- a/tsc-suppressions.json +++ b/tsc-suppressions.json @@ -146,11 +146,6 @@ "count": 1 } }, - "packages/assets-controller/src/data-sources/PriceDataSource.test.ts": { - "TS2352": { - "count": 1 - } - }, "packages/assets-controller/src/data-sources/RpcDataSource.test.ts": { "TS2353": { "count": 8 @@ -161,11 +156,6 @@ "count": 2 } }, - "packages/assets-controller/src/data-sources/TokenDataSource.test.ts": { - "TS2322": { - "count": 1 - } - }, "packages/assets-controller/src/middlewares/CustomAssetGraduationMiddleware.test.ts": { "TS2554": { "count": 2