From a75012f4aafab9503d3faeedfbfffbaf69af3510 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 16 Oct 2026 09:54:18 +0200 Subject: [PATCH 01/14] fix(assets-controller): remove assetsForPriceUpdate --- oxlint-suppressions.json | 9 +- packages/assets-controller/CHANGELOG.md | 8 + .../src/AssetsController.test.ts | 57 +-- .../assets-controller/src/AssetsController.ts | 240 +++++----- packages/assets-controller/src/README.md | 34 +- .../src/data-sources/PriceDataSource.test.ts | 311 +++++++++---- .../src/data-sources/PriceDataSource.ts | 211 +++++---- .../src/data-sources/TokenDataSource.test.ts | 43 +- .../src/data-sources/TokenDataSource.ts | 205 +++++---- packages/assets-controller/src/index.ts | 2 - .../middlewares/DetectionMiddleware.test.ts | 429 ------------------ .../src/middlewares/DetectionMiddleware.ts | 201 -------- .../src/middlewares/index.ts | 2 - ...tering.accounts-api-v6.integration.test.ts | 4 - ...c-spam-token-filtering.integration.test.ts | 4 - .../pipeline/buildFastFetchSources.test.ts | 9 +- .../src/pipeline/buildFastFetchSources.ts | 22 +- .../src/pipeline/buildUpdateSources.test.ts | 29 +- .../src/pipeline/buildUpdateSources.ts | 3 - ...eSources.price-updates.integration.test.ts | 24 +- .../src/pipeline/buildWsUpdateSources.test.ts | 16 +- .../src/pipeline/buildWsUpdateSources.ts | 3 - packages/assets-controller/src/types.ts | 2 - tsc-suppressions.json | 5 - 24 files changed, 670 insertions(+), 1203 deletions(-) delete mode 100644 packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts delete mode 100644 packages/assets-controller/src/middlewares/DetectionMiddleware.ts diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 7df780443d1..543641684b0 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -725,7 +725,7 @@ "count": 3 }, "typescript/no-unsafe-assignment": { - "count": 22 + "count": 21 }, "typescript/no-unsafe-call": { "count": 4 @@ -739,7 +739,7 @@ "count": 1 }, "typescript/no-unsafe-assignment": { - "count": 5 + "count": 4 } }, "packages/assets-controller/src/__fixtures__/MockAssetControllerMessenger.ts": { @@ -801,10 +801,7 @@ }, "packages/assets-controller/src/data-sources/PriceDataSource.test.ts": { "typescript/no-unsafe-assignment": { - "count": 8 - }, - "typescript/no-unsafe-member-access": { - "count": 3 + "count": 7 } }, "packages/assets-controller/src/data-sources/RpcDataSource.test.ts": { diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 80f84594cc7..708364dcdd7 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `syncAssetsPrices(assetIds)`, which force-fetches spot prices for an explicit CAIP-19 asset list through `PriceDataSource` and merges them into state. Prices are shared, so the method takes no account or chain scope + ### Changed +- **BREAKING:** Remove `DetectionMiddleware`. `PriceDataSource` prices custom assets, balances, and metadata already in state plus balances on the current response, excluding hidden assets. `TokenDataSource` fetches metadata for state balances that have none and for response balances, and excludes hidden assets +- **BREAKING:** Remove `assetsForPriceUpdate` from `getAssets` options. Refresh an explicit asset list with `syncAssetsPrices` instead +- Price enrichment relies on the price freshness cache to avoid redundant requests + - Bump `@metamask/keyring-api` from `^24.1.0` to `^24.3.0` ([#10792](https://github.com/MetaMask/core/pull/10792), [#10844](https://github.com/MetaMask/core/pull/10844)) - Bump `@metamask/keyring-internal-api` from `^12.1.0` to `^12.1.2` ([#10792](https://github.com/MetaMask/core/pull/10792), [#10844](https://github.com/MetaMask/core/pull/10844)) - Bump `@metamask/keyring-snap-client` from `^10.0.0` to `^10.0.1` ([#10792](https://github.com/MetaMask/core/pull/10792)) diff --git a/packages/assets-controller/src/AssetsController.test.ts b/packages/assets-controller/src/AssetsController.test.ts index c6b670b0c19..25f425bd214 100644 --- a/packages/assets-controller/src/AssetsController.test.ts +++ b/packages/assets-controller/src/AssetsController.test.ts @@ -4128,18 +4128,18 @@ describe('AssetsController', () => { await withController(({ controller }) => { expect(controller.state.selectedCurrency).toBe('usd'); - const getAssetsSpy = jest.spyOn(controller, 'getAssets'); + const refreshSpy = jest.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 = { @@ -4152,17 +4152,15 @@ describe('AssetsController', () => { }; await withController({ state: initialState }, ({ controller }) => { - const getAssetsSpy = jest.spyOn(controller, 'getAssets'); + const refreshSpy = jest + .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(); }); }); @@ -4178,14 +4176,14 @@ describe('AssetsController', () => { await withController( { state: initialState, isBasicFunctionality: () => false }, ({ controller }) => { - const getAssetsSpy = jest.spyOn(controller, 'getAssets'); + const refreshSpy = jest.spyOn(controller, 'syncAssetsPrices'); controller.setSelectedCurrency('eur'); expect(controller.state.selectedCurrency).toBe('eur'); - expect(getAssetsSpy).not.toHaveBeenCalled(); + expect(refreshSpy).not.toHaveBeenCalled(); - getAssetsSpy.mockRestore(); + refreshSpy.mockRestore(); }, ); }); @@ -5011,7 +5009,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). @@ -5040,21 +5037,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(); }, ); @@ -5930,21 +5919,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 17d814a2b2e..686693c9be6 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, @@ -916,8 +912,6 @@ export class AssetsController extends BaseController< readonly #priceDataSource: PriceDataSource; - readonly #detectionMiddleware: DetectionMiddleware; - readonly #customAssetGraduationMiddleware: CustomAssetGraduationMiddleware; readonly #rpcFallbackMiddleware: RpcFallbackMiddleware; @@ -1060,9 +1054,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 => { @@ -1492,24 +1483,20 @@ export class AssetsController extends BaseController< */ async #runStartupRefresh(accounts: InternalAccount[]): Promise { const releaseLock = await this.#accountRefreshMutex.acquire(); + // Seed before the fetch so PriceDataSource includes controller-managed + // native and default tracked assets in the same price pass. + this.#ensureNativeBalancesDefaultZero(); + this.#ensureDefaultTrackedAssetsSeeded(); try { await this.getAssets(accounts, { chainIds: [...this.#enabledChains], forceUpdate: true, }); - // Seed before subscribe so the price poll / update fetch sees natives - // and default tracked assets that were never returned by balance APIs. - this.#ensureNativeBalancesDefaultZero(); - 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,7 +1621,6 @@ export class AssetsController extends BaseController< */ bypassServerCache?: boolean; dataTypes?: DataType[]; - assetsForPriceUpdate?: Caip19AssetId[]; }, ): Promise>> { const chainIds = options?.chainIds ?? [...this.#enabledChains]; @@ -1646,42 +1632,95 @@ 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. + * + * @param assetIds - CAIP-19 asset IDs to sync. + */ + async syncAssetsPrices(assetIds: Caip19AssetId[]): Promise { + if (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. @@ -1693,7 +1732,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( @@ -1704,7 +1742,6 @@ export class AssetsController extends BaseController< dataTypes: DataType[]; forceUpdate: true; bypassServerCache?: boolean; - assetsForPriceUpdate?: Caip19AssetId[]; }, ): DataRequest { const customAssets: Caip19AssetId[] = []; @@ -1736,7 +1773,6 @@ export class AssetsController extends BaseController< stakedBalanceDataSource: this.#stakedBalanceDataSource, customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -1780,7 +1816,6 @@ export class AssetsController extends BaseController< stakedBalanceDataSource: this.#stakedBalanceDataSource, customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -1939,7 +1974,6 @@ export class AssetsController extends BaseController< const { response } = await this.#executeMiddlewares({ sources: [ createParallelBalanceMiddleware(slowSources), - this.#detectionMiddleware, ...(isBasicFunctionality ? [ createParallelMiddleware([ @@ -2538,74 +2572,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) => { - log('Failed to fetch missing prices', { error }); - }); - } - // ============================================================================ // SUBSCRIPTIONS // ============================================================================ @@ -4039,6 +4014,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], @@ -4046,10 +4023,7 @@ export class AssetsController extends BaseController< }); } - this.#ensureNativeBalancesDefaultZero(); - this.#ensureDefaultTrackedAssetsSeeded(); this.#subscribeAssets({ skipInitialFetch: true }); - this.#fetchMissingPricesWithoutCache(accounts, [...this.#enabledChains]); } finally { releaseLock(); } @@ -4094,21 +4068,18 @@ export class AssetsController extends BaseController< // 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(); + this.#ensureNativeBalancesDefaultZero(); + // Seed default tracked assets (mUSD) before the fetch so they are included + // in the same price pass. + if (addedChains.length > 0) { + this.#ensureDefaultTrackedAssetsSeeded(addedChains); + } 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]); } /** @@ -4140,8 +4111,6 @@ export class AssetsController extends BaseController< }); this.#ensureDefaultTrackedAssetsSeeded([caipChainId]); - const accounts = this.#getSelectedAccounts(); - this.#fetchMissingPricesWithoutCache(accounts, [caipChainId]); } /** @@ -4209,15 +4178,13 @@ export class AssetsController extends BaseController< await this.#refreshActiveChainsOnNetworkSwitch(); this.#subscribeAssets(); + this.#ensureNativeBalancesDefaultZero(); await this.getAssets(accounts, { chainIds: [selectedChainId], forceUpdate: true, dataTypes: ['balance', 'metadata', 'price'], }); - - this.#ensureNativeBalancesDefaultZero(); - this.#fetchMissingPricesWithoutCache(accounts, [selectedChainId]); } finally { releaseLock(); } @@ -4258,13 +4225,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]); } /** @@ -4319,7 +4285,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4330,7 +4295,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4388,7 +4352,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, @@ -4399,7 +4362,6 @@ export class AssetsController extends BaseController< customAssetGraduationMiddleware: this.#customAssetGraduationMiddleware, rpcFallbackMiddleware: this.#rpcFallbackMiddleware, - detectionMiddleware: this.#detectionMiddleware, tokenDataSource: this.#tokenDataSource, priceDataSource: this.#priceDataSource, }, diff --git a/packages/assets-controller/src/README.md b/packages/assets-controller/src/README.md index 33535f73a5f..bdb93d89925 100644 --- a/packages/assets-controller/src/README.md +++ b/packages/assets-controller/src/README.md @@ -105,9 +105,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 +114,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 +150,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 +187,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) @@ -810,11 +806,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 +828,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 +838,6 @@ flowchart TB RPC[RpcDataSource - Order 4] TOK[TokenDataSource] PRICE[PriceDataSource] - DET[DetectionMiddleware] end subgraph ExternalDeps["External Dependencies"] diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index c7415a37446..a414a814086 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -21,9 +21,22 @@ const MOCK_TOKEN_ASSET = 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; const MOCK_NATIVE_ASSET = 'eip155:1/slip44:60' as Caip19AssetId; +type FetchV3SpotPricesArgs = [ + assetIds: string[], + queryOptions?: { + currency?: SupportedCurrency; + includeMarketData?: boolean; + cacheOnly?: boolean; + }, + options?: Record, +]; + type MockApiClient = { prices: { - fetchV3SpotPrices: jest.Mock; + fetchV3SpotPrices: jest.Mock< + Promise>, + FetchV3SpotPricesArgs + >; fetchPriceV2SupportedNetworks: jest.Mock; }; }; @@ -87,7 +100,9 @@ function createMockApiClient( ): MockApiClient { return { prices: { - fetchV3SpotPrices: jest.fn().mockResolvedValue(priceResponse), + fetchV3SpotPrices: jest + .fn>, FetchV3SpotPricesArgs>() + .mockResolvedValue(priceResponse), fetchPriceV2SupportedNetworks: jest.fn().mockResolvedValue({ fullSupport: supportedNetworks, partialSupport: { @@ -238,6 +253,135 @@ describe('PriceDataSource', () => { controller.destroy(); }); + it('middleware prices the union of response and state balance assets', async () => { + const { controller, apiClient } = setupController({ + balanceState: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, + }, + }, + priceResponse: { + [MOCK_NATIVE_ASSET]: createMockPriceData(2500), + [MOCK_TOKEN_ASSET]: createMockPriceData(1), + }, + }); + const context = createMiddlewareContext({ + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '10' }, + }, + }, + }, + }); + + await controller.assetsMiddleware(context, jest.fn()); + + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + expect.arrayContaining([ + MOCK_NATIVE_ASSET, + normalizeAssetId(MOCK_TOKEN_ASSET), + ]), + expect.anything(), + ); + expect(apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]).toHaveLength(2); + + controller.destroy(); + }); + + it('middleware excludes hidden assets from response and state balances', async () => { + const getAssetsState = (): AssetsControllerState => + ({ + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, + [MOCK_TOKEN_ASSET]: { amount: '10' }, + }, + }, + assetPreferences: { + [MOCK_TOKEN_ASSET]: { hidden: true }, + }, + }) as unknown as AssetsControllerState; + const { controller, apiClient } = setupController({ + getAssetsState, + priceResponse: { + [MOCK_NATIVE_ASSET]: createMockPriceData(2500), + }, + }); + const context = createMiddlewareContext({ + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '10' }, + }, + }, + }, + }); + + await controller.assetsMiddleware(context, jest.fn()); + + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + [MOCK_NATIVE_ASSET], + expect.anything(), + ); + + controller.destroy(); + }); + + it('middleware prices custom assets and metadata from state, excluding hidden assets', async () => { + const metadataOnlyAsset = + 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId; + const customAsset = + 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId; + const hiddenMetadataAsset = + 'eip155:1/erc20:0x3333333333333333333333333333333333333333' as Caip19AssetId; + const getAssetsState = (): AssetsControllerState => + ({ + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1' }, + }, + }, + assetsInfo: { + [metadataOnlyAsset]: { symbol: 'META' }, + [hiddenMetadataAsset]: { symbol: 'HIDE' }, + }, + customAssets: { + 'mock-account-id': [customAsset], + }, + assetPreferences: { + [hiddenMetadataAsset]: { hidden: true }, + }, + }) as unknown as AssetsControllerState; + const { controller, apiClient } = setupController({ + getAssetsState, + priceResponse: { + [MOCK_NATIVE_ASSET]: createMockPriceData(2500), + [metadataOnlyAsset]: createMockPriceData(1), + [customAsset]: createMockPriceData(2), + }, + }); + + await controller.assetsMiddleware( + createMiddlewareContext({ response: {} }), + jest.fn(), + ); + + expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( + expect.arrayContaining([ + MOCK_NATIVE_ASSET, + normalizeAssetId(metadataOnlyAsset), + normalizeAssetId(customAsset), + ]), + expect.anything(), + ); + expect(apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]).not.toContain( + normalizeAssetId(hiddenMetadataAsset), + ); + + controller.destroy(); + }); + it('fetch skips malformed asset IDs in balance state and still fetches prices for valid assets', async () => { const { controller, apiClient } = setupController({ balanceState: { @@ -871,57 +1015,28 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware passes to next when assetsForPriceUpdate is empty and no detected assets', async () => { - const { controller, apiClient } = setupController(); - - const next = jest.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [] }), - response: {}, - }); - - 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('fetchAssetPrices requests spot prices for the given assets', async () => { const { controller, apiClient } = setupController({ priceResponse: { [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), }, }); - const next = jest.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - request: createDataRequest({ assetsForPriceUpdate: [MOCK_TOKEN_ASSET] }), - response: {}, - }); - - await controller.assetsMiddleware(context, next); + const prices = await controller.fetchAssetPrices([MOCK_TOKEN_ASSET]); expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( [MOCK_TOKEN_ASSET], { currency: 'usd', includeMarketData: true }, ); - expect(context.response.assetsPrice?.[MOCK_TOKEN_ASSET]).toStrictEqual({ + expect(prices[MOCK_TOKEN_ASSET]).toMatchObject({ assetPriceType: 'fungible', - price: 1.0, - usdPrice: 1.0, - pricePercentChange1d: 2.5, - lastUpdated: expect.any(Number), - marketCap: 1000000000, - totalVolume: 50000000, + price: 1, }); - expect(next).toHaveBeenCalledWith(context); controller.destroy(); }); - it('middleware fetches prices for detected assets', async () => { + it('middleware fetches prices for assets in the balance response', async () => { const { controller, apiClient } = setupController({ priceResponse: { [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), @@ -931,8 +1046,10 @@ describe('PriceDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -957,7 +1074,7 @@ describe('PriceDataSource', () => { 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 +1085,10 @@ describe('PriceDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [tronBandwidthAsset], + assetsBalance: { + 'mock-account-id': { + [tronBandwidthAsset]: { amount: '1' }, + }, }, }, }); @@ -992,8 +1111,10 @@ describe('PriceDataSource', () => { const next = jest.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 +1147,10 @@ describe('PriceDataSource', () => { lastUpdated: Date.now(), }, }, - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1140,19 +1263,24 @@ describe('PriceDataSource', () => { }); const next = jest.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 +1288,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 +1312,20 @@ describe('PriceDataSource', () => { apiClient.prices.fetchV3SpotPrices.mockReturnValue(apiPromise); const next = jest.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 +1373,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 = jest.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(), ); @@ -1291,7 +1426,7 @@ describe('PriceDataSource', () => { [MOCK_NATIVE_ASSET]: { amount: '1.5' }, }, }, - }) as AssetsControllerState, + }) as unknown as AssetsControllerState, }); await controller2.fetch(createDataRequest()); @@ -1391,17 +1526,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 +1550,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 +1584,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 +1604,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..1a492770736 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -231,53 +231,17 @@ 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. Collects custom assets, balances, and metadata from state, plus response balances + * 2. Excludes hidden, non-priceable, and unsupported assets + * 3. Fetches stale prices according to the freshness cache + * 4. Enriches the response and continues the middleware chain * * @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); - } - } - } + const assetIds = this.#getAssetIdsForPriceFetch(request, response); if (assetIds.size === 0) { return next(ctx); @@ -300,10 +264,6 @@ export class PriceDataSource { return next(ctx); } - if (request.forceUpdate) { - this.#deduper.invalidateKeys(supportedAssetIds); - } - try { const spotPrices = await this.#fetchSpotPrices(supportedAssetIds); response.assetsPrice = { @@ -319,6 +279,33 @@ 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 priceableAssetIds = assetIds.filter(isPriceableAsset); + if (priceableAssetIds.length === 0) { + return {}; + } + + const supportedNetworks = await this.#getSupportedNetworks(); + const supportedAssetIds = this.#filterAssetsByNetwork( + priceableAssetIds, + supportedNetworks, + ); + if (supportedAssetIds.length === 0) { + return {}; + } + + this.#deduper.invalidateKeys(supportedAssetIds); + return this.#fetchSpotPrices(supportedAssetIds); + } + // ============================================================================ // HELPERS // ============================================================================ @@ -454,54 +441,104 @@ export class PriceDataSource { */ #getAssetIdsFromBalanceState(request: DataRequest): Caip19AssetId[] { try { - const state = this.#getAssetsState(); - const assetIds = new Set(); + return [ + ...this.#getAssetIdsForPriceFetch(request, { + assetsBalance: {}, + }), + ]; + } catch (error) { + log('Failed to get asset IDs from balance state', { error }); + return []; + } + } - 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; - } + /** + * Assets to price for this request: custom assets, balances, and metadata + * already in state, plus balances on the current response, excluding hidden + * assets. Scoped to the request's accounts and chains. + * + * @param request - Account and chain scope for the fetch. + * @param response - Current pipeline response. + * @returns Deduplicated, normalized CAIP-19 asset IDs. + */ + #getAssetIdsForPriceFetch( + request: DataRequest, + response: Pick, + ): Set { + const state = this.#getAssetsState(); + const accountIds = request.accountsWithSupportedChains.map( + ({ account }) => account.id, + ); + const accountFilter = + accountIds.length > 0 ? new Set(accountIds) : undefined; + const chainFilter = + request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; + const hiddenAssetIds = new Set( + Object.entries(state.assetPreferences ?? {}) + .filter(([, preferences]) => preferences.hidden) + .map(([assetId]) => + safeNormalizeAssetId(assetId as Caip19AssetId).toLowerCase(), + ), + ); + const assetIds = new Map(); - 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); - } + const addAssetId = (assetId: string): void => { + try { + const normalizedAssetId = safeNormalizeAssetId( + assetId as Caip19AssetId, + ); + const { chainId } = parseCaipAssetType(normalizedAssetId); + const key = normalizedAssetId.toLowerCase(); + if ( + (chainFilter && !chainFilter.has(chainId)) || + hiddenAssetIds.has(key) + ) { + return; } + assetIds.set(key, normalizedAssetId); + } catch (error) { + log('Skipping malformed asset ID for price fetch', { assetId, error }); } + }; - return [...assetIds]; - } catch (error) { - log('Failed to get asset IDs from balance state', { error }); - return []; + const addBalances = ( + balances: DataResponse['assetsBalance'] | undefined, + applyAccountFilter: boolean, + ): void => { + for (const [accountId, accountBalances] of Object.entries( + balances ?? {}, + )) { + if ( + applyAccountFilter && + accountFilter && + !accountFilter.has(accountId) + ) { + continue; + } + + for (const assetId of Object.keys(accountBalances)) { + addAssetId(assetId); + } + } + }; + + addBalances(state.assetsBalance, true); + for (const [accountId, customAssetIds] of Object.entries( + state.customAssets ?? {}, + )) { + if (accountFilter && !accountFilter.has(accountId)) { + continue; + } + for (const assetId of customAssetIds) { + addAssetId(assetId); + } } + for (const assetId of Object.keys(state.assetsInfo ?? {})) { + addAssetId(assetId); + } + addBalances(response.assetsBalance, true); + + return new Set(assetIds.values()); } /** diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts index c3b511c35f4..998e4851e69 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts @@ -12,6 +12,7 @@ import type { AssetsControllerState, Context, DataRequest, + DataResponse, Caip19AssetId, ChainId, } from '../types.js'; @@ -133,11 +134,33 @@ function createDataRequest( } as DataRequest; } +function withResponseBalances(response: DataResponse = {}): DataResponse { + if (response.assetsBalance || !response.detectedAssets) { + return response; + } + + const assetsBalance: NonNullable = {}; + for (const [accountId, assetIds] of Object.entries(response.detectedAssets)) { + if (assetIds.length === 0) { + continue; + } + assetsBalance[accountId] = Object.fromEntries( + assetIds.map((assetId) => [assetId, { amount: '1' }]), + ); + } + + if (Object.keys(assetsBalance).length === 0) { + return response; + } + + return { ...response, assetsBalance }; +} + function createMiddlewareContext(overrides?: Partial): Context { return { request: createDataRequest(), - response: {}, ...overrides, + response: withResponseBalances(overrides?.response), }; } @@ -324,9 +347,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; @@ -337,6 +359,14 @@ describe('TokenDataSource', () => { createMockAssetResponse(lowOccurrenceAsset, { occurrences: 1 }), ], suggestedOccurrenceFloors: { '1': 3 }, + getAssetsState: (): Partial => ({ + assetsInfo: {}, + assetsBalance: { + 'mock-account-id': { + [lowOccurrenceAsset]: { amount: '50' }, + }, + }, + }), }); const next = jest.fn().mockResolvedValue(undefined); @@ -1053,7 +1083,7 @@ describe('TokenDataSource', () => { const next = jest.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: { @@ -1575,8 +1605,7 @@ describe('TokenDataSource', () => { }); const next = jest.fn().mockResolvedValue(undefined); - // detectedAssets is intentionally omitted (undefined) to mirror the real-world - // case where DetectionMiddleware finds zero balances and zero custom assets + // No balances on the response: only enabled native assets are fetched. const context = createMiddlewareContext({ response: {}, }); diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index 224dd6eae7f..379591be3fb 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -17,6 +17,7 @@ import type { AssetsControllerState, Middleware, FungibleAssetMetadata, + DataRequest, DataResponse, } from '../types.js'; import { fetchWithTimeout } from '../utils/index.js'; @@ -195,12 +196,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 +397,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 +432,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 ?? {}, @@ -523,35 +525,122 @@ export class TokenDataSource { }); } + /** + * Metadata to fetch for this request. + * + * State balances that have no metadata yet are heals: they are enriched and + * not spam-filtered. 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 request - Account and chain scope. + * @param response - Current pipeline response. + * @returns Asset IDs to fetch, and the subset that must skip spam filtering. + */ + #collectMetadataAssetIds( + request: DataRequest, + response: DataResponse, + ): { + assetIdsNeedingMetadata: Set; + balanceHealAssetIds: Set; + } { + const state = this.#getAssetsState(); + const stateMetadata = state.assetsInfo ?? {}; + const accountIds = request.accountsWithSupportedChains.map( + ({ account }) => account.id, + ); + const accountFilter = + accountIds.length > 0 ? new Set(accountIds) : undefined; + const chainFilter = + request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; + const hiddenAssetIds = new Set( + Object.entries(state.assetPreferences ?? {}) + .filter(([, preferences]) => preferences.hidden) + .map(([assetId]) => assetId.toLowerCase()), + ); + 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 (response.assetsInfo?.[assetId as Caip19AssetId]?.image) { + return false; + } + if (stateMetadata[assetId as Caip19AssetId]?.image) { + 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 }; + } + /** * 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 have none, 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 { customAssets } = this.#getAssetsState(); + const { assetIdsNeedingMetadata, balanceHealAssetIds } = + this.#collectMetadataAssetIds(request, response); // Custom assets are user-imported — exempt from spam filtering. // State stores asset IDs in their normalized (checksummed) form, but the @@ -572,58 +661,6 @@ export class TokenDataSource { 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..84cbea49b4d 100644 --- a/packages/assets-controller/src/index.ts +++ b/packages/assets-controller/src/index.ts @@ -168,12 +168,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 7007170465b..00000000000 --- a/packages/assets-controller/src/middlewares/DetectionMiddleware.test.ts +++ /dev/null @@ -1,429 +0,0 @@ -import type { InternalAccount } from '@metamask/keyring-internal-api'; -import { Messenger } from '@metamask/messenger'; - -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(() => { - jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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/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/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 db46373cb3a..92b16e49406 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 @@ -30,7 +30,6 @@ import { RpcDataSource } from '../data-sources/RpcDataSource.js'; import { StakedBalanceDataSource } from '../data-sources/StakedBalanceDataSource.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, @@ -189,9 +188,6 @@ async function runPipeline( getAssetsState: (): AssetsControllerState => state, isBalanceV6Enabled: (): boolean => true, }), - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, 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 a682e968559..f4b1f79a756 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 @@ -28,7 +28,6 @@ import { RpcDataSource } from '../data-sources/RpcDataSource.js'; import { StakedBalanceDataSource } from '../data-sources/StakedBalanceDataSource.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, @@ -174,9 +173,6 @@ async function runPipeline( rpcDataSource: rpcOverride ?? rpcDataSource, getAssetsState: (): AssetsControllerState => state, }), - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts index 56c23068db8..a9a0ee40751 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.test.ts @@ -25,7 +25,6 @@ function buildSources(): FastFetchSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -35,14 +34,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: [ 'ParallelBalanceMiddleware', 'CustomAssetGraduationMiddleware', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, @@ -54,16 +52,15 @@ describe('buildFastFetchSources', () => { expected: [ 'ParallelBalanceMiddleware', 'RpcFallbackMiddleware', - 'DetectionMiddleware', 'ParallelMiddleware', ], }, { - title: 'runs only the staking balance and detection', + title: 'runs only the staking balance', isBasicFunctionality: false, includeCustomAssetGraduation: true, // No network-backed source may run when the user has opted out. - expected: ['StakedBalanceDataSource', 'DetectionMiddleware'], + expected: ['StakedBalanceDataSource'], }, ])( '$title', diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts index 8b5f59ed643..666dfd1580c 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts @@ -17,29 +17,27 @@ export type FastFetchSources = { stakedBalanceDataSource: BalanceSource; customAssetGraduationMiddleware: AssetsDataSource; rpcFallbackMiddleware: AssetsDataSource; - detectionMiddleware: AssetsDataSource; tokenDataSource: AssetsDataSource; priceDataSource: AssetsDataSource; }; /** * Compose the fast fetch lane: balances in parallel → custom-asset graduation → - * RPC fallback → detection → token metadata and prices in parallel. + * 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, only the staking balance and - * detection run; no network-backed source is used. + * @param options.isBasicFunctionality - When false, only the staking balance + * runs; no network-backed source is used. * @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 @@ -58,13 +56,12 @@ export function buildFastFetchSources( stakedBalanceDataSource, customAssetGraduationMiddleware, rpcFallbackMiddleware, - detectionMiddleware, tokenDataSource, priceDataSource, } = sources; if (!options.isBasicFunctionality) { - return [stakedBalanceDataSource, detectionMiddleware]; + return [stakedBalanceDataSource]; } return [ @@ -76,7 +73,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 4bf5248c390..1b21cbd31e5 100644 --- a/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildUpdateSources.test.ts @@ -15,7 +15,6 @@ function buildSources(): UpdateSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -24,53 +23,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 5696a0e5f3b..2b000bfe360 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 @@ -40,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, @@ -179,9 +178,6 @@ async function runWsUpdatePass({ getAssetsState: (): AssetsControllerState => state, }), rpcFallbackMiddleware, - detectionMiddleware: new DetectionMiddleware({ - getAssetsState: (): AssetsControllerState => state, - }), tokenDataSource, priceDataSource, }, @@ -293,15 +289,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]), @@ -363,10 +350,6 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { }); 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]), ); @@ -400,9 +383,10 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { ).toBeUndefined(); }); - it('does not re-price the already-priced token', () => { - expect(result.request.assetsForPriceUpdate ?? []).toStrictEqual([]); - expect(result.mocks.priceAPI.priceBatches).toStrictEqual([]); + it('refreshes the held token price through the freshness cache', () => { + expect(askedAbout(result.mocks.priceAPI.priceBatches)).toStrictEqual( + new Set([USDC_ASSET_ID_LOWERCASE]), + ); }); it('does not refetch metadata for the enriched token, only the native asset', () => { diff --git a/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts b/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts index 77cbf6b0ac6..6e40d2410fc 100644 --- a/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts +++ b/packages/assets-controller/src/pipeline/buildWsUpdateSources.test.ts @@ -22,7 +22,6 @@ function buildSources(): WsUpdateSources { 'CustomAssetGraduationMiddleware', ), rpcFallbackMiddleware: stubSource('RpcFallbackMiddleware'), - detectionMiddleware: stubSource('DetectionMiddleware'), tokenDataSource: stubTokenSource('TokenDataSource'), priceDataSource: stubSource('PriceDataSource'), }; @@ -31,39 +30,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..6b9ad6e2a01 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[]; }; /** diff --git a/tsc-suppressions.json b/tsc-suppressions.json index 8e5ebaa1b03..1393c161350 100644 --- a/tsc-suppressions.json +++ b/tsc-suppressions.json @@ -154,11 +154,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 From 21348990baac0116b082509241e13505bf8e431b Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Tue, 20 Oct 2026 22:33:58 +0200 Subject: [PATCH 02/14] clean code --- .../2026-10-20-remove-detection-middleware.md | 146 ++++++++++++++++++ packages/assets-controller/src/README.md | 7 +- .../src/pipeline/buildFastFetchSources.ts | 7 +- 3 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 packages/assets-controller/docs/decisions/2026-10-20-remove-detection-middleware.md 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..18f2c9ac11a --- /dev/null +++ b/packages/assets-controller/docs/decisions/2026-10-20-remove-detection-middleware.md @@ -0,0 +1,146 @@ +# Remove DetectionMiddleware + +- Date: 2026-10-20 +- Status: Accepted +- Package: `@metamask/assets-controller` + +`DetectionMiddleware` no longer builds a shared list for metadata and prices. +`TokenDataSource` and `PriceDataSource` each choose their own assets from the +request, controller state, and the current balance response. + +This is not RPC token detection. `TokenDetector` and `tokenDetectionEnabled` +still decide which ERC-20s a balance source reports. This note is only about +the enrichment step that used to sit between those balances and Token / Price. + +## What detection used to do + +`DetectionMiddleware` ran on the balance data type, before Token and Price, +and wrote two bags those sources then read: + +- `response.detectedAssets`: assets that were genuinely new. A balance on the + response was included only when it was absent from both `state.assetsBalance` + and `state.assetsInfo`. Custom assets were included on the same rule. Anything + already tracked was left out on purpose. The price subscription was supposed + to refresh those later. +- `request.assetsForPriceUpdate`: detected assets that had no price yet, plus + every balance on the response that still had no price (for example a native + seeded before the first price poll). + +Token and Price could not run until that list existed, and neither source +could see a tracked balance that was missing metadata, or a hidden asset that +should have been skipped. Callers that wanted prices for an explicit list +passed `assetsForPriceUpdate` into `getAssets`, which dragged the list through +the balance pipeline. + +## What replaced it + +Fast, update, and websocket lanes no longer insert a detection step. After +balances (and graduation / RPC fallback / the websocket occurrence filter, +where those still apply), Token and Price run in parallel. Each reads state +itself. + +An explicit price refresh is `AssetsController.syncAssetsPrices(assetIds)`. +It calls `PriceDataSource.fetchAssetPrices`, which invalidates the freshness +cache for those ids, and merges the result into `assetsPrice`. It does not +take an account or chain, and it does not run the balance pipeline. +`setSelectedCurrency` uses it after invalidating the cache, passing every +asset id currently in `assetsBalance`. + +```mermaid +flowchart LR + Bal["Balance sources"] --> Enrich + subgraph Enrich ["Parallel"] + direction TB + T["TokenDataSource
metadata for balances that need it"] + P["PriceDataSource
prices for state + response, minus hidden"] + end + Enrich --> State["Write state"] + Sync["syncAssetsPrices(assetIds)"] --> PriceOnly["PriceDataSource.fetchAssetPrices
bypass freshness cache"] --> State +``` + +## PriceDataSource + +The middleware runs when the request asks for `price`. + +Candidates, scoped to the request's accounts and chains: + +| Source | Account filter | Chain filter | +| --- | --- | --- | +| `state.assetsBalance` | Yes | Yes | +| `state.customAssets` | Yes | Yes | +| `state.assetsInfo` | No | Yes | +| `response.assetsBalance` | Yes | Yes | + +Then drop: + +- hidden assets (`assetPreferences.hidden`) +- non-priceable ids (`isPriceableAsset`: staking synthetics such as + `slip44:*-*`, and Tron bandwidth / energy resources) +- chains the price API does not support. An empty supported-network response + keeps the candidates rather than pricing nothing + +The remaining ids go through the price freshness cache (`DedupingBatchFetcher`, +TTL from `priceFreshnessTtlMs`, otherwise the poll interval). A price fetched +recently is not requested again. Concurrent callers share the in-flight +request. The subscription poll uses the same cache, so a middleware fetch and +a poll do not double-hit the API. + +`fetchAssetPrices` is the exception: it invalidates those keys first, then +fetches. That is the path `syncAssetsPrices` and a currency change need, +because the cached numbers are stale or in the wrong currency. + +## TokenDataSource + +The middleware runs when the request asks for `metadata`. + +`#collectMetadataAssetIds` builds two sets: + +- **Needs metadata.** A state balance or a response balance that has no image + in the response or in `state.assetsInfo`, is not a staking-contract id, and + is not hidden. State balances are also limited to the request's accounts and + chains. Response balances are not re-filtered by account or chain; they are + whatever the balance source just returned. +- **Balance heals.** State balances in the set above, plus response balances + that are already in `state.assetsBalance` or `state.assetsInfo`. A heal is + enriched and not spam-filtered. A response balance that is new to state is + spam-filtered. + +Native asset ids from `getNativeAssetIds()` are always added, even when no +balance mentioned them. + +Supported networks are applied before the Tokens API v3 call. After the +response: + +| Asset | Spam rule | +| --- | --- | +| Native | Always kept | +| Custom asset (user import) | Always kept | +| mUSD | Always kept | +| Balance heal | Kept, no occurrence filter | +| New EVM ERC-20 | Kept only when occurrences meet the per-chain suggested floor (default 3). Missing occurrence data counts as zero and is dropped | +| New non-EVM token | Blockaid scan | + +Fetched metadata is merged onto the response. A fetch failure logs and leaves +the response otherwise unchanged. + +## What changed for callers + +| Before | After | +| --- | --- | +| `getAssets` option `assetsForPriceUpdate` | `syncAssetsPrices(assetIds)` | +| Import `DetectionMiddleware` | Drop it. Token and Price select assets themselves | +| `FastFetchSources` / `UpdateSources` / `WsUpdateSources` take `detectionMiddleware` | Those types no longer have that field | +| Only brand-new assets (absent from balance and metadata) were enriched in the pipeline pass | Tracked balances that still lack metadata are healed. Prices cover the scoped state set plus the response, and the freshness cache skips recent ones | +| Hidden assets could still be queued | Hidden assets are excluded by both sources | + +## Where the code lives + +| Concern | Location | +| --- | --- | +| Explicit price refresh | `AssetsController.syncAssetsPrices` | +| Currency change | `AssetsController.setSelectedCurrency` | +| Price candidate set | `PriceDataSource.#getAssetIdsForPriceFetch` | +| Price freshness cache | `DedupingBatchFetcher` inside `PriceDataSource` | +| Metadata candidate set | `TokenDataSource.#collectMetadataAssetIds` | +| Metadata middleware | `TokenDataSource.assetsMiddleware` | +| Lane composition | `buildFastFetchSources`, `buildUpdateSources`, `buildWsUpdateSources` | diff --git a/packages/assets-controller/src/README.md b/packages/assets-controller/src/README.md index bdb93d89925..e5354fc4cb6 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 │ diff --git a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts index 4d6f0b910f9..0db4d1df178 100644 --- a/packages/assets-controller/src/pipeline/buildFastFetchSources.ts +++ b/packages/assets-controller/src/pipeline/buildFastFetchSources.ts @@ -31,10 +31,9 @@ export type FastFetchSources = { * * @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. + * @param options.isBasicFunctionality - When false, the lane is empty. No + * 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 From 0e1c36852648f8983893a010e5ca6629182e80db Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Wed, 21 Oct 2026 15:29:57 +0200 Subject: [PATCH 03/14] fix comments --- packages/assets-controller/CHANGELOG.md | 2 +- .../2026-10-20-remove-detection-middleware.md | 12 ++++---- .../AssetsController-method-action-types.ts | 15 ++++++++++ .../src/AssetsController.test.ts | 29 +++++++++++++++++++ .../assets-controller/src/AssetsController.ts | 12 +++++--- packages/assets-controller/src/index.ts | 1 + 6 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 9261feb3c9b..584b480cb58 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add `syncAssetsPrices(assetIds)`, which force-fetches spot prices for an explicit CAIP-19 asset list through `PriceDataSource` and merges them into state. Prices are shared, so the method takes no account or chain scope +- 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. Prices are shared, so the method takes no account or chain scope ### 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 index 640a28bf819..9f1faf93535 100644 --- 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 @@ -124,17 +124,17 @@ are. ## If you call this package -| You used to | Do this now | -| ----------------------------------------------------- | --------------------------------------------------------- | -| Pass `assetsForPriceUpdate` into `getAssets` | Call `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 | +| 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` | +| Refresh prices for a list of tokens | `AssetsController:syncAssetsPrices` | | User changed currency | `AssetsController.setSelectedCurrency` | | Which tokens get a price | `PriceDataSource` | | Which tokens get a name and icon | `TokenDataSource` | diff --git a/packages/assets-controller/src/AssetsController-method-action-types.ts b/packages/assets-controller/src/AssetsController-method-action-types.ts index 68590b2ce8a..0916525b02d 100644 --- a/packages/assets-controller/src/AssetsController-method-action-types.ts +++ b/packages/assets-controller/src/AssetsController-method-action-types.ts @@ -10,6 +10,20 @@ 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. + * + * @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 +214,7 @@ export type AssetsControllerSetSelectedCurrencyAction = { */ export type AssetsControllerMethodActions = | AssetsControllerGetAssetsAction + | AssetsControllerSyncAssetsPricesAction | AssetsControllerGetAssetsBalanceAction | AssetsControllerGetAssetMetadataAction | AssetsControllerGetAccountAssetByIDAction diff --git a/packages/assets-controller/src/AssetsController.test.ts b/packages/assets-controller/src/AssetsController.test.ts index 25f425bd214..7c66d1ec546 100644 --- a/packages/assets-controller/src/AssetsController.test.ts +++ b/packages/assets-controller/src/AssetsController.test.ts @@ -4111,6 +4111,35 @@ 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 = jest + .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(); + }); + }); + describe('setSelectedCurrency', () => { it('updates selectedCurrency in state', async () => { await withController(({ controller }) => { diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index 222e10d7e46..af2d02a1332 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -214,6 +214,7 @@ const MESSENGER_EXPOSED_METHODS = [ 'getAccountAssetsByIDs', 'getAccountAssetsByScope', 'getAssetsPrice', + 'syncAssetsPrices', 'getExchangeRatesForBridge', 'getStateForTransactionPay', 'addCustomAsset', @@ -1483,19 +1484,21 @@ export class AssetsController extends BaseController< */ async #runStartupRefresh(accounts: InternalAccount[]): Promise { const releaseLock = await this.#accountRefreshMutex.acquire(); - // Seed before the fetch so PriceDataSource includes controller-managed - // native and default tracked assets in the same price pass. - this.#ensureNativeBalancesDefaultZero(); - this.#ensureDefaultTrackedAssetsSeeded(); try { await this.getAssets(accounts, { chainIds: [...this.#enabledChains], forceUpdate: true, }); + // Seed before subscribe so the price poll / update fetch sees natives + // and default tracked assets that were never returned by balance APIs. + this.#ensureNativeBalancesDefaultZero(); + this.#ensureDefaultTrackedAssetsSeeded(); // Balances were just force-fetched — skip AccountsApi's subscribe-time poll. this.#subscribeAssets({ skipInitialFetch: true }); } catch (error) { log('Failed to fetch assets on startup', error); + this.#ensureNativeBalancesDefaultZero(); + this.#ensureDefaultTrackedAssetsSeeded(); this.#subscribeAssets({ skipInitialFetch: true }); } finally { releaseLock(); @@ -4494,6 +4497,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/index.ts b/packages/assets-controller/src/index.ts index 84cbea49b4d..008725ee3df 100644 --- a/packages/assets-controller/src/index.ts +++ b/packages/assets-controller/src/index.ts @@ -47,6 +47,7 @@ export type { AssetsControllerGetAccountAssetsByIDsAction, AssetsControllerGetAccountAssetsByScopeAction, AssetsControllerGetAssetsPriceAction, + AssetsControllerSyncAssetsPricesAction, AssetsControllerAddCustomAssetAction, AssetsControllerRemoveCustomAssetAction, AssetsControllerGetCustomAssetsAction, From 0ed714fc38acfbba6c697263adf13d02a3582600 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Wed, 21 Oct 2026 16:57:51 +0200 Subject: [PATCH 04/14] clean tests --- .../src/data-sources/PriceDataSource.test.ts | 202 ++++++++++++------ 1 file changed, 135 insertions(+), 67 deletions(-) diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index 4990809eaa9..ee42fe5cd03 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -1,6 +1,7 @@ import type { SupportedCurrency } from '@metamask/core-backend'; import type { InternalAccount } from '@metamask/keyring-internal-api'; +import { getDefaultAssetsControllerState } from '../AssetsController.js'; import type { ChainId, DataRequest, @@ -121,6 +122,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, @@ -277,21 +297,18 @@ describe('PriceDataSource', () => { await controller.assetsMiddleware(context, jest.fn()); - expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( - expect.arrayContaining([ - MOCK_NATIVE_ASSET, - normalizeAssetId(MOCK_TOKEN_ASSET), - ]), - expect.anything(), - ); - expect(apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]).toHaveLength(2); + const assetsCalled = apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]; + expect(assetsCalled).toStrictEqual([ + MOCK_NATIVE_ASSET, + normalizeAssetId(MOCK_TOKEN_ASSET), + ]); controller.destroy(); }); it('middleware excludes hidden assets from response and state balances', async () => { const getAssetsState = (): AssetsControllerState => - ({ + createTestAssetsState({ assetsBalance: { 'mock-account-id': { [MOCK_NATIVE_ASSET]: { amount: '1.5' }, @@ -301,7 +318,7 @@ describe('PriceDataSource', () => { assetPreferences: { [MOCK_TOKEN_ASSET]: { hidden: true }, }, - }) as unknown as AssetsControllerState; + }); const { controller, apiClient } = setupController({ getAssetsState, priceResponse: { @@ -328,59 +345,100 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware prices custom assets and metadata from state, excluding hidden assets', async () => { - const metadataOnlyAsset = - 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId; - const customAsset = - 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId; - const hiddenMetadataAsset = - 'eip155:1/erc20:0x3333333333333333333333333333333333333333' as Caip19AssetId; - const getAssetsState = (): AssetsControllerState => - ({ - assetsBalance: { - 'mock-account-id': { - [MOCK_NATIVE_ASSET]: { amount: '1' }, - }, - }, - assetsInfo: { - [metadataOnlyAsset]: { symbol: 'META' }, - [hiddenMetadataAsset]: { symbol: 'HIDE' }, - }, - customAssets: { - 'mock-account-id': [customAsset], - }, - assetPreferences: { - [hiddenMetadataAsset]: { hidden: true }, + it.each([ + { + source: 'balance', + hidden: false, + outcome: 'prices', + assetId: MOCK_NATIVE_ASSET, + }, + { + source: 'balance', + hidden: true, + outcome: 'skips', + assetId: MOCK_NATIVE_ASSET, + }, + { + source: 'metadata', + hidden: false, + outcome: 'prices', + assetId: + 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId, + }, + { + source: 'metadata', + hidden: true, + outcome: 'skips', + assetId: + 'eip155:1/erc20:0x3333333333333333333333333333333333333333' as Caip19AssetId, + }, + { + source: 'custom', + hidden: false, + outcome: 'prices', + assetId: + 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId, + }, + { + source: 'custom', + hidden: true, + outcome: 'skips', + assetId: + 'eip155:1/erc20:0x4444444444444444444444444444444444444444' as Caip19AssetId, + }, + ])( + 'middleware $outcome a $source asset when hidden is $hidden', + async ({ source, hidden, outcome, 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] } } + : {}), + ...(hidden + ? { assetPreferences: { [assetId]: { hidden: true } } } + : {}), + }), + priceResponse: { + [normalizeAssetId(assetId)]: createMockPriceData(1), }, - }) as unknown as AssetsControllerState; - const { controller, apiClient } = setupController({ - getAssetsState, - priceResponse: { - [MOCK_NATIVE_ASSET]: createMockPriceData(2500), - [metadataOnlyAsset]: createMockPriceData(1), - [customAsset]: createMockPriceData(2), - }, - }); + }); - await controller.assetsMiddleware( - createMiddlewareContext({ response: {} }), - jest.fn(), - ); + await controller.assetsMiddleware( + createMiddlewareContext({ response: {} }), + jest.fn(), + ); - expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith( - expect.arrayContaining([ - MOCK_NATIVE_ASSET, - normalizeAssetId(metadataOnlyAsset), - normalizeAssetId(customAsset), - ]), - expect.anything(), - ); - expect(apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]).not.toContain( - normalizeAssetId(hiddenMetadataAsset), - ); + if (outcome === 'prices') { + expect( + apiClient.prices.fetchV3SpotPrices.mock.calls[0][0], + ).toStrictEqual([normalizeAssetId(assetId)]); + } else { + expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); + } - controller.destroy(); - }); + controller.destroy(); + }, + ); it('fetch skips malformed asset IDs in balance state and still fetches prices for valid assets', async () => { const { controller, apiClient } = setupController({ @@ -1077,7 +1135,7 @@ describe('PriceDataSource', () => { 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' }, @@ -1085,9 +1143,14 @@ describe('PriceDataSource', () => { }, }, assetsPrice: { - [normalizeAssetId(MOCK_TOKEN_ASSET)]: { price: 1 }, + [normalizeAssetId(MOCK_TOKEN_ASSET)]: { + assetPriceType: 'fungible', + price: 1, + usdPrice: 1, + lastUpdated: 1, + }, }, - }) as unknown as AssetsControllerState, + }), priceResponse: { [MOCK_NATIVE_ASSET]: createMockPriceData(2500), }, @@ -1119,16 +1182,21 @@ describe('PriceDataSource', () => { 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]: { price: 1 }, + [MOCK_TOKEN_ASSET]: { + assetPriceType: 'fungible', + price: 1, + usdPrice: 1, + lastUpdated: 1, + }, }, - }) as unknown as AssetsControllerState, + }), priceResponse: { [MOCK_TOKEN_ASSET]: createMockPriceData(1.0), }, @@ -1490,13 +1558,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 unknown as AssetsControllerState, + }), }); await controller2.fetch(createDataRequest()); From c43bca46da4ab18e907d214df528a49fa395e1e6 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Thu, 22 Oct 2026 12:42:53 +0200 Subject: [PATCH 05/14] utils functions --- .../2026-10-20-remove-detection-middleware.md | 49 ++- .../src/data-sources/PriceDataSource.test.ts | 14 +- .../src/data-sources/PriceDataSource.ts | 106 +---- .../src/data-sources/TokenDataSource.ts | 113 +----- .../utils/getAssetIdsForMetadataFetch.test.ts | 374 ++++++++++++++++++ .../src/utils/getAssetIdsForMetadataFetch.ts | 163 ++++++++ .../utils/getAssetIdsForPriceFetch.test.ts | 331 ++++++++++++++++ .../src/utils/getAssetIdsForPriceFetch.ts | 181 +++++++++ 8 files changed, 1112 insertions(+), 219 deletions(-) create mode 100644 packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts create mode 100644 packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts create mode 100644 packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts create mode 100644 packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts 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 index 9f1faf93535..57f743f5cce 100644 --- 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 @@ -1,4 +1,4 @@ -# How the wallet picks token names and prices +# How the wallet fetch and picks token metadata and prices - Date: 2026-10-20 - Status: Accepted @@ -19,15 +19,15 @@ after a balance has already been found. ## Words used below -| Word | Meaning | -| ------------- | ---------------------------------------------------------------------------------------------------------------- | -| Balance | How much of a token an account holds. | -| Name and icon | Symbol, name, decimals, and image. Stored as metadata. | -| 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. | +| Word | Meaning | +| -------------- | ---------------------------------------------------------------------------------------------------------------- | +| Balance | How much of a token an account holds. | +| Token metadata | Symbol, name, decimals, and image. | +| 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 @@ -44,7 +44,7 @@ together. ```mermaid flowchart LR Bal["Fetch balances"] --> Det["Detection
one list of new tokens"] - Det --> Names["Names and icons
only tokens on that list"] + 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 @@ -57,7 +57,7 @@ each one chooses its own tokens. Hidden tokens are skipped by both. ```mermaid flowchart LR - Bal["Fetch balances"] --> Names["Names and icons
tokens that still need them"] + Bal["Fetch balances"] --> Names["Token metadata
tokens that still need them"] Bal --> Prices["Prices
only tokens that
still have no price"] Names --> Save["Save"] Prices --> Save @@ -100,7 +100,7 @@ Changing the display currency is the exception. Those prices are in the wrong currency, so the cache is cleared and every token the user holds is priced again. -## Names and icons +## Token metadata A name and icon are fetched when we do not already have an image for the token. That includes tokens the user already holds, and tokens that just arrived in @@ -122,6 +122,27 @@ appeared for the first time is checked so spam does not land in the wallet: If the name-and-icon 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 have no image remain candidates for name and icon fetching, 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 and price services. Held tokens that are missing a + name or a price are now asked for on every refresh. A token the service has + no image or price for is asked for again each time. The one-minute price + cache and the shared in-flight request limit this, but do not remove it. +- 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 | @@ -137,5 +158,5 @@ are. | Refresh prices for a list of tokens | `AssetsController:syncAssetsPrices` | | User changed currency | `AssetsController.setSelectedCurrency` | | Which tokens get a price | `PriceDataSource` | -| Which tokens get a name and icon | `TokenDataSource` | +| Which tokens get token metadata | `TokenDataSource` | | Order of a refresh | `buildFastFetchSources`, `buildUpdateSources`, `buildWsUpdateSources` | diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index ee42fe5cd03..45dd740b526 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -428,13 +428,13 @@ describe('PriceDataSource', () => { jest.fn(), ); - if (outcome === 'prices') { - expect( - apiClient.prices.fetchV3SpotPrices.mock.calls[0][0], - ).toStrictEqual([normalizeAssetId(assetId)]); - } else { - expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); - } + const expectedCalls = + outcome === 'prices' ? [[normalizeAssetId(assetId)]] : []; + expect( + apiClient.prices.fetchV3SpotPrices.mock.calls.map( + ([assetIds]) => assetIds, + ), + ).toStrictEqual(expectedCalls); controller.destroy(); }, diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index e744c009913..1a889847172 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -17,7 +17,8 @@ import type { AssetsControllerState, } from '../types.js'; import { DedupingBatchFetcher } from '../utils/dedupingBatchFetcher.js'; -import { fetchWithTimeout, safeNormalizeAssetId } from '../utils/index.js'; +import { getAssetIdsForPriceFetch } from '../utils/getAssetIdsForPriceFetch.js'; +import { fetchWithTimeout } from '../utils/index.js'; import type { SubscriptionRequest } from './AbstractDataSource.js'; import { reduceInBatchesSerially } from './evm-rpc-services/index.js'; @@ -241,7 +242,11 @@ export class PriceDataSource { get assetsMiddleware(): Middleware { return forDataTypes(['price'], async (ctx, next) => { const { response, request } = ctx; - const assetIds = this.#getAssetIdsForPriceFetch(request, response); + const assetIds = getAssetIdsForPriceFetch({ + state: this.#getAssetsState(), + request, + response, + }); if (assetIds.size === 0) { return next(ctx); @@ -452,8 +457,10 @@ export class PriceDataSource { #getAssetIdsFromBalanceState(request: DataRequest): Caip19AssetId[] { try { return [ - ...this.#getAssetIdsForPriceFetch(request, { - assetsBalance: {}, + ...getAssetIdsForPriceFetch({ + state: this.#getAssetsState(), + request, + response: { assetsBalance: {} }, }), ]; } catch (error) { @@ -462,99 +469,10 @@ export class PriceDataSource { } } - /** - * Assets to price for this request: custom assets, balances, and metadata - * already in state, plus balances on the current response, excluding hidden - * assets. Scoped to the request's accounts and chains. - * - * @param request - Account and chain scope for the fetch. - * @param response - Current pipeline response. - * @returns Deduplicated, normalized CAIP-19 asset IDs. - */ - #getAssetIdsForPriceFetch( - request: DataRequest, - response: Pick, - ): Set { - const state = this.#getAssetsState(); - const accountIds = request.accountsWithSupportedChains.map( - ({ account }) => account.id, - ); - const accountFilter = - accountIds.length > 0 ? new Set(accountIds) : undefined; - const chainFilter = - request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; - const hiddenAssetIds = new Set( - Object.entries(state.assetPreferences ?? {}) - .filter(([, preferences]) => preferences.hidden) - .map(([assetId]) => - safeNormalizeAssetId(assetId as Caip19AssetId).toLowerCase(), - ), - ); - const assetIds = new Map(); - - const addAssetId = (assetId: string): void => { - try { - const normalizedAssetId = safeNormalizeAssetId( - assetId as Caip19AssetId, - ); - const { chainId } = parseCaipAssetType(normalizedAssetId); - const key = normalizedAssetId.toLowerCase(); - if ( - (chainFilter && !chainFilter.has(chainId)) || - hiddenAssetIds.has(key) - ) { - return; - } - assetIds.set(key, normalizedAssetId); - } catch (error) { - log('Skipping malformed asset ID for price fetch', { assetId, error }); - } - }; - - const addBalances = ( - balances: DataResponse['assetsBalance'] | undefined, - applyAccountFilter: boolean, - ): void => { - for (const [accountId, accountBalances] of Object.entries( - balances ?? {}, - )) { - if ( - applyAccountFilter && - accountFilter && - !accountFilter.has(accountId) - ) { - continue; - } - - for (const assetId of Object.keys(accountBalances)) { - addAssetId(assetId); - } - } - }; - - addBalances(state.assetsBalance, true); - for (const [accountId, customAssetIds] of Object.entries( - state.customAssets ?? {}, - )) { - if (accountFilter && !accountFilter.has(accountId)) { - continue; - } - for (const assetId of customAssetIds) { - addAssetId(assetId); - } - } - for (const assetId of Object.keys(state.assetsInfo ?? {})) { - addAssetId(assetId); - } - addBalances(response.assetsBalance, true); - - return new Set(assetIds.values()); - } - /** * Keep asset IDs that have no stored price. * - * Callers pass ids already normalized by `#getAssetIdsForPriceFetch`. A + * Callers pass ids already normalized by `getAssetIdsForPriceFetch`. A * lower-case id stored by the price API does not match that checksummed id. * * The price poll and `fetchAssetPrices` update prices that are already stored. diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index 379591be3fb..adcaf0250d7 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -17,14 +17,11 @@ import type { AssetsControllerState, Middleware, FungibleAssetMetadata, - DataRequest, 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 @@ -525,103 +522,6 @@ export class TokenDataSource { }); } - /** - * Metadata to fetch for this request. - * - * State balances that have no metadata yet are heals: they are enriched and - * not spam-filtered. 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 request - Account and chain scope. - * @param response - Current pipeline response. - * @returns Asset IDs to fetch, and the subset that must skip spam filtering. - */ - #collectMetadataAssetIds( - request: DataRequest, - response: DataResponse, - ): { - assetIdsNeedingMetadata: Set; - balanceHealAssetIds: Set; - } { - const state = this.#getAssetsState(); - const stateMetadata = state.assetsInfo ?? {}; - const accountIds = request.accountsWithSupportedChains.map( - ({ account }) => account.id, - ); - const accountFilter = - accountIds.length > 0 ? new Set(accountIds) : undefined; - const chainFilter = - request.chainIds.length > 0 ? new Set(request.chainIds) : undefined; - const hiddenAssetIds = new Set( - Object.entries(state.assetPreferences ?? {}) - .filter(([, preferences]) => preferences.hidden) - .map(([assetId]) => assetId.toLowerCase()), - ); - 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 (response.assetsInfo?.[assetId as Caip19AssetId]?.image) { - return false; - } - if (stateMetadata[assetId as Caip19AssetId]?.image) { - 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 }; - } - /** * Get the middleware for enriching responses with token metadata. * @@ -638,9 +538,14 @@ export class TokenDataSource { get assetsMiddleware(): Middleware { return forDataTypes(['metadata'], async (ctx, next) => { const { request, response } = ctx; - const { customAssets } = this.#getAssetsState(); + const assetsState = this.#getAssetsState(); + const { customAssets } = assetsState; const { assetIdsNeedingMetadata, balanceHealAssetIds } = - this.#collectMetadataAssetIds(request, response); + 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 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..602e60be8f5 --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts @@ -0,0 +1,374 @@ +import type { InternalAccount } from '@metamask/keyring-internal-api'; + +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 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 that already have an image in state metadata', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + assetsInfo: { [TOKEN_MAINNET]: METADATA_WITH_IMAGE }, + }), + request: createRequest(), + response: {}, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + it('excludes assets that already have an image in response metadata', () => { + const result = getAssetIdsForMetadataFetch({ + state: emptyState(), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + assetsInfo: { [TOKEN_MAINNET]: METADATA_WITH_IMAGE }, + }, + }); + + expect(sortedIds(result.assetIdsNeedingMetadata)).toStrictEqual([]); + expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); + }); + + 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..2f836e6af2a --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts @@ -0,0 +1,163 @@ +import { parseCaipAssetType } from '@metamask/utils'; + +import { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js'; +import type { + 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 that have no metadata yet are heals: they are enriched and + * not spam-filtered. 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 (response.assetsInfo?.[assetId as Caip19AssetId]?.image) { + return false; + } + if (stateMetadata[assetId as Caip19AssetId]?.image) { + 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 }; +} + +/** + * 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..74c5b1b97bb --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts @@ -0,0 +1,331 @@ +import type { InternalAccount } from '@metamask/keyring-internal-api'; + +import type { + AssetBalance, + AssetsControllerState, + Caip19AssetId, + ChainId, + DataRequest, + FungibleAssetMetadata, +} from '../types.js'; +import { getAssetIdsForPriceFetch } 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 }, + }, + }), + response: {}, + expected: [NATIVE_MAINNET], + }, + { + name: 'response balances in account/chain scope', + state: emptyState(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + expected: [normalizeAssetId(TOKEN_MAINNET)], + }, + { + name: 'custom assets in account/chain scope', + state: emptyState({ + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + }), + response: {}, + expected: [normalizeAssetId(CUSTOM_MAINNET)], + }, + { + name: 'assetsInfo metadata in chain scope', + state: emptyState({ + assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, + }), + response: {}, + expected: [normalizeAssetId(METADATA_MAINNET)], + }, + { + name: 'union of state balance, response balance, custom, and metadata', + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, + }, + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, + }), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + expected: [ + NATIVE_MAINNET, + normalizeAssetId(CUSTOM_MAINNET), + normalizeAssetId(METADATA_MAINNET), + normalizeAssetId(TOKEN_MAINNET), + ].sort(), + }, + ])('includes $name', ({ state, response, expected }) => { + const result = getAssetIdsForPriceFetch({ + state, + request: createRequest(), + response, + }); + + 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 } }, + }), + response: {}, + expected: [NATIVE_MAINNET], + }, + { + name: 'hidden response balance', + state: emptyState({ + assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, + }), + response: { + assetsBalance: { + [ACCOUNT_A]: { + [NATIVE_MAINNET]: ZERO_BALANCE, + [TOKEN_MAINNET]: ZERO_BALANCE, + }, + }, + }, + expected: [NATIVE_MAINNET], + }, + { + name: 'hidden custom asset', + state: emptyState({ + customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, + assetPreferences: { [CUSTOM_MAINNET]: { hidden: true } }, + }), + response: {}, + expected: [], + }, + { + name: 'hidden metadata asset', + state: emptyState({ + assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, + assetPreferences: { [METADATA_MAINNET]: { hidden: true } }, + }), + response: {}, + expected: [], + }, + ])('excludes $name', ({ state, response, expected }) => { + const result = getAssetIdsForPriceFetch({ + state, + request: createRequest(), + response, + }); + + 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] }), + response: { + assetsBalance: { + [ACCOUNT_B]: { [TOKEN_POLYGON]: ZERO_BALANCE }, + }, + }, + }); + + 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] }), + response: { + assetsBalance: { + [ACCOUNT_A]: { + [TOKEN_MAINNET]: ZERO_BALANCE, + [TOKEN_POLYGON]: ZERO_BALANCE, + }, + }, + }, + }); + + expect(sortedIds(result)).toStrictEqual( + [ + NATIVE_MAINNET, + normalizeAssetId(CUSTOM_MAINNET), + normalizeAssetId(METADATA_MAINNET), + normalizeAssetId(TOKEN_MAINNET), + ].sort(), + ); + }); + + it('deduplicates the same asset across state, response, custom, and metadata', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState({ + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + customAssets: { [ACCOUNT_A]: [TOKEN_MAINNET] }, + assetsInfo: { [TOKEN_MAINNET]: TOKEN_METADATA }, + }), + request: createRequest(), + response: { + assetsBalance: { + [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, + }, + }, + }); + + 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(), + response: {}, + }); + + expect([...result]).toStrictEqual([NATIVE_MAINNET]); + }); + + it('returns an empty set when no candidates are in scope', () => { + const result = getAssetIdsForPriceFetch({ + state: emptyState(), + request: createRequest(), + response: {}, + }); + + 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..940205be762 --- /dev/null +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts @@ -0,0 +1,181 @@ +import { parseCaipAssetType } from '@metamask/utils'; + +import { projectLogger, createModuleLogger } from '../logger.js'; +import type { + AssetsControllerState, + Caip19AssetId, + DataRequest, + DataResponse, +} from '../types.js'; +import { safeNormalizeAssetId } from './normalizeAssetId.js'; + +const log = createModuleLogger(projectLogger, 'getAssetIdsForPriceFetch'); + +export type GetAssetIdsForPriceFetchParams = { + /** Current AssetsController state slices used for candidate collection. */ + state: Pick< + AssetsControllerState, + 'assetsBalance' | 'customAssets' | 'assetsInfo' | 'assetPreferences' + >; + /** Account and chain scope for the fetch. */ + request: Pick; + /** Current pipeline response balances to include. */ + response: Pick; +}; + +/** + * Assets to price for this request: custom assets, balances, and metadata + * already in state, plus balances on the current response, excluding hidden + * assets. Scoped to the request's accounts and chains. + * + * @param params - State, request scope, and response balances. + * @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 to include. + * @returns Deduplicated, normalized CAIP-19 asset IDs. + */ +export function getAssetIdsForPriceFetch({ + state, + request, + response, +}: GetAssetIdsForPriceFetchParams): Set { + const accountFilter = buildAccountFilter(request); + const chainFilter = buildChainFilter(request); + const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); + const assetIds = new Map(); + + const addAssetId = (assetId: string): void => { + addNormalizedAssetId(assetId, assetIds, chainFilter, hiddenAssetIds); + }; + + addBalancesFromAccounts(state.assetsBalance, accountFilter, addAssetId); + addCustomAssets(state.customAssets, accountFilter, addAssetId); + for (const assetId of Object.keys(state.assetsInfo ?? {})) { + addAssetId(assetId); + } + addBalancesFromAccounts(response.assetsBalance, accountFilter, addAssetId); + + return new Set(assetIds.values()); +} + +/** + * 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 normalized keys of assets the user has marked hidden. + * + * @param assetPreferences - Preference map from controller state. + * @returns Set of lower-case normalized CAIP-19 keys. + */ +function collectHiddenAssetIdKeys( + assetPreferences: AssetsControllerState['assetPreferences'] | undefined, +): Set { + return new Set( + Object.entries(assetPreferences ?? {}) + .filter(([, preferences]) => preferences.hidden) + .map(([assetId]) => + safeNormalizeAssetId(assetId as Caip19AssetId).toLowerCase(), + ), + ); +} + +/** + * Normalize `assetId`, apply chain/hidden filters, and store under a + * case-insensitive key. Malformed IDs are skipped. + * + * @param assetId - Candidate CAIP-19 asset ID. + * @param assetIds - Output map (lower-case key → normalized ID). + * @param chainFilter - Optional allowed chain IDs. + * @param hiddenAssetIds - Lower-case keys of hidden assets. + */ +function addNormalizedAssetId( + assetId: string, + assetIds: Map, + chainFilter: Set | undefined, + hiddenAssetIds: Set, +): void { + try { + const normalizedAssetId = safeNormalizeAssetId(assetId as Caip19AssetId); + const { chainId } = parseCaipAssetType(normalizedAssetId); + const key = normalizedAssetId.toLowerCase(); + if ((chainFilter && !chainFilter.has(chainId)) || hiddenAssetIds.has(key)) { + return; + } + assetIds.set(key, normalizedAssetId); + } 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); + } + } +} From 7acf6a45f90636ee27e9b3cd8b69a6667fac293f Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Thu, 22 Oct 2026 18:49:54 +0200 Subject: [PATCH 06/14] clean price --- packages/assets-controller/CHANGELOG.md | 7 +- .../2026-10-20-remove-detection-middleware.md | 30 ++- .../src/AssetsController.test.ts | 27 +++ .../assets-controller/src/AssetsController.ts | 6 +- .../src/data-sources/PriceDataSource.test.ts | 55 +---- .../src/data-sources/PriceDataSource.ts | 141 +++--------- .../utils/getAssetIdsForPriceFetch.test.ts | 160 +++++++------ .../src/utils/getAssetIdsForPriceFetch.ts | 212 +++++++++++++++--- packages/assets-controller/src/utils/index.ts | 1 + .../src/utils/normalizeAssetId.test.ts | 7 + .../src/utils/normalizeAssetId.ts | 20 ++ 11 files changed, 395 insertions(+), 271 deletions(-) diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 584b480cb58..8fc876387d8 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -13,13 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- **BREAKING:** Remove `DetectionMiddleware`. A balance refresh prices custom assets, balances, and metadata already in state plus balances on the current response only when they have no stored price, and excludes hidden assets. The price poll still refreshes stored prices. `TokenDataSource` fetches metadata for state balances that have none and for response balances, and excludes hidden assets +- **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 and prices held balances, custom assets, and metadata that this response did not include. `TokenDataSource` fetches metadata for state balances that have none and for response balances, and excludes hidden assets - **BREAKING:** Remove `assetsForPriceUpdate` from `getAssets` options. Refresh an explicit asset list with `syncAssetsPrices` instead - A balance refresh skips tokens that already have a stored price. The price poll uses the freshness cache so a recent price is not requested again - - 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)) +### Fixed + +- `syncAssetsPrices` returns without calling the Price API when basic functionality is off + ## [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 index 57f743f5cce..a36ce645ecd 100644 --- 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 @@ -58,7 +58,7 @@ 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
only tokens that
still have no price"] + Bal --> Prices["Prices
response tokens that
still have no price"] Names --> Save["Save"] Prices --> Save ``` @@ -75,18 +75,15 @@ flowchart LR ## Prices -A balance refresh fetches a price only when the wallet does not have one yet, -and the token is any of these, on the accounts and chains this refresh asked -about: +A balance refresh fetches a price only for tokens on that response that the +wallet does not have a price for yet, on the accounts and chains this refresh +asked about. -- A token the user already holds -- A token the user added by hand -- A token we already have a name or icon for -- A token that just came back in this refresh's balances - -A token that already has a price is left alone. The separate price poll updates -those. A caller that names specific tokens uses `syncAssetsPrices`, which -fetches them again even when a price is already stored. +A token that already has a price is left alone. The price poll updates those, +and it also prices held tokens, custom tokens, and tokens we already have +metadata for when this response did not include them. A caller that names +specific tokens uses `syncAssetsPrices`, which fetches them again even when a +price is already stored. Hidden tokens are skipped. So are tokens that cannot have a market price, such as staking placeholders and Tron bandwidth or energy. Tokens on a chain the @@ -136,10 +133,11 @@ What we gain: What it costs: -- More calls to the token and price services. Held tokens that are missing a - name or a price are now asked for on every refresh. A token the service has - no image or price for is asked for again each time. The one-minute price - cache and the shared in-flight request limit this, but do not remove it. +- More calls to the token service. Held tokens that are missing a name or icon + are asked for on every refresh. A token the service has no image 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. diff --git a/packages/assets-controller/src/AssetsController.test.ts b/packages/assets-controller/src/AssetsController.test.ts index 7c66d1ec546..084f83aef24 100644 --- a/packages/assets-controller/src/AssetsController.test.ts +++ b/packages/assets-controller/src/AssetsController.test.ts @@ -4138,6 +4138,33 @@ describe('AssetsController', () => { fetchSpy.mockRestore(); }); + + it('returns without fetching prices when basic functionality is off', async () => { + const fetchSpy = jest + .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', () => { diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index af2d02a1332..25cfb1f19bc 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -1658,10 +1658,14 @@ export class AssetsController extends BaseController< * 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 (assetIds.length === 0) { + if (!this.#isBasicFunctionality() || assetIds.length === 0) { return; } diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index 45dd740b526..aa6c0cde886 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -273,7 +273,7 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware prices the union of response and state balance assets', async () => { + it('middleware prices response balances and leaves state-only balances to the poll', async () => { const { controller, apiClient } = setupController({ balanceState: { 'mock-account-id': { @@ -281,7 +281,6 @@ describe('PriceDataSource', () => { }, }, priceResponse: { - [MOCK_NATIVE_ASSET]: createMockPriceData(2500), [MOCK_TOKEN_ASSET]: createMockPriceData(1), }, }); @@ -298,21 +297,17 @@ describe('PriceDataSource', () => { await controller.assetsMiddleware(context, jest.fn()); const assetsCalled = apiClient.prices.fetchV3SpotPrices.mock.calls[0][0]; - expect(assetsCalled).toStrictEqual([ - MOCK_NATIVE_ASSET, - normalizeAssetId(MOCK_TOKEN_ASSET), - ]); + expect(assetsCalled).toStrictEqual([normalizeAssetId(MOCK_TOKEN_ASSET)]); controller.destroy(); }); - it('middleware excludes hidden assets from response and state balances', async () => { + it('middleware excludes hidden assets on the response', async () => { const getAssetsState = (): AssetsControllerState => createTestAssetsState({ assetsBalance: { 'mock-account-id': { [MOCK_NATIVE_ASSET]: { amount: '1.5' }, - [MOCK_TOKEN_ASSET]: { amount: '10' }, }, }, assetPreferences: { @@ -329,6 +324,7 @@ describe('PriceDataSource', () => { response: { assetsBalance: { 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1.5' }, [MOCK_TOKEN_ASSET]: { amount: '10' }, }, }, @@ -348,47 +344,21 @@ describe('PriceDataSource', () => { it.each([ { source: 'balance', - hidden: false, - outcome: 'prices', - assetId: MOCK_NATIVE_ASSET, - }, - { - source: 'balance', - hidden: true, - outcome: 'skips', assetId: MOCK_NATIVE_ASSET, }, { source: 'metadata', - hidden: false, - outcome: 'prices', assetId: 'eip155:1/erc20:0x1111111111111111111111111111111111111111' as Caip19AssetId, }, - { - source: 'metadata', - hidden: true, - outcome: 'skips', - assetId: - 'eip155:1/erc20:0x3333333333333333333333333333333333333333' as Caip19AssetId, - }, { source: 'custom', - hidden: false, - outcome: 'prices', assetId: 'eip155:1/erc20:0x2222222222222222222222222222222222222222' as Caip19AssetId, }, - { - source: 'custom', - hidden: true, - outcome: 'skips', - assetId: - 'eip155:1/erc20:0x4444444444444444444444444444444444444444' as Caip19AssetId, - }, ])( - 'middleware $outcome a $source asset when hidden is $hidden', - async ({ source, hidden, outcome, assetId }) => { + 'middleware does not price a state-only $source asset', + async ({ source, assetId }) => { const { controller, apiClient } = setupController({ getAssetsState: () => createTestAssetsState({ @@ -414,12 +384,9 @@ describe('PriceDataSource', () => { ...(source === 'custom' ? { customAssets: { 'mock-account-id': [assetId] } } : {}), - ...(hidden - ? { assetPreferences: { [assetId]: { hidden: true } } } - : {}), }), priceResponse: { - [normalizeAssetId(assetId)]: createMockPriceData(1), + [assetId]: createMockPriceData(1), }, }); @@ -428,13 +395,7 @@ describe('PriceDataSource', () => { jest.fn(), ); - const expectedCalls = - outcome === 'prices' ? [[normalizeAssetId(assetId)]] : []; - expect( - apiClient.prices.fetchV3SpotPrices.mock.calls.map( - ([assetIds]) => assetIds, - ), - ).toStrictEqual(expectedCalls); + expect(apiClient.prices.fetchV3SpotPrices).not.toHaveBeenCalled(); controller.destroy(); }, diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index 1a889847172..4b31646c5d8 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,12 @@ import type { AssetsControllerState, } from '../types.js'; import { DedupingBatchFetcher } from '../utils/dedupingBatchFetcher.js'; -import { getAssetIdsForPriceFetch } from '../utils/getAssetIdsForPriceFetch.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'; @@ -114,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< @@ -232,45 +213,24 @@ export class PriceDataSource { * Get the middleware for enriching responses with price data. * * This middleware: - * 1. Collects custom assets, balances, and metadata from state, plus response balances - * 2. Excludes hidden, non-priceable, unsupported, and already-priced assets - * 3. Fetches the remaining prices, skipping any asked for within the freshness window - * 4. Enriches the response and continues the middleware chain + * 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) => { const { response, request } = ctx; - const assetIds = getAssetIdsForPriceFetch({ + const unpricedAssetIds = await getUnpricedResponseAssetIds({ state: this.#getAssetsState(), - request, response, + getSupportedNetworks: () => this.#getSupportedNetworks(), }); - - 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) { - return next(ctx); - } - - const unpricedAssetIds = - this.#filterAssetsMissingPrice(supportedAssetIds); if (unpricedAssetIds.length === 0) { return next(ctx); } @@ -303,16 +263,8 @@ export class PriceDataSource { async fetchAssetPrices( assetIds: Caip19AssetId[], ): Promise> { - const priceableAssetIds = assetIds.filter(isPriceableAsset); - if (priceableAssetIds.length === 0) { - return {}; - } - - const supportedNetworks = await this.#getSupportedNetworks(); - const supportedAssetIds = this.#filterAssetsByNetwork( - priceableAssetIds, - supportedNetworks, - ); + const supportedAssetIds = + await this.#filterToSupportedPriceableAssets(assetIds); if (supportedAssetIds.length === 0) { return {}; } @@ -460,7 +412,6 @@ export class PriceDataSource { ...getAssetIdsForPriceFetch({ state: this.#getAssetsState(), request, - response: { assetsBalance: {} }, }), ]; } catch (error) { @@ -470,20 +421,22 @@ export class PriceDataSource { } /** - * Keep asset IDs that have no stored price. - * - * Callers pass ids already normalized by `getAssetIdsForPriceFetch`. A - * lower-case id stored by the price API does not match that checksummed id. + * Drop non-priceable assets, then assets on chains the price API does not + * support. An empty supported-network set keeps every priceable asset. * - * The price poll and `fetchAssetPrices` update prices that are already stored. - * - * @param assetIds - Normalized CAIP-19 asset IDs. - * @returns IDs with no entry in `assetsPrice` under that id. + * @param assetIds - Candidate CAIP-19 asset IDs. + * @returns Priceable IDs on a supported chain. */ - #filterAssetsMissingPrice(assetIds: Caip19AssetId[]): Caip19AssetId[] { - const storedPrices = this.#getAssetsState().assetsPrice ?? {}; + async #filterToSupportedPriceableAssets( + assetIds: Caip19AssetId[], + ): Promise { + const priceableAssetIds = assetIds.filter(isPriceableAsset); + if (priceableAssetIds.length === 0) { + return []; + } - return assetIds.filter((assetId) => storedPrices[assetId] === undefined); + const supportedNetworks = await this.#getSupportedNetworks(); + return this.#filterAssetsByNetwork(priceableAssetIds, supportedNetworks); } /** @@ -518,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); } // ============================================================================ @@ -548,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/utils/getAssetIdsForPriceFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts index 74c5b1b97bb..d7be0599acd 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts @@ -8,7 +8,11 @@ import type { DataRequest, FungibleAssetMetadata, } from '../types.js'; -import { getAssetIdsForPriceFetch } from './getAssetIdsForPriceFetch.js'; +import { + getAssetIdsForPriceFetch, + getResponseAssetIdsForPriceFetch, + getUnpricedResponseAssetIds, +} from './getAssetIdsForPriceFetch.js'; import { normalizeAssetId } from './normalizeAssetId.js'; const CHAIN_MAINNET = 'eip155:1' as ChainId; @@ -101,25 +105,13 @@ describe('getAssetIdsForPriceFetch', () => { [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, }, }), - response: {}, expected: [NATIVE_MAINNET], }, - { - name: 'response balances in account/chain scope', - state: emptyState(), - response: { - assetsBalance: { - [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, - }, - }, - expected: [normalizeAssetId(TOKEN_MAINNET)], - }, { name: 'custom assets in account/chain scope', state: emptyState({ customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, }), - response: {}, expected: [normalizeAssetId(CUSTOM_MAINNET)], }, { @@ -127,11 +119,10 @@ describe('getAssetIdsForPriceFetch', () => { state: emptyState({ assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, }), - response: {}, expected: [normalizeAssetId(METADATA_MAINNET)], }, { - name: 'union of state balance, response balance, custom, and metadata', + name: 'union of state balance, custom, and metadata', state: emptyState({ assetsBalance: { [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, @@ -139,23 +130,16 @@ describe('getAssetIdsForPriceFetch', () => { customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, }), - response: { - assetsBalance: { - [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, - }, - }, expected: [ NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET), normalizeAssetId(METADATA_MAINNET), - normalizeAssetId(TOKEN_MAINNET), ].sort(), }, - ])('includes $name', ({ state, response, expected }) => { + ])('includes $name', ({ state, expected }) => { const result = getAssetIdsForPriceFetch({ state, request: createRequest(), - response, }); expect(sortedIds(result)).toStrictEqual([...expected].sort()); @@ -173,22 +157,6 @@ describe('getAssetIdsForPriceFetch', () => { }, assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, }), - response: {}, - expected: [NATIVE_MAINNET], - }, - { - name: 'hidden response balance', - state: emptyState({ - assetPreferences: { [TOKEN_MAINNET]: { hidden: true } }, - }), - response: { - assetsBalance: { - [ACCOUNT_A]: { - [NATIVE_MAINNET]: ZERO_BALANCE, - [TOKEN_MAINNET]: ZERO_BALANCE, - }, - }, - }, expected: [NATIVE_MAINNET], }, { @@ -197,7 +165,6 @@ describe('getAssetIdsForPriceFetch', () => { customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, assetPreferences: { [CUSTOM_MAINNET]: { hidden: true } }, }), - response: {}, expected: [], }, { @@ -206,14 +173,12 @@ describe('getAssetIdsForPriceFetch', () => { assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, assetPreferences: { [METADATA_MAINNET]: { hidden: true } }, }), - response: {}, expected: [], }, - ])('excludes $name', ({ state, response, expected }) => { + ])('excludes $name', ({ state, expected }) => { const result = getAssetIdsForPriceFetch({ state, request: createRequest(), - response, }); expect(sortedIds(result)).toStrictEqual(expected); @@ -232,11 +197,6 @@ describe('getAssetIdsForPriceFetch', () => { }, }), request: createRequest({ accountIds: [ACCOUNT_A] }), - response: { - assetsBalance: { - [ACCOUNT_B]: { [TOKEN_POLYGON]: ZERO_BALANCE }, - }, - }, }); expect(sortedIds(result)).toStrictEqual( @@ -262,14 +222,6 @@ describe('getAssetIdsForPriceFetch', () => { }, }), request: createRequest({ chainIds: [CHAIN_MAINNET] }), - response: { - assetsBalance: { - [ACCOUNT_A]: { - [TOKEN_MAINNET]: ZERO_BALANCE, - [TOKEN_POLYGON]: ZERO_BALANCE, - }, - }, - }, }); expect(sortedIds(result)).toStrictEqual( @@ -277,12 +229,11 @@ describe('getAssetIdsForPriceFetch', () => { NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET), normalizeAssetId(METADATA_MAINNET), - normalizeAssetId(TOKEN_MAINNET), ].sort(), ); }); - it('deduplicates the same asset across state, response, custom, and metadata', () => { + it('deduplicates the same asset across state balance, custom, and metadata', () => { const result = getAssetIdsForPriceFetch({ state: emptyState({ assetsBalance: { @@ -292,11 +243,6 @@ describe('getAssetIdsForPriceFetch', () => { assetsInfo: { [TOKEN_MAINNET]: TOKEN_METADATA }, }), request: createRequest(), - response: { - assetsBalance: { - [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, - }, - }, }); expect([...result]).toStrictEqual([normalizeAssetId(TOKEN_MAINNET)]); @@ -313,17 +259,101 @@ describe('getAssetIdsForPriceFetch', () => { }, }), request: createRequest(), - response: {}, }); 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 = jest.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 = jest.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(), - response: {}, }); expect([...result]).toStrictEqual([]); diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts index 940205be762..7d36c84d897 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts @@ -7,10 +7,33 @@ import type { DataRequest, DataResponse, } from '../types.js'; -import { safeNormalizeAssetId } from './normalizeAssetId.js'; +import { tryNormalizeAssetId } 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< @@ -19,33 +42,31 @@ export type GetAssetIdsForPriceFetchParams = { >; /** Account and chain scope for the fetch. */ request: Pick; - /** Current pipeline response balances to include. */ - response: Pick; }; /** - * Assets to price for this request: custom assets, balances, and metadata - * already in state, plus balances on the current response, excluding hidden - * assets. Scoped to the request's accounts and chains. + * Assets the price poll should price: custom assets, balances, and metadata + * already in state, excluding hidden assets. Scoped to the request's accounts + * and chains. * - * @param params - State, request scope, and response balances. + * 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. - * @param params.response - Current pipeline response balances to include. - * @returns Deduplicated, normalized CAIP-19 asset IDs. + * @returns Deduplicated, checksummed CAIP-19 asset IDs. */ export function getAssetIdsForPriceFetch({ state, request, - response, }: GetAssetIdsForPriceFetchParams): Set { + const assetIds = new Set(); + const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); const accountFilter = buildAccountFilter(request); const chainFilter = buildChainFilter(request); - const hiddenAssetIds = collectHiddenAssetIdKeys(state.assetPreferences); - const assetIds = new Map(); - const addAssetId = (assetId: string): void => { - addNormalizedAssetId(assetId, assetIds, chainFilter, hiddenAssetIds); + addStoredAssetId(assetId, assetIds, chainFilter, hiddenAssetIds); }; addBalancesFromAccounts(state.assetsBalance, accountFilter, addAssetId); @@ -53,9 +74,123 @@ export function getAssetIdsForPriceFetch({ for (const assetId of Object.keys(state.assetsInfo ?? {})) { addAssetId(assetId); } - addBalancesFromAccounts(response.assetsBalance, accountFilter, addAssetId); - return new Set(assetIds.values()); + 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)) { + const id = tryNormalizeAssetId(assetId as Caip19AssetId); + if (id === undefined) { + log('Skipping malformed asset ID for price fetch', { assetId }); + continue; + } + assetIds.add(id); + } + } + 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); } /** @@ -88,46 +223,55 @@ function buildChainFilter( } /** - * Lower-case normalized keys of assets the user has marked hidden. + * 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 lower-case normalized CAIP-19 keys. + * @returns Set of hidden CAIP-19 asset IDs. */ function collectHiddenAssetIdKeys( assetPreferences: AssetsControllerState['assetPreferences'] | undefined, -): Set { +): Set { return new Set( Object.entries(assetPreferences ?? {}) .filter(([, preferences]) => preferences.hidden) - .map(([assetId]) => - safeNormalizeAssetId(assetId as Caip19AssetId).toLowerCase(), - ), + .flatMap(([assetId]) => { + const id = tryNormalizeAssetId(assetId as Caip19AssetId); + return id === undefined ? [] : [id]; + }), ); } /** - * Normalize `assetId`, apply chain/hidden filters, and store under a - * case-insensitive key. Malformed IDs are skipped. + * Checksum `assetId`, then keep it when it is in chain scope and not hidden. + * An id that fails checksumming is skipped. {@link tryNormalizeAssetId} uses + * the memoized checksum, so an id that is already checksummed is cheap. * * @param assetId - Candidate CAIP-19 asset ID. - * @param assetIds - Output map (lower-case key → normalized ID). + * @param assetIds - Collected ids. * @param chainFilter - Optional allowed chain IDs. - * @param hiddenAssetIds - Lower-case keys of hidden assets. + * @param hiddenAssetIds - Hidden asset IDs from state. */ -function addNormalizedAssetId( +function addStoredAssetId( assetId: string, - assetIds: Map, + assetIds: Set, chainFilter: Set | undefined, - hiddenAssetIds: Set, + hiddenAssetIds: Set, ): void { + const id = tryNormalizeAssetId(assetId as Caip19AssetId); + if (id === undefined) { + log('Skipping malformed asset ID for price fetch', { assetId }); + return; + } + if (hiddenAssetIds.has(id)) { + return; + } try { - const normalizedAssetId = safeNormalizeAssetId(assetId as Caip19AssetId); - const { chainId } = parseCaipAssetType(normalizedAssetId); - const key = normalizedAssetId.toLowerCase(); - if ((chainFilter && !chainFilter.has(chainId)) || hiddenAssetIds.has(key)) { + const { chainId } = parseCaipAssetType(id); + if (chainFilter && !chainFilter.has(chainId)) { return; } - assetIds.set(key, normalizedAssetId); + assetIds.add(id); } catch (error) { log('Skipping malformed asset ID for price fetch', { assetId, error }); } diff --git a/packages/assets-controller/src/utils/index.ts b/packages/assets-controller/src/utils/index.ts index 2344ed2bc1b..7c7cd2e27b6 100644 --- a/packages/assets-controller/src/utils/index.ts +++ b/packages/assets-controller/src/utils/index.ts @@ -3,6 +3,7 @@ export { normalizeAmountString } from './normalizeAmountString.js'; export { normalizeAssetId, safeNormalizeAssetId, + tryNormalizeAssetId, clearNormalizeAssetIdCacheForTesting, } from './normalizeAssetId.js'; export { diff --git a/packages/assets-controller/src/utils/normalizeAssetId.test.ts b/packages/assets-controller/src/utils/normalizeAssetId.test.ts index aa0ef81b94a..8c9fc77f640 100644 --- a/packages/assets-controller/src/utils/normalizeAssetId.test.ts +++ b/packages/assets-controller/src/utils/normalizeAssetId.test.ts @@ -2,6 +2,7 @@ import type { Caip19AssetId } from '../types.js'; import { clearNormalizeAssetIdCacheForTesting, normalizeAssetId, + tryNormalizeAssetId, } from './normalizeAssetId.js'; describe('normalizeAssetId', () => { @@ -25,6 +26,12 @@ describe('normalizeAssetId', () => { expect(normalizeAssetId(solanaId)).toBe(solanaId); }); + it('returns undefined when checksumming throws', () => { + expect( + tryNormalizeAssetId('not-a-valid-caip19' as Caip19AssetId), + ).toBeUndefined(); + }); + it('memoizes results for the same asset ID', () => { const input = 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; diff --git a/packages/assets-controller/src/utils/normalizeAssetId.ts b/packages/assets-controller/src/utils/normalizeAssetId.ts index b791f0c952a..00ecf5910cd 100644 --- a/packages/assets-controller/src/utils/normalizeAssetId.ts +++ b/packages/assets-controller/src/utils/normalizeAssetId.ts @@ -44,6 +44,9 @@ export const normalizeAssetId: ((assetId: Caip19AssetId) => Caip19AssetId) & * make {@link normalizeAssetId} throw; callers processing untrusted input * use this so one bad ID cannot abort a whole batch. * + * Callers that must not store a lowercase id should use + * {@link tryNormalizeAssetId} and skip `undefined`. + * * @param assetId - The CAIP-19 asset ID to normalize. * @returns The normalized ID, or the original on failure. */ @@ -55,6 +58,23 @@ export function safeNormalizeAssetId(assetId: Caip19AssetId): Caip19AssetId { } } +/** + * Checksum a CAIP-19 asset ID. Returns `undefined` when checksumming throws, + * so the caller can skip that id instead of keeping a lowercase original. + * + * @param assetId - The CAIP-19 asset ID to normalize. + * @returns The checksummed ID, or `undefined` when it cannot be checksummed. + */ +export function tryNormalizeAssetId( + assetId: Caip19AssetId, +): Caip19AssetId | undefined { + try { + return normalizeAssetId(assetId); + } catch { + return undefined; + } +} + /** * Clears the {@link normalizeAssetId} memoize cache. Exported for unit tests. */ From 54de23f48a69993d8989b0ef0b941bcd14d7cb19 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Thu, 22 Oct 2026 21:58:10 +0200 Subject: [PATCH 07/14] fix types check --- .../src/AssetsController-method-action-types.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/assets-controller/src/AssetsController-method-action-types.ts b/packages/assets-controller/src/AssetsController-method-action-types.ts index 0916525b02d..f456418eb3b 100644 --- a/packages/assets-controller/src/AssetsController-method-action-types.ts +++ b/packages/assets-controller/src/AssetsController-method-action-types.ts @@ -17,6 +17,10 @@ export type AssetsControllerGetAssetsAction = { * 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 = { From ad7c31ded39c7b9e68c9d7014503dddd75416c9f Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Thu, 22 Oct 2026 22:18:01 +0200 Subject: [PATCH 08/14] remove detected assets --- packages/assets-controller/CHANGELOG.md | 5 + ...tering.accounts-api-v6.integration.test.ts | 2 +- .../assets-controller/src/AssetsController.ts | 40 +-- packages/assets-controller/src/README.md | 29 +- .../src/__fixtures__/assetSurfaces.ts | 24 -- .../src/data-sources/PriceDataSource.test.ts | 17 +- .../src/data-sources/PriceDataSource.ts | 2 +- .../src/data-sources/RpcDataSource.test.ts | 6 +- .../src/data-sources/RpcDataSource.ts | 6 - .../src/data-sources/TokenDataSource.test.ts | 278 ++++++++---------- .../src/data-sources/TokenDataSource.ts | 11 - packages/assets-controller/src/index.ts | 2 - .../src/middlewares/ParallelMiddleware.ts | 6 - ...tering.accounts-api-v6.integration.test.ts | 4 +- ...c-spam-token-filtering.integration.test.ts | 4 +- ...eSources.price-updates.integration.test.ts | 14 - packages/assets-controller/src/types.ts | 10 - tsc-suppressions.json | 5 - 18 files changed, 138 insertions(+), 327 deletions(-) diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 02ffa1623de..79e31a90e04 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -21,6 +21,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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 +- **BREAKING:** Remove `detectedAssets` from `DataResponse` and the unused `AssetsDetectedEvent` type. Newly detected tokens still arrive as balances and metadata + ### Fixed - `syncAssetsPrices` returns without calling the Price API when basic functionality is off 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 89493ff2438..8739fae37af 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 @@ -174,7 +174,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.ts b/packages/assets-controller/src/AssetsController.ts index 25cfb1f19bc..f3e5131cf4f 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -308,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 @@ -550,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 }; @@ -2987,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: { @@ -3275,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, }); } @@ -3310,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(); } diff --git a/packages/assets-controller/src/README.md b/packages/assets-controller/src/README.md index e5354fc4cb6..8cd99a71f61 100644 --- a/packages/assets-controller/src/README.md +++ b/packages/assets-controller/src/README.md @@ -196,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 @@ -615,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 @@ -938,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/data-sources/PriceDataSource.test.ts b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts index aa6c0cde886..10c470ebba1 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.test.ts @@ -1004,7 +1004,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 = jest.fn().mockResolvedValue(undefined); @@ -1019,21 +1019,6 @@ describe('PriceDataSource', () => { controller.destroy(); }); - it('middleware passes to next when detected assets is empty', async () => { - const { controller } = setupController(); - - const next = jest.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - response: { detectedAssets: {} }, - }); - - await controller.assetsMiddleware(context, next); - - expect(next).toHaveBeenCalledWith(context); - - controller.destroy(); - }); - it('fetchAssetPrices requests spot prices for the given assets', async () => { const { controller, apiClient } = setupController({ priceResponse: { diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index 4b31646c5d8..fd67ffc668a 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -638,7 +638,7 @@ export class PriceDataSource { this.#deduper.invalidate(); } - /** + /**Assets * Destroy the data source and clean up all subscriptions. */ destroy(): void { diff --git a/packages/assets-controller/src/data-sources/RpcDataSource.test.ts b/packages/assets-controller/src/data-sources/RpcDataSource.test.ts index 2da78a64870..57c8807759f 100644 --- a/packages/assets-controller/src/data-sources/RpcDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/RpcDataSource.test.ts @@ -1561,14 +1561,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 = jest @@ -1600,9 +1599,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 998e4851e69..e1ccecfdd6e 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts @@ -12,7 +12,6 @@ import type { AssetsControllerState, Context, DataRequest, - DataResponse, Caip19AssetId, ChainId, } from '../types.js'; @@ -134,33 +133,11 @@ function createDataRequest( } as DataRequest; } -function withResponseBalances(response: DataResponse = {}): DataResponse { - if (response.assetsBalance || !response.detectedAssets) { - return response; - } - - const assetsBalance: NonNullable = {}; - for (const [accountId, assetIds] of Object.entries(response.detectedAssets)) { - if (assetIds.length === 0) { - continue; - } - assetsBalance[accountId] = Object.fromEntries( - assetIds.map((assetId) => [assetId, { amount: '1' }]), - ); - } - - if (Object.keys(assetsBalance).length === 0) { - return response; - } - - return { ...response, assetsBalance }; -} - function createMiddlewareContext(overrides?: Partial): Context { return { request: createDataRequest(), ...overrides, - response: withResponseBalances(overrides?.response), + response: overrides?.response ?? {}, }; } @@ -238,7 +215,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(), }); @@ -253,22 +230,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 = jest.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'], @@ -278,8 +240,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -393,7 +357,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; @@ -407,9 +371,6 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [spamAsset], - }, assetsBalance: { 'mock-account-id': { [spamAsset]: { amount: '50' }, @@ -428,9 +389,6 @@ 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 () => { @@ -442,8 +400,10 @@ describe('TokenDataSource', () => { const next = jest.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]: { @@ -483,8 +443,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -505,8 +467,10 @@ describe('TokenDataSource', () => { const next = jest.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]: { @@ -548,8 +512,11 @@ describe('TokenDataSource', () => { const next = jest.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' }, + }, }, }, }); @@ -582,8 +549,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [unsupportedAsset], + assetsBalance: { + 'mock-account-id': { + [unsupportedAsset]: { amount: '1' }, + }, }, }, }); @@ -606,8 +575,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -631,8 +602,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -658,8 +631,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_NATIVE_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1' }, + }, }, }, }); @@ -687,8 +662,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_SPL_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_SPL_ASSET]: { amount: '1' }, + }, }, }, }); @@ -737,8 +714,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [assetId], + assetsBalance: { + 'mock-account-id': { + [assetId]: { amount: '1' }, + }, }, }, }); @@ -771,8 +750,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' }, + }, }, }, }); @@ -783,7 +764,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; @@ -802,9 +783,13 @@ describe('TokenDataSource', () => { const next = jest.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' }, + }, }, }, }); @@ -837,9 +822,13 @@ describe('TokenDataSource', () => { const next = jest.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' }, + }, }, }, }); @@ -877,8 +866,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': assetIds, + assetsBalance: { + 'mock-account-id': Object.fromEntries( + assetIds.map((assetId) => [assetId, { amount: '1' }]), + ), }, }, }); @@ -908,8 +899,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -940,11 +933,11 @@ describe('TokenDataSource', () => { const next = jest.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' }, + }, }, }, }); @@ -982,9 +975,6 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET, spamAsset], - }, assetsBalance: { 'mock-account-id': { [MOCK_TOKEN_ASSET]: { amount: '100' }, @@ -1005,13 +995,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 () => { @@ -1031,9 +1014,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' }, @@ -1311,8 +1291,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest({ chainIds: ['eip155:143'] }), response: { - detectedAssets: { - 'mock-account-id': [monadToken], + assetsBalance: { + 'mock-account-id': { + [monadToken]: { amount: '1' }, + }, }, }, }); @@ -1341,8 +1323,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest({ chainIds: ['eip155:137'] }), response: { - detectedAssets: { - 'mock-account-id': [polygonToken], + assetsBalance: { + 'mock-account-id': { + [polygonToken]: { amount: '1' }, + }, }, }, }); @@ -1369,8 +1353,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [spamAsset], + assetsBalance: { + 'mock-account-id': { + [spamAsset]: { amount: '1' }, + }, }, }, }); @@ -1409,8 +1395,10 @@ describe('TokenDataSource', () => { const context = createMiddlewareContext({ request: createDataRequest(), response: { - detectedAssets: { - 'mock-account-id': [checksummedCustomAsset], + assetsBalance: { + 'mock-account-id': { + [checksummedCustomAsset]: { amount: '1' }, + }, }, }, }); @@ -1444,8 +1432,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [musdAsset], + assetsBalance: { + 'mock-account-id': { + [musdAsset]: { amount: '1' }, + }, }, }, }); @@ -1470,8 +1460,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [noOccurrenceAsset], + assetsBalance: { + 'mock-account-id': { + [noOccurrenceAsset]: { amount: '1' }, + }, }, }, }); @@ -1517,8 +1509,11 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [maliciousSolanaToken, benignSolanaToken], + assetsBalance: { + 'mock-account-id': { + [maliciousSolanaToken]: { amount: '1' }, + [benignSolanaToken]: { amount: '1' }, + }, }, }, }); @@ -1573,8 +1568,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1591,7 +1588,7 @@ describe('TokenDataSource', () => { ); }); - it('middleware fetches native asset IDs even when detectedAssets is undefined', async () => { + it('middleware fetches native asset IDs when the response has no balances', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -1619,7 +1616,7 @@ describe('TokenDataSource', () => { expect(context.response.assetsInfo?.[MOCK_NATIVE_ASSET]).toBeDefined(); }); - it('middleware fetches native asset IDs when detectedAssets is an empty object', async () => { + it('middleware deduplicates native asset IDs with response balances', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -1635,39 +1632,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [], - }, - }, - }); - - 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 deduplicates native asset IDs with detected assets', 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 = jest.fn().mockResolvedValue(undefined); - const context = createMiddlewareContext({ - response: { - detectedAssets: { - 'mock-account-id': [MOCK_NATIVE_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '1' }, + }, }, }, }); @@ -1702,8 +1670,10 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ response: { - detectedAssets: { - 'mock-account-id': [MOCK_TOKEN_ASSET], + assetsBalance: { + 'mock-account-id': { + [MOCK_TOKEN_ASSET]: { amount: '1' }, + }, }, }, }); diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index adcaf0250d7..1ce00b51c8f 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -172,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()), - ); - } - } } // ============================================================================ diff --git a/packages/assets-controller/src/index.ts b/packages/assets-controller/src/index.ts index 008725ee3df..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 { @@ -110,7 +109,6 @@ export type { BalanceChangeEvent, PriceChangeEvent, MetadataChangeEvent, - AssetsDetectedEvent, } from './types.js'; // Data sources - base class and types 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/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 27c205d232b..6f55742bab2 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 @@ -2,7 +2,6 @@ import { parseCaipAssetType } from '@metamask/utils'; import { cleanAll } from 'nock'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -64,7 +63,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, @@ -239,7 +237,7 @@ describe('assets pipeline (Accounts API v6): BNB Chain spam token (CDOGE)', () = 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(); 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 32ec686bc7d..a5ea96f9c70 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 @@ -2,7 +2,6 @@ import { parseCaipAssetType } from '@metamask/utils'; import { cleanAll } from 'nock'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -53,7 +52,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, @@ -219,7 +217,7 @@ describe('assets pipeline: BNB Chain spam token (CDOGE)', () => { 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(); 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 590448acace..12b3afa4f7f 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 @@ -2,7 +2,6 @@ import { parseCaipAssetType } from '@metamask/utils'; import { cleanAll } from 'nock'; import { - RESPONSE_DETECTED_ASSETS_SURFACE, RESPONSE_METADATA_SURFACE, RESPONSE_PRICES_SURFACE, responseBalancesSurface, @@ -66,7 +65,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 = { @@ -343,12 +341,6 @@ 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', () => { expect(askedAbout(result.mocks.priceAPI.priceBatches)).toStrictEqual( new Set([ETH_ASSET_ID]), @@ -377,12 +369,6 @@ 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.mocks.priceAPI.priceBatches).toStrictEqual([]); }); diff --git a/packages/assets-controller/src/types.ts b/packages/assets-controller/src/types.ts index 6b9ad6e2a01..512bda93a71 100644 --- a/packages/assets-controller/src/types.ts +++ b/packages/assets-controller/src/types.ts @@ -384,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. @@ -665,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/tsc-suppressions.json b/tsc-suppressions.json index 83d9bd7fac2..5fe3a48f0ac 100644 --- a/tsc-suppressions.json +++ b/tsc-suppressions.json @@ -159,11 +159,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 From 19b00fb9b367ae862e16e2f2ba775db259c2eb2f Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Thu, 22 Oct 2026 22:51:24 +0200 Subject: [PATCH 09/14] fix code --- packages/assets-controller/CHANGELOG.md | 1 + .../src/AssetsController.test.ts | 201 ++++++++++++++++++ .../assets-controller/src/AssetsController.ts | 23 +- .../src/data-sources/PriceDataSource.ts | 2 +- 4 files changed, 218 insertions(+), 9 deletions(-) diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 79e31a90e04..6d30312e921 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Seed native and default tracked assets before refreshing price subscriptions when networks are enabled or the selected EVM network changes, so zero-balance assets the API omits (such as mUSD) are priced on that subscribe instead of waiting for the next poll - `syncAssetsPrices` returns without calling the Price API when basic functionality is off ## [19.1.0] diff --git a/packages/assets-controller/src/AssetsController.test.ts b/packages/assets-controller/src/AssetsController.test.ts index 084f83aef24..85a68d3e2f8 100644 --- a/packages/assets-controller/src/AssetsController.test.ts +++ b/packages/assets-controller/src/AssetsController.test.ts @@ -5192,6 +5192,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 = jest + .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 @@ -5479,6 +5530,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: jest.fn().mockResolvedValue({ + fullSupport: [1, 11155111], + partialSupport: [], + }), + fetchV5MultiAccountBalances: jest.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 = jest + .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', () => { diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index f3e5131cf4f..0e1d0008eb2 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -4033,18 +4033,21 @@ 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. - // 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. + // Seed before subscribe. The price poll snapshots balance state as soon + // as subscribe starts, and a balance refresh only prices assets on that + // response. Zero-balance natives and default tracked assets (mUSD) that + // the API omits would otherwise stay unpriced until the next poll. const accounts = this.#getSelectedAccounts(); this.#ensureNativeBalancesDefaultZero(); - // Seed default tracked assets (mUSD) before the fetch so they are included - // in the same price pass. 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. if (addedChains.length > 0 && accounts.length > 0) { await this.getAssets(accounts, { chainIds: addedChains, @@ -4148,8 +4151,12 @@ export class AssetsController extends BaseController< try { await this.#refreshActiveChainsOnNetworkSwitch(); - this.#subscribeAssets(); + // Seed before subscribe so the price poll snapshots the new native + // zeros. The balance refresh that follows only prices assets the API + // returns, so a native the API omits would stay unpriced until the + // next poll. this.#ensureNativeBalancesDefaultZero(); + this.#subscribeAssets(); await this.getAssets(accounts, { chainIds: [selectedChainId], diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index fd67ffc668a..4b31646c5d8 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -638,7 +638,7 @@ export class PriceDataSource { this.#deduper.invalidate(); } - /**Assets + /** * Destroy the data source and clean up all subscriptions. */ destroy(): void { From cc592fcc392cd6755e4a9a030034372a7a87cb0e Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 23 Oct 2026 13:10:20 +0200 Subject: [PATCH 10/14] update TokenDataSource --- .../2026-10-20-remove-detection-middleware.md | 104 ++++++++++++------ .../src/data-sources/TokenDataSource.test.ts | 100 ++++++----------- .../src/data-sources/TokenDataSource.ts | 7 +- .../src/utils/getAssetIdsForPriceFetch.ts | 30 ++--- packages/assets-controller/src/utils/index.ts | 1 - .../src/utils/normalizeAssetId.test.ts | 7 -- .../src/utils/normalizeAssetId.ts | 20 ---- 7 files changed, 116 insertions(+), 153 deletions(-) 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 index a36ce645ecd..355e058f9c5 100644 --- 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 @@ -75,46 +75,82 @@ flowchart LR ## Prices -A balance refresh fetches a price only for tokens on that response that the -wallet does not have a price for yet, on the accounts and chains this refresh -asked about. +A balance refresh and the price poll retrieve different tokens. -A token that already has a price is left alone. The price poll updates those, -and it also prices held tokens, custom tokens, and tokens we already have -metadata for when this response did not include them. A caller that names -specific tokens uses `syncAssetsPrices`, which fetches them again even when a -price is already stored. +**On a balance refresh**, prices come only from tokens on this response. A +token is retrieved when all of these are true: -Hidden tokens are skipped. So are tokens that cannot have a market price, such -as staking placeholders and Tron bandwidth or energy. Tokens on a chain the -price service does not support are skipped too. If that service cannot be -reached, we still ask for the tokens we had, rather than pricing nothing. +- 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 price the poll fetched in the last minute is not fetched again. Two polls -that need the same price share one request. +A token that already has a stored price is left alone on a refresh. -Changing the display currency is the exception. Those prices are in the wrong -currency, so the cache is cleared and every token the user holds is priced -again. +**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. +- Every custom token on those accounts. +- Every token we already have saved metadata for, on those chains. + +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 -A name and icon are fetched when we do not already have an image for the token. -That includes tokens the user already holds, and tokens that just arrived in -this refresh. Hidden tokens and staking placeholders are skipped. The chain's -own coin is always included, even when this refresh did not mention it. +A name and icon are fetched for a token that does not already have an image. +The list is built from two places. + +**From saved balances.** Every token an account holds in state, limited to the +accounts and chains this refresh asked about. An empty account list means +every account. An empty chain list means every chain. Each of these is a heal: +the missing name or icon is filled in, and the token is not spam-filtered. + +**From this refresh's balances.** Every token on the response. The balance +step already limited that response to this refresh. A response token is a heal +when it is already in saved balances, or when we already have any saved +metadata for it. A response token that is in neither is new. + +A token is left off both lists when any of these is true: + +- The user hid it. +- This response already includes an image for it. +- Saved metadata already includes an image for it. +- It is a staking-contract placeholder. + +Of what remains, only chains the token service supports are asked for. If none +of the tokens are on a supported chain, nothing is fetched. -A token the user already holds is filled in and kept. A token that just -appeared for the first time is checked so spam does not land in the wallet: +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 token the user already holds | Keep it, and fill in the missing name or icon | -| A new Ethereum-style token | Keep it only if enough other sources list it. The usual bar is 3. Unknown tokens are treated as spam and dropped | -| A new token on another chain | Ask Blockaid, and drop it if that scan says it is malicious | +| 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 fill in the missing name or icon | +| 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 name-and-icon request fails, balances already fetched are left as they are. @@ -155,6 +191,8 @@ What it costs: | ----------------------------------- | --------------------------------------------------------------------- | | Refresh prices for a list of tokens | `AssetsController:syncAssetsPrices` | | User changed currency | `AssetsController.setSelectedCurrency` | -| Which tokens get a price | `PriceDataSource` | -| Which tokens get token metadata | `TokenDataSource` | +| 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/data-sources/TokenDataSource.test.ts b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts index e1ccecfdd6e..30766633bfc 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts @@ -1540,7 +1540,13 @@ describe('TokenDataSource', () => { const next = jest.fn().mockResolvedValue(undefined); const context = createMiddlewareContext({ - response: {}, + response: { + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '0' }, + }, + }, + }, }); await controller.assetsMiddleware(context, next); @@ -1551,18 +1557,12 @@ describe('TokenDataSource', () => { ); }); - it('middleware always includes native asset IDs in the fetch', 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_TOKEN_ASSET), - createMockAssetResponse(MOCK_NATIVE_ASSET, { - name: 'Ethereum', - symbol: 'ETH', - }), - ], + assetsResponse: [createMockAssetResponse(MOCK_TOKEN_ASSET)], }); const next = jest.fn().mockResolvedValue(undefined); @@ -1579,54 +1579,32 @@ describe('TokenDataSource', () => { 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 when the response has no balances', 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 = jest.fn().mockResolvedValue(undefined); - // No balances on the response: only enabled native assets are fetched. - const context = createMiddlewareContext({ - response: {}, - }); - - 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 response balances', async () => { + it('middleware skips a native balance that already has an image', 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', + }, + }, + assetsBalance: { + 'mock-account-id': { + [MOCK_NATIVE_ASSET]: { amount: '0' }, + }, + }, + }), }); const next = jest.fn().mockResolvedValue(undefined); @@ -1634,7 +1612,7 @@ describe('TokenDataSource', () => { response: { assetsBalance: { 'mock-account-id': { - [MOCK_NATIVE_ASSET]: { amount: '1' }, + [MOCK_TOKEN_ASSET]: { amount: '1' }, }, }, }, @@ -1643,27 +1621,21 @@ 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', - }), ], }); @@ -1672,7 +1644,7 @@ describe('TokenDataSource', () => { response: { assetsBalance: { 'mock-account-id': { - [MOCK_TOKEN_ASSET]: { amount: '1' }, + [MOCK_NATIVE_ASSET]: { amount: '1' }, }, }, }, @@ -1681,11 +1653,7 @@ 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 }), ); }); diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index 1ce00b51c8f..bf495693f48 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -529,6 +529,7 @@ export class TokenDataSource { const { request, response } = ctx; const assetsState = this.#getAssetsState(); const { customAssets } = assetsState; + const nativeAssetIdsList = this.#getNativeAssetIds(); const { assetIdsNeedingMetadata, balanceHealAssetIds } = getAssetIdsForMetadataFetch({ state: assetsState, @@ -546,14 +547,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); - } if (assetIdsNeedingMetadata.size === 0) { return next(ctx); diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts index 7d36c84d897..c8b17aad6d6 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts @@ -7,7 +7,7 @@ import type { DataRequest, DataResponse, } from '../types.js'; -import { tryNormalizeAssetId } from './normalizeAssetId.js'; +import { normalizeAssetId } from './normalizeAssetId.js'; const log = createModuleLogger(projectLogger, 'getAssetIdsForPriceFetch'); @@ -97,12 +97,7 @@ export function getResponseAssetIdsForPriceFetch({ const assetIds = new Set(); for (const accountBalances of Object.values(response.assetsBalance ?? {})) { for (const assetId of Object.keys(accountBalances)) { - const id = tryNormalizeAssetId(assetId as Caip19AssetId); - if (id === undefined) { - log('Skipping malformed asset ID for price fetch', { assetId }); - continue; - } - assetIds.add(id); + assetIds.add(normalizeAssetId(assetId as Caip19AssetId)); } } return assetIds; @@ -235,17 +230,14 @@ function collectHiddenAssetIdKeys( return new Set( Object.entries(assetPreferences ?? {}) .filter(([, preferences]) => preferences.hidden) - .flatMap(([assetId]) => { - const id = tryNormalizeAssetId(assetId as Caip19AssetId); - return id === undefined ? [] : [id]; - }), + .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 tryNormalizeAssetId} uses - * the memoized checksum, so an id that is already checksummed is cheap. + * 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. @@ -258,15 +250,11 @@ function addStoredAssetId( chainFilter: Set | undefined, hiddenAssetIds: Set, ): void { - const id = tryNormalizeAssetId(assetId as Caip19AssetId); - if (id === undefined) { - log('Skipping malformed asset ID for price fetch', { assetId }); - return; - } - if (hiddenAssetIds.has(id)) { - return; - } try { + const id = normalizeAssetId(assetId as Caip19AssetId); + if (hiddenAssetIds.has(id)) { + return; + } const { chainId } = parseCaipAssetType(id); if (chainFilter && !chainFilter.has(chainId)) { return; diff --git a/packages/assets-controller/src/utils/index.ts b/packages/assets-controller/src/utils/index.ts index 7c7cd2e27b6..2344ed2bc1b 100644 --- a/packages/assets-controller/src/utils/index.ts +++ b/packages/assets-controller/src/utils/index.ts @@ -3,7 +3,6 @@ export { normalizeAmountString } from './normalizeAmountString.js'; export { normalizeAssetId, safeNormalizeAssetId, - tryNormalizeAssetId, clearNormalizeAssetIdCacheForTesting, } from './normalizeAssetId.js'; export { diff --git a/packages/assets-controller/src/utils/normalizeAssetId.test.ts b/packages/assets-controller/src/utils/normalizeAssetId.test.ts index 8c9fc77f640..aa0ef81b94a 100644 --- a/packages/assets-controller/src/utils/normalizeAssetId.test.ts +++ b/packages/assets-controller/src/utils/normalizeAssetId.test.ts @@ -2,7 +2,6 @@ import type { Caip19AssetId } from '../types.js'; import { clearNormalizeAssetIdCacheForTesting, normalizeAssetId, - tryNormalizeAssetId, } from './normalizeAssetId.js'; describe('normalizeAssetId', () => { @@ -26,12 +25,6 @@ describe('normalizeAssetId', () => { expect(normalizeAssetId(solanaId)).toBe(solanaId); }); - it('returns undefined when checksumming throws', () => { - expect( - tryNormalizeAssetId('not-a-valid-caip19' as Caip19AssetId), - ).toBeUndefined(); - }); - it('memoizes results for the same asset ID', () => { const input = 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Caip19AssetId; diff --git a/packages/assets-controller/src/utils/normalizeAssetId.ts b/packages/assets-controller/src/utils/normalizeAssetId.ts index 00ecf5910cd..b791f0c952a 100644 --- a/packages/assets-controller/src/utils/normalizeAssetId.ts +++ b/packages/assets-controller/src/utils/normalizeAssetId.ts @@ -44,9 +44,6 @@ export const normalizeAssetId: ((assetId: Caip19AssetId) => Caip19AssetId) & * make {@link normalizeAssetId} throw; callers processing untrusted input * use this so one bad ID cannot abort a whole batch. * - * Callers that must not store a lowercase id should use - * {@link tryNormalizeAssetId} and skip `undefined`. - * * @param assetId - The CAIP-19 asset ID to normalize. * @returns The normalized ID, or the original on failure. */ @@ -58,23 +55,6 @@ export function safeNormalizeAssetId(assetId: Caip19AssetId): Caip19AssetId { } } -/** - * Checksum a CAIP-19 asset ID. Returns `undefined` when checksumming throws, - * so the caller can skip that id instead of keeping a lowercase original. - * - * @param assetId - The CAIP-19 asset ID to normalize. - * @returns The checksummed ID, or `undefined` when it cannot be checksummed. - */ -export function tryNormalizeAssetId( - assetId: Caip19AssetId, -): Caip19AssetId | undefined { - try { - return normalizeAssetId(assetId); - } catch { - return undefined; - } -} - /** * Clears the {@link normalizeAssetId} memoize cache. Exported for unit tests. */ From 3f51475ba2c113cd162b271440c4aa78bb747b7f Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 23 Oct 2026 15:08:44 +0200 Subject: [PATCH 11/14] clean code --- packages/assets-controller/CHANGELOG.md | 17 ++--- .../2026-10-20-remove-detection-middleware.md | 71 +++++++++++-------- .../assets-controller/src/AssetsController.ts | 8 --- .../src/data-sources/PriceDataSource.ts | 4 +- .../src/data-sources/TokenDataSource.ts | 5 +- .../utils/getAssetIdsForMetadataFetch.test.ts | 50 +++++++++++-- .../src/utils/getAssetIdsForMetadataFetch.ts | 57 +++++++++++++-- .../utils/getAssetIdsForPriceFetch.test.ts | 26 ++----- .../src/utils/getAssetIdsForPriceFetch.ts | 18 ++--- 9 files changed, 165 insertions(+), 91 deletions(-) diff --git a/packages/assets-controller/CHANGELOG.md b/packages/assets-controller/CHANGELOG.md index 6d30312e921..d14b37da4d8 100644 --- a/packages/assets-controller/CHANGELOG.md +++ b/packages/assets-controller/CHANGELOG.md @@ -9,27 +9,22 @@ 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. Prices are shared, so the method takes no account or chain scope +- 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 -- **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 and prices held balances, custom assets, and metadata that this response did not include. `TokenDataSource` fetches metadata for state balances that have none and for response balances, and excludes hidden assets -- **BREAKING:** Remove `assetsForPriceUpdate` from `getAssets` options. Refresh an explicit asset list with `syncAssetsPrices` instead -- A balance refresh skips tokens that already have a stored price. The price poll uses the freshness cache so a recent price is not requested again +- 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 -- **BREAKING:** Remove `detectedAssets` from `DataResponse` and the unused `AssetsDetectedEvent` type. Newly detected tokens still arrive as balances and metadata - -### Fixed - -- Seed native and default tracked assets before refreshing price subscriptions when networks are enabled or the selected EVM network changes, so zero-balance assets the API omits (such as mUSD) are priced on that subscribe instead of waiting for the next poll -- `syncAssetsPrices` returns without calling the Price API when basic functionality is off +- **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] 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 index 355e058f9c5..e21ad6d071c 100644 --- 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 @@ -19,11 +19,11 @@ 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, and image. | -| Price | The token's value in the user's currency, such as USD. | +| 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. | @@ -96,9 +96,10 @@ 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. -- Every custom token on those accounts. -- Every token we already have saved metadata for, on those chains. + 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 @@ -117,28 +118,35 @@ user holds is priced again. ## Token metadata -A name and icon are fetched for a token that does not already have an image. -The list is built from two places. +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. -**From saved balances.** Every token an account holds in state, limited to the -accounts and chains this refresh asked about. An empty account list means -every account. An empty chain list means every chain. Each of these is a heal: -the missing name or icon is filled in, and the token is not spam-filtered. +A record is complete when it has a symbol, a name, decimals, an image, an +aggregator list, and an occurrence count. -**From this refresh's balances.** Every token on the response. The balance -step already limited that response to this refresh. A response token is a heal -when it is already in saved balances, or when we already have any saved -metadata for it. A response token that is in neither is new. +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 includes an image for it. -- Saved metadata already includes an image for 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 token service supports are asked for. If none -of the tokens are on a supported chain, nothing is fetched. +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: @@ -147,12 +155,12 @@ After the token service answers, each returned token is kept or dropped: | 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 fill in the missing name or icon | +| 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 name-and-icon request fails, balances already fetched are left as they +If the token-service request fails, balances already fetched are left as they are. ## Trade-offs @@ -162,18 +170,19 @@ 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 have no image remain candidates for name and icon fetching, without - relying on a detection list. + 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 a name or icon - are asked for on every refresh. A token the service has no image 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. +- 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. diff --git a/packages/assets-controller/src/AssetsController.ts b/packages/assets-controller/src/AssetsController.ts index 0e1d0008eb2..7a046a342be 100644 --- a/packages/assets-controller/src/AssetsController.ts +++ b/packages/assets-controller/src/AssetsController.ts @@ -4033,10 +4033,6 @@ 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. - // Seed before subscribe. The price poll snapshots balance state as soon - // as subscribe starts, and a balance refresh only prices assets on that - // response. Zero-balance natives and default tracked assets (mUSD) that - // the API omits would otherwise stay unpriced until the next poll. const accounts = this.#getSelectedAccounts(); this.#ensureNativeBalancesDefaultZero(); if (addedChains.length > 0) { @@ -4151,10 +4147,6 @@ export class AssetsController extends BaseController< try { await this.#refreshActiveChainsOnNetworkSwitch(); - // Seed before subscribe so the price poll snapshots the new native - // zeros. The balance refresh that follows only prices assets the API - // returns, so a native the API omits would stay unpriced until the - // next poll. this.#ensureNativeBalancesDefaultZero(); this.#subscribeAssets(); diff --git a/packages/assets-controller/src/data-sources/PriceDataSource.ts b/packages/assets-controller/src/data-sources/PriceDataSource.ts index 4b31646c5d8..3a72188c988 100644 --- a/packages/assets-controller/src/data-sources/PriceDataSource.ts +++ b/packages/assets-controller/src/data-sources/PriceDataSource.ts @@ -479,8 +479,8 @@ 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. * diff --git a/packages/assets-controller/src/data-sources/TokenDataSource.ts b/packages/assets-controller/src/data-sources/TokenDataSource.ts index bf495693f48..b7460820d0b 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.ts @@ -515,8 +515,9 @@ export class TokenDataSource { * Get the middleware for enriching responses with token metadata. * * This middleware: - * 1. Fetches metadata for state balances that have none, and for every - * balance on the current response, excluding hidden assets + * 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 diff --git a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts index 602e60be8f5..b3ae10747c2 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts @@ -25,6 +25,16 @@ const STAKING_MAINNET = 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', @@ -178,13 +188,13 @@ describe('getAssetIdsForMetadataFetch', () => { ]); }); - it('excludes assets that already have an image in state metadata', () => { + 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]: METADATA_WITH_IMAGE }, + assetsInfo: { [TOKEN_MAINNET]: COMPLETE_METADATA }, }), request: createRequest(), response: {}, @@ -194,7 +204,7 @@ describe('getAssetIdsForMetadataFetch', () => { expect(sortedIds(result.balanceHealAssetIds)).toStrictEqual([]); }); - it('excludes assets that already have an image in response metadata', () => { + it('excludes assets whose response metadata has every client-used field', () => { const result = getAssetIdsForMetadataFetch({ state: emptyState(), request: createRequest(), @@ -202,7 +212,7 @@ describe('getAssetIdsForMetadataFetch', () => { assetsBalance: { [ACCOUNT_A]: { [TOKEN_MAINNET]: ZERO_BALANCE }, }, - assetsInfo: { [TOKEN_MAINNET]: METADATA_WITH_IMAGE }, + assetsInfo: { [TOKEN_MAINNET]: COMPLETE_METADATA }, }, }); @@ -210,6 +220,38 @@ describe('getAssetIdsForMetadataFetch', () => { 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({ diff --git a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts index 2f836e6af2a..907ddffafc4 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.ts @@ -2,6 +2,7 @@ import { parseCaipAssetType } from '@metamask/utils'; import { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js'; import type { + AssetMetadata, AssetsControllerState, Caip19AssetId, DataRequest, @@ -33,8 +34,10 @@ export type GetAssetIdsForMetadataFetchResult = { /** * Metadata to fetch for this request. * - * State balances that have no metadata yet are heals: they are enriched and - * not spam-filtered. Response balances are included too; a response balance + * 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. * @@ -64,10 +67,12 @@ export function getAssetIdsForMetadataFetch({ if (hiddenAssetIds.has(assetId.toLowerCase())) { return false; } - if (response.assetsInfo?.[assetId as Caip19AssetId]?.image) { + if ( + hasMetadataUsedByClients(response.assetsInfo?.[assetId as Caip19AssetId]) + ) { return false; } - if (stateMetadata[assetId as Caip19AssetId]?.image) { + if (hasMetadataUsedByClients(stateMetadata[assetId as Caip19AssetId])) { return false; } return !isStakingContractAssetId(assetId); @@ -117,6 +122,50 @@ export function getAssetIdsForMetadataFetch({ 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). diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts index d7be0599acd..8b0784f29cb 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts @@ -115,14 +115,7 @@ describe('getAssetIdsForPriceFetch', () => { expected: [normalizeAssetId(CUSTOM_MAINNET)], }, { - name: 'assetsInfo metadata in chain scope', - state: emptyState({ - assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, - }), - expected: [normalizeAssetId(METADATA_MAINNET)], - }, - { - name: 'union of state balance, custom, and metadata', + name: 'union of state balance and custom assets', state: emptyState({ assetsBalance: { [ACCOUNT_A]: { [NATIVE_MAINNET]: ZERO_BALANCE }, @@ -130,11 +123,7 @@ describe('getAssetIdsForPriceFetch', () => { customAssets: { [ACCOUNT_A]: [CUSTOM_MAINNET] }, assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, }), - expected: [ - NATIVE_MAINNET, - normalizeAssetId(CUSTOM_MAINNET), - normalizeAssetId(METADATA_MAINNET), - ].sort(), + expected: [NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET)].sort(), }, ])('includes $name', ({ state, expected }) => { const result = getAssetIdsForPriceFetch({ @@ -168,10 +157,9 @@ describe('getAssetIdsForPriceFetch', () => { expected: [], }, { - name: 'hidden metadata asset', + name: 'metadata with no balance', state: emptyState({ assetsInfo: { [METADATA_MAINNET]: TOKEN_METADATA }, - assetPreferences: { [METADATA_MAINNET]: { hidden: true } }, }), expected: [], }, @@ -225,15 +213,11 @@ describe('getAssetIdsForPriceFetch', () => { }); expect(sortedIds(result)).toStrictEqual( - [ - NATIVE_MAINNET, - normalizeAssetId(CUSTOM_MAINNET), - normalizeAssetId(METADATA_MAINNET), - ].sort(), + [NATIVE_MAINNET, normalizeAssetId(CUSTOM_MAINNET)].sort(), ); }); - it('deduplicates the same asset across state balance, custom, and metadata', () => { + it('deduplicates the same asset across a state balance and a custom asset', () => { const result = getAssetIdsForPriceFetch({ state: emptyState({ assetsBalance: { diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts index c8b17aad6d6..38846f30f94 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.ts @@ -38,16 +38,17 @@ export type GetAssetIdsForPriceFetchParams = { /** Current AssetsController state slices used for candidate collection. */ state: Pick< AssetsControllerState, - 'assetsBalance' | 'customAssets' | 'assetsInfo' | 'assetPreferences' + 'assetsBalance' | 'customAssets' | 'assetPreferences' >; /** Account and chain scope for the fetch. */ request: Pick; }; /** - * Assets the price poll should price: custom assets, balances, and metadata - * already in state, excluding hidden assets. Scoped to the request's accounts - * and chains. + * 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}. @@ -71,9 +72,6 @@ export function getAssetIdsForPriceFetch({ addBalancesFromAccounts(state.assetsBalance, accountFilter, addAssetId); addCustomAssets(state.customAssets, accountFilter, addAssetId); - for (const assetId of Object.keys(state.assetsInfo ?? {})) { - addAssetId(assetId); - } return assetIds; } @@ -97,7 +95,11 @@ export function getResponseAssetIdsForPriceFetch({ const assetIds = new Set(); for (const accountBalances of Object.values(response.assetsBalance ?? {})) { for (const assetId of Object.keys(accountBalances)) { - assetIds.add(normalizeAssetId(assetId as Caip19AssetId)); + try { + assetIds.add(normalizeAssetId(assetId as Caip19AssetId)); + } catch (error) { + log('Skipping malformed asset ID for price fetch', { assetId, error }); + } } } return assetIds; From 5af7e966b02c350222dd1cd595589d539253f802 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 23 Oct 2026 15:30:51 +0200 Subject: [PATCH 12/14] clean code --- .../2026-10-20-remove-detection-middleware.md | 18 +++++++------- ...oller.ws-price-updates.integration.test.ts | 2 +- .../__fixtures__/ws-price-updates/wsWallet.ts | 2 ++ .../src/data-sources/TokenDataSource.test.ts | 12 +++++++--- ...tering.accounts-api-v6.integration.test.ts | 24 +++++++++++++------ ...c-spam-token-filtering.integration.test.ts | 24 +++++++++++++------ ...eSources.price-updates.integration.test.ts | 4 ++-- 7 files changed, 57 insertions(+), 29 deletions(-) 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 index e21ad6d071c..88e37490cd5 100644 --- 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 @@ -19,15 +19,15 @@ 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. | +| 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 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 7f6f3b3402d..a2a31999aed 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 @@ -317,7 +317,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/__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/TokenDataSource.test.ts b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts index 30766633bfc..edcb78de124 100644 --- a/packages/assets-controller/src/data-sources/TokenDataSource.test.ts +++ b/packages/assets-controller/src/data-sources/TokenDataSource.test.ts @@ -391,7 +391,7 @@ describe('TokenDataSource', () => { ).toBeUndefined(); }); - 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'], @@ -412,6 +412,8 @@ describe('TokenDataSource', () => { symbol: 'EXT', decimals: 18, image: 'https://existing.com/icon.png', + aggregators: ['metamask'], + occurrences: 5, }, }, }, @@ -423,7 +425,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'], @@ -435,6 +437,8 @@ describe('TokenDataSource', () => { symbol: 'STT', decimals: 18, image: 'https://state.com/icon.png', + aggregators: ['metamask'], + occurrences: 5, }, }, }), @@ -1584,7 +1588,7 @@ describe('TokenDataSource', () => { ); }); - it('middleware skips a native balance that already has an image', async () => { + it('middleware skips a native balance whose metadata is already complete', async () => { const { controller, apiClient } = setupController({ messenger: createTestMessenger(), supportedNetworks: ['eip155:1'], @@ -1597,6 +1601,8 @@ describe('TokenDataSource', () => { name: 'Ethereum', decimals: 18, image: 'https://example.com/eth.png', + aggregators: [], + occurrences: 1, }, }, assetsBalance: { 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 6f55742bab2..0d135d9d73c 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 @@ -230,7 +230,7 @@ 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 () => { @@ -244,12 +244,22 @@ describe('assets pipeline (Accounts API v6): BNB Chain spam token (CDOGE)', () = }, ); - 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 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 a5ea96f9c70..268f93106ca 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 @@ -210,7 +210,7 @@ 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 () => { @@ -224,12 +224,22 @@ describe('assets pipeline: BNB Chain spam token (CDOGE)', () => { }, ); - 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, 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 12b3afa4f7f..b4b4319b9c1 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 @@ -373,9 +373,9 @@ describe('websocket update pipeline: prices for surfaced holdings', () => { 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(), ); }); From ffbcb7860a46c9d3b6439e30c75d517466e47948 Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 23 Oct 2026 21:19:27 +0200 Subject: [PATCH 13/14] clean oxlint --- oxlint-suppressions.json | 7 +------ .../src/utils/getAssetIdsForMetadataFetch.test.ts | 1 + .../src/utils/getAssetIdsForPriceFetch.test.ts | 5 +++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 10f20e0fe4a..4c6d8e2682b 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -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 @@ -6045,4 +6040,4 @@ "count": 1 } } -} +} \ No newline at end of file diff --git a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts index b3ae10747c2..dfb66c95f53 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForMetadataFetch.test.ts @@ -1,4 +1,5 @@ import type { InternalAccount } from '@metamask/keyring-internal-api'; +import { describe, expect, it } from 'vitest'; import type { AssetBalance, diff --git a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts index 8b0784f29cb..3bcc84c1b4a 100644 --- a/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts +++ b/packages/assets-controller/src/utils/getAssetIdsForPriceFetch.test.ts @@ -1,4 +1,5 @@ import type { InternalAccount } from '@metamask/keyring-internal-api'; +import { describe, expect, it, vi } from 'vitest'; import type { AssetBalance, @@ -294,7 +295,7 @@ describe('getAssetIdsForPriceFetch', () => { }); it('skips hidden response balances', async () => { - const getSupportedNetworks = jest.fn(async () => new Set([CHAIN_MAINNET])); + const getSupportedNetworks = vi.fn(async () => new Set([CHAIN_MAINNET])); const result = await getUnpricedResponseAssetIds({ state: { @@ -316,7 +317,7 @@ describe('getAssetIdsForPriceFetch', () => { }); it('does not load supported networks when nothing priceable remains', async () => { - const getSupportedNetworks = jest.fn(); + const getSupportedNetworks = vi.fn(); const result = await getUnpricedResponseAssetIds({ state: { ...emptyState(), assetsPrice: {} }, From ff066f43e4f732054abb19f03af7f1659574809c Mon Sep 17 00:00:00 2001 From: Kriys94 Date: Fri, 23 Oct 2026 21:21:13 +0200 Subject: [PATCH 14/14] chore: format oxlint suppressions Co-authored-by: Cursor --- oxlint-suppressions.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 4c6d8e2682b..322c19d277b 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -6040,4 +6040,4 @@ "count": 1 } } -} \ No newline at end of file +}