@metamask-previews/assets-controller 11.0.0-preview-1203ebcfc → 11.0.0-preview-6756b9ebc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -2
- package/dist/data-sources/TokenDataSource.cjs +11 -54
- package/dist/data-sources/TokenDataSource.cjs.map +1 -1
- package/dist/data-sources/TokenDataSource.d.cts +0 -2
- package/dist/data-sources/TokenDataSource.d.cts.map +1 -1
- package/dist/data-sources/TokenDataSource.d.mts +0 -2
- package/dist/data-sources/TokenDataSource.d.mts.map +1 -1
- package/dist/data-sources/TokenDataSource.mjs +11 -54
- package/dist/data-sources/TokenDataSource.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.cjs +27 -86
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.cts +3 -4
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.mts +3 -4
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.mjs +27 -86
- package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -13,8 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
13
13
|
|
|
14
14
|
### Changed
|
|
15
15
|
|
|
16
|
-
- `TokenDataSource` EVM spam filtering now uses per-chain floors from Token API `GET /v1/suggestedOccurrenceFloors` (`queryApiClient.token.fetchV1SuggestedOccurrenceFloors`) instead of a hardcoded minimum of 3 occurrences. Chains missing from the response (or a failed floors fetch) still fall back to 3 ([#9537](https://github.com/MetaMask/core/pull/9537))
|
|
17
|
-
- `TokensApiClient` (used by `RpcDataSource` / `TokenDetector`) now sets the token-list `occurrenceFloor` query param from the same Token API `GET /v1/suggestedOccurrenceFloors` endpoint (cached 1h), replacing the hardcoded Linea/MegaETH/Tempo special cases. Missing chains or failed fetches fall back to 3 ([#9537](https://github.com/MetaMask/core/pull/9537))
|
|
18
16
|
- Bump `@metamask/network-enablement-controller` from `^5.5.0` to `^5.6.0` ([#9520](https://github.com/MetaMask/core/pull/9520))
|
|
19
17
|
- Bump `@metamask/phishing-controller` from `^17.2.1` to `^17.3.0` ([#9532](https://github.com/MetaMask/core/pull/9532))
|
|
20
18
|
|
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _TokenDataSource_instances, _TokenDataSource_apiClient, _TokenDataSource_getNativeAssetIds, _TokenDataSource_getAssetType, _TokenDataSource_messenger, _TokenDataSource_fetchTimeoutMs, _TokenDataSource_getSupportedNetworks,
|
|
13
|
+
var _TokenDataSource_instances, _TokenDataSource_apiClient, _TokenDataSource_getNativeAssetIds, _TokenDataSource_getAssetType, _TokenDataSource_messenger, _TokenDataSource_fetchTimeoutMs, _TokenDataSource_getSupportedNetworks, _TokenDataSource_filterAssetsByNetwork, _TokenDataSource_filterBlockaidSpamTokens;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.TokenDataSource = exports.CaipAssetNamespace = void 0;
|
|
16
16
|
const core_backend_1 = require("@metamask/core-backend");
|
|
@@ -31,11 +31,10 @@ const TOKENS_API_BATCH_SIZE = 50;
|
|
|
31
31
|
/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */
|
|
32
32
|
const BULK_SCAN_BATCH_SIZE = 100;
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* the floors request fails). Non-EVM tokens are filtered via Blockaid instead.
|
|
34
|
+
* Minimum number of aggregator occurrences required for an EVM ERC-20 token to
|
|
35
|
+
* pass the spam filter. Non-EVM tokens are filtered via Blockaid bulk scan instead.
|
|
37
36
|
*/
|
|
38
|
-
const
|
|
37
|
+
const MIN_TOKEN_OCCURRENCES = 3;
|
|
39
38
|
/** CAIP-19 `assetNamespace` segments used across filtering logic. */
|
|
40
39
|
var CaipAssetNamespace;
|
|
41
40
|
(function (CaipAssetNamespace) {
|
|
@@ -82,23 +81,6 @@ function transformV3AssetResponseToMetadata(assetId, assetData, getAssetType) {
|
|
|
82
81
|
};
|
|
83
82
|
return metadata;
|
|
84
83
|
}
|
|
85
|
-
/**
|
|
86
|
-
* Resolve the occurrence floor for an EVM asset from suggested floors keyed by
|
|
87
|
-
* decimal chain ID (e.g. `{ "1": 3, "143": 1 }`).
|
|
88
|
-
*
|
|
89
|
-
* @param assetId - CAIP-19 asset ID.
|
|
90
|
-
* @param floors - Map of decimal chain ID → suggested floor.
|
|
91
|
-
* @returns Floor to apply, or {@link DEFAULT_OCCURRENCE_FLOOR} when missing.
|
|
92
|
-
*/
|
|
93
|
-
function getOccurrenceFloorForAsset(assetId, floors) {
|
|
94
|
-
try {
|
|
95
|
-
const { chain } = (0, utils_1.parseCaipAssetType)(assetId);
|
|
96
|
-
return floors[chain.reference] ?? DEFAULT_OCCURRENCE_FLOOR;
|
|
97
|
-
}
|
|
98
|
-
catch {
|
|
99
|
-
return DEFAULT_OCCURRENCE_FLOOR;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
84
|
// ============================================================================
|
|
103
85
|
// TOKEN DATA SOURCE
|
|
104
86
|
// ============================================================================
|
|
@@ -108,8 +90,6 @@ function getOccurrenceFloorForAsset(assetId, floors) {
|
|
|
108
90
|
* This middleware-based data source:
|
|
109
91
|
* - Checks detected assets for missing metadata/images
|
|
110
92
|
* - Fetches metadata from Tokens API v3 for assets needing enrichment
|
|
111
|
-
* - Filters EVM ERC-20 spam using per-chain floors from Token API
|
|
112
|
-
* `/v1/suggestedOccurrenceFloors` (default floor 3)
|
|
113
93
|
* - Merges fetched metadata into the response
|
|
114
94
|
*
|
|
115
95
|
* Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid
|
|
@@ -192,12 +172,8 @@ class TokenDataSource {
|
|
|
192
172
|
if (assetIdsNeedingMetadata.size === 0) {
|
|
193
173
|
return next(ctx);
|
|
194
174
|
}
|
|
195
|
-
// Filter asset IDs to only include supported networks
|
|
196
|
-
|
|
197
|
-
const [supportedNetworks, suggestedOccurrenceFloors] = await Promise.all([
|
|
198
|
-
__classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSupportedNetworks).call(this),
|
|
199
|
-
__classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSuggestedOccurrenceFloors).call(this),
|
|
200
|
-
]);
|
|
175
|
+
// Filter asset IDs to only include supported networks
|
|
176
|
+
const supportedNetworks = await __classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSupportedNetworks).call(this);
|
|
201
177
|
const supportedAssetIds = __classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_filterAssetsByNetwork).call(this, [...assetIdsNeedingMetadata], supportedNetworks);
|
|
202
178
|
if (supportedAssetIds.length === 0) {
|
|
203
179
|
return next(ctx);
|
|
@@ -240,16 +216,14 @@ class TokenDataSource {
|
|
|
240
216
|
nonEvmTokenIds.push(assetId);
|
|
241
217
|
}
|
|
242
218
|
}
|
|
243
|
-
// EVM: require
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
// (undefined) are treated the same as zero occurrences and filtered out.
|
|
219
|
+
// EVM: require minimum occurrence count to suppress low-signal tokens.
|
|
220
|
+
// Tokens with no occurrence data (undefined) are treated the same as
|
|
221
|
+
// zero occurrences and filtered out.
|
|
247
222
|
// Custom assets (user-imported) bypass the occurrence filter — users
|
|
248
223
|
// can import whatever they want and we must keep their metadata even
|
|
249
|
-
// if the API has fewer aggregator hits
|
|
224
|
+
// if the API has fewer than `MIN_TOKEN_OCCURRENCES` aggregator hits.
|
|
250
225
|
const allowedEvmIds = new Set(evmErc20Ids.filter((id) => customAssetIds.has(id.toLowerCase()) ||
|
|
251
|
-
(occurrencesByAssetId.get(id) ?? 0) >=
|
|
252
|
-
getOccurrenceFloorForAsset(id, suggestedOccurrenceFloors) ||
|
|
226
|
+
(occurrencesByAssetId.get(id) ?? 0) >= MIN_TOKEN_OCCURRENCES ||
|
|
253
227
|
id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`)));
|
|
254
228
|
// Non-EVM: Blockaid bulk scan.
|
|
255
229
|
// Custom assets (user-imported) bypass Blockaid filtering.
|
|
@@ -326,23 +300,6 @@ async function _TokenDataSource_getSupportedNetworks() {
|
|
|
326
300
|
log('Failed to fetch supported networks', { error });
|
|
327
301
|
return new Set();
|
|
328
302
|
}
|
|
329
|
-
}, _TokenDataSource_getSuggestedOccurrenceFloors =
|
|
330
|
-
/**
|
|
331
|
-
* Fetches per-chain suggested occurrence floors from Token API
|
|
332
|
-
* (`GET /v1/suggestedOccurrenceFloors`). Caching is handled by
|
|
333
|
-
* ApiPlatformClient. Fails open to an empty map so callers fall back to
|
|
334
|
-
* {@link DEFAULT_OCCURRENCE_FLOOR}.
|
|
335
|
-
*
|
|
336
|
-
* @returns Map of decimal chain ID → suggested occurrence floor.
|
|
337
|
-
*/
|
|
338
|
-
async function _TokenDataSource_getSuggestedOccurrenceFloors() {
|
|
339
|
-
try {
|
|
340
|
-
return await (0, utils_2.fetchWithTimeout)(() => __classPrivateFieldGet(this, _TokenDataSource_apiClient, "f").token.fetchV1SuggestedOccurrenceFloors(), __classPrivateFieldGet(this, _TokenDataSource_fetchTimeoutMs, "f"));
|
|
341
|
-
}
|
|
342
|
-
catch (error) {
|
|
343
|
-
log('Failed to fetch suggested occurrence floors', { error });
|
|
344
|
-
return {};
|
|
345
|
-
}
|
|
346
303
|
}, _TokenDataSource_filterAssetsByNetwork = function _TokenDataSource_filterAssetsByNetwork(assetIds, supportedNetworks) {
|
|
347
304
|
return assetIds.filter((assetId) => {
|
|
348
305
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenDataSource.cjs","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yDAA2D;AAK3D,uEAAoE;AACpE,2CAAyE;AAIzE,0CAA8D;AAC9D,wCAAwC;AAOxC,8CAA4C;AAC5C,mEAG4B;AAE5B,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAC1C,MAAM,wBAAwB,GAAG,KAAM,CAAC;AAExC,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,eAAe,CAAC,CAAC;AAE/D,4CAA4C;AAC5C,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,yFAAyF;AACzF,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAEjC;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAEnC,qEAAqE;AACrE,IAAY,kBAIX;AAJD,WAAY,kBAAkB;IAC5B,uCAAiB,CAAA;IACjB,qCAAe,CAAA;IACf,qCAAe,CAAA;AACjB,CAAC,EAJW,kBAAkB,kCAAlB,kBAAkB,QAI7B;AAED,MAAM,sBAAsB,GAAG,4CAA4C,CAAC;AA4B5E,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;;;;;;;;GAYG;AACH,SAAS,kCAAkC,CACzC,OAAsB,EACtB,SAA0B,EAC1B,YAA+D;IAE/D,MAAM,QAAQ,GAA0B;QACtC,IAAI,EAAE,YAAY,CAAC,OAAO,CAAC;QAC3B,2BAA2B;QAC3B,IAAI,EAAE,SAAS,CAAC,IAAI;QACpB,MAAM,EAAE,SAAS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AAU,CAC3C,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACpB,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,mCAAmC,EAAE;gBACxD,OAAO;gBACP,MAAM,EAAE,KAAK;aACd,CAAC,CACH,CACF,CAAC;YAEF,MAAM,YAAY,GAA0B,EAAE,CAAC;YAC/C,KAAK,MAAM,MAAM,IAAI,YAAY,EAAE,CAAC;gBAClC,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;oBAClC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5C,CAAC;YACH,CAAC;YAED,KAAK,MAAM,KAAK,IAAI,YAAY,EAAE,CAAC;gBACjC,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBAC3C,IAAI,MAAM,EAAE,WAAW,KAAK,yCAAmB,CAAC,SAAS,EAAE,CAAC;oBAC1D,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qDAAqD,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { V3AssetResponse } from '@metamask/core-backend';\nimport { ApiPlatformClient } from '@metamask/core-backend';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { KnownCaipNamespace, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { AssetsControllerMessenger } from '../AssetsController';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport { forDataTypes } from '../types';\nimport type {\n Caip19AssetId,\n AssetMetadata,\n Middleware,\n FungibleAssetMetadata,\n} from '../types';\nimport { fetchWithTimeout } from '../utils';\nimport {\n isStakingContractAssetId,\n reduceInBatchesSerially,\n} from './evm-rpc-services';\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nconst CONTROLLER_NAME = 'TokenDataSource';\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\n/** Max asset IDs per tokens API request. */\nconst TOKENS_API_BATCH_SIZE = 50;\n\n/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */\nconst BULK_SCAN_BATCH_SIZE = 100;\n\n/**\n * Fallback minimum aggregator occurrences for EVM ERC-20 spam filtering when\n * Token API `/v1/suggestedOccurrenceFloors` has no entry for the chain (or\n * the floors request fails). Non-EVM tokens are filtered via Blockaid instead.\n */\nconst DEFAULT_OCCURRENCE_FLOOR = 3;\n\n/** CAIP-19 `assetNamespace` segments used across filtering logic. */\nexport enum CaipAssetNamespace {\n Slip44 = 'slip44',\n Erc20 = 'erc20',\n Token = 'token',\n}\n\nconst MUSD_ADDRESS_LOWERCASE = '0xaca92e438df0b2401ff60da7e4337b687a2435da';\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type TokenDataSourceOptions = {\n /** ApiPlatformClient for API calls with caching */\n queryApiClient: ApiPlatformClient;\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n getNativeAssetIds: () => string[];\n /** Returns the asset type ('native' | 'erc20' | 'spl') for a given CAIP-19 asset ID */\n getAssetType: (assetId: Caip19AssetId) => 'native' | 'erc20' | 'spl';\n /**\n * Timeout in ms for a single Tokens API call (default: 15000). When it\n * fires, the batch rejects so metadata enrichment proceeds without it.\n */\n fetchTimeoutMs?: number;\n};\n\n/**\n * Messenger actions `TokenDataSource` may invoke (via {@link AssetsControllerMessenger}).\n * Not re-exported from the package public `index` (repo ESLint); import from this module when\n * typing a messenger in the same package or tests.\n */\nexport type TokenDataSourceAllowedActions =\n PhishingControllerBulkScanTokensAction;\n\n// ============================================================================\n// HELPER FUNCTIONS\n// ============================================================================\n\n/**\n * Transform V3 API response to FungibleAssetMetadata for state storage.\n *\n * Mapping:\n * - assetId → used to derive `type` (native/erc20/spl)\n * - iconUrl → image\n * - All other fields map directly\n *\n * @param assetId - CAIP-19 asset ID used to derive token type.\n * @param assetData - V3 API response data.\n * @param getAssetType - Returns the asset type for a given CAIP-19 asset ID.\n * @returns FungibleAssetMetadata for state storage.\n */\nfunction transformV3AssetResponseToMetadata(\n assetId: Caip19AssetId,\n assetData: V3AssetResponse,\n getAssetType: (id: Caip19AssetId) => 'native' | 'erc20' | 'spl',\n): AssetMetadata {\n const metadata: FungibleAssetMetadata = {\n type: getAssetType(assetId),\n // BaseAssetMetadata fields\n name: assetData.name,\n symbol: assetData.symbol,\n decimals: assetData.decimals,\n image: assetData.iconUrl,\n // Direct mapping fields\n coingeckoId: assetData.coingeckoId,\n occurrences: assetData.occurrences,\n aggregators: assetData.aggregators,\n labels: assetData.labels,\n erc20Permit: assetData.erc20Permit,\n fees: assetData.fees,\n honeypotStatus: assetData.honeypotStatus,\n storage: assetData.storage,\n isContractVerified: assetData.isContractVerified,\n description: assetData.description,\n };\n\n return metadata;\n}\n\n/**\n * Resolve the occurrence floor for an EVM asset from suggested floors keyed by\n * decimal chain ID (e.g. `{ \"1\": 3, \"143\": 1 }`).\n *\n * @param assetId - CAIP-19 asset ID.\n * @param floors - Map of decimal chain ID → suggested floor.\n * @returns Floor to apply, or {@link DEFAULT_OCCURRENCE_FLOOR} when missing.\n */\nfunction getOccurrenceFloorForAsset(\n assetId: string,\n floors: Record<string, number>,\n): number {\n try {\n const { chain } = parseCaipAssetType(assetId as CaipAssetType);\n return floors[chain.reference] ?? DEFAULT_OCCURRENCE_FLOOR;\n } catch {\n return DEFAULT_OCCURRENCE_FLOOR;\n }\n}\n\n// ============================================================================\n// TOKEN DATA SOURCE\n// ============================================================================\n\n/**\n * TokenDataSource enriches responses with token metadata from the Tokens API.\n *\n * This middleware-based data source:\n * - Checks detected assets for missing metadata/images\n * - Fetches metadata from Tokens API v3 for assets needing enrichment\n * - Filters EVM ERC-20 spam using per-chain floors from Token API\n * `/v1/suggestedOccurrenceFloors` (default floor 3)\n * - Merges fetched metadata into the response\n *\n * Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid\n * token scans.\n */\nexport class TokenDataSource {\n readonly name = CONTROLLER_NAME;\n\n getName(): string {\n return this.name;\n }\n\n /** ApiPlatformClient for cached API calls */\n readonly #apiClient: ApiPlatformClient;\n\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n readonly #getNativeAssetIds: () => string[];\n\n /** Returns the asset type for a given CAIP-19 asset ID */\n readonly #getAssetType: (\n assetId: Caip19AssetId,\n ) => 'native' | 'erc20' | 'spl';\n\n /** Shared controller messenger — used for `PhishingController:bulkScanTokens`. */\n readonly #messenger: AssetsControllerMessenger;\n\n readonly #fetchTimeoutMs: number;\n\n constructor(\n messenger: AssetsControllerMessenger,\n options: TokenDataSourceOptions,\n ) {\n this.#messenger = messenger;\n this.#apiClient = options.queryApiClient;\n this.#getNativeAssetIds = options.getNativeAssetIds;\n this.#getAssetType = options.getAssetType;\n this.#fetchTimeoutMs = options.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n }\n\n /**\n * Gets the supported networks from the API.\n * Caching is handled by ApiPlatformClient.\n *\n * @returns Set of supported chain IDs in CAIP format\n */\n async #getSupportedNetworks(): Promise<Set<string>> {\n try {\n // Use v2/supportedNetworks which returns CAIP chain IDs\n // ApiPlatformClient handles caching\n const response = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchTokenV2SupportedNetworks(),\n this.#fetchTimeoutMs,\n );\n\n // Combine full and partial support networks\n const allNetworks = [...response.fullSupport, ...response.partialSupport];\n\n return new Set(allNetworks);\n } catch (error) {\n log('Failed to fetch supported networks', { error });\n return new Set();\n }\n }\n\n /**\n * Fetches per-chain suggested occurrence floors from Token API\n * (`GET /v1/suggestedOccurrenceFloors`). Caching is handled by\n * ApiPlatformClient. Fails open to an empty map so callers fall back to\n * {@link DEFAULT_OCCURRENCE_FLOOR}.\n *\n * @returns Map of decimal chain ID → suggested occurrence floor.\n */\n async #getSuggestedOccurrenceFloors(): Promise<Record<string, number>> {\n try {\n return await fetchWithTimeout(\n () => this.#apiClient.token.fetchV1SuggestedOccurrenceFloors(),\n this.#fetchTimeoutMs,\n );\n } catch (error) {\n log('Failed to fetch suggested occurrence floors', { error });\n return {};\n }\n }\n\n /**\n * Filters asset IDs to only include those from supported networks.\n *\n * @param assetIds - Array of CAIP-19 asset IDs\n * @param supportedNetworks - Set of supported chain IDs\n * @returns Array of asset IDs from supported networks\n */\n #filterAssetsByNetwork(\n assetIds: string[],\n supportedNetworks: Set<string>,\n ): string[] {\n return assetIds.filter((assetId) => {\n try {\n const parsed = parseCaipAssetType(assetId as CaipAssetType);\n // chainId is in format \"eip155:1\" or \"tron:728126428\"\n // parsed.chain has namespace and reference properties\n const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`;\n return supportedNetworks.has(chainId);\n } catch {\n // If we can't parse the asset ID, filter it out\n return false;\n }\n });\n }\n\n /**\n * Filters non-EVM fungible `token` assets flagged as malicious by Blockaid\n * via `PhishingController:bulkScanTokens`. Only the `token` namespace (e.g.\n * Solana mints) is scanned; native (`slip44`) and EVM assets are not handled\n * here (EVM uses occurrence-count filtering instead). Fails open on error.\n *\n * @param assets - CAIP-19 asset IDs to filter (non-EVM only).\n * @returns Asset IDs with malicious tokens removed.\n */\n async #filterBlockaidSpamTokens(assets: string[]): Promise<string[]> {\n if (assets.length === 0) {\n return assets;\n }\n\n const tokensByChain: Record<string, { asset: string; address: string }[]> =\n {};\n\n for (const asset of assets) {\n try {\n const { assetNamespace, assetReference, chain } = parseCaipAssetType(\n asset as CaipAssetType,\n );\n\n if (assetNamespace === CaipAssetNamespace.Token) {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n } catch {\n // Malformed or unsupported for bulk scan — keep asset (fail open)\n }\n }\n\n if (Object.keys(tokensByChain).length === 0) {\n return assets;\n }\n\n const rejectedAssets = new Set<string>();\n\n try {\n for (const [chainId, tokenEntries] of Object.entries(tokensByChain)) {\n const addresses = tokenEntries.map((entry) => entry.address);\n const batches: string[][] = [];\n for (let i = 0; i < addresses.length; i += BULK_SCAN_BATCH_SIZE) {\n batches.push(addresses.slice(i, i + BULK_SCAN_BATCH_SIZE));\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.#messenger.call('PhishingController:bulkScanTokens', {\n chainId,\n tokens: batch,\n }),\n ),\n );\n\n const scanResponse: BulkTokenScanResponse = {};\n for (const result of batchResults) {\n if (result.status === 'fulfilled') {\n Object.assign(scanResponse, result.value);\n }\n }\n\n for (const entry of tokenEntries) {\n const result = scanResponse[entry.address];\n if (result?.result_type === TokenScanResultType.Malicious) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n } catch (error) {\n log('Blockaid bulk token scan failed; keeping all tokens', { error });\n return assets;\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * Get the middleware for enriching responses with token metadata.\n *\n * This middleware:\n * 1. Extracts the response from context\n * 2. Fetches metadata for detected assets (assets without metadata)\n * 3. Enriches the response with fetched metadata\n * 4. Calls next() at the end to continue the middleware chain\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return forDataTypes(['metadata'], async (ctx, next) => {\n // Extract response from context\n const { response } = ctx;\n\n const { assetsInfo: stateMetadata, customAssets } = ctx.getAssetsState();\n const assetIdsNeedingMetadata = new Set<string>();\n\n // Custom assets are user-imported — exempt from spam filtering.\n // State stores asset IDs in their normalized (checksummed) form, but the\n // V3 Tokens API can return them lower-cased. Lowercase both sides so the\n // bypass is robust to address-case differences across data sources.\n const customAssetIds = new Set<string>(\n Object.values(customAssets ?? {})\n .flat()\n .map((id) => id.toLowerCase()),\n );\n\n // Always include native asset IDs from NetworkEnablementController\n const nativeAssetIdsList = this.#getNativeAssetIds();\n const nativeAssetIds = new Set(\n nativeAssetIdsList.map((id) => id.toLowerCase()),\n );\n for (const nativeAssetId of nativeAssetIdsList) {\n assetIdsNeedingMetadata.add(nativeAssetId);\n }\n\n // Also fetch metadata for detected assets that are missing it\n if (response.detectedAssets) {\n for (const detectedIds of Object.values(response.detectedAssets)) {\n for (const assetId of detectedIds) {\n // Skip if response already has metadata with image\n const responseMetadata = response.assetsInfo?.[assetId];\n if (responseMetadata?.image) {\n continue;\n }\n\n // Skip if state already has metadata with image\n const existingMetadata = stateMetadata[assetId];\n if (existingMetadata?.image) {\n continue;\n }\n\n // Skip staking contracts; we use built-in metadata and do not fetch from the tokens API\n if (isStakingContractAssetId(assetId)) {\n continue;\n }\n\n assetIdsNeedingMetadata.add(assetId);\n }\n }\n }\n\n if (assetIdsNeedingMetadata.size === 0) {\n return next(ctx);\n }\n\n // Filter asset IDs to only include supported networks; load per-chain\n // occurrence floors in parallel (Token API suggested floors).\n const [supportedNetworks, suggestedOccurrenceFloors] = await Promise.all([\n this.#getSupportedNetworks(),\n this.#getSuggestedOccurrenceFloors(),\n ]);\n const supportedAssetIds = this.#filterAssetsByNetwork(\n [...assetIdsNeedingMetadata],\n supportedNetworks,\n );\n\n if (supportedAssetIds.length === 0) {\n return next(ctx);\n }\n\n try {\n const fetchOptions = {\n includeIconUrl: true,\n includeMarketData: true,\n includeMetadata: true,\n includeLabels: true,\n includeRwaData: true,\n includeAggregators: true,\n includeOccurrences: true,\n };\n\n const metadataResponse = await reduceInBatchesSerially<\n string,\n V3AssetResponse[]\n >({\n values: supportedAssetIds,\n batchSize: TOKENS_API_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResponse = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchV3Assets(batch, fetchOptions),\n this.#fetchTimeoutMs,\n );\n return [...(workingResult as V3AssetResponse[]), ...batchResponse];\n },\n initialResult: [],\n });\n\n // Split assets by chain type: EVM uses occurrence-count filtering;\n // non-EVM non-native uses Blockaid; native assets are always allowed.\n const occurrencesByAssetId = new Map(\n metadataResponse.map((a) => [a.assetId, a.occurrences]),\n );\n\n const evmErc20Ids: string[] = [];\n const nonEvmTokenIds: string[] = [];\n\n for (const assetData of metadataResponse) {\n const assetId = assetData.assetId as Caip19AssetId;\n const { assetNamespace, chain } = parseCaipAssetType(assetId);\n if (nativeAssetIds.has(assetId.toLowerCase())) {\n // Native assets are always kept — no filtering.\n } else if (\n assetNamespace === CaipAssetNamespace.Erc20 &&\n chain.namespace === KnownCaipNamespace.Eip155\n ) {\n evmErc20Ids.push(assetId);\n } else if (assetNamespace === CaipAssetNamespace.Token) {\n nonEvmTokenIds.push(assetId);\n }\n }\n\n // EVM: require per-chain suggested occurrence floor (from Token API\n // `/v1/suggestedOccurrenceFloors`, default {@link DEFAULT_OCCURRENCE_FLOOR})\n // to suppress low-signal tokens. Tokens with no occurrence data\n // (undefined) are treated the same as zero occurrences and filtered out.\n // Custom assets (user-imported) bypass the occurrence filter — users\n // can import whatever they want and we must keep their metadata even\n // if the API has fewer aggregator hits than the floor.\n const allowedEvmIds = new Set(\n evmErc20Ids.filter(\n (id) =>\n customAssetIds.has(id.toLowerCase()) ||\n (occurrencesByAssetId.get(id) ?? 0) >=\n getOccurrenceFloorForAsset(id, suggestedOccurrenceFloors) ||\n id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`),\n ),\n );\n\n // Non-EVM: Blockaid bulk scan.\n // Custom assets (user-imported) bypass Blockaid filtering.\n const nonEvmToScan = nonEvmTokenIds.filter(\n (id) => !customAssetIds.has(id.toLowerCase()),\n );\n const allowedNonEvmIds = new Set([\n ...nonEvmTokenIds.filter((id) =>\n customAssetIds.has(id.toLowerCase()),\n ),\n ...(await this.#filterBlockaidSpamTokens(nonEvmToScan)),\n ]);\n\n // Start with every asset the API returned; only remove those that\n // fail their respective filter (EVM occurrences / non-EVM Blockaid).\n // Native (slip44) and unrecognised namespaces are kept (fail open).\n const allowedAssetIds = new Set(metadataResponse.map((a) => a.assetId));\n\n for (const id of evmErc20Ids) {\n if (!allowedEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n for (const id of nonEvmTokenIds) {\n if (!allowedNonEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n\n response.assetsInfo ??= {};\n\n const filteredOutAssets = new Set<string>();\n\n for (const assetData of metadataResponse) {\n if (!allowedAssetIds.has(assetData.assetId)) {\n filteredOutAssets.add(assetData.assetId);\n continue;\n }\n\n const caipAssetId = assetData.assetId as Caip19AssetId;\n response.assetsInfo[caipAssetId] = transformV3AssetResponseToMetadata(\n caipAssetId,\n assetData,\n this.#getAssetType,\n );\n }\n\n if (filteredOutAssets.size > 0) {\n if (response.assetsBalance) {\n for (const accountBalances of Object.values(\n response.assetsBalance,\n )) {\n for (const assetId of filteredOutAssets) {\n delete (accountBalances as Record<string, unknown>)[assetId];\n }\n }\n }\n\n if (response.detectedAssets) {\n for (const [accountId, assetIds] of Object.entries(\n response.detectedAssets,\n )) {\n response.detectedAssets[accountId] = assetIds.filter(\n (id) => !filteredOutAssets.has(id),\n );\n }\n }\n }\n } catch (error) {\n log('Failed to fetch metadata', { error });\n }\n\n // Call next() at the end to continue the middleware chain\n return next(ctx);\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TokenDataSource.cjs","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yDAA2D;AAK3D,uEAAoE;AACpE,2CAAyE;AAIzE,0CAA8D;AAC9D,wCAAwC;AAOxC,8CAA4C;AAC5C,mEAG4B;AAE5B,+EAA+E;AAC/E,YAAY;AACZ,+E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,CAAC,KAAK,EAAE,EAAE,CACpB,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,mCAAmC,EAAE;gBACxD,OAAO;gBACP,MAAM,EAAE,KAAK;aACd,CAAC,CACH,CACF,CAAC;YAEF,MAAM,YAAY,GAA0B,EAAE,CAAC;YAC/C,KAAK,MAAM,MAAM,IAAI,YAAY,EAAE,CAAC;gBAClC,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;oBAClC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5C,CAAC;YACH,CAAC;YAED,KAAK,MAAM,KAAK,IAAI,YAAY,EAAE,CAAC;gBACjC,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBAC3C,IAAI,MAAM,EAAE,WAAW,KAAK,yCAAmB,CAAC,SAAS,EAAE,CAAC;oBAC1D,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qDAAqD,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { V3AssetResponse } from '@metamask/core-backend';\nimport { ApiPlatformClient } from '@metamask/core-backend';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { KnownCaipNamespace, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { AssetsControllerMessenger } from '../AssetsController';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport { forDataTypes } from '../types';\nimport type {\n Caip19AssetId,\n AssetMetadata,\n Middleware,\n FungibleAssetMetadata,\n} from '../types';\nimport { fetchWithTimeout } from '../utils';\nimport {\n isStakingContractAssetId,\n reduceInBatchesSerially,\n} from './evm-rpc-services';\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nconst CONTROLLER_NAME = 'TokenDataSource';\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\n/** Max asset IDs per tokens API request. */\nconst TOKENS_API_BATCH_SIZE = 50;\n\n/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */\nconst BULK_SCAN_BATCH_SIZE = 100;\n\n/**\n * Minimum number of aggregator occurrences required for an EVM ERC-20 token to\n * pass the spam filter. Non-EVM tokens are filtered via Blockaid bulk scan instead.\n */\nconst MIN_TOKEN_OCCURRENCES = 3;\n\n/** CAIP-19 `assetNamespace` segments used across filtering logic. */\nexport enum CaipAssetNamespace {\n Slip44 = 'slip44',\n Erc20 = 'erc20',\n Token = 'token',\n}\n\nconst MUSD_ADDRESS_LOWERCASE = '0xaca92e438df0b2401ff60da7e4337b687a2435da';\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type TokenDataSourceOptions = {\n /** ApiPlatformClient for API calls with caching */\n queryApiClient: ApiPlatformClient;\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n getNativeAssetIds: () => string[];\n /** Returns the asset type ('native' | 'erc20' | 'spl') for a given CAIP-19 asset ID */\n getAssetType: (assetId: Caip19AssetId) => 'native' | 'erc20' | 'spl';\n /**\n * Timeout in ms for a single Tokens API call (default: 15000). When it\n * fires, the batch rejects so metadata enrichment proceeds without it.\n */\n fetchTimeoutMs?: number;\n};\n\n/**\n * Messenger actions `TokenDataSource` may invoke (via {@link AssetsControllerMessenger}).\n * Not re-exported from the package public `index` (repo ESLint); import from this module when\n * typing a messenger in the same package or tests.\n */\nexport type TokenDataSourceAllowedActions =\n PhishingControllerBulkScanTokensAction;\n\n// ============================================================================\n// HELPER FUNCTIONS\n// ============================================================================\n\n/**\n * Transform V3 API response to FungibleAssetMetadata for state storage.\n *\n * Mapping:\n * - assetId → used to derive `type` (native/erc20/spl)\n * - iconUrl → image\n * - All other fields map directly\n *\n * @param assetId - CAIP-19 asset ID used to derive token type.\n * @param assetData - V3 API response data.\n * @param getAssetType - Returns the asset type for a given CAIP-19 asset ID.\n * @returns FungibleAssetMetadata for state storage.\n */\nfunction transformV3AssetResponseToMetadata(\n assetId: Caip19AssetId,\n assetData: V3AssetResponse,\n getAssetType: (id: Caip19AssetId) => 'native' | 'erc20' | 'spl',\n): AssetMetadata {\n const metadata: FungibleAssetMetadata = {\n type: getAssetType(assetId),\n // BaseAssetMetadata fields\n name: assetData.name,\n symbol: assetData.symbol,\n decimals: assetData.decimals,\n image: assetData.iconUrl,\n // Direct mapping fields\n coingeckoId: assetData.coingeckoId,\n occurrences: assetData.occurrences,\n aggregators: assetData.aggregators,\n labels: assetData.labels,\n erc20Permit: assetData.erc20Permit,\n fees: assetData.fees,\n honeypotStatus: assetData.honeypotStatus,\n storage: assetData.storage,\n isContractVerified: assetData.isContractVerified,\n description: assetData.description,\n };\n\n return metadata;\n}\n\n// ============================================================================\n// TOKEN DATA SOURCE\n// ============================================================================\n\n/**\n * TokenDataSource enriches responses with token metadata from the Tokens API.\n *\n * This middleware-based data source:\n * - Checks detected assets for missing metadata/images\n * - Fetches metadata from Tokens API v3 for assets needing enrichment\n * - Merges fetched metadata into the response\n *\n * Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid\n * token scans.\n */\nexport class TokenDataSource {\n readonly name = CONTROLLER_NAME;\n\n getName(): string {\n return this.name;\n }\n\n /** ApiPlatformClient for cached API calls */\n readonly #apiClient: ApiPlatformClient;\n\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n readonly #getNativeAssetIds: () => string[];\n\n /** Returns the asset type for a given CAIP-19 asset ID */\n readonly #getAssetType: (\n assetId: Caip19AssetId,\n ) => 'native' | 'erc20' | 'spl';\n\n /** Shared controller messenger — used for `PhishingController:bulkScanTokens`. */\n readonly #messenger: AssetsControllerMessenger;\n\n readonly #fetchTimeoutMs: number;\n\n constructor(\n messenger: AssetsControllerMessenger,\n options: TokenDataSourceOptions,\n ) {\n this.#messenger = messenger;\n this.#apiClient = options.queryApiClient;\n this.#getNativeAssetIds = options.getNativeAssetIds;\n this.#getAssetType = options.getAssetType;\n this.#fetchTimeoutMs = options.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n }\n\n /**\n * Gets the supported networks from the API.\n * Caching is handled by ApiPlatformClient.\n *\n * @returns Set of supported chain IDs in CAIP format\n */\n async #getSupportedNetworks(): Promise<Set<string>> {\n try {\n // Use v2/supportedNetworks which returns CAIP chain IDs\n // ApiPlatformClient handles caching\n const response = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchTokenV2SupportedNetworks(),\n this.#fetchTimeoutMs,\n );\n\n // Combine full and partial support networks\n const allNetworks = [...response.fullSupport, ...response.partialSupport];\n\n return new Set(allNetworks);\n } catch (error) {\n log('Failed to fetch supported networks', { error });\n return new Set();\n }\n }\n\n /**\n * Filters asset IDs to only include those from supported networks.\n *\n * @param assetIds - Array of CAIP-19 asset IDs\n * @param supportedNetworks - Set of supported chain IDs\n * @returns Array of asset IDs from supported networks\n */\n #filterAssetsByNetwork(\n assetIds: string[],\n supportedNetworks: Set<string>,\n ): string[] {\n return assetIds.filter((assetId) => {\n try {\n const parsed = parseCaipAssetType(assetId as CaipAssetType);\n // chainId is in format \"eip155:1\" or \"tron:728126428\"\n // parsed.chain has namespace and reference properties\n const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`;\n return supportedNetworks.has(chainId);\n } catch {\n // If we can't parse the asset ID, filter it out\n return false;\n }\n });\n }\n\n /**\n * Filters non-EVM fungible `token` assets flagged as malicious by Blockaid\n * via `PhishingController:bulkScanTokens`. Only the `token` namespace (e.g.\n * Solana mints) is scanned; native (`slip44`) and EVM assets are not handled\n * here (EVM uses occurrence-count filtering instead). Fails open on error.\n *\n * @param assets - CAIP-19 asset IDs to filter (non-EVM only).\n * @returns Asset IDs with malicious tokens removed.\n */\n async #filterBlockaidSpamTokens(assets: string[]): Promise<string[]> {\n if (assets.length === 0) {\n return assets;\n }\n\n const tokensByChain: Record<string, { asset: string; address: string }[]> =\n {};\n\n for (const asset of assets) {\n try {\n const { assetNamespace, assetReference, chain } = parseCaipAssetType(\n asset as CaipAssetType,\n );\n\n if (assetNamespace === CaipAssetNamespace.Token) {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n } catch {\n // Malformed or unsupported for bulk scan — keep asset (fail open)\n }\n }\n\n if (Object.keys(tokensByChain).length === 0) {\n return assets;\n }\n\n const rejectedAssets = new Set<string>();\n\n try {\n for (const [chainId, tokenEntries] of Object.entries(tokensByChain)) {\n const addresses = tokenEntries.map((entry) => entry.address);\n const batches: string[][] = [];\n for (let i = 0; i < addresses.length; i += BULK_SCAN_BATCH_SIZE) {\n batches.push(addresses.slice(i, i + BULK_SCAN_BATCH_SIZE));\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.#messenger.call('PhishingController:bulkScanTokens', {\n chainId,\n tokens: batch,\n }),\n ),\n );\n\n const scanResponse: BulkTokenScanResponse = {};\n for (const result of batchResults) {\n if (result.status === 'fulfilled') {\n Object.assign(scanResponse, result.value);\n }\n }\n\n for (const entry of tokenEntries) {\n const result = scanResponse[entry.address];\n if (result?.result_type === TokenScanResultType.Malicious) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n } catch (error) {\n log('Blockaid bulk token scan failed; keeping all tokens', { error });\n return assets;\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * Get the middleware for enriching responses with token metadata.\n *\n * This middleware:\n * 1. Extracts the response from context\n * 2. Fetches metadata for detected assets (assets without metadata)\n * 3. Enriches the response with fetched metadata\n * 4. Calls next() at the end to continue the middleware chain\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return forDataTypes(['metadata'], async (ctx, next) => {\n // Extract response from context\n const { response } = ctx;\n\n const { assetsInfo: stateMetadata, customAssets } = ctx.getAssetsState();\n const assetIdsNeedingMetadata = new Set<string>();\n\n // Custom assets are user-imported — exempt from spam filtering.\n // State stores asset IDs in their normalized (checksummed) form, but the\n // V3 Tokens API can return them lower-cased. Lowercase both sides so the\n // bypass is robust to address-case differences across data sources.\n const customAssetIds = new Set<string>(\n Object.values(customAssets ?? {})\n .flat()\n .map((id) => id.toLowerCase()),\n );\n\n // Always include native asset IDs from NetworkEnablementController\n const nativeAssetIdsList = this.#getNativeAssetIds();\n const nativeAssetIds = new Set(\n nativeAssetIdsList.map((id) => id.toLowerCase()),\n );\n for (const nativeAssetId of nativeAssetIdsList) {\n assetIdsNeedingMetadata.add(nativeAssetId);\n }\n\n // Also fetch metadata for detected assets that are missing it\n if (response.detectedAssets) {\n for (const detectedIds of Object.values(response.detectedAssets)) {\n for (const assetId of detectedIds) {\n // Skip if response already has metadata with image\n const responseMetadata = response.assetsInfo?.[assetId];\n if (responseMetadata?.image) {\n continue;\n }\n\n // Skip if state already has metadata with image\n const existingMetadata = stateMetadata[assetId];\n if (existingMetadata?.image) {\n continue;\n }\n\n // Skip staking contracts; we use built-in metadata and do not fetch from the tokens API\n if (isStakingContractAssetId(assetId)) {\n continue;\n }\n\n assetIdsNeedingMetadata.add(assetId);\n }\n }\n }\n\n if (assetIdsNeedingMetadata.size === 0) {\n return next(ctx);\n }\n\n // Filter asset IDs to only include supported networks\n const supportedNetworks = await this.#getSupportedNetworks();\n const supportedAssetIds = this.#filterAssetsByNetwork(\n [...assetIdsNeedingMetadata],\n supportedNetworks,\n );\n\n if (supportedAssetIds.length === 0) {\n return next(ctx);\n }\n\n try {\n const fetchOptions = {\n includeIconUrl: true,\n includeMarketData: true,\n includeMetadata: true,\n includeLabels: true,\n includeRwaData: true,\n includeAggregators: true,\n includeOccurrences: true,\n };\n\n const metadataResponse = await reduceInBatchesSerially<\n string,\n V3AssetResponse[]\n >({\n values: supportedAssetIds,\n batchSize: TOKENS_API_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResponse = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchV3Assets(batch, fetchOptions),\n this.#fetchTimeoutMs,\n );\n return [...(workingResult as V3AssetResponse[]), ...batchResponse];\n },\n initialResult: [],\n });\n\n // Split assets by chain type: EVM uses occurrence-count filtering;\n // non-EVM non-native uses Blockaid; native assets are always allowed.\n const occurrencesByAssetId = new Map(\n metadataResponse.map((a) => [a.assetId, a.occurrences]),\n );\n\n const evmErc20Ids: string[] = [];\n const nonEvmTokenIds: string[] = [];\n\n for (const assetData of metadataResponse) {\n const assetId = assetData.assetId as Caip19AssetId;\n const { assetNamespace, chain } = parseCaipAssetType(assetId);\n if (nativeAssetIds.has(assetId.toLowerCase())) {\n // Native assets are always kept — no filtering.\n } else if (\n assetNamespace === CaipAssetNamespace.Erc20 &&\n chain.namespace === KnownCaipNamespace.Eip155\n ) {\n evmErc20Ids.push(assetId);\n } else if (assetNamespace === CaipAssetNamespace.Token) {\n nonEvmTokenIds.push(assetId);\n }\n }\n\n // EVM: require minimum occurrence count to suppress low-signal tokens.\n // Tokens with no occurrence data (undefined) are treated the same as\n // zero occurrences and filtered out.\n // Custom assets (user-imported) bypass the occurrence filter — users\n // can import whatever they want and we must keep their metadata even\n // if the API has fewer than `MIN_TOKEN_OCCURRENCES` aggregator hits.\n const allowedEvmIds = new Set(\n evmErc20Ids.filter(\n (id) =>\n customAssetIds.has(id.toLowerCase()) ||\n (occurrencesByAssetId.get(id) ?? 0) >= MIN_TOKEN_OCCURRENCES ||\n id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`),\n ),\n );\n\n // Non-EVM: Blockaid bulk scan.\n // Custom assets (user-imported) bypass Blockaid filtering.\n const nonEvmToScan = nonEvmTokenIds.filter(\n (id) => !customAssetIds.has(id.toLowerCase()),\n );\n const allowedNonEvmIds = new Set([\n ...nonEvmTokenIds.filter((id) =>\n customAssetIds.has(id.toLowerCase()),\n ),\n ...(await this.#filterBlockaidSpamTokens(nonEvmToScan)),\n ]);\n\n // Start with every asset the API returned; only remove those that\n // fail their respective filter (EVM occurrences / non-EVM Blockaid).\n // Native (slip44) and unrecognised namespaces are kept (fail open).\n const allowedAssetIds = new Set(metadataResponse.map((a) => a.assetId));\n\n for (const id of evmErc20Ids) {\n if (!allowedEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n for (const id of nonEvmTokenIds) {\n if (!allowedNonEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n\n response.assetsInfo ??= {};\n\n const filteredOutAssets = new Set<string>();\n\n for (const assetData of metadataResponse) {\n if (!allowedAssetIds.has(assetData.assetId)) {\n filteredOutAssets.add(assetData.assetId);\n continue;\n }\n\n const caipAssetId = assetData.assetId as Caip19AssetId;\n response.assetsInfo[caipAssetId] = transformV3AssetResponseToMetadata(\n caipAssetId,\n assetData,\n this.#getAssetType,\n );\n }\n\n if (filteredOutAssets.size > 0) {\n if (response.assetsBalance) {\n for (const accountBalances of Object.values(\n response.assetsBalance,\n )) {\n for (const assetId of filteredOutAssets) {\n delete (accountBalances as Record<string, unknown>)[assetId];\n }\n }\n }\n\n if (response.detectedAssets) {\n for (const [accountId, assetIds] of Object.entries(\n response.detectedAssets,\n )) {\n response.detectedAssets[accountId] = assetIds.filter(\n (id) => !filteredOutAssets.has(id),\n );\n }\n }\n }\n } catch (error) {\n log('Failed to fetch metadata', { error });\n }\n\n // Call next() at the end to continue the middleware chain\n return next(ctx);\n });\n }\n}\n"]}
|
|
@@ -33,8 +33,6 @@ export type TokenDataSourceAllowedActions = PhishingControllerBulkScanTokensActi
|
|
|
33
33
|
* This middleware-based data source:
|
|
34
34
|
* - Checks detected assets for missing metadata/images
|
|
35
35
|
* - Fetches metadata from Tokens API v3 for assets needing enrichment
|
|
36
|
-
* - Filters EVM ERC-20 spam using per-chain floors from Token API
|
|
37
|
-
* `/v1/suggestedOccurrenceFloors` (default floor 3)
|
|
38
36
|
* - Merges fetched metadata into the response
|
|
39
37
|
*
|
|
40
38
|
* Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenDataSource.d.cts","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAC3D,OAAO,KAAK,EAEV,sCAAsC,EACvC,sCAAsC;AAKvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,gCAA4B;AAGrE,OAAO,KAAK,EACV,aAAa,EAEb,UAAU,EAEX,qBAAiB;
|
|
1
|
+
{"version":3,"file":"TokenDataSource.d.cts","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAC3D,OAAO,KAAK,EAEV,sCAAsC,EACvC,sCAAsC;AAKvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,gCAA4B;AAGrE,OAAO,KAAK,EACV,aAAa,EAEb,UAAU,EAEX,qBAAiB;AA4BlB,qEAAqE;AACrE,oBAAY,kBAAkB;IAC5B,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,KAAK,UAAU;CAChB;AAQD,MAAM,MAAM,sBAAsB,GAAG;IACnC,mDAAmD;IACnD,cAAc,EAAE,iBAAiB,CAAC;IAClC,8EAA8E;IAC9E,iBAAiB,EAAE,MAAM,MAAM,EAAE,CAAC;IAClC,uFAAuF;IACvF,YAAY,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,QAAQ,GAAG,OAAO,GAAG,KAAK,CAAC;IACrE;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,GACvC,sCAAsC,CAAC;AAmDzC;;;;;;;;;;GAUG;AACH,qBAAa,eAAe;;IAC1B,QAAQ,CAAC,IAAI,qBAAmB;IAEhC,OAAO,IAAI,MAAM;gBAqBf,SAAS,EAAE,yBAAyB,EACpC,OAAO,EAAE,sBAAsB;IA2IjC;;;;;;;;;;OAUG;IACH,IAAI,gBAAgB,IAAI,UAAU,CAgNjC;CACF"}
|
|
@@ -33,8 +33,6 @@ export type TokenDataSourceAllowedActions = PhishingControllerBulkScanTokensActi
|
|
|
33
33
|
* This middleware-based data source:
|
|
34
34
|
* - Checks detected assets for missing metadata/images
|
|
35
35
|
* - Fetches metadata from Tokens API v3 for assets needing enrichment
|
|
36
|
-
* - Filters EVM ERC-20 spam using per-chain floors from Token API
|
|
37
|
-
* `/v1/suggestedOccurrenceFloors` (default floor 3)
|
|
38
36
|
* - Merges fetched metadata into the response
|
|
39
37
|
*
|
|
40
38
|
* Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenDataSource.d.mts","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAC3D,OAAO,KAAK,EAEV,sCAAsC,EACvC,sCAAsC;AAKvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,gCAA4B;AAGrE,OAAO,KAAK,EACV,aAAa,EAEb,UAAU,EAEX,qBAAiB;
|
|
1
|
+
{"version":3,"file":"TokenDataSource.d.mts","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAC3D,OAAO,KAAK,EAEV,sCAAsC,EACvC,sCAAsC;AAKvC,OAAO,KAAK,EAAE,yBAAyB,EAAE,gCAA4B;AAGrE,OAAO,KAAK,EACV,aAAa,EAEb,UAAU,EAEX,qBAAiB;AA4BlB,qEAAqE;AACrE,oBAAY,kBAAkB;IAC5B,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,KAAK,UAAU;CAChB;AAQD,MAAM,MAAM,sBAAsB,GAAG;IACnC,mDAAmD;IACnD,cAAc,EAAE,iBAAiB,CAAC;IAClC,8EAA8E;IAC9E,iBAAiB,EAAE,MAAM,MAAM,EAAE,CAAC;IAClC,uFAAuF;IACvF,YAAY,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,QAAQ,GAAG,OAAO,GAAG,KAAK,CAAC;IACrE;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,6BAA6B,GACvC,sCAAsC,CAAC;AAmDzC;;;;;;;;;;GAUG;AACH,qBAAa,eAAe;;IAC1B,QAAQ,CAAC,IAAI,qBAAmB;IAEhC,OAAO,IAAI,MAAM;gBAqBf,SAAS,EAAE,yBAAyB,EACpC,OAAO,EAAE,sBAAsB;IA2IjC;;;;;;;;;;OAUG;IACH,IAAI,gBAAgB,IAAI,UAAU,CAgNjC;CACF"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _TokenDataSource_instances, _TokenDataSource_apiClient, _TokenDataSource_getNativeAssetIds, _TokenDataSource_getAssetType, _TokenDataSource_messenger, _TokenDataSource_fetchTimeoutMs, _TokenDataSource_getSupportedNetworks,
|
|
12
|
+
var _TokenDataSource_instances, _TokenDataSource_apiClient, _TokenDataSource_getNativeAssetIds, _TokenDataSource_getAssetType, _TokenDataSource_messenger, _TokenDataSource_fetchTimeoutMs, _TokenDataSource_getSupportedNetworks, _TokenDataSource_filterAssetsByNetwork, _TokenDataSource_filterBlockaidSpamTokens;
|
|
13
13
|
import { ApiPlatformClient } from "@metamask/core-backend";
|
|
14
14
|
import { TokenScanResultType } from "@metamask/phishing-controller";
|
|
15
15
|
import { KnownCaipNamespace, parseCaipAssetType } from "@metamask/utils";
|
|
@@ -28,11 +28,10 @@ const TOKENS_API_BATCH_SIZE = 50;
|
|
|
28
28
|
/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */
|
|
29
29
|
const BULK_SCAN_BATCH_SIZE = 100;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* the floors request fails). Non-EVM tokens are filtered via Blockaid instead.
|
|
31
|
+
* Minimum number of aggregator occurrences required for an EVM ERC-20 token to
|
|
32
|
+
* pass the spam filter. Non-EVM tokens are filtered via Blockaid bulk scan instead.
|
|
34
33
|
*/
|
|
35
|
-
const
|
|
34
|
+
const MIN_TOKEN_OCCURRENCES = 3;
|
|
36
35
|
/** CAIP-19 `assetNamespace` segments used across filtering logic. */
|
|
37
36
|
export var CaipAssetNamespace;
|
|
38
37
|
(function (CaipAssetNamespace) {
|
|
@@ -79,23 +78,6 @@ function transformV3AssetResponseToMetadata(assetId, assetData, getAssetType) {
|
|
|
79
78
|
};
|
|
80
79
|
return metadata;
|
|
81
80
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Resolve the occurrence floor for an EVM asset from suggested floors keyed by
|
|
84
|
-
* decimal chain ID (e.g. `{ "1": 3, "143": 1 }`).
|
|
85
|
-
*
|
|
86
|
-
* @param assetId - CAIP-19 asset ID.
|
|
87
|
-
* @param floors - Map of decimal chain ID → suggested floor.
|
|
88
|
-
* @returns Floor to apply, or {@link DEFAULT_OCCURRENCE_FLOOR} when missing.
|
|
89
|
-
*/
|
|
90
|
-
function getOccurrenceFloorForAsset(assetId, floors) {
|
|
91
|
-
try {
|
|
92
|
-
const { chain } = parseCaipAssetType(assetId);
|
|
93
|
-
return floors[chain.reference] ?? DEFAULT_OCCURRENCE_FLOOR;
|
|
94
|
-
}
|
|
95
|
-
catch {
|
|
96
|
-
return DEFAULT_OCCURRENCE_FLOOR;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
81
|
// ============================================================================
|
|
100
82
|
// TOKEN DATA SOURCE
|
|
101
83
|
// ============================================================================
|
|
@@ -105,8 +87,6 @@ function getOccurrenceFloorForAsset(assetId, floors) {
|
|
|
105
87
|
* This middleware-based data source:
|
|
106
88
|
* - Checks detected assets for missing metadata/images
|
|
107
89
|
* - Fetches metadata from Tokens API v3 for assets needing enrichment
|
|
108
|
-
* - Filters EVM ERC-20 spam using per-chain floors from Token API
|
|
109
|
-
* `/v1/suggestedOccurrenceFloors` (default floor 3)
|
|
110
90
|
* - Merges fetched metadata into the response
|
|
111
91
|
*
|
|
112
92
|
* Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid
|
|
@@ -189,12 +169,8 @@ export class TokenDataSource {
|
|
|
189
169
|
if (assetIdsNeedingMetadata.size === 0) {
|
|
190
170
|
return next(ctx);
|
|
191
171
|
}
|
|
192
|
-
// Filter asset IDs to only include supported networks
|
|
193
|
-
|
|
194
|
-
const [supportedNetworks, suggestedOccurrenceFloors] = await Promise.all([
|
|
195
|
-
__classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSupportedNetworks).call(this),
|
|
196
|
-
__classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSuggestedOccurrenceFloors).call(this),
|
|
197
|
-
]);
|
|
172
|
+
// Filter asset IDs to only include supported networks
|
|
173
|
+
const supportedNetworks = await __classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_getSupportedNetworks).call(this);
|
|
198
174
|
const supportedAssetIds = __classPrivateFieldGet(this, _TokenDataSource_instances, "m", _TokenDataSource_filterAssetsByNetwork).call(this, [...assetIdsNeedingMetadata], supportedNetworks);
|
|
199
175
|
if (supportedAssetIds.length === 0) {
|
|
200
176
|
return next(ctx);
|
|
@@ -237,16 +213,14 @@ export class TokenDataSource {
|
|
|
237
213
|
nonEvmTokenIds.push(assetId);
|
|
238
214
|
}
|
|
239
215
|
}
|
|
240
|
-
// EVM: require
|
|
241
|
-
//
|
|
242
|
-
//
|
|
243
|
-
// (undefined) are treated the same as zero occurrences and filtered out.
|
|
216
|
+
// EVM: require minimum occurrence count to suppress low-signal tokens.
|
|
217
|
+
// Tokens with no occurrence data (undefined) are treated the same as
|
|
218
|
+
// zero occurrences and filtered out.
|
|
244
219
|
// Custom assets (user-imported) bypass the occurrence filter — users
|
|
245
220
|
// can import whatever they want and we must keep their metadata even
|
|
246
|
-
// if the API has fewer aggregator hits
|
|
221
|
+
// if the API has fewer than `MIN_TOKEN_OCCURRENCES` aggregator hits.
|
|
247
222
|
const allowedEvmIds = new Set(evmErc20Ids.filter((id) => customAssetIds.has(id.toLowerCase()) ||
|
|
248
|
-
(occurrencesByAssetId.get(id) ?? 0) >=
|
|
249
|
-
getOccurrenceFloorForAsset(id, suggestedOccurrenceFloors) ||
|
|
223
|
+
(occurrencesByAssetId.get(id) ?? 0) >= MIN_TOKEN_OCCURRENCES ||
|
|
250
224
|
id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`)));
|
|
251
225
|
// Non-EVM: Blockaid bulk scan.
|
|
252
226
|
// Custom assets (user-imported) bypass Blockaid filtering.
|
|
@@ -322,23 +296,6 @@ async function _TokenDataSource_getSupportedNetworks() {
|
|
|
322
296
|
log('Failed to fetch supported networks', { error });
|
|
323
297
|
return new Set();
|
|
324
298
|
}
|
|
325
|
-
}, _TokenDataSource_getSuggestedOccurrenceFloors =
|
|
326
|
-
/**
|
|
327
|
-
* Fetches per-chain suggested occurrence floors from Token API
|
|
328
|
-
* (`GET /v1/suggestedOccurrenceFloors`). Caching is handled by
|
|
329
|
-
* ApiPlatformClient. Fails open to an empty map so callers fall back to
|
|
330
|
-
* {@link DEFAULT_OCCURRENCE_FLOOR}.
|
|
331
|
-
*
|
|
332
|
-
* @returns Map of decimal chain ID → suggested occurrence floor.
|
|
333
|
-
*/
|
|
334
|
-
async function _TokenDataSource_getSuggestedOccurrenceFloors() {
|
|
335
|
-
try {
|
|
336
|
-
return await fetchWithTimeout(() => __classPrivateFieldGet(this, _TokenDataSource_apiClient, "f").token.fetchV1SuggestedOccurrenceFloors(), __classPrivateFieldGet(this, _TokenDataSource_fetchTimeoutMs, "f"));
|
|
337
|
-
}
|
|
338
|
-
catch (error) {
|
|
339
|
-
log('Failed to fetch suggested occurrence floors', { error });
|
|
340
|
-
return {};
|
|
341
|
-
}
|
|
342
299
|
}, _TokenDataSource_filterAssetsByNetwork = function _TokenDataSource_filterAssetsByNetwork(assetIds, supportedNetworks) {
|
|
343
300
|
return assetIds.filter((assetId) => {
|
|
344
301
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenDataSource.mjs","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAK3D,OAAO,EAAE,mBAAmB,EAAE,sCAAsC;AACpE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,wBAAwB;AAIzE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAkB;AAC9D,OAAO,EAAE,YAAY,EAAE,qBAAiB;AAOxC,OAAO,EAAE,gBAAgB,EAAE,2BAAiB;AAC5C,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACxB,qCAA2B;AAE5B,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAC1C,MAAM,wBAAwB,GAAG,KAAM,CAAC;AAExC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D,4CAA4C;AAC5C,MAAM,qBAAqB,GAAG,EAAE,C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H,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qDAAqD,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { V3AssetResponse } from '@metamask/core-backend';\nimport { ApiPlatformClient } from '@metamask/core-backend';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { KnownCaipNamespace, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { AssetsControllerMessenger } from '../AssetsController';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport { forDataTypes } from '../types';\nimport type {\n Caip19AssetId,\n AssetMetadata,\n Middleware,\n FungibleAssetMetadata,\n} from '../types';\nimport { fetchWithTimeout } from '../utils';\nimport {\n isStakingContractAssetId,\n reduceInBatchesSerially,\n} from './evm-rpc-services';\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nconst CONTROLLER_NAME = 'TokenDataSource';\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\n/** Max asset IDs per tokens API request. */\nconst TOKENS_API_BATCH_SIZE = 50;\n\n/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */\nconst BULK_SCAN_BATCH_SIZE = 100;\n\n/**\n * Fallback minimum aggregator occurrences for EVM ERC-20 spam filtering when\n * Token API `/v1/suggestedOccurrenceFloors` has no entry for the chain (or\n * the floors request fails). Non-EVM tokens are filtered via Blockaid instead.\n */\nconst DEFAULT_OCCURRENCE_FLOOR = 3;\n\n/** CAIP-19 `assetNamespace` segments used across filtering logic. */\nexport enum CaipAssetNamespace {\n Slip44 = 'slip44',\n Erc20 = 'erc20',\n Token = 'token',\n}\n\nconst MUSD_ADDRESS_LOWERCASE = '0xaca92e438df0b2401ff60da7e4337b687a2435da';\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type TokenDataSourceOptions = {\n /** ApiPlatformClient for API calls with caching */\n queryApiClient: ApiPlatformClient;\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n getNativeAssetIds: () => string[];\n /** Returns the asset type ('native' | 'erc20' | 'spl') for a given CAIP-19 asset ID */\n getAssetType: (assetId: Caip19AssetId) => 'native' | 'erc20' | 'spl';\n /**\n * Timeout in ms for a single Tokens API call (default: 15000). When it\n * fires, the batch rejects so metadata enrichment proceeds without it.\n */\n fetchTimeoutMs?: number;\n};\n\n/**\n * Messenger actions `TokenDataSource` may invoke (via {@link AssetsControllerMessenger}).\n * Not re-exported from the package public `index` (repo ESLint); import from this module when\n * typing a messenger in the same package or tests.\n */\nexport type TokenDataSourceAllowedActions =\n PhishingControllerBulkScanTokensAction;\n\n// ============================================================================\n// HELPER FUNCTIONS\n// ============================================================================\n\n/**\n * Transform V3 API response to FungibleAssetMetadata for state storage.\n *\n * Mapping:\n * - assetId → used to derive `type` (native/erc20/spl)\n * - iconUrl → image\n * - All other fields map directly\n *\n * @param assetId - CAIP-19 asset ID used to derive token type.\n * @param assetData - V3 API response data.\n * @param getAssetType - Returns the asset type for a given CAIP-19 asset ID.\n * @returns FungibleAssetMetadata for state storage.\n */\nfunction transformV3AssetResponseToMetadata(\n assetId: Caip19AssetId,\n assetData: V3AssetResponse,\n getAssetType: (id: Caip19AssetId) => 'native' | 'erc20' | 'spl',\n): AssetMetadata {\n const metadata: FungibleAssetMetadata = {\n type: getAssetType(assetId),\n // BaseAssetMetadata fields\n name: assetData.name,\n symbol: assetData.symbol,\n decimals: assetData.decimals,\n image: assetData.iconUrl,\n // Direct mapping fields\n coingeckoId: assetData.coingeckoId,\n occurrences: assetData.occurrences,\n aggregators: assetData.aggregators,\n labels: assetData.labels,\n erc20Permit: assetData.erc20Permit,\n fees: assetData.fees,\n honeypotStatus: assetData.honeypotStatus,\n storage: assetData.storage,\n isContractVerified: assetData.isContractVerified,\n description: assetData.description,\n };\n\n return metadata;\n}\n\n/**\n * Resolve the occurrence floor for an EVM asset from suggested floors keyed by\n * decimal chain ID (e.g. `{ \"1\": 3, \"143\": 1 }`).\n *\n * @param assetId - CAIP-19 asset ID.\n * @param floors - Map of decimal chain ID → suggested floor.\n * @returns Floor to apply, or {@link DEFAULT_OCCURRENCE_FLOOR} when missing.\n */\nfunction getOccurrenceFloorForAsset(\n assetId: string,\n floors: Record<string, number>,\n): number {\n try {\n const { chain } = parseCaipAssetType(assetId as CaipAssetType);\n return floors[chain.reference] ?? DEFAULT_OCCURRENCE_FLOOR;\n } catch {\n return DEFAULT_OCCURRENCE_FLOOR;\n }\n}\n\n// ============================================================================\n// TOKEN DATA SOURCE\n// ============================================================================\n\n/**\n * TokenDataSource enriches responses with token metadata from the Tokens API.\n *\n * This middleware-based data source:\n * - Checks detected assets for missing metadata/images\n * - Fetches metadata from Tokens API v3 for assets needing enrichment\n * - Filters EVM ERC-20 spam using per-chain floors from Token API\n * `/v1/suggestedOccurrenceFloors` (default floor 3)\n * - Merges fetched metadata into the response\n *\n * Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid\n * token scans.\n */\nexport class TokenDataSource {\n readonly name = CONTROLLER_NAME;\n\n getName(): string {\n return this.name;\n }\n\n /** ApiPlatformClient for cached API calls */\n readonly #apiClient: ApiPlatformClient;\n\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n readonly #getNativeAssetIds: () => string[];\n\n /** Returns the asset type for a given CAIP-19 asset ID */\n readonly #getAssetType: (\n assetId: Caip19AssetId,\n ) => 'native' | 'erc20' | 'spl';\n\n /** Shared controller messenger — used for `PhishingController:bulkScanTokens`. */\n readonly #messenger: AssetsControllerMessenger;\n\n readonly #fetchTimeoutMs: number;\n\n constructor(\n messenger: AssetsControllerMessenger,\n options: TokenDataSourceOptions,\n ) {\n this.#messenger = messenger;\n this.#apiClient = options.queryApiClient;\n this.#getNativeAssetIds = options.getNativeAssetIds;\n this.#getAssetType = options.getAssetType;\n this.#fetchTimeoutMs = options.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n }\n\n /**\n * Gets the supported networks from the API.\n * Caching is handled by ApiPlatformClient.\n *\n * @returns Set of supported chain IDs in CAIP format\n */\n async #getSupportedNetworks(): Promise<Set<string>> {\n try {\n // Use v2/supportedNetworks which returns CAIP chain IDs\n // ApiPlatformClient handles caching\n const response = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchTokenV2SupportedNetworks(),\n this.#fetchTimeoutMs,\n );\n\n // Combine full and partial support networks\n const allNetworks = [...response.fullSupport, ...response.partialSupport];\n\n return new Set(allNetworks);\n } catch (error) {\n log('Failed to fetch supported networks', { error });\n return new Set();\n }\n }\n\n /**\n * Fetches per-chain suggested occurrence floors from Token API\n * (`GET /v1/suggestedOccurrenceFloors`). Caching is handled by\n * ApiPlatformClient. Fails open to an empty map so callers fall back to\n * {@link DEFAULT_OCCURRENCE_FLOOR}.\n *\n * @returns Map of decimal chain ID → suggested occurrence floor.\n */\n async #getSuggestedOccurrenceFloors(): Promise<Record<string, number>> {\n try {\n return await fetchWithTimeout(\n () => this.#apiClient.token.fetchV1SuggestedOccurrenceFloors(),\n this.#fetchTimeoutMs,\n );\n } catch (error) {\n log('Failed to fetch suggested occurrence floors', { error });\n return {};\n }\n }\n\n /**\n * Filters asset IDs to only include those from supported networks.\n *\n * @param assetIds - Array of CAIP-19 asset IDs\n * @param supportedNetworks - Set of supported chain IDs\n * @returns Array of asset IDs from supported networks\n */\n #filterAssetsByNetwork(\n assetIds: string[],\n supportedNetworks: Set<string>,\n ): string[] {\n return assetIds.filter((assetId) => {\n try {\n const parsed = parseCaipAssetType(assetId as CaipAssetType);\n // chainId is in format \"eip155:1\" or \"tron:728126428\"\n // parsed.chain has namespace and reference properties\n const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`;\n return supportedNetworks.has(chainId);\n } catch {\n // If we can't parse the asset ID, filter it out\n return false;\n }\n });\n }\n\n /**\n * Filters non-EVM fungible `token` assets flagged as malicious by Blockaid\n * via `PhishingController:bulkScanTokens`. Only the `token` namespace (e.g.\n * Solana mints) is scanned; native (`slip44`) and EVM assets are not handled\n * here (EVM uses occurrence-count filtering instead). Fails open on error.\n *\n * @param assets - CAIP-19 asset IDs to filter (non-EVM only).\n * @returns Asset IDs with malicious tokens removed.\n */\n async #filterBlockaidSpamTokens(assets: string[]): Promise<string[]> {\n if (assets.length === 0) {\n return assets;\n }\n\n const tokensByChain: Record<string, { asset: string; address: string }[]> =\n {};\n\n for (const asset of assets) {\n try {\n const { assetNamespace, assetReference, chain } = parseCaipAssetType(\n asset as CaipAssetType,\n );\n\n if (assetNamespace === CaipAssetNamespace.Token) {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n } catch {\n // Malformed or unsupported for bulk scan — keep asset (fail open)\n }\n }\n\n if (Object.keys(tokensByChain).length === 0) {\n return assets;\n }\n\n const rejectedAssets = new Set<string>();\n\n try {\n for (const [chainId, tokenEntries] of Object.entries(tokensByChain)) {\n const addresses = tokenEntries.map((entry) => entry.address);\n const batches: string[][] = [];\n for (let i = 0; i < addresses.length; i += BULK_SCAN_BATCH_SIZE) {\n batches.push(addresses.slice(i, i + BULK_SCAN_BATCH_SIZE));\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.#messenger.call('PhishingController:bulkScanTokens', {\n chainId,\n tokens: batch,\n }),\n ),\n );\n\n const scanResponse: BulkTokenScanResponse = {};\n for (const result of batchResults) {\n if (result.status === 'fulfilled') {\n Object.assign(scanResponse, result.value);\n }\n }\n\n for (const entry of tokenEntries) {\n const result = scanResponse[entry.address];\n if (result?.result_type === TokenScanResultType.Malicious) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n } catch (error) {\n log('Blockaid bulk token scan failed; keeping all tokens', { error });\n return assets;\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * Get the middleware for enriching responses with token metadata.\n *\n * This middleware:\n * 1. Extracts the response from context\n * 2. Fetches metadata for detected assets (assets without metadata)\n * 3. Enriches the response with fetched metadata\n * 4. Calls next() at the end to continue the middleware chain\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return forDataTypes(['metadata'], async (ctx, next) => {\n // Extract response from context\n const { response } = ctx;\n\n const { assetsInfo: stateMetadata, customAssets } = ctx.getAssetsState();\n const assetIdsNeedingMetadata = new Set<string>();\n\n // Custom assets are user-imported — exempt from spam filtering.\n // State stores asset IDs in their normalized (checksummed) form, but the\n // V3 Tokens API can return them lower-cased. Lowercase both sides so the\n // bypass is robust to address-case differences across data sources.\n const customAssetIds = new Set<string>(\n Object.values(customAssets ?? {})\n .flat()\n .map((id) => id.toLowerCase()),\n );\n\n // Always include native asset IDs from NetworkEnablementController\n const nativeAssetIdsList = this.#getNativeAssetIds();\n const nativeAssetIds = new Set(\n nativeAssetIdsList.map((id) => id.toLowerCase()),\n );\n for (const nativeAssetId of nativeAssetIdsList) {\n assetIdsNeedingMetadata.add(nativeAssetId);\n }\n\n // Also fetch metadata for detected assets that are missing it\n if (response.detectedAssets) {\n for (const detectedIds of Object.values(response.detectedAssets)) {\n for (const assetId of detectedIds) {\n // Skip if response already has metadata with image\n const responseMetadata = response.assetsInfo?.[assetId];\n if (responseMetadata?.image) {\n continue;\n }\n\n // Skip if state already has metadata with image\n const existingMetadata = stateMetadata[assetId];\n if (existingMetadata?.image) {\n continue;\n }\n\n // Skip staking contracts; we use built-in metadata and do not fetch from the tokens API\n if (isStakingContractAssetId(assetId)) {\n continue;\n }\n\n assetIdsNeedingMetadata.add(assetId);\n }\n }\n }\n\n if (assetIdsNeedingMetadata.size === 0) {\n return next(ctx);\n }\n\n // Filter asset IDs to only include supported networks; load per-chain\n // occurrence floors in parallel (Token API suggested floors).\n const [supportedNetworks, suggestedOccurrenceFloors] = await Promise.all([\n this.#getSupportedNetworks(),\n this.#getSuggestedOccurrenceFloors(),\n ]);\n const supportedAssetIds = this.#filterAssetsByNetwork(\n [...assetIdsNeedingMetadata],\n supportedNetworks,\n );\n\n if (supportedAssetIds.length === 0) {\n return next(ctx);\n }\n\n try {\n const fetchOptions = {\n includeIconUrl: true,\n includeMarketData: true,\n includeMetadata: true,\n includeLabels: true,\n includeRwaData: true,\n includeAggregators: true,\n includeOccurrences: true,\n };\n\n const metadataResponse = await reduceInBatchesSerially<\n string,\n V3AssetResponse[]\n >({\n values: supportedAssetIds,\n batchSize: TOKENS_API_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResponse = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchV3Assets(batch, fetchOptions),\n this.#fetchTimeoutMs,\n );\n return [...(workingResult as V3AssetResponse[]), ...batchResponse];\n },\n initialResult: [],\n });\n\n // Split assets by chain type: EVM uses occurrence-count filtering;\n // non-EVM non-native uses Blockaid; native assets are always allowed.\n const occurrencesByAssetId = new Map(\n metadataResponse.map((a) => [a.assetId, a.occurrences]),\n );\n\n const evmErc20Ids: string[] = [];\n const nonEvmTokenIds: string[] = [];\n\n for (const assetData of metadataResponse) {\n const assetId = assetData.assetId as Caip19AssetId;\n const { assetNamespace, chain } = parseCaipAssetType(assetId);\n if (nativeAssetIds.has(assetId.toLowerCase())) {\n // Native assets are always kept — no filtering.\n } else if (\n assetNamespace === CaipAssetNamespace.Erc20 &&\n chain.namespace === KnownCaipNamespace.Eip155\n ) {\n evmErc20Ids.push(assetId);\n } else if (assetNamespace === CaipAssetNamespace.Token) {\n nonEvmTokenIds.push(assetId);\n }\n }\n\n // EVM: require per-chain suggested occurrence floor (from Token API\n // `/v1/suggestedOccurrenceFloors`, default {@link DEFAULT_OCCURRENCE_FLOOR})\n // to suppress low-signal tokens. Tokens with no occurrence data\n // (undefined) are treated the same as zero occurrences and filtered out.\n // Custom assets (user-imported) bypass the occurrence filter — users\n // can import whatever they want and we must keep their metadata even\n // if the API has fewer aggregator hits than the floor.\n const allowedEvmIds = new Set(\n evmErc20Ids.filter(\n (id) =>\n customAssetIds.has(id.toLowerCase()) ||\n (occurrencesByAssetId.get(id) ?? 0) >=\n getOccurrenceFloorForAsset(id, suggestedOccurrenceFloors) ||\n id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`),\n ),\n );\n\n // Non-EVM: Blockaid bulk scan.\n // Custom assets (user-imported) bypass Blockaid filtering.\n const nonEvmToScan = nonEvmTokenIds.filter(\n (id) => !customAssetIds.has(id.toLowerCase()),\n );\n const allowedNonEvmIds = new Set([\n ...nonEvmTokenIds.filter((id) =>\n customAssetIds.has(id.toLowerCase()),\n ),\n ...(await this.#filterBlockaidSpamTokens(nonEvmToScan)),\n ]);\n\n // Start with every asset the API returned; only remove those that\n // fail their respective filter (EVM occurrences / non-EVM Blockaid).\n // Native (slip44) and unrecognised namespaces are kept (fail open).\n const allowedAssetIds = new Set(metadataResponse.map((a) => a.assetId));\n\n for (const id of evmErc20Ids) {\n if (!allowedEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n for (const id of nonEvmTokenIds) {\n if (!allowedNonEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n\n response.assetsInfo ??= {};\n\n const filteredOutAssets = new Set<string>();\n\n for (const assetData of metadataResponse) {\n if (!allowedAssetIds.has(assetData.assetId)) {\n filteredOutAssets.add(assetData.assetId);\n continue;\n }\n\n const caipAssetId = assetData.assetId as Caip19AssetId;\n response.assetsInfo[caipAssetId] = transformV3AssetResponseToMetadata(\n caipAssetId,\n assetData,\n this.#getAssetType,\n );\n }\n\n if (filteredOutAssets.size > 0) {\n if (response.assetsBalance) {\n for (const accountBalances of Object.values(\n response.assetsBalance,\n )) {\n for (const assetId of filteredOutAssets) {\n delete (accountBalances as Record<string, unknown>)[assetId];\n }\n }\n }\n\n if (response.detectedAssets) {\n for (const [accountId, assetIds] of Object.entries(\n response.detectedAssets,\n )) {\n response.detectedAssets[accountId] = assetIds.filter(\n (id) => !filteredOutAssets.has(id),\n );\n }\n }\n }\n } catch (error) {\n log('Failed to fetch metadata', { error });\n }\n\n // Call next() at the end to continue the middleware chain\n return next(ctx);\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TokenDataSource.mjs","sourceRoot":"","sources":["../../src/data-sources/TokenDataSource.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,+BAA+B;AAK3D,OAAO,EAAE,mBAAmB,EAAE,sCAAsC;AACpE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,wBAAwB;AAIzE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAkB;AAC9D,OAAO,EAAE,YAAY,EAAE,qBAAiB;AAOxC,OAAO,EAAE,gBAAgB,EAAE,2BAAiB;AAC5C,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACxB,qCAA2B;AAE5B,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAC1C,MAAM,wBAAwB,GAAG,KAAM,CAAC;AAExC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D,4CAA4C;AAC5C,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC,yFAAyF;AACzF,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAEjC;;;GAGG;AACH,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC,qEAAqE;AACrE,MAAM,CAAN,IAAY,kBAIX;AAJD,WAAY,kBAAkB;IAC5B,uCAAiB,CAAA;IACjB,qCAAe,CAAA;IACf,qCAAe,CAAA;AACjB,CAAC,EAJW,kBAAkB,KAAlB,kB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,KAAK,mBAAmB,CAAC,SAAS,EAAE,CAAC;oBAC1D,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qDAAqD,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { V3AssetResponse } from '@metamask/core-backend';\nimport { ApiPlatformClient } from '@metamask/core-backend';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { KnownCaipNamespace, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { AssetsControllerMessenger } from '../AssetsController';\nimport { projectLogger, createModuleLogger } from '../logger';\nimport { forDataTypes } from '../types';\nimport type {\n Caip19AssetId,\n AssetMetadata,\n Middleware,\n FungibleAssetMetadata,\n} from '../types';\nimport { fetchWithTimeout } from '../utils';\nimport {\n isStakingContractAssetId,\n reduceInBatchesSerially,\n} from './evm-rpc-services';\n\n// ============================================================================\n// CONSTANTS\n// ============================================================================\n\nconst CONTROLLER_NAME = 'TokenDataSource';\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\n/** Max asset IDs per tokens API request. */\nconst TOKENS_API_BATCH_SIZE = 50;\n\n/** Max tokens per PhishingController:bulkScanTokens request (see PhishingController). */\nconst BULK_SCAN_BATCH_SIZE = 100;\n\n/**\n * Minimum number of aggregator occurrences required for an EVM ERC-20 token to\n * pass the spam filter. Non-EVM tokens are filtered via Blockaid bulk scan instead.\n */\nconst MIN_TOKEN_OCCURRENCES = 3;\n\n/** CAIP-19 `assetNamespace` segments used across filtering logic. */\nexport enum CaipAssetNamespace {\n Slip44 = 'slip44',\n Erc20 = 'erc20',\n Token = 'token',\n}\n\nconst MUSD_ADDRESS_LOWERCASE = '0xaca92e438df0b2401ff60da7e4337b687a2435da';\n\n// ============================================================================\n// OPTIONS\n// ============================================================================\n\nexport type TokenDataSourceOptions = {\n /** ApiPlatformClient for API calls with caching */\n queryApiClient: ApiPlatformClient;\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n getNativeAssetIds: () => string[];\n /** Returns the asset type ('native' | 'erc20' | 'spl') for a given CAIP-19 asset ID */\n getAssetType: (assetId: Caip19AssetId) => 'native' | 'erc20' | 'spl';\n /**\n * Timeout in ms for a single Tokens API call (default: 15000). When it\n * fires, the batch rejects so metadata enrichment proceeds without it.\n */\n fetchTimeoutMs?: number;\n};\n\n/**\n * Messenger actions `TokenDataSource` may invoke (via {@link AssetsControllerMessenger}).\n * Not re-exported from the package public `index` (repo ESLint); import from this module when\n * typing a messenger in the same package or tests.\n */\nexport type TokenDataSourceAllowedActions =\n PhishingControllerBulkScanTokensAction;\n\n// ============================================================================\n// HELPER FUNCTIONS\n// ============================================================================\n\n/**\n * Transform V3 API response to FungibleAssetMetadata for state storage.\n *\n * Mapping:\n * - assetId → used to derive `type` (native/erc20/spl)\n * - iconUrl → image\n * - All other fields map directly\n *\n * @param assetId - CAIP-19 asset ID used to derive token type.\n * @param assetData - V3 API response data.\n * @param getAssetType - Returns the asset type for a given CAIP-19 asset ID.\n * @returns FungibleAssetMetadata for state storage.\n */\nfunction transformV3AssetResponseToMetadata(\n assetId: Caip19AssetId,\n assetData: V3AssetResponse,\n getAssetType: (id: Caip19AssetId) => 'native' | 'erc20' | 'spl',\n): AssetMetadata {\n const metadata: FungibleAssetMetadata = {\n type: getAssetType(assetId),\n // BaseAssetMetadata fields\n name: assetData.name,\n symbol: assetData.symbol,\n decimals: assetData.decimals,\n image: assetData.iconUrl,\n // Direct mapping fields\n coingeckoId: assetData.coingeckoId,\n occurrences: assetData.occurrences,\n aggregators: assetData.aggregators,\n labels: assetData.labels,\n erc20Permit: assetData.erc20Permit,\n fees: assetData.fees,\n honeypotStatus: assetData.honeypotStatus,\n storage: assetData.storage,\n isContractVerified: assetData.isContractVerified,\n description: assetData.description,\n };\n\n return metadata;\n}\n\n// ============================================================================\n// TOKEN DATA SOURCE\n// ============================================================================\n\n/**\n * TokenDataSource enriches responses with token metadata from the Tokens API.\n *\n * This middleware-based data source:\n * - Checks detected assets for missing metadata/images\n * - Fetches metadata from Tokens API v3 for assets needing enrichment\n * - Merges fetched metadata into the response\n *\n * Pass the same {@link AssetsControllerMessenger} as other data sources for Blockaid\n * token scans.\n */\nexport class TokenDataSource {\n readonly name = CONTROLLER_NAME;\n\n getName(): string {\n return this.name;\n }\n\n /** ApiPlatformClient for cached API calls */\n readonly #apiClient: ApiPlatformClient;\n\n /** Returns CAIP-19 native asset IDs from NetworkEnablementController state */\n readonly #getNativeAssetIds: () => string[];\n\n /** Returns the asset type for a given CAIP-19 asset ID */\n readonly #getAssetType: (\n assetId: Caip19AssetId,\n ) => 'native' | 'erc20' | 'spl';\n\n /** Shared controller messenger — used for `PhishingController:bulkScanTokens`. */\n readonly #messenger: AssetsControllerMessenger;\n\n readonly #fetchTimeoutMs: number;\n\n constructor(\n messenger: AssetsControllerMessenger,\n options: TokenDataSourceOptions,\n ) {\n this.#messenger = messenger;\n this.#apiClient = options.queryApiClient;\n this.#getNativeAssetIds = options.getNativeAssetIds;\n this.#getAssetType = options.getAssetType;\n this.#fetchTimeoutMs = options.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n }\n\n /**\n * Gets the supported networks from the API.\n * Caching is handled by ApiPlatformClient.\n *\n * @returns Set of supported chain IDs in CAIP format\n */\n async #getSupportedNetworks(): Promise<Set<string>> {\n try {\n // Use v2/supportedNetworks which returns CAIP chain IDs\n // ApiPlatformClient handles caching\n const response = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchTokenV2SupportedNetworks(),\n this.#fetchTimeoutMs,\n );\n\n // Combine full and partial support networks\n const allNetworks = [...response.fullSupport, ...response.partialSupport];\n\n return new Set(allNetworks);\n } catch (error) {\n log('Failed to fetch supported networks', { error });\n return new Set();\n }\n }\n\n /**\n * Filters asset IDs to only include those from supported networks.\n *\n * @param assetIds - Array of CAIP-19 asset IDs\n * @param supportedNetworks - Set of supported chain IDs\n * @returns Array of asset IDs from supported networks\n */\n #filterAssetsByNetwork(\n assetIds: string[],\n supportedNetworks: Set<string>,\n ): string[] {\n return assetIds.filter((assetId) => {\n try {\n const parsed = parseCaipAssetType(assetId as CaipAssetType);\n // chainId is in format \"eip155:1\" or \"tron:728126428\"\n // parsed.chain has namespace and reference properties\n const chainId = `${parsed.chain.namespace}:${parsed.chain.reference}`;\n return supportedNetworks.has(chainId);\n } catch {\n // If we can't parse the asset ID, filter it out\n return false;\n }\n });\n }\n\n /**\n * Filters non-EVM fungible `token` assets flagged as malicious by Blockaid\n * via `PhishingController:bulkScanTokens`. Only the `token` namespace (e.g.\n * Solana mints) is scanned; native (`slip44`) and EVM assets are not handled\n * here (EVM uses occurrence-count filtering instead). Fails open on error.\n *\n * @param assets - CAIP-19 asset IDs to filter (non-EVM only).\n * @returns Asset IDs with malicious tokens removed.\n */\n async #filterBlockaidSpamTokens(assets: string[]): Promise<string[]> {\n if (assets.length === 0) {\n return assets;\n }\n\n const tokensByChain: Record<string, { asset: string; address: string }[]> =\n {};\n\n for (const asset of assets) {\n try {\n const { assetNamespace, assetReference, chain } = parseCaipAssetType(\n asset as CaipAssetType,\n );\n\n if (assetNamespace === CaipAssetNamespace.Token) {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n } catch {\n // Malformed or unsupported for bulk scan — keep asset (fail open)\n }\n }\n\n if (Object.keys(tokensByChain).length === 0) {\n return assets;\n }\n\n const rejectedAssets = new Set<string>();\n\n try {\n for (const [chainId, tokenEntries] of Object.entries(tokensByChain)) {\n const addresses = tokenEntries.map((entry) => entry.address);\n const batches: string[][] = [];\n for (let i = 0; i < addresses.length; i += BULK_SCAN_BATCH_SIZE) {\n batches.push(addresses.slice(i, i + BULK_SCAN_BATCH_SIZE));\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.#messenger.call('PhishingController:bulkScanTokens', {\n chainId,\n tokens: batch,\n }),\n ),\n );\n\n const scanResponse: BulkTokenScanResponse = {};\n for (const result of batchResults) {\n if (result.status === 'fulfilled') {\n Object.assign(scanResponse, result.value);\n }\n }\n\n for (const entry of tokenEntries) {\n const result = scanResponse[entry.address];\n if (result?.result_type === TokenScanResultType.Malicious) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n } catch (error) {\n log('Blockaid bulk token scan failed; keeping all tokens', { error });\n return assets;\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * Get the middleware for enriching responses with token metadata.\n *\n * This middleware:\n * 1. Extracts the response from context\n * 2. Fetches metadata for detected assets (assets without metadata)\n * 3. Enriches the response with fetched metadata\n * 4. Calls next() at the end to continue the middleware chain\n *\n * @returns The middleware function for the assets pipeline.\n */\n get assetsMiddleware(): Middleware {\n return forDataTypes(['metadata'], async (ctx, next) => {\n // Extract response from context\n const { response } = ctx;\n\n const { assetsInfo: stateMetadata, customAssets } = ctx.getAssetsState();\n const assetIdsNeedingMetadata = new Set<string>();\n\n // Custom assets are user-imported — exempt from spam filtering.\n // State stores asset IDs in their normalized (checksummed) form, but the\n // V3 Tokens API can return them lower-cased. Lowercase both sides so the\n // bypass is robust to address-case differences across data sources.\n const customAssetIds = new Set<string>(\n Object.values(customAssets ?? {})\n .flat()\n .map((id) => id.toLowerCase()),\n );\n\n // Always include native asset IDs from NetworkEnablementController\n const nativeAssetIdsList = this.#getNativeAssetIds();\n const nativeAssetIds = new Set(\n nativeAssetIdsList.map((id) => id.toLowerCase()),\n );\n for (const nativeAssetId of nativeAssetIdsList) {\n assetIdsNeedingMetadata.add(nativeAssetId);\n }\n\n // Also fetch metadata for detected assets that are missing it\n if (response.detectedAssets) {\n for (const detectedIds of Object.values(response.detectedAssets)) {\n for (const assetId of detectedIds) {\n // Skip if response already has metadata with image\n const responseMetadata = response.assetsInfo?.[assetId];\n if (responseMetadata?.image) {\n continue;\n }\n\n // Skip if state already has metadata with image\n const existingMetadata = stateMetadata[assetId];\n if (existingMetadata?.image) {\n continue;\n }\n\n // Skip staking contracts; we use built-in metadata and do not fetch from the tokens API\n if (isStakingContractAssetId(assetId)) {\n continue;\n }\n\n assetIdsNeedingMetadata.add(assetId);\n }\n }\n }\n\n if (assetIdsNeedingMetadata.size === 0) {\n return next(ctx);\n }\n\n // Filter asset IDs to only include supported networks\n const supportedNetworks = await this.#getSupportedNetworks();\n const supportedAssetIds = this.#filterAssetsByNetwork(\n [...assetIdsNeedingMetadata],\n supportedNetworks,\n );\n\n if (supportedAssetIds.length === 0) {\n return next(ctx);\n }\n\n try {\n const fetchOptions = {\n includeIconUrl: true,\n includeMarketData: true,\n includeMetadata: true,\n includeLabels: true,\n includeRwaData: true,\n includeAggregators: true,\n includeOccurrences: true,\n };\n\n const metadataResponse = await reduceInBatchesSerially<\n string,\n V3AssetResponse[]\n >({\n values: supportedAssetIds,\n batchSize: TOKENS_API_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResponse = await fetchWithTimeout(\n () => this.#apiClient.tokens.fetchV3Assets(batch, fetchOptions),\n this.#fetchTimeoutMs,\n );\n return [...(workingResult as V3AssetResponse[]), ...batchResponse];\n },\n initialResult: [],\n });\n\n // Split assets by chain type: EVM uses occurrence-count filtering;\n // non-EVM non-native uses Blockaid; native assets are always allowed.\n const occurrencesByAssetId = new Map(\n metadataResponse.map((a) => [a.assetId, a.occurrences]),\n );\n\n const evmErc20Ids: string[] = [];\n const nonEvmTokenIds: string[] = [];\n\n for (const assetData of metadataResponse) {\n const assetId = assetData.assetId as Caip19AssetId;\n const { assetNamespace, chain } = parseCaipAssetType(assetId);\n if (nativeAssetIds.has(assetId.toLowerCase())) {\n // Native assets are always kept — no filtering.\n } else if (\n assetNamespace === CaipAssetNamespace.Erc20 &&\n chain.namespace === KnownCaipNamespace.Eip155\n ) {\n evmErc20Ids.push(assetId);\n } else if (assetNamespace === CaipAssetNamespace.Token) {\n nonEvmTokenIds.push(assetId);\n }\n }\n\n // EVM: require minimum occurrence count to suppress low-signal tokens.\n // Tokens with no occurrence data (undefined) are treated the same as\n // zero occurrences and filtered out.\n // Custom assets (user-imported) bypass the occurrence filter — users\n // can import whatever they want and we must keep their metadata even\n // if the API has fewer than `MIN_TOKEN_OCCURRENCES` aggregator hits.\n const allowedEvmIds = new Set(\n evmErc20Ids.filter(\n (id) =>\n customAssetIds.has(id.toLowerCase()) ||\n (occurrencesByAssetId.get(id) ?? 0) >= MIN_TOKEN_OCCURRENCES ||\n id.includes(`/erc20:${MUSD_ADDRESS_LOWERCASE}`),\n ),\n );\n\n // Non-EVM: Blockaid bulk scan.\n // Custom assets (user-imported) bypass Blockaid filtering.\n const nonEvmToScan = nonEvmTokenIds.filter(\n (id) => !customAssetIds.has(id.toLowerCase()),\n );\n const allowedNonEvmIds = new Set([\n ...nonEvmTokenIds.filter((id) =>\n customAssetIds.has(id.toLowerCase()),\n ),\n ...(await this.#filterBlockaidSpamTokens(nonEvmToScan)),\n ]);\n\n // Start with every asset the API returned; only remove those that\n // fail their respective filter (EVM occurrences / non-EVM Blockaid).\n // Native (slip44) and unrecognised namespaces are kept (fail open).\n const allowedAssetIds = new Set(metadataResponse.map((a) => a.assetId));\n\n for (const id of evmErc20Ids) {\n if (!allowedEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n for (const id of nonEvmTokenIds) {\n if (!allowedNonEvmIds.has(id)) {\n allowedAssetIds.delete(id);\n }\n }\n\n response.assetsInfo ??= {};\n\n const filteredOutAssets = new Set<string>();\n\n for (const assetData of metadataResponse) {\n if (!allowedAssetIds.has(assetData.assetId)) {\n filteredOutAssets.add(assetData.assetId);\n continue;\n }\n\n const caipAssetId = assetData.assetId as Caip19AssetId;\n response.assetsInfo[caipAssetId] = transformV3AssetResponseToMetadata(\n caipAssetId,\n assetData,\n this.#getAssetType,\n );\n }\n\n if (filteredOutAssets.size > 0) {\n if (response.assetsBalance) {\n for (const accountBalances of Object.values(\n response.assetsBalance,\n )) {\n for (const assetId of filteredOutAssets) {\n delete (accountBalances as Record<string, unknown>)[assetId];\n }\n }\n }\n\n if (response.detectedAssets) {\n for (const [accountId, assetIds] of Object.entries(\n response.detectedAssets,\n )) {\n response.detectedAssets[accountId] = assetIds.filter(\n (id) => !filteredOutAssets.has(id),\n );\n }\n }\n }\n } catch (error) {\n log('Failed to fetch metadata', { error });\n }\n\n // Call next() at the end to continue the middleware chain\n return next(ctx);\n });\n }\n}\n"]}
|
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _TokensApiClient_instances, _TokensApiClient_fetch, _TokensApiClient_queryClient, _TokensApiClient_supportedChainIds, _TokensApiClient_supportedChainIdsCachedAt, _TokensApiClient_supportedChainIdsRefreshPromise,
|
|
13
|
+
var _TokensApiClient_instances, _TokensApiClient_fetch, _TokensApiClient_queryClient, _TokensApiClient_supportedChainIds, _TokensApiClient_supportedChainIdsCachedAt, _TokensApiClient_supportedChainIdsRefreshPromise, _TokensApiClient_isSupportedChain, _TokensApiClient_refreshSupportedChainIds, _TokensApiClient_fetchTokenListUncached;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.TokensApiClient = void 0;
|
|
16
16
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
@@ -27,17 +27,6 @@ const TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';
|
|
|
27
27
|
* that are not in the supported-networks list.
|
|
28
28
|
*/
|
|
29
29
|
const SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;
|
|
30
|
-
/**
|
|
31
|
-
* Per-chain suggested occurrence floors used as the `occurrenceFloor` query
|
|
32
|
-
* param when fetching token lists (and aligned with TokenDataSource spam
|
|
33
|
-
* filtering). Keys are decimal chain IDs.
|
|
34
|
-
*/
|
|
35
|
-
const SUGGESTED_OCCURRENCE_FLOORS_URL = `${TOKEN_END_POINT_API}/v1/suggestedOccurrenceFloors`;
|
|
36
|
-
/**
|
|
37
|
-
* Fallback `occurrenceFloor` when `/v1/suggestedOccurrenceFloors` has no entry
|
|
38
|
-
* for the chain, or the floors request fails.
|
|
39
|
-
*/
|
|
40
|
-
const DEFAULT_OCCURRENCE_FLOOR = 3;
|
|
41
30
|
/**
|
|
42
31
|
* How long to keep the supported-networks response in the instance-level
|
|
43
32
|
* cache before refreshing it. One hour is sufficient — the list rarely
|
|
@@ -46,10 +35,27 @@ const DEFAULT_OCCURRENCE_FLOOR = 3;
|
|
|
46
35
|
*/
|
|
47
36
|
const SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60000;
|
|
48
37
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
38
|
+
* Tempo Mainnet — not yet present in `@metamask/controller-utils`'s `ChainId`
|
|
39
|
+
* map at the time of writing, so it's spelled out as a literal here exactly as
|
|
40
|
+
* `TokenListController` does (see `token-service.ts:getTokensURL`).
|
|
41
|
+
*/
|
|
42
|
+
const TEMPO_MAINNET_CHAIN_ID = '0x1079';
|
|
43
|
+
/**
|
|
44
|
+
* Per-chain occurrence floor, mirroring `TokenListController.getTokensURL`:
|
|
45
|
+
* Linea mainnet, MegaETH mainnet, and Tempo mainnet have thinner aggregator
|
|
46
|
+
* coverage so we lower the floor; everything else uses the default 3.
|
|
47
|
+
*
|
|
48
|
+
* @param hexChainId - Hex chain ID.
|
|
49
|
+
* @returns The occurrence floor to send to the Tokens API.
|
|
51
50
|
*/
|
|
52
|
-
|
|
51
|
+
function getOccurrenceFloor(hexChainId) {
|
|
52
|
+
if (hexChainId === controller_utils_1.ChainId['linea-mainnet'] ||
|
|
53
|
+
hexChainId === controller_utils_1.ChainId['megaeth-mainnet'] ||
|
|
54
|
+
hexChainId === TEMPO_MAINNET_CHAIN_ID) {
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
return 3;
|
|
58
|
+
}
|
|
53
59
|
/**
|
|
54
60
|
* TanStack-Query cache config for the cached `fetchTokenList` path.
|
|
55
61
|
*
|
|
@@ -66,10 +72,9 @@ const TOKEN_LIST_GC_TIME_MS = 60 * 60000;
|
|
|
66
72
|
*
|
|
67
73
|
* Fetches the per-chain ERC-20 token list from the same endpoint that
|
|
68
74
|
* `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),
|
|
69
|
-
* with the same query parameters
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* filter is still applied client-side.
|
|
75
|
+
* with the same query parameters and the same per-chain occurrence floor /
|
|
76
|
+
* Linea aggregator filter. This keeps RPC token detection in lockstep with
|
|
77
|
+
* the wallet's primary token list.
|
|
73
78
|
*
|
|
74
79
|
* Before fetching a chain's token list, the client checks
|
|
75
80
|
* `/v2/supportedNetworks` and returns `[]` immediately for chains that are
|
|
@@ -95,12 +100,6 @@ class TokensApiClient {
|
|
|
95
100
|
* calls arrive before the first one resolves.
|
|
96
101
|
*/
|
|
97
102
|
_TokensApiClient_supportedChainIdsRefreshPromise.set(this, void 0);
|
|
98
|
-
/** Decimal chain ID → suggested occurrence floor from Token API. */
|
|
99
|
-
_TokensApiClient_suggestedOccurrenceFloors.set(this, void 0);
|
|
100
|
-
/** Timestamp of the last successful floors fetch. */
|
|
101
|
-
_TokensApiClient_suggestedOccurrenceFloorsCachedAt.set(this, 0);
|
|
102
|
-
/** In-flight floors request shared across concurrent callers. */
|
|
103
|
-
_TokensApiClient_suggestedOccurrenceFloorsRefreshPromise.set(this, void 0);
|
|
104
103
|
__classPrivateFieldSet(this, _TokensApiClient_fetch, config?.fetch ?? globalThis.fetch.bind(globalThis), "f");
|
|
105
104
|
__classPrivateFieldSet(this, _TokensApiClient_queryClient, config?.queryClient, "f");
|
|
106
105
|
}
|
|
@@ -141,7 +140,7 @@ class TokensApiClient {
|
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
exports.TokensApiClient = TokensApiClient;
|
|
144
|
-
_TokensApiClient_fetch = new WeakMap(), _TokensApiClient_queryClient = new WeakMap(), _TokensApiClient_supportedChainIds = new WeakMap(), _TokensApiClient_supportedChainIdsCachedAt = new WeakMap(), _TokensApiClient_supportedChainIdsRefreshPromise = new WeakMap(),
|
|
143
|
+
_TokensApiClient_fetch = new WeakMap(), _TokensApiClient_queryClient = new WeakMap(), _TokensApiClient_supportedChainIds = new WeakMap(), _TokensApiClient_supportedChainIdsCachedAt = new WeakMap(), _TokensApiClient_supportedChainIdsRefreshPromise = new WeakMap(), _TokensApiClient_instances = new WeakSet(), _TokensApiClient_isSupportedChain =
|
|
145
144
|
/**
|
|
146
145
|
* Check whether the given chain is present in the supported-networks list.
|
|
147
146
|
*
|
|
@@ -190,68 +189,10 @@ async function _TokensApiClient_refreshSupportedChainIds(now) {
|
|
|
190
189
|
}
|
|
191
190
|
})(), "f");
|
|
192
191
|
return __classPrivateFieldGet(this, _TokensApiClient_supportedChainIdsRefreshPromise, "f");
|
|
193
|
-
}, _TokensApiClient_getOccurrenceFloor =
|
|
194
|
-
/**
|
|
195
|
-
* Resolve the `occurrenceFloor` query param for a chain from Token API
|
|
196
|
-
* `/v1/suggestedOccurrenceFloors`. Falls back to
|
|
197
|
-
* {@link DEFAULT_OCCURRENCE_FLOOR} when the chain is missing or the request
|
|
198
|
-
* fails.
|
|
199
|
-
*
|
|
200
|
-
* @param hexChainId - Hex chain ID.
|
|
201
|
-
* @returns Occurrence floor to send to `/tokens/{chainId}`.
|
|
202
|
-
*/
|
|
203
|
-
async function _TokensApiClient_getOccurrenceFloor(hexChainId) {
|
|
204
|
-
const now = Date.now();
|
|
205
|
-
if (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloors, "f") === undefined ||
|
|
206
|
-
now - __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, "f") >=
|
|
207
|
-
SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS) {
|
|
208
|
-
await __classPrivateFieldGet(this, _TokensApiClient_instances, "m", _TokensApiClient_refreshSuggestedOccurrenceFloors).call(this, now);
|
|
209
|
-
}
|
|
210
|
-
const decimalChainId = String((0, controller_utils_1.convertHexToDecimal)(hexChainId));
|
|
211
|
-
return (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloors, "f")?.[decimalChainId] ??
|
|
212
|
-
DEFAULT_OCCURRENCE_FLOOR);
|
|
213
|
-
}, _TokensApiClient_refreshSuggestedOccurrenceFloors =
|
|
214
|
-
/**
|
|
215
|
-
* Fetch `/v1/suggestedOccurrenceFloors` and update the instance cache.
|
|
216
|
-
* Concurrent callers share the in-flight Promise. Failures leave the cache
|
|
217
|
-
* empty so {@link #getOccurrenceFloor} falls back to the default.
|
|
218
|
-
*
|
|
219
|
-
* @param now - Current timestamp used to stamp the cache entry.
|
|
220
|
-
* @returns A promise that resolves once the cache has been refreshed.
|
|
221
|
-
*/
|
|
222
|
-
async function _TokensApiClient_refreshSuggestedOccurrenceFloors(now) {
|
|
223
|
-
if (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f") !== undefined) {
|
|
224
|
-
return __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f");
|
|
225
|
-
}
|
|
226
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, (async () => {
|
|
227
|
-
try {
|
|
228
|
-
const response = await __classPrivateFieldGet(this, _TokensApiClient_fetch, "f").call(this, SUGGESTED_OCCURRENCE_FLOORS_URL);
|
|
229
|
-
if (response.ok) {
|
|
230
|
-
const data = (await response.json());
|
|
231
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, data && typeof data === 'object' && !Array.isArray(data)
|
|
232
|
-
? data
|
|
233
|
-
: {}, "f");
|
|
234
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, {}, "f");
|
|
238
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
catch {
|
|
242
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, {}, "f");
|
|
243
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
244
|
-
}
|
|
245
|
-
finally {
|
|
246
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, undefined, "f");
|
|
247
|
-
}
|
|
248
|
-
})(), "f");
|
|
249
|
-
return __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f");
|
|
250
192
|
}, _TokensApiClient_fetchTokenListUncached = async function _TokensApiClient_fetchTokenListUncached(hexChainId) {
|
|
251
193
|
const decimalChainId = (0, controller_utils_1.convertHexToDecimal)(hexChainId);
|
|
252
|
-
const occurrenceFloor =
|
|
253
|
-
//
|
|
254
|
-
// but `occurrenceFloor` comes from `/v1/suggestedOccurrenceFloors`.
|
|
194
|
+
const occurrenceFloor = getOccurrenceFloor(hexChainId);
|
|
195
|
+
// Mirrors `TokenListController.getTokensURL` exactly (token-service.ts).
|
|
255
196
|
// No `first=...` cap — the API returns the full per-chain list bounded
|
|
256
197
|
// server-side by `occurrenceFloor`.
|
|
257
198
|
const url = `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokensApiClient.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iEAGoC;AAIpC;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,kCAAkC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,GAAG,mBAAmB,uBAAuB,CAAC;AAE7E;;;;GAIG;AACH,MAAM,+BAA+B,GAAG,GAAG,mBAAmB,+BAA+B,CAAC;AAE9F;;;GAGG;AACH,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAEnC;;;;;GAKG;AACH,MAAM,+BAA+B,GAAG,EAAE,GAAG,KAAM,CAAC;AAEpD;;;GAGG;AACH,MAAM,wCAAwC,GAAG,EAAE,GAAG,KAAM,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,wBAAwB,GAAG,CAAC,GAAG,KAAM,CAAC;AAC5C,MAAM,qBAAqB,GAAG,EAAE,GAAG,KAAM,CAAC;AAoD1C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAa,eAAe;IA2B1B,YAAY,MAA8B;;QA1BjC,yCAAgC;QAEhC,+CAA+C;QAExD,uEAAuE;QACvE,qDAA4C;QAE5C,sEAAsE;QACtE,qDAA6B,CAAC,EAAC;QAE/B;;;;WAIG;QACH,mEAA4D;QAE5D,oEAAoE;QACpE,6DAA+D;QAE/D,qDAAqD;QACrD,6DAAqC,CAAC,EAAC;QAEvC,iEAAiE;QACjE,2EAAoE;QAGlE,uBAAA,IAAI,0BAAU,MAAM,EAAE,KAAK,IAAI,UAAU,CA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O,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,EAAE;QACzB,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,EAAE;QACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ,IAAI,EAAE;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,WAAW,EAAE,IAAI,CAAC,WAAW;KAC9B,CAAC,CAAC,CAAC;AACN,CAAC;AAGH;;;;;;;;;;GAUG;AACH,SAAS,yBAAyB,CAChC,UAAmB,EACnB,KAAyB;IAEzB,IAAI,UAAU,KAAK,0BAAiB,CAAC,eAAe,CAAC,EAAE,CAAC;QACtD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,EAAE,CAAC;YAC3C,OAAO,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,WAAW,CAAC,MAAM,IAAI,CAAC,CAAC;QACtE,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n ChainId as ControllerChainId,\n convertHexToDecimal,\n} from '@metamask/controller-utils';\n\nimport type { ChainId, TokenListEntry } from '../types';\n\n/**\n * Same host + path that `TokenListController` (assets-controllers) uses\n * (`token-service.ts` → `getTokensURL`). Sharing this endpoint keeps the\n * RPC token detector aligned with the rest of the wallet's token listing\n * (occurrence floors, aggregator filters, icon URLs, etc.).\n */\nconst TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';\n\n/**\n * Endpoint that returns which CAIP chain IDs the Tokens API supports.\n * Used to skip detection (and avoid unnecessary network requests) for chains\n * that are not in the supported-networks list.\n */\nconst SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;\n\n/**\n * Per-chain suggested occurrence floors used as the `occurrenceFloor` query\n * param when fetching token lists (and aligned with TokenDataSource spam\n * filtering). Keys are decimal chain IDs.\n */\nconst SUGGESTED_OCCURRENCE_FLOORS_URL = `${TOKEN_END_POINT_API}/v1/suggestedOccurrenceFloors`;\n\n/**\n * Fallback `occurrenceFloor` when `/v1/suggestedOccurrenceFloors` has no entry\n * for the chain, or the floors request fails.\n */\nconst DEFAULT_OCCURRENCE_FLOOR = 3;\n\n/**\n * How long to keep the supported-networks response in the instance-level\n * cache before refreshing it. One hour is sufficient — the list rarely\n * changes and a stale cache just means we may do an unnecessary token-list\n * fetch for a newly-unsupported chain (harmless).\n */\nconst SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * How long to keep suggested occurrence floors cached. Same TTL as supported\n * networks — floors change infrequently.\n */\nconst SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * TanStack-Query cache config for the cached `fetchTokenList` path.\n *\n * The Tokens API per-chain list barely changes between polls, so we keep\n * results fresh for a few minutes (`staleTime`) and retain them in cache for\n * an hour (`gcTime`) so re-detections across accounts/chains hit the cache.\n * These tunings only apply when a `queryClient` is provided to the client;\n * the uncached fallback path (used in standalone tests) is unaffected.\n */\nconst TOKEN_LIST_STALE_TIME_MS = 5 * 60_000;\nconst TOKEN_LIST_GC_TIME_MS = 60 * 60_000;\n\n/**\n * Shape of the `/v2/supportedNetworks` response.\n */\ntype SupportedNetworksResponse = {\n fullSupport?: string[];\n partialSupport?: string[];\n};\n\n/**\n * Shape of a single item returned by the Tokens API `/tokens/{chainId}`\n * endpoint. Mirrors `TokenListToken` in\n * `packages/assets-controllers/src/TokenListController.ts` and the response\n * parsed by `fetchTokenListByChainId` in `token-service.ts`.\n */\ntype ApiTokenListItem = {\n address: string;\n symbol?: string;\n name?: string;\n decimals?: number;\n occurrences?: number;\n aggregators?: string[];\n iconUrl?: string;\n};\n\n/**\n * Minimal structural type for the TanStack Query client method we use.\n * Avoids a direct dependency on `@tanstack/query-core` while still being\n * fully compatible with the shared `QueryClient` exposed by\n * `ApiPlatformClient.queryClient` (`@metamask/core-backend`).\n */\nexport type TokenListQueryClient = {\n fetchQuery<TData>(options: {\n queryKey: readonly unknown[];\n queryFn: () => Promise<TData>;\n staleTime?: number;\n gcTime?: number;\n }): Promise<TData>;\n};\n\nexport type TokensApiClientConfig = {\n /** Fetch function (defaults to globalThis.fetch). */\n fetch?: typeof globalThis.fetch;\n /**\n * Optional TanStack-Query client used to cache token-list responses across\n * detector polls / accounts / instances. When omitted, every call hits the\n * network directly (preserves prior behaviour for tests and standalone use).\n */\n queryClient?: TokenListQueryClient;\n};\n\n/**\n * Client for the MetaMask Tokens API.\n *\n * Fetches the per-chain ERC-20 token list from the same endpoint that\n * `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),\n * with the same query parameters. The `occurrenceFloor` query param comes from\n * Token API `/v1/suggestedOccurrenceFloors` (fallback {@link DEFAULT_OCCURRENCE_FLOOR}),\n * matching `TokenDataSource` spam filtering. Linea's post-response aggregator\n * filter is still applied client-side.\n *\n * Before fetching a chain's token list, the client checks\n * `/v2/supportedNetworks` and returns `[]` immediately for chains that are\n * not in the supported list, avoiding unnecessary API calls. The supported-\n * networks response is cached in-memory for one hour.\n *\n * When constructed with a `queryClient`, token-list results are cached and\n * deduped via TanStack Query (5 min staleTime, 1 h gcTime), so concurrent\n * detection cycles for the same chain coalesce into a single network request.\n */\nexport class TokensApiClient {\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #queryClient: TokenListQueryClient | undefined;\n\n /** CAIP chain IDs returned by `/v2/supportedNetworks` (both tiers). */\n #supportedChainIds: Set<string> | undefined;\n\n /** Timestamp of the last successful `/v2/supportedNetworks` fetch. */\n #supportedChainIdsCachedAt = 0;\n\n /**\n * In-flight `/v2/supportedNetworks` request shared across concurrent callers\n * so only one network request is made even when multiple `fetchTokenList`\n * calls arrive before the first one resolves.\n */\n #supportedChainIdsRefreshPromise: Promise<void> | undefined;\n\n /** Decimal chain ID → suggested occurrence floor from Token API. */\n #suggestedOccurrenceFloors: Record<string, number> | undefined;\n\n /** Timestamp of the last successful floors fetch. */\n #suggestedOccurrenceFloorsCachedAt = 0;\n\n /** In-flight floors request shared across concurrent callers. */\n #suggestedOccurrenceFloorsRefreshPromise: Promise<void> | undefined;\n\n constructor(config?: TokensApiClientConfig) {\n this.#fetch = config?.fetch ?? globalThis.fetch.bind(globalThis);\n this.#queryClient = config?.queryClient;\n }\n\n /**\n * Fetch the list of ERC-20 tokens for a chain from the Tokens API.\n *\n * Returns `[]` immediately (without hitting the token-list endpoint) when\n * the chain is not in the `/v2/supportedNetworks` response, or when the\n * supported-networks check fails for any reason.\n *\n * @param hexChainId - Chain ID in hex format (e.g. `'0x1'` for Ethereum mainnet).\n * @returns Array of token list entries with address and metadata, or an empty\n * array if the chain is unsupported or the request fails.\n */\n async fetchTokenList(hexChainId: ChainId): Promise<TokenListEntry[]> {\n // Treat any error from the supported-networks check as \"not supported\":\n // the token-list endpoint is only contacted for known-supported chains.\n if (!(await this.#isSupportedChain(hexChainId).catch(() => false))) {\n return [];\n }\n\n const queryClient = this.#queryClient;\n if (queryClient === undefined) {\n return this.#fetchTokenListUncached(hexChainId);\n }\n\n return queryClient.fetchQuery({\n // Namespacing keeps this key from colliding with other clients that\n // share the same QueryClient (e.g. core-backend's ApiPlatformClient).\n queryKey: [\n 'assets-controller',\n 'rpc-detection',\n 'token-list',\n { chainId: hexChainId },\n ],\n queryFn: () => this.#fetchTokenListUncached(hexChainId),\n staleTime: TOKEN_LIST_STALE_TIME_MS,\n gcTime: TOKEN_LIST_GC_TIME_MS,\n });\n }\n\n /**\n * Check whether the given chain is present in the supported-networks list.\n *\n * Uses an instance-level cache (1 h TTL). Throws if the network request\n * fails — callers should handle this (e.g. via `.catch(() => false)`).\n *\n * @param hexChainId - Hex chain ID to check.\n * @returns `true` when the chain is in the supported-networks list.\n */\n async #isSupportedChain(hexChainId: ChainId): Promise<boolean> {\n const now = Date.now();\n if (\n this.#supportedChainIds === undefined ||\n now - this.#supportedChainIdsCachedAt >= SUPPORTED_NETWORKS_CACHE_TTL_MS\n ) {\n await this.#refreshSupportedChainIds(now);\n }\n\n const caipChainId = `eip155:${convertHexToDecimal(hexChainId)}`;\n return this.#supportedChainIds?.has(caipChainId) ?? false;\n }\n\n /**\n * Fetch `/v2/supportedNetworks` and update the instance cache.\n * Concurrent callers share the same in-flight Promise so only one network\n * request is made. If the response is not 2xx the cache is left unchanged\n * (the chain will be treated as unsupported this cycle).\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSupportedChainIds(now: number): Promise<void> {\n if (this.#supportedChainIdsRefreshPromise !== undefined) {\n return this.#supportedChainIdsRefreshPromise;\n }\n\n this.#supportedChainIdsRefreshPromise = (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUPPORTED_NETWORKS_URL);\n if (response.ok) {\n const data = (await response.json()) as SupportedNetworksResponse;\n this.#supportedChainIds = new Set([\n ...(data.fullSupport ?? []),\n ...(data.partialSupport ?? []),\n ]);\n this.#supportedChainIdsCachedAt = now;\n }\n } finally {\n this.#supportedChainIdsRefreshPromise = undefined;\n }\n })();\n\n return this.#supportedChainIdsRefreshPromise;\n }\n\n /**\n * Resolve the `occurrenceFloor` query param for a chain from Token API\n * `/v1/suggestedOccurrenceFloors`. Falls back to\n * {@link DEFAULT_OCCURRENCE_FLOOR} when the chain is missing or the request\n * fails.\n *\n * @param hexChainId - Hex chain ID.\n * @returns Occurrence floor to send to `/tokens/{chainId}`.\n */\n async #getOccurrenceFloor(hexChainId: ChainId): Promise<number> {\n const now = Date.now();\n if (\n this.#suggestedOccurrenceFloors === undefined ||\n now - this.#suggestedOccurrenceFloorsCachedAt >=\n SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS\n ) {\n await this.#refreshSuggestedOccurrenceFloors(now);\n }\n\n const decimalChainId = String(convertHexToDecimal(hexChainId));\n return (\n this.#suggestedOccurrenceFloors?.[decimalChainId] ??\n DEFAULT_OCCURRENCE_FLOOR\n );\n }\n\n /**\n * Fetch `/v1/suggestedOccurrenceFloors` and update the instance cache.\n * Concurrent callers share the in-flight Promise. Failures leave the cache\n * empty so {@link #getOccurrenceFloor} falls back to the default.\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSuggestedOccurrenceFloors(now: number): Promise<void> {\n if (this.#suggestedOccurrenceFloorsRefreshPromise !== undefined) {\n return this.#suggestedOccurrenceFloorsRefreshPromise;\n }\n\n this.#suggestedOccurrenceFloorsRefreshPromise =\n (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUGGESTED_OCCURRENCE_FLOORS_URL);\n if (response.ok) {\n const data = (await response.json()) as Record<string, number>;\n this.#suggestedOccurrenceFloors =\n data && typeof data === 'object' && !Array.isArray(data)\n ? data\n : {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n } else {\n this.#suggestedOccurrenceFloors = {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n }\n } catch {\n this.#suggestedOccurrenceFloors = {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n } finally {\n this.#suggestedOccurrenceFloorsRefreshPromise = undefined;\n }\n })();\n\n return this.#suggestedOccurrenceFloorsRefreshPromise;\n }\n\n async #fetchTokenListUncached(\n hexChainId: ChainId,\n ): Promise<TokenListEntry[]> {\n const decimalChainId = convertHexToDecimal(hexChainId);\n const occurrenceFloor = await this.#getOccurrenceFloor(hexChainId);\n\n // Same query shape as `TokenListController.getTokensURL` (token-service.ts),\n // but `occurrenceFloor` comes from `/v1/suggestedOccurrenceFloors`.\n // No `first=...` cap — the API returns the full per-chain list bounded\n // server-side by `occurrenceFloor`.\n const url =\n `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +\n `?occurrenceFloor=${occurrenceFloor}` +\n `&includeNativeAssets=false` +\n `&includeTokenFees=false` +\n `&includeAssetType=false` +\n `&includeERC20Permit=false` +\n `&includeStorage=false` +\n `&includeRwaData=true`;\n\n let response: Response;\n try {\n response = await this.#fetch(url);\n } catch (error) {\n console.error(\n `Tokens API request failed for chain ${hexChainId}:`,\n error,\n );\n return [];\n }\n\n if (!response.ok) {\n console.error(\n `Tokens API responded with ${response.status} for chain ${hexChainId}`,\n );\n return [];\n }\n\n const raw = (await response.json()) as unknown;\n const items: ApiTokenListItem[] = Array.isArray(raw)\n ? (raw as ApiTokenListItem[])\n : [];\n\n const filtered = applyChainSpecificFilters(hexChainId, items);\n\n return filtered.map((item) => ({\n address: item.address,\n symbol: item.symbol ?? '',\n name: item.name ?? '',\n decimals: item.decimals ?? 18,\n iconUrl: item.iconUrl,\n aggregators: item.aggregators,\n occurrences: item.occurrences,\n }));\n }\n}\n\n/**\n * Apply chain-specific filters to a raw token list response, mirroring\n * `fetchTokenListByChainId` in `assets-controllers/src/token-service.ts`.\n *\n * For Linea mainnet, the API returns extras with low aggregator coverage, so\n * we keep only entries flagged by Linea's own team or seen by ≥3 aggregators.\n *\n * @param hexChainId - Hex chain ID.\n * @param items - Raw items from the API response.\n * @returns Items after chain-specific filtering.\n */\nfunction applyChainSpecificFilters(\n hexChainId: ChainId,\n items: ApiTokenListItem[],\n): ApiTokenListItem[] {\n if (hexChainId === ControllerChainId['linea-mainnet']) {\n return items.filter((item) => {\n const aggregators = item.aggregators ?? [];\n return aggregators.includes('lineaTeam') || aggregators.length >= 3;\n });\n }\n return items;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TokensApiClient.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iEAGoC;AAIpC;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,kCAAkC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,GAAG,mBAAmB,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Content":["import {\n ChainId as ControllerChainId,\n convertHexToDecimal,\n} from '@metamask/controller-utils';\n\nimport type { ChainId, TokenListEntry } from '../types';\n\n/**\n * Same host + path that `TokenListController` (assets-controllers) uses\n * (`token-service.ts` → `getTokensURL`). Sharing this endpoint keeps the\n * RPC token detector aligned with the rest of the wallet's token listing\n * (occurrence floors, aggregator filters, icon URLs, etc.).\n */\nconst TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';\n\n/**\n * Endpoint that returns which CAIP chain IDs the Tokens API supports.\n * Used to skip detection (and avoid unnecessary network requests) for chains\n * that are not in the supported-networks list.\n */\nconst SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;\n\n/**\n * How long to keep the supported-networks response in the instance-level\n * cache before refreshing it. One hour is sufficient — the list rarely\n * changes and a stale cache just means we may do an unnecessary token-list\n * fetch for a newly-unsupported chain (harmless).\n */\nconst SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * Tempo Mainnet — not yet present in `@metamask/controller-utils`'s `ChainId`\n * map at the time of writing, so it's spelled out as a literal here exactly as\n * `TokenListController` does (see `token-service.ts:getTokensURL`).\n */\nconst TEMPO_MAINNET_CHAIN_ID = '0x1079' as const;\n\n/**\n * Per-chain occurrence floor, mirroring `TokenListController.getTokensURL`:\n * Linea mainnet, MegaETH mainnet, and Tempo mainnet have thinner aggregator\n * coverage so we lower the floor; everything else uses the default 3.\n *\n * @param hexChainId - Hex chain ID.\n * @returns The occurrence floor to send to the Tokens API.\n */\nfunction getOccurrenceFloor(hexChainId: ChainId): number {\n if (\n hexChainId === ControllerChainId['linea-mainnet'] ||\n hexChainId === ControllerChainId['megaeth-mainnet'] ||\n hexChainId === TEMPO_MAINNET_CHAIN_ID\n ) {\n return 1;\n }\n return 3;\n}\n\n/**\n * TanStack-Query cache config for the cached `fetchTokenList` path.\n *\n * The Tokens API per-chain list barely changes between polls, so we keep\n * results fresh for a few minutes (`staleTime`) and retain them in cache for\n * an hour (`gcTime`) so re-detections across accounts/chains hit the cache.\n * These tunings only apply when a `queryClient` is provided to the client;\n * the uncached fallback path (used in standalone tests) is unaffected.\n */\nconst TOKEN_LIST_STALE_TIME_MS = 5 * 60_000;\nconst TOKEN_LIST_GC_TIME_MS = 60 * 60_000;\n\n/**\n * Shape of the `/v2/supportedNetworks` response.\n */\ntype SupportedNetworksResponse = {\n fullSupport?: string[];\n partialSupport?: string[];\n};\n\n/**\n * Shape of a single item returned by the Tokens API `/tokens/{chainId}`\n * endpoint. Mirrors `TokenListToken` in\n * `packages/assets-controllers/src/TokenListController.ts` and the response\n * parsed by `fetchTokenListByChainId` in `token-service.ts`.\n */\ntype ApiTokenListItem = {\n address: string;\n symbol?: string;\n name?: string;\n decimals?: number;\n occurrences?: number;\n aggregators?: string[];\n iconUrl?: string;\n};\n\n/**\n * Minimal structural type for the TanStack Query client method we use.\n * Avoids a direct dependency on `@tanstack/query-core` while still being\n * fully compatible with the shared `QueryClient` exposed by\n * `ApiPlatformClient.queryClient` (`@metamask/core-backend`).\n */\nexport type TokenListQueryClient = {\n fetchQuery<TData>(options: {\n queryKey: readonly unknown[];\n queryFn: () => Promise<TData>;\n staleTime?: number;\n gcTime?: number;\n }): Promise<TData>;\n};\n\nexport type TokensApiClientConfig = {\n /** Fetch function (defaults to globalThis.fetch). */\n fetch?: typeof globalThis.fetch;\n /**\n * Optional TanStack-Query client used to cache token-list responses across\n * detector polls / accounts / instances. When omitted, every call hits the\n * network directly (preserves prior behaviour for tests and standalone use).\n */\n queryClient?: TokenListQueryClient;\n};\n\n/**\n * Client for the MetaMask Tokens API.\n *\n * Fetches the per-chain ERC-20 token list from the same endpoint that\n * `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),\n * with the same query parameters and the same per-chain occurrence floor /\n * Linea aggregator filter. This keeps RPC token detection in lockstep with\n * the wallet's primary token list.\n *\n * Before fetching a chain's token list, the client checks\n * `/v2/supportedNetworks` and returns `[]` immediately for chains that are\n * not in the supported list, avoiding unnecessary API calls. The supported-\n * networks response is cached in-memory for one hour.\n *\n * When constructed with a `queryClient`, token-list results are cached and\n * deduped via TanStack Query (5 min staleTime, 1 h gcTime), so concurrent\n * detection cycles for the same chain coalesce into a single network request.\n */\nexport class TokensApiClient {\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #queryClient: TokenListQueryClient | undefined;\n\n /** CAIP chain IDs returned by `/v2/supportedNetworks` (both tiers). */\n #supportedChainIds: Set<string> | undefined;\n\n /** Timestamp of the last successful `/v2/supportedNetworks` fetch. */\n #supportedChainIdsCachedAt = 0;\n\n /**\n * In-flight `/v2/supportedNetworks` request shared across concurrent callers\n * so only one network request is made even when multiple `fetchTokenList`\n * calls arrive before the first one resolves.\n */\n #supportedChainIdsRefreshPromise: Promise<void> | undefined;\n\n constructor(config?: TokensApiClientConfig) {\n this.#fetch = config?.fetch ?? globalThis.fetch.bind(globalThis);\n this.#queryClient = config?.queryClient;\n }\n\n /**\n * Fetch the list of ERC-20 tokens for a chain from the Tokens API.\n *\n * Returns `[]` immediately (without hitting the token-list endpoint) when\n * the chain is not in the `/v2/supportedNetworks` response, or when the\n * supported-networks check fails for any reason.\n *\n * @param hexChainId - Chain ID in hex format (e.g. `'0x1'` for Ethereum mainnet).\n * @returns Array of token list entries with address and metadata, or an empty\n * array if the chain is unsupported or the request fails.\n */\n async fetchTokenList(hexChainId: ChainId): Promise<TokenListEntry[]> {\n // Treat any error from the supported-networks check as \"not supported\":\n // the token-list endpoint is only contacted for known-supported chains.\n if (!(await this.#isSupportedChain(hexChainId).catch(() => false))) {\n return [];\n }\n\n const queryClient = this.#queryClient;\n if (queryClient === undefined) {\n return this.#fetchTokenListUncached(hexChainId);\n }\n\n return queryClient.fetchQuery({\n // Namespacing keeps this key from colliding with other clients that\n // share the same QueryClient (e.g. core-backend's ApiPlatformClient).\n queryKey: [\n 'assets-controller',\n 'rpc-detection',\n 'token-list',\n { chainId: hexChainId },\n ],\n queryFn: () => this.#fetchTokenListUncached(hexChainId),\n staleTime: TOKEN_LIST_STALE_TIME_MS,\n gcTime: TOKEN_LIST_GC_TIME_MS,\n });\n }\n\n /**\n * Check whether the given chain is present in the supported-networks list.\n *\n * Uses an instance-level cache (1 h TTL). Throws if the network request\n * fails — callers should handle this (e.g. via `.catch(() => false)`).\n *\n * @param hexChainId - Hex chain ID to check.\n * @returns `true` when the chain is in the supported-networks list.\n */\n async #isSupportedChain(hexChainId: ChainId): Promise<boolean> {\n const now = Date.now();\n if (\n this.#supportedChainIds === undefined ||\n now - this.#supportedChainIdsCachedAt >= SUPPORTED_NETWORKS_CACHE_TTL_MS\n ) {\n await this.#refreshSupportedChainIds(now);\n }\n\n const caipChainId = `eip155:${convertHexToDecimal(hexChainId)}`;\n return this.#supportedChainIds?.has(caipChainId) ?? false;\n }\n\n /**\n * Fetch `/v2/supportedNetworks` and update the instance cache.\n * Concurrent callers share the same in-flight Promise so only one network\n * request is made. If the response is not 2xx the cache is left unchanged\n * (the chain will be treated as unsupported this cycle).\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSupportedChainIds(now: number): Promise<void> {\n if (this.#supportedChainIdsRefreshPromise !== undefined) {\n return this.#supportedChainIdsRefreshPromise;\n }\n\n this.#supportedChainIdsRefreshPromise = (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUPPORTED_NETWORKS_URL);\n if (response.ok) {\n const data = (await response.json()) as SupportedNetworksResponse;\n this.#supportedChainIds = new Set([\n ...(data.fullSupport ?? []),\n ...(data.partialSupport ?? []),\n ]);\n this.#supportedChainIdsCachedAt = now;\n }\n } finally {\n this.#supportedChainIdsRefreshPromise = undefined;\n }\n })();\n\n return this.#supportedChainIdsRefreshPromise;\n }\n\n async #fetchTokenListUncached(\n hexChainId: ChainId,\n ): Promise<TokenListEntry[]> {\n const decimalChainId = convertHexToDecimal(hexChainId);\n const occurrenceFloor = getOccurrenceFloor(hexChainId);\n\n // Mirrors `TokenListController.getTokensURL` exactly (token-service.ts).\n // No `first=...` cap — the API returns the full per-chain list bounded\n // server-side by `occurrenceFloor`.\n const url =\n `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +\n `?occurrenceFloor=${occurrenceFloor}` +\n `&includeNativeAssets=false` +\n `&includeTokenFees=false` +\n `&includeAssetType=false` +\n `&includeERC20Permit=false` +\n `&includeStorage=false` +\n `&includeRwaData=true`;\n\n let response: Response;\n try {\n response = await this.#fetch(url);\n } catch (error) {\n console.error(\n `Tokens API request failed for chain ${hexChainId}:`,\n error,\n );\n return [];\n }\n\n if (!response.ok) {\n console.error(\n `Tokens API responded with ${response.status} for chain ${hexChainId}`,\n );\n return [];\n }\n\n const raw = (await response.json()) as unknown;\n const items: ApiTokenListItem[] = Array.isArray(raw)\n ? (raw as ApiTokenListItem[])\n : [];\n\n const filtered = applyChainSpecificFilters(hexChainId, items);\n\n return filtered.map((item) => ({\n address: item.address,\n symbol: item.symbol ?? '',\n name: item.name ?? '',\n decimals: item.decimals ?? 18,\n iconUrl: item.iconUrl,\n aggregators: item.aggregators,\n occurrences: item.occurrences,\n }));\n }\n}\n\n/**\n * Apply chain-specific filters to a raw token list response, mirroring\n * `fetchTokenListByChainId` in `assets-controllers/src/token-service.ts`.\n *\n * For Linea mainnet, the API returns extras with low aggregator coverage, so\n * we keep only entries flagged by Linea's own team or seen by ≥3 aggregators.\n *\n * @param hexChainId - Hex chain ID.\n * @param items - Raw items from the API response.\n * @returns Items after chain-specific filtering.\n */\nfunction applyChainSpecificFilters(\n hexChainId: ChainId,\n items: ApiTokenListItem[],\n): ApiTokenListItem[] {\n if (hexChainId === ControllerChainId['linea-mainnet']) {\n return items.filter((item) => {\n const aggregators = item.aggregators ?? [];\n return aggregators.includes('lineaTeam') || aggregators.length >= 3;\n });\n }\n return items;\n}\n"]}
|
|
@@ -28,10 +28,9 @@ export type TokensApiClientConfig = {
|
|
|
28
28
|
*
|
|
29
29
|
* Fetches the per-chain ERC-20 token list from the same endpoint that
|
|
30
30
|
* `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),
|
|
31
|
-
* with the same query parameters
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* filter is still applied client-side.
|
|
31
|
+
* with the same query parameters and the same per-chain occurrence floor /
|
|
32
|
+
* Linea aggregator filter. This keeps RPC token detection in lockstep with
|
|
33
|
+
* the wallet's primary token list.
|
|
35
34
|
*
|
|
36
35
|
* Before fetching a chain's token list, the client checks
|
|
37
36
|
* `/v2/supportedNetworks` and returns `[]` immediately for chains that are
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokensApiClient.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,2BAAiB;
|
|
1
|
+
{"version":3,"file":"TokensApiClient.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,2BAAiB;AAuFxD;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE;QACzB,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;QAC7B,OAAO,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC;QAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,qDAAqD;IACrD,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC;;;;OAIG;IACH,WAAW,CAAC,EAAE,oBAAoB,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,eAAe;;gBAkBd,MAAM,CAAC,EAAE,qBAAqB;IAK1C;;;;;;;;;;OAUG;IACG,cAAc,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;CAwIrE"}
|
|
@@ -28,10 +28,9 @@ export type TokensApiClientConfig = {
|
|
|
28
28
|
*
|
|
29
29
|
* Fetches the per-chain ERC-20 token list from the same endpoint that
|
|
30
30
|
* `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),
|
|
31
|
-
* with the same query parameters
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* filter is still applied client-side.
|
|
31
|
+
* with the same query parameters and the same per-chain occurrence floor /
|
|
32
|
+
* Linea aggregator filter. This keeps RPC token detection in lockstep with
|
|
33
|
+
* the wallet's primary token list.
|
|
35
34
|
*
|
|
36
35
|
* Before fetching a chain's token list, the client checks
|
|
37
36
|
* `/v2/supportedNetworks` and returns `[]` immediately for chains that are
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokensApiClient.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,2BAAiB;
|
|
1
|
+
{"version":3,"file":"TokensApiClient.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,2BAAiB;AAuFxD;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE;QACzB,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;QAC7B,OAAO,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC;QAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,qDAAqD;IACrD,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC;;;;OAIG;IACH,WAAW,CAAC,EAAE,oBAAoB,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,eAAe;;gBAkBd,MAAM,CAAC,EAAE,qBAAqB;IAK1C;;;;;;;;;;OAUG;IACG,cAAc,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;CAwIrE"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _TokensApiClient_instances, _TokensApiClient_fetch, _TokensApiClient_queryClient, _TokensApiClient_supportedChainIds, _TokensApiClient_supportedChainIdsCachedAt, _TokensApiClient_supportedChainIdsRefreshPromise,
|
|
12
|
+
var _TokensApiClient_instances, _TokensApiClient_fetch, _TokensApiClient_queryClient, _TokensApiClient_supportedChainIds, _TokensApiClient_supportedChainIdsCachedAt, _TokensApiClient_supportedChainIdsRefreshPromise, _TokensApiClient_isSupportedChain, _TokensApiClient_refreshSupportedChainIds, _TokensApiClient_fetchTokenListUncached;
|
|
13
13
|
import { ChainId as ControllerChainId, convertHexToDecimal } from "@metamask/controller-utils";
|
|
14
14
|
/**
|
|
15
15
|
* Same host + path that `TokenListController` (assets-controllers) uses
|
|
@@ -24,17 +24,6 @@ const TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';
|
|
|
24
24
|
* that are not in the supported-networks list.
|
|
25
25
|
*/
|
|
26
26
|
const SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;
|
|
27
|
-
/**
|
|
28
|
-
* Per-chain suggested occurrence floors used as the `occurrenceFloor` query
|
|
29
|
-
* param when fetching token lists (and aligned with TokenDataSource spam
|
|
30
|
-
* filtering). Keys are decimal chain IDs.
|
|
31
|
-
*/
|
|
32
|
-
const SUGGESTED_OCCURRENCE_FLOORS_URL = `${TOKEN_END_POINT_API}/v1/suggestedOccurrenceFloors`;
|
|
33
|
-
/**
|
|
34
|
-
* Fallback `occurrenceFloor` when `/v1/suggestedOccurrenceFloors` has no entry
|
|
35
|
-
* for the chain, or the floors request fails.
|
|
36
|
-
*/
|
|
37
|
-
const DEFAULT_OCCURRENCE_FLOOR = 3;
|
|
38
27
|
/**
|
|
39
28
|
* How long to keep the supported-networks response in the instance-level
|
|
40
29
|
* cache before refreshing it. One hour is sufficient — the list rarely
|
|
@@ -43,10 +32,27 @@ const DEFAULT_OCCURRENCE_FLOOR = 3;
|
|
|
43
32
|
*/
|
|
44
33
|
const SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60000;
|
|
45
34
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
35
|
+
* Tempo Mainnet — not yet present in `@metamask/controller-utils`'s `ChainId`
|
|
36
|
+
* map at the time of writing, so it's spelled out as a literal here exactly as
|
|
37
|
+
* `TokenListController` does (see `token-service.ts:getTokensURL`).
|
|
38
|
+
*/
|
|
39
|
+
const TEMPO_MAINNET_CHAIN_ID = '0x1079';
|
|
40
|
+
/**
|
|
41
|
+
* Per-chain occurrence floor, mirroring `TokenListController.getTokensURL`:
|
|
42
|
+
* Linea mainnet, MegaETH mainnet, and Tempo mainnet have thinner aggregator
|
|
43
|
+
* coverage so we lower the floor; everything else uses the default 3.
|
|
44
|
+
*
|
|
45
|
+
* @param hexChainId - Hex chain ID.
|
|
46
|
+
* @returns The occurrence floor to send to the Tokens API.
|
|
48
47
|
*/
|
|
49
|
-
|
|
48
|
+
function getOccurrenceFloor(hexChainId) {
|
|
49
|
+
if (hexChainId === ControllerChainId['linea-mainnet'] ||
|
|
50
|
+
hexChainId === ControllerChainId['megaeth-mainnet'] ||
|
|
51
|
+
hexChainId === TEMPO_MAINNET_CHAIN_ID) {
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
return 3;
|
|
55
|
+
}
|
|
50
56
|
/**
|
|
51
57
|
* TanStack-Query cache config for the cached `fetchTokenList` path.
|
|
52
58
|
*
|
|
@@ -63,10 +69,9 @@ const TOKEN_LIST_GC_TIME_MS = 60 * 60000;
|
|
|
63
69
|
*
|
|
64
70
|
* Fetches the per-chain ERC-20 token list from the same endpoint that
|
|
65
71
|
* `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),
|
|
66
|
-
* with the same query parameters
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* filter is still applied client-side.
|
|
72
|
+
* with the same query parameters and the same per-chain occurrence floor /
|
|
73
|
+
* Linea aggregator filter. This keeps RPC token detection in lockstep with
|
|
74
|
+
* the wallet's primary token list.
|
|
70
75
|
*
|
|
71
76
|
* Before fetching a chain's token list, the client checks
|
|
72
77
|
* `/v2/supportedNetworks` and returns `[]` immediately for chains that are
|
|
@@ -92,12 +97,6 @@ export class TokensApiClient {
|
|
|
92
97
|
* calls arrive before the first one resolves.
|
|
93
98
|
*/
|
|
94
99
|
_TokensApiClient_supportedChainIdsRefreshPromise.set(this, void 0);
|
|
95
|
-
/** Decimal chain ID → suggested occurrence floor from Token API. */
|
|
96
|
-
_TokensApiClient_suggestedOccurrenceFloors.set(this, void 0);
|
|
97
|
-
/** Timestamp of the last successful floors fetch. */
|
|
98
|
-
_TokensApiClient_suggestedOccurrenceFloorsCachedAt.set(this, 0);
|
|
99
|
-
/** In-flight floors request shared across concurrent callers. */
|
|
100
|
-
_TokensApiClient_suggestedOccurrenceFloorsRefreshPromise.set(this, void 0);
|
|
101
100
|
__classPrivateFieldSet(this, _TokensApiClient_fetch, config?.fetch ?? globalThis.fetch.bind(globalThis), "f");
|
|
102
101
|
__classPrivateFieldSet(this, _TokensApiClient_queryClient, config?.queryClient, "f");
|
|
103
102
|
}
|
|
@@ -137,7 +136,7 @@ export class TokensApiClient {
|
|
|
137
136
|
});
|
|
138
137
|
}
|
|
139
138
|
}
|
|
140
|
-
_TokensApiClient_fetch = new WeakMap(), _TokensApiClient_queryClient = new WeakMap(), _TokensApiClient_supportedChainIds = new WeakMap(), _TokensApiClient_supportedChainIdsCachedAt = new WeakMap(), _TokensApiClient_supportedChainIdsRefreshPromise = new WeakMap(),
|
|
139
|
+
_TokensApiClient_fetch = new WeakMap(), _TokensApiClient_queryClient = new WeakMap(), _TokensApiClient_supportedChainIds = new WeakMap(), _TokensApiClient_supportedChainIdsCachedAt = new WeakMap(), _TokensApiClient_supportedChainIdsRefreshPromise = new WeakMap(), _TokensApiClient_instances = new WeakSet(), _TokensApiClient_isSupportedChain =
|
|
141
140
|
/**
|
|
142
141
|
* Check whether the given chain is present in the supported-networks list.
|
|
143
142
|
*
|
|
@@ -186,68 +185,10 @@ async function _TokensApiClient_refreshSupportedChainIds(now) {
|
|
|
186
185
|
}
|
|
187
186
|
})(), "f");
|
|
188
187
|
return __classPrivateFieldGet(this, _TokensApiClient_supportedChainIdsRefreshPromise, "f");
|
|
189
|
-
}, _TokensApiClient_getOccurrenceFloor =
|
|
190
|
-
/**
|
|
191
|
-
* Resolve the `occurrenceFloor` query param for a chain from Token API
|
|
192
|
-
* `/v1/suggestedOccurrenceFloors`. Falls back to
|
|
193
|
-
* {@link DEFAULT_OCCURRENCE_FLOOR} when the chain is missing or the request
|
|
194
|
-
* fails.
|
|
195
|
-
*
|
|
196
|
-
* @param hexChainId - Hex chain ID.
|
|
197
|
-
* @returns Occurrence floor to send to `/tokens/{chainId}`.
|
|
198
|
-
*/
|
|
199
|
-
async function _TokensApiClient_getOccurrenceFloor(hexChainId) {
|
|
200
|
-
const now = Date.now();
|
|
201
|
-
if (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloors, "f") === undefined ||
|
|
202
|
-
now - __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, "f") >=
|
|
203
|
-
SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS) {
|
|
204
|
-
await __classPrivateFieldGet(this, _TokensApiClient_instances, "m", _TokensApiClient_refreshSuggestedOccurrenceFloors).call(this, now);
|
|
205
|
-
}
|
|
206
|
-
const decimalChainId = String(convertHexToDecimal(hexChainId));
|
|
207
|
-
return (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloors, "f")?.[decimalChainId] ??
|
|
208
|
-
DEFAULT_OCCURRENCE_FLOOR);
|
|
209
|
-
}, _TokensApiClient_refreshSuggestedOccurrenceFloors =
|
|
210
|
-
/**
|
|
211
|
-
* Fetch `/v1/suggestedOccurrenceFloors` and update the instance cache.
|
|
212
|
-
* Concurrent callers share the in-flight Promise. Failures leave the cache
|
|
213
|
-
* empty so {@link #getOccurrenceFloor} falls back to the default.
|
|
214
|
-
*
|
|
215
|
-
* @param now - Current timestamp used to stamp the cache entry.
|
|
216
|
-
* @returns A promise that resolves once the cache has been refreshed.
|
|
217
|
-
*/
|
|
218
|
-
async function _TokensApiClient_refreshSuggestedOccurrenceFloors(now) {
|
|
219
|
-
if (__classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f") !== undefined) {
|
|
220
|
-
return __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f");
|
|
221
|
-
}
|
|
222
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, (async () => {
|
|
223
|
-
try {
|
|
224
|
-
const response = await __classPrivateFieldGet(this, _TokensApiClient_fetch, "f").call(this, SUGGESTED_OCCURRENCE_FLOORS_URL);
|
|
225
|
-
if (response.ok) {
|
|
226
|
-
const data = (await response.json());
|
|
227
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, data && typeof data === 'object' && !Array.isArray(data)
|
|
228
|
-
? data
|
|
229
|
-
: {}, "f");
|
|
230
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, {}, "f");
|
|
234
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
catch {
|
|
238
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloors, {}, "f");
|
|
239
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsCachedAt, now, "f");
|
|
240
|
-
}
|
|
241
|
-
finally {
|
|
242
|
-
__classPrivateFieldSet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, undefined, "f");
|
|
243
|
-
}
|
|
244
|
-
})(), "f");
|
|
245
|
-
return __classPrivateFieldGet(this, _TokensApiClient_suggestedOccurrenceFloorsRefreshPromise, "f");
|
|
246
188
|
}, _TokensApiClient_fetchTokenListUncached = async function _TokensApiClient_fetchTokenListUncached(hexChainId) {
|
|
247
189
|
const decimalChainId = convertHexToDecimal(hexChainId);
|
|
248
|
-
const occurrenceFloor =
|
|
249
|
-
//
|
|
250
|
-
// but `occurrenceFloor` comes from `/v1/suggestedOccurrenceFloors`.
|
|
190
|
+
const occurrenceFloor = getOccurrenceFloor(hexChainId);
|
|
191
|
+
// Mirrors `TokenListController.getTokensURL` exactly (token-service.ts).
|
|
251
192
|
// No `first=...` cap — the API returns the full per-chain list bounded
|
|
252
193
|
// server-side by `occurrenceFloor`.
|
|
253
194
|
const url = `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokensApiClient.mjs","sourceRoot":"","sources":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EAAE;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,WAAW,EAAE,IAAI,CAAC,WAAW;KAC9B,CAAC,CAAC,CAAC;AACN,CAAC;AAGH;;;;;;;;;;GAUG;AACH,SAAS,yBAAyB,CAChC,UAAmB,EACnB,KAAyB;IAEzB,IAAI,UAAU,KAAK,iBAAiB,CAAC,eAAe,CAAC,EAAE,CAAC;QACtD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,EAAE,CAAC;YAC3C,OAAO,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,WAAW,CAAC,MAAM,IAAI,CAAC,CAAC;QACtE,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n ChainId as ControllerChainId,\n convertHexToDecimal,\n} from '@metamask/controller-utils';\n\nimport type { ChainId, TokenListEntry } from '../types';\n\n/**\n * Same host + path that `TokenListController` (assets-controllers) uses\n * (`token-service.ts` → `getTokensURL`). Sharing this endpoint keeps the\n * RPC token detector aligned with the rest of the wallet's token listing\n * (occurrence floors, aggregator filters, icon URLs, etc.).\n */\nconst TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';\n\n/**\n * Endpoint that returns which CAIP chain IDs the Tokens API supports.\n * Used to skip detection (and avoid unnecessary network requests) for chains\n * that are not in the supported-networks list.\n */\nconst SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;\n\n/**\n * Per-chain suggested occurrence floors used as the `occurrenceFloor` query\n * param when fetching token lists (and aligned with TokenDataSource spam\n * filtering). Keys are decimal chain IDs.\n */\nconst SUGGESTED_OCCURRENCE_FLOORS_URL = `${TOKEN_END_POINT_API}/v1/suggestedOccurrenceFloors`;\n\n/**\n * Fallback `occurrenceFloor` when `/v1/suggestedOccurrenceFloors` has no entry\n * for the chain, or the floors request fails.\n */\nconst DEFAULT_OCCURRENCE_FLOOR = 3;\n\n/**\n * How long to keep the supported-networks response in the instance-level\n * cache before refreshing it. One hour is sufficient — the list rarely\n * changes and a stale cache just means we may do an unnecessary token-list\n * fetch for a newly-unsupported chain (harmless).\n */\nconst SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * How long to keep suggested occurrence floors cached. Same TTL as supported\n * networks — floors change infrequently.\n */\nconst SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * TanStack-Query cache config for the cached `fetchTokenList` path.\n *\n * The Tokens API per-chain list barely changes between polls, so we keep\n * results fresh for a few minutes (`staleTime`) and retain them in cache for\n * an hour (`gcTime`) so re-detections across accounts/chains hit the cache.\n * These tunings only apply when a `queryClient` is provided to the client;\n * the uncached fallback path (used in standalone tests) is unaffected.\n */\nconst TOKEN_LIST_STALE_TIME_MS = 5 * 60_000;\nconst TOKEN_LIST_GC_TIME_MS = 60 * 60_000;\n\n/**\n * Shape of the `/v2/supportedNetworks` response.\n */\ntype SupportedNetworksResponse = {\n fullSupport?: string[];\n partialSupport?: string[];\n};\n\n/**\n * Shape of a single item returned by the Tokens API `/tokens/{chainId}`\n * endpoint. Mirrors `TokenListToken` in\n * `packages/assets-controllers/src/TokenListController.ts` and the response\n * parsed by `fetchTokenListByChainId` in `token-service.ts`.\n */\ntype ApiTokenListItem = {\n address: string;\n symbol?: string;\n name?: string;\n decimals?: number;\n occurrences?: number;\n aggregators?: string[];\n iconUrl?: string;\n};\n\n/**\n * Minimal structural type for the TanStack Query client method we use.\n * Avoids a direct dependency on `@tanstack/query-core` while still being\n * fully compatible with the shared `QueryClient` exposed by\n * `ApiPlatformClient.queryClient` (`@metamask/core-backend`).\n */\nexport type TokenListQueryClient = {\n fetchQuery<TData>(options: {\n queryKey: readonly unknown[];\n queryFn: () => Promise<TData>;\n staleTime?: number;\n gcTime?: number;\n }): Promise<TData>;\n};\n\nexport type TokensApiClientConfig = {\n /** Fetch function (defaults to globalThis.fetch). */\n fetch?: typeof globalThis.fetch;\n /**\n * Optional TanStack-Query client used to cache token-list responses across\n * detector polls / accounts / instances. When omitted, every call hits the\n * network directly (preserves prior behaviour for tests and standalone use).\n */\n queryClient?: TokenListQueryClient;\n};\n\n/**\n * Client for the MetaMask Tokens API.\n *\n * Fetches the per-chain ERC-20 token list from the same endpoint that\n * `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),\n * with the same query parameters. The `occurrenceFloor` query param comes from\n * Token API `/v1/suggestedOccurrenceFloors` (fallback {@link DEFAULT_OCCURRENCE_FLOOR}),\n * matching `TokenDataSource` spam filtering. Linea's post-response aggregator\n * filter is still applied client-side.\n *\n * Before fetching a chain's token list, the client checks\n * `/v2/supportedNetworks` and returns `[]` immediately for chains that are\n * not in the supported list, avoiding unnecessary API calls. The supported-\n * networks response is cached in-memory for one hour.\n *\n * When constructed with a `queryClient`, token-list results are cached and\n * deduped via TanStack Query (5 min staleTime, 1 h gcTime), so concurrent\n * detection cycles for the same chain coalesce into a single network request.\n */\nexport class TokensApiClient {\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #queryClient: TokenListQueryClient | undefined;\n\n /** CAIP chain IDs returned by `/v2/supportedNetworks` (both tiers). */\n #supportedChainIds: Set<string> | undefined;\n\n /** Timestamp of the last successful `/v2/supportedNetworks` fetch. */\n #supportedChainIdsCachedAt = 0;\n\n /**\n * In-flight `/v2/supportedNetworks` request shared across concurrent callers\n * so only one network request is made even when multiple `fetchTokenList`\n * calls arrive before the first one resolves.\n */\n #supportedChainIdsRefreshPromise: Promise<void> | undefined;\n\n /** Decimal chain ID → suggested occurrence floor from Token API. */\n #suggestedOccurrenceFloors: Record<string, number> | undefined;\n\n /** Timestamp of the last successful floors fetch. */\n #suggestedOccurrenceFloorsCachedAt = 0;\n\n /** In-flight floors request shared across concurrent callers. */\n #suggestedOccurrenceFloorsRefreshPromise: Promise<void> | undefined;\n\n constructor(config?: TokensApiClientConfig) {\n this.#fetch = config?.fetch ?? globalThis.fetch.bind(globalThis);\n this.#queryClient = config?.queryClient;\n }\n\n /**\n * Fetch the list of ERC-20 tokens for a chain from the Tokens API.\n *\n * Returns `[]` immediately (without hitting the token-list endpoint) when\n * the chain is not in the `/v2/supportedNetworks` response, or when the\n * supported-networks check fails for any reason.\n *\n * @param hexChainId - Chain ID in hex format (e.g. `'0x1'` for Ethereum mainnet).\n * @returns Array of token list entries with address and metadata, or an empty\n * array if the chain is unsupported or the request fails.\n */\n async fetchTokenList(hexChainId: ChainId): Promise<TokenListEntry[]> {\n // Treat any error from the supported-networks check as \"not supported\":\n // the token-list endpoint is only contacted for known-supported chains.\n if (!(await this.#isSupportedChain(hexChainId).catch(() => false))) {\n return [];\n }\n\n const queryClient = this.#queryClient;\n if (queryClient === undefined) {\n return this.#fetchTokenListUncached(hexChainId);\n }\n\n return queryClient.fetchQuery({\n // Namespacing keeps this key from colliding with other clients that\n // share the same QueryClient (e.g. core-backend's ApiPlatformClient).\n queryKey: [\n 'assets-controller',\n 'rpc-detection',\n 'token-list',\n { chainId: hexChainId },\n ],\n queryFn: () => this.#fetchTokenListUncached(hexChainId),\n staleTime: TOKEN_LIST_STALE_TIME_MS,\n gcTime: TOKEN_LIST_GC_TIME_MS,\n });\n }\n\n /**\n * Check whether the given chain is present in the supported-networks list.\n *\n * Uses an instance-level cache (1 h TTL). Throws if the network request\n * fails — callers should handle this (e.g. via `.catch(() => false)`).\n *\n * @param hexChainId - Hex chain ID to check.\n * @returns `true` when the chain is in the supported-networks list.\n */\n async #isSupportedChain(hexChainId: ChainId): Promise<boolean> {\n const now = Date.now();\n if (\n this.#supportedChainIds === undefined ||\n now - this.#supportedChainIdsCachedAt >= SUPPORTED_NETWORKS_CACHE_TTL_MS\n ) {\n await this.#refreshSupportedChainIds(now);\n }\n\n const caipChainId = `eip155:${convertHexToDecimal(hexChainId)}`;\n return this.#supportedChainIds?.has(caipChainId) ?? false;\n }\n\n /**\n * Fetch `/v2/supportedNetworks` and update the instance cache.\n * Concurrent callers share the same in-flight Promise so only one network\n * request is made. If the response is not 2xx the cache is left unchanged\n * (the chain will be treated as unsupported this cycle).\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSupportedChainIds(now: number): Promise<void> {\n if (this.#supportedChainIdsRefreshPromise !== undefined) {\n return this.#supportedChainIdsRefreshPromise;\n }\n\n this.#supportedChainIdsRefreshPromise = (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUPPORTED_NETWORKS_URL);\n if (response.ok) {\n const data = (await response.json()) as SupportedNetworksResponse;\n this.#supportedChainIds = new Set([\n ...(data.fullSupport ?? []),\n ...(data.partialSupport ?? []),\n ]);\n this.#supportedChainIdsCachedAt = now;\n }\n } finally {\n this.#supportedChainIdsRefreshPromise = undefined;\n }\n })();\n\n return this.#supportedChainIdsRefreshPromise;\n }\n\n /**\n * Resolve the `occurrenceFloor` query param for a chain from Token API\n * `/v1/suggestedOccurrenceFloors`. Falls back to\n * {@link DEFAULT_OCCURRENCE_FLOOR} when the chain is missing or the request\n * fails.\n *\n * @param hexChainId - Hex chain ID.\n * @returns Occurrence floor to send to `/tokens/{chainId}`.\n */\n async #getOccurrenceFloor(hexChainId: ChainId): Promise<number> {\n const now = Date.now();\n if (\n this.#suggestedOccurrenceFloors === undefined ||\n now - this.#suggestedOccurrenceFloorsCachedAt >=\n SUGGESTED_OCCURRENCE_FLOORS_CACHE_TTL_MS\n ) {\n await this.#refreshSuggestedOccurrenceFloors(now);\n }\n\n const decimalChainId = String(convertHexToDecimal(hexChainId));\n return (\n this.#suggestedOccurrenceFloors?.[decimalChainId] ??\n DEFAULT_OCCURRENCE_FLOOR\n );\n }\n\n /**\n * Fetch `/v1/suggestedOccurrenceFloors` and update the instance cache.\n * Concurrent callers share the in-flight Promise. Failures leave the cache\n * empty so {@link #getOccurrenceFloor} falls back to the default.\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSuggestedOccurrenceFloors(now: number): Promise<void> {\n if (this.#suggestedOccurrenceFloorsRefreshPromise !== undefined) {\n return this.#suggestedOccurrenceFloorsRefreshPromise;\n }\n\n this.#suggestedOccurrenceFloorsRefreshPromise =\n (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUGGESTED_OCCURRENCE_FLOORS_URL);\n if (response.ok) {\n const data = (await response.json()) as Record<string, number>;\n this.#suggestedOccurrenceFloors =\n data && typeof data === 'object' && !Array.isArray(data)\n ? data\n : {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n } else {\n this.#suggestedOccurrenceFloors = {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n }\n } catch {\n this.#suggestedOccurrenceFloors = {};\n this.#suggestedOccurrenceFloorsCachedAt = now;\n } finally {\n this.#suggestedOccurrenceFloorsRefreshPromise = undefined;\n }\n })();\n\n return this.#suggestedOccurrenceFloorsRefreshPromise;\n }\n\n async #fetchTokenListUncached(\n hexChainId: ChainId,\n ): Promise<TokenListEntry[]> {\n const decimalChainId = convertHexToDecimal(hexChainId);\n const occurrenceFloor = await this.#getOccurrenceFloor(hexChainId);\n\n // Same query shape as `TokenListController.getTokensURL` (token-service.ts),\n // but `occurrenceFloor` comes from `/v1/suggestedOccurrenceFloors`.\n // No `first=...` cap — the API returns the full per-chain list bounded\n // server-side by `occurrenceFloor`.\n const url =\n `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +\n `?occurrenceFloor=${occurrenceFloor}` +\n `&includeNativeAssets=false` +\n `&includeTokenFees=false` +\n `&includeAssetType=false` +\n `&includeERC20Permit=false` +\n `&includeStorage=false` +\n `&includeRwaData=true`;\n\n let response: Response;\n try {\n response = await this.#fetch(url);\n } catch (error) {\n console.error(\n `Tokens API request failed for chain ${hexChainId}:`,\n error,\n );\n return [];\n }\n\n if (!response.ok) {\n console.error(\n `Tokens API responded with ${response.status} for chain ${hexChainId}`,\n );\n return [];\n }\n\n const raw = (await response.json()) as unknown;\n const items: ApiTokenListItem[] = Array.isArray(raw)\n ? (raw as ApiTokenListItem[])\n : [];\n\n const filtered = applyChainSpecificFilters(hexChainId, items);\n\n return filtered.map((item) => ({\n address: item.address,\n symbol: item.symbol ?? '',\n name: item.name ?? '',\n decimals: item.decimals ?? 18,\n iconUrl: item.iconUrl,\n aggregators: item.aggregators,\n occurrences: item.occurrences,\n }));\n }\n}\n\n/**\n * Apply chain-specific filters to a raw token list response, mirroring\n * `fetchTokenListByChainId` in `assets-controllers/src/token-service.ts`.\n *\n * For Linea mainnet, the API returns extras with low aggregator coverage, so\n * we keep only entries flagged by Linea's own team or seen by ≥3 aggregators.\n *\n * @param hexChainId - Hex chain ID.\n * @param items - Raw items from the API response.\n * @returns Items after chain-specific filtering.\n */\nfunction applyChainSpecificFilters(\n hexChainId: ChainId,\n items: ApiTokenListItem[],\n): ApiTokenListItem[] {\n if (hexChainId === ControllerChainId['linea-mainnet']) {\n return items.filter((item) => {\n const aggregators = item.aggregators ?? [];\n return aggregators.includes('lineaTeam') || aggregators.length >= 3;\n });\n }\n return items;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TokensApiClient.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/clients/TokensApiClient.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,OAAO,IAAI,iBAAiB,EAC5B,mBAAmB,EACpB,mCAAmC;AAIpC;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,kCAAkC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,GAAG,mBAAmB,uBAAuB,CAAC;AAE7E;;;;;GAKG;AACH,MAAM,+BAA+B,GAAG,EAAE,GAAG,KAAM,CAAC;AAEpD;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,QAAiB,CAAC;AAEjD;;;;;;;GAOG;AACH,SAAS,kBAAkB,CAAC,UAAmB;IA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AC;IAC/C,MAAM,KAAK,GAAuB,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;QAClD,CAAC,CAAE,GAA0B;QAC7B,CAAC,CAAC,EAAE,CAAC;IAEP,MAAM,QAAQ,GAAG,yBAAyB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAE9D,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,EAAE;QACzB,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,EAAE;QACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ,IAAI,EAAE;QAC7B,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,WAAW,EAAE,IAAI,CAAC,WAAW;KAC9B,CAAC,CAAC,CAAC;AACN,CAAC;AAGH;;;;;;;;;;GAUG;AACH,SAAS,yBAAyB,CAChC,UAAmB,EACnB,KAAyB;IAEzB,IAAI,UAAU,KAAK,iBAAiB,CAAC,eAAe,CAAC,EAAE,CAAC;QACtD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,EAAE,CAAC;YAC3C,OAAO,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,WAAW,CAAC,MAAM,IAAI,CAAC,CAAC;QACtE,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n ChainId as ControllerChainId,\n convertHexToDecimal,\n} from '@metamask/controller-utils';\n\nimport type { ChainId, TokenListEntry } from '../types';\n\n/**\n * Same host + path that `TokenListController` (assets-controllers) uses\n * (`token-service.ts` → `getTokensURL`). Sharing this endpoint keeps the\n * RPC token detector aligned with the rest of the wallet's token listing\n * (occurrence floors, aggregator filters, icon URLs, etc.).\n */\nconst TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';\n\n/**\n * Endpoint that returns which CAIP chain IDs the Tokens API supports.\n * Used to skip detection (and avoid unnecessary network requests) for chains\n * that are not in the supported-networks list.\n */\nconst SUPPORTED_NETWORKS_URL = `${TOKEN_END_POINT_API}/v2/supportedNetworks`;\n\n/**\n * How long to keep the supported-networks response in the instance-level\n * cache before refreshing it. One hour is sufficient — the list rarely\n * changes and a stale cache just means we may do an unnecessary token-list\n * fetch for a newly-unsupported chain (harmless).\n */\nconst SUPPORTED_NETWORKS_CACHE_TTL_MS = 60 * 60_000;\n\n/**\n * Tempo Mainnet — not yet present in `@metamask/controller-utils`'s `ChainId`\n * map at the time of writing, so it's spelled out as a literal here exactly as\n * `TokenListController` does (see `token-service.ts:getTokensURL`).\n */\nconst TEMPO_MAINNET_CHAIN_ID = '0x1079' as const;\n\n/**\n * Per-chain occurrence floor, mirroring `TokenListController.getTokensURL`:\n * Linea mainnet, MegaETH mainnet, and Tempo mainnet have thinner aggregator\n * coverage so we lower the floor; everything else uses the default 3.\n *\n * @param hexChainId - Hex chain ID.\n * @returns The occurrence floor to send to the Tokens API.\n */\nfunction getOccurrenceFloor(hexChainId: ChainId): number {\n if (\n hexChainId === ControllerChainId['linea-mainnet'] ||\n hexChainId === ControllerChainId['megaeth-mainnet'] ||\n hexChainId === TEMPO_MAINNET_CHAIN_ID\n ) {\n return 1;\n }\n return 3;\n}\n\n/**\n * TanStack-Query cache config for the cached `fetchTokenList` path.\n *\n * The Tokens API per-chain list barely changes between polls, so we keep\n * results fresh for a few minutes (`staleTime`) and retain them in cache for\n * an hour (`gcTime`) so re-detections across accounts/chains hit the cache.\n * These tunings only apply when a `queryClient` is provided to the client;\n * the uncached fallback path (used in standalone tests) is unaffected.\n */\nconst TOKEN_LIST_STALE_TIME_MS = 5 * 60_000;\nconst TOKEN_LIST_GC_TIME_MS = 60 * 60_000;\n\n/**\n * Shape of the `/v2/supportedNetworks` response.\n */\ntype SupportedNetworksResponse = {\n fullSupport?: string[];\n partialSupport?: string[];\n};\n\n/**\n * Shape of a single item returned by the Tokens API `/tokens/{chainId}`\n * endpoint. Mirrors `TokenListToken` in\n * `packages/assets-controllers/src/TokenListController.ts` and the response\n * parsed by `fetchTokenListByChainId` in `token-service.ts`.\n */\ntype ApiTokenListItem = {\n address: string;\n symbol?: string;\n name?: string;\n decimals?: number;\n occurrences?: number;\n aggregators?: string[];\n iconUrl?: string;\n};\n\n/**\n * Minimal structural type for the TanStack Query client method we use.\n * Avoids a direct dependency on `@tanstack/query-core` while still being\n * fully compatible with the shared `QueryClient` exposed by\n * `ApiPlatformClient.queryClient` (`@metamask/core-backend`).\n */\nexport type TokenListQueryClient = {\n fetchQuery<TData>(options: {\n queryKey: readonly unknown[];\n queryFn: () => Promise<TData>;\n staleTime?: number;\n gcTime?: number;\n }): Promise<TData>;\n};\n\nexport type TokensApiClientConfig = {\n /** Fetch function (defaults to globalThis.fetch). */\n fetch?: typeof globalThis.fetch;\n /**\n * Optional TanStack-Query client used to cache token-list responses across\n * detector polls / accounts / instances. When omitted, every call hits the\n * network directly (preserves prior behaviour for tests and standalone use).\n */\n queryClient?: TokenListQueryClient;\n};\n\n/**\n * Client for the MetaMask Tokens API.\n *\n * Fetches the per-chain ERC-20 token list from the same endpoint that\n * `TokenListController` uses (`token.api.cx.metamask.io/tokens/{chainId}`),\n * with the same query parameters and the same per-chain occurrence floor /\n * Linea aggregator filter. This keeps RPC token detection in lockstep with\n * the wallet's primary token list.\n *\n * Before fetching a chain's token list, the client checks\n * `/v2/supportedNetworks` and returns `[]` immediately for chains that are\n * not in the supported list, avoiding unnecessary API calls. The supported-\n * networks response is cached in-memory for one hour.\n *\n * When constructed with a `queryClient`, token-list results are cached and\n * deduped via TanStack Query (5 min staleTime, 1 h gcTime), so concurrent\n * detection cycles for the same chain coalesce into a single network request.\n */\nexport class TokensApiClient {\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #queryClient: TokenListQueryClient | undefined;\n\n /** CAIP chain IDs returned by `/v2/supportedNetworks` (both tiers). */\n #supportedChainIds: Set<string> | undefined;\n\n /** Timestamp of the last successful `/v2/supportedNetworks` fetch. */\n #supportedChainIdsCachedAt = 0;\n\n /**\n * In-flight `/v2/supportedNetworks` request shared across concurrent callers\n * so only one network request is made even when multiple `fetchTokenList`\n * calls arrive before the first one resolves.\n */\n #supportedChainIdsRefreshPromise: Promise<void> | undefined;\n\n constructor(config?: TokensApiClientConfig) {\n this.#fetch = config?.fetch ?? globalThis.fetch.bind(globalThis);\n this.#queryClient = config?.queryClient;\n }\n\n /**\n * Fetch the list of ERC-20 tokens for a chain from the Tokens API.\n *\n * Returns `[]` immediately (without hitting the token-list endpoint) when\n * the chain is not in the `/v2/supportedNetworks` response, or when the\n * supported-networks check fails for any reason.\n *\n * @param hexChainId - Chain ID in hex format (e.g. `'0x1'` for Ethereum mainnet).\n * @returns Array of token list entries with address and metadata, or an empty\n * array if the chain is unsupported or the request fails.\n */\n async fetchTokenList(hexChainId: ChainId): Promise<TokenListEntry[]> {\n // Treat any error from the supported-networks check as \"not supported\":\n // the token-list endpoint is only contacted for known-supported chains.\n if (!(await this.#isSupportedChain(hexChainId).catch(() => false))) {\n return [];\n }\n\n const queryClient = this.#queryClient;\n if (queryClient === undefined) {\n return this.#fetchTokenListUncached(hexChainId);\n }\n\n return queryClient.fetchQuery({\n // Namespacing keeps this key from colliding with other clients that\n // share the same QueryClient (e.g. core-backend's ApiPlatformClient).\n queryKey: [\n 'assets-controller',\n 'rpc-detection',\n 'token-list',\n { chainId: hexChainId },\n ],\n queryFn: () => this.#fetchTokenListUncached(hexChainId),\n staleTime: TOKEN_LIST_STALE_TIME_MS,\n gcTime: TOKEN_LIST_GC_TIME_MS,\n });\n }\n\n /**\n * Check whether the given chain is present in the supported-networks list.\n *\n * Uses an instance-level cache (1 h TTL). Throws if the network request\n * fails — callers should handle this (e.g. via `.catch(() => false)`).\n *\n * @param hexChainId - Hex chain ID to check.\n * @returns `true` when the chain is in the supported-networks list.\n */\n async #isSupportedChain(hexChainId: ChainId): Promise<boolean> {\n const now = Date.now();\n if (\n this.#supportedChainIds === undefined ||\n now - this.#supportedChainIdsCachedAt >= SUPPORTED_NETWORKS_CACHE_TTL_MS\n ) {\n await this.#refreshSupportedChainIds(now);\n }\n\n const caipChainId = `eip155:${convertHexToDecimal(hexChainId)}`;\n return this.#supportedChainIds?.has(caipChainId) ?? false;\n }\n\n /**\n * Fetch `/v2/supportedNetworks` and update the instance cache.\n * Concurrent callers share the same in-flight Promise so only one network\n * request is made. If the response is not 2xx the cache is left unchanged\n * (the chain will be treated as unsupported this cycle).\n *\n * @param now - Current timestamp used to stamp the cache entry.\n * @returns A promise that resolves once the cache has been refreshed.\n */\n async #refreshSupportedChainIds(now: number): Promise<void> {\n if (this.#supportedChainIdsRefreshPromise !== undefined) {\n return this.#supportedChainIdsRefreshPromise;\n }\n\n this.#supportedChainIdsRefreshPromise = (async (): Promise<void> => {\n try {\n const response = await this.#fetch(SUPPORTED_NETWORKS_URL);\n if (response.ok) {\n const data = (await response.json()) as SupportedNetworksResponse;\n this.#supportedChainIds = new Set([\n ...(data.fullSupport ?? []),\n ...(data.partialSupport ?? []),\n ]);\n this.#supportedChainIdsCachedAt = now;\n }\n } finally {\n this.#supportedChainIdsRefreshPromise = undefined;\n }\n })();\n\n return this.#supportedChainIdsRefreshPromise;\n }\n\n async #fetchTokenListUncached(\n hexChainId: ChainId,\n ): Promise<TokenListEntry[]> {\n const decimalChainId = convertHexToDecimal(hexChainId);\n const occurrenceFloor = getOccurrenceFloor(hexChainId);\n\n // Mirrors `TokenListController.getTokensURL` exactly (token-service.ts).\n // No `first=...` cap — the API returns the full per-chain list bounded\n // server-side by `occurrenceFloor`.\n const url =\n `${TOKEN_END_POINT_API}/tokens/${decimalChainId}` +\n `?occurrenceFloor=${occurrenceFloor}` +\n `&includeNativeAssets=false` +\n `&includeTokenFees=false` +\n `&includeAssetType=false` +\n `&includeERC20Permit=false` +\n `&includeStorage=false` +\n `&includeRwaData=true`;\n\n let response: Response;\n try {\n response = await this.#fetch(url);\n } catch (error) {\n console.error(\n `Tokens API request failed for chain ${hexChainId}:`,\n error,\n );\n return [];\n }\n\n if (!response.ok) {\n console.error(\n `Tokens API responded with ${response.status} for chain ${hexChainId}`,\n );\n return [];\n }\n\n const raw = (await response.json()) as unknown;\n const items: ApiTokenListItem[] = Array.isArray(raw)\n ? (raw as ApiTokenListItem[])\n : [];\n\n const filtered = applyChainSpecificFilters(hexChainId, items);\n\n return filtered.map((item) => ({\n address: item.address,\n symbol: item.symbol ?? '',\n name: item.name ?? '',\n decimals: item.decimals ?? 18,\n iconUrl: item.iconUrl,\n aggregators: item.aggregators,\n occurrences: item.occurrences,\n }));\n }\n}\n\n/**\n * Apply chain-specific filters to a raw token list response, mirroring\n * `fetchTokenListByChainId` in `assets-controllers/src/token-service.ts`.\n *\n * For Linea mainnet, the API returns extras with low aggregator coverage, so\n * we keep only entries flagged by Linea's own team or seen by ≥3 aggregators.\n *\n * @param hexChainId - Hex chain ID.\n * @param items - Raw items from the API response.\n * @returns Items after chain-specific filtering.\n */\nfunction applyChainSpecificFilters(\n hexChainId: ChainId,\n items: ApiTokenListItem[],\n): ApiTokenListItem[] {\n if (hexChainId === ControllerChainId['linea-mainnet']) {\n return items.filter((item) => {\n const aggregators = item.aggregators ?? [];\n return aggregators.includes('lineaTeam') || aggregators.length >= 3;\n });\n }\n return items;\n}\n"]}
|
package/package.json
CHANGED