@metamask-previews/assets-controller 16.1.0-preview-fb486948f → 16.1.0-preview-aecbce4ff
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 -15
- package/dist/AssetsController.d.ts +3 -7
- package/dist/AssetsController.d.ts.map +1 -1
- package/dist/AssetsController.js +223 -520
- package/dist/AssetsController.js.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.ts +1 -12
- package/dist/data-sources/AccountsApiDataSource.d.ts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.js +47 -155
- package/dist/data-sources/AccountsApiDataSource.js.map +1 -1
- package/dist/data-sources/RpcDataSource.d.ts +1 -6
- package/dist/data-sources/RpcDataSource.d.ts.map +1 -1
- package/dist/data-sources/RpcDataSource.js +34 -44
- package/dist/data-sources/RpcDataSource.js.map +1 -1
- package/dist/middlewares/ParallelMiddleware.d.ts.map +1 -1
- package/dist/middlewares/ParallelMiddleware.js +0 -9
- package/dist/middlewares/ParallelMiddleware.js.map +1 -1
- package/dist/middlewares/RpcFallbackMiddleware.d.ts +14 -12
- package/dist/middlewares/RpcFallbackMiddleware.d.ts.map +1 -1
- package/dist/middlewares/RpcFallbackMiddleware.js +120 -170
- package/dist/middlewares/RpcFallbackMiddleware.js.map +1 -1
- package/dist/pipeline/buildFastFetchSources.d.ts +0 -5
- package/dist/pipeline/buildFastFetchSources.d.ts.map +1 -1
- package/dist/pipeline/buildFastFetchSources.js +1 -7
- package/dist/pipeline/buildFastFetchSources.js.map +1 -1
- package/dist/types.d.ts +19 -28
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ParallelMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/ParallelMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,SAAS,CAAC;AAW7B,+EAA+E;AAC/E,eAAe;A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C;YAEF,MAAM,OAAO,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YAC1D,MAAM,cAAc,GAAG,kBAAkB,CACvC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CACzC,CAAC;YAEF,MAAM,oBAAoB,GAA2B;gBACnD,GAAG,OAAO,CAAC,oBAAoB;aAChC,CAAC;YACF,KAAK,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,YAAY,EAAE,CAAC;gBACtD,oBAAoB,CAAC,GAAG,wBAAwB,IAAI,UAAU,EAAE,CAAC;oBAC/D,UAAU,CAAC;YACf,CAAC;YAED,OAAO,IAAI,CAAC;gBACV,GAAG,OAAO;gBACV,QAAQ,EAAE,kBAAkB,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;gBAChE,oBAAoB;aACrB,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import pLimit from 'p-limit';\n\nimport type {\n ChainId,\n Context,\n DataRequest,\n DataResponse,\n Middleware,\n AssetsDataSource,\n} from '../types.js';\n\n// ============================================================================\n// MERGE HELPER\n// ============================================================================\n\n/**\n * Deep-merge multiple DataResponses into one.\n * Used when running balance data sources in parallel.\n *\n * @param responses - Array of DataResponse from each source.\n * @returns Single merged DataResponse.\n */\nexport function mergeDataResponses(responses: DataResponse[]): DataResponse {\n const merged: DataResponse = {};\n\n for (const response of responses) {\n if (response.assetsBalance) {\n merged.assetsBalance ??= {};\n for (const [accountId, accountBalances] of Object.entries(\n response.assetsBalance,\n )) {\n merged.assetsBalance[accountId] = {\n ...(merged.assetsBalance[accountId] ?? {}),\n ...accountBalances,\n };\n }\n }\n if (response.assetsInfo) {\n merged.assetsInfo = {\n ...(merged.assetsInfo ?? {}),\n ...response.assetsInfo,\n };\n }\n if (response.assetsPrice) {\n merged.assetsPrice = {\n ...(merged.assetsPrice ?? {}),\n ...response.assetsPrice,\n };\n }\n if (response.errors) {\n merged.errors = {\n ...(merged.errors ?? {}),\n ...response.errors,\n };\n }\n if (\n response.unprocessedCustomAssets &&\n response.unprocessedCustomAssets.length > 0\n ) {\n merged.unprocessedCustomAssets = [\n ...new Set([\n ...(merged.unprocessedCustomAssets ?? []),\n ...response.unprocessedCustomAssets,\n ]),\n ];\n }\n if (response.detectedAssets) {\n merged.detectedAssets = {\n ...(merged.detectedAssets ?? {}),\n ...response.detectedAssets,\n };\n }\n if (response.updateMode === 'full') {\n merged.updateMode = 'full';\n } else if (\n response.updateMode === 'merge' &&\n merged.updateMode !== 'full'\n ) {\n merged.updateMode = 'merge';\n }\n if (response.replaceCoveredChainBalances) {\n merged.replaceCoveredChainBalances = true;\n }\n }\n merged.updateMode ??= 'merge';\n\n return merged;\n}\n\n// ============================================================================\n// PARALLEL BALANCE MIDDLEWARE\n// ============================================================================\n\nconst PARALLEL_BALANCE_MIDDLEWARE_NAME = 'ParallelBalanceMiddleware';\n\n/** Max concurrent balance source calls (round 1 and fallback). */\nconst BALANCE_CONCURRENCY = 3;\n\nexport type BalanceSource = {\n getName(): string;\n /** Chains this source can fetch (e.g. from getActiveChainsSync()). Used to partition chains with no overlap. */\n getActiveChainsSync(): ChainId[];\n assetsMiddleware: Middleware;\n};\n\n/**\n * Partition request.chainIds so each chain is assigned to exactly one source\n * (by source order: first source that supports the chain gets it). Ensures no\n * chain overlap across data source calls.\n *\n * @param request - The data request with chainIds to partition.\n * @param sources - Balance sources in priority order (e.g. AccountsAPI, Snap, Rpc).\n * @returns Array of requests, one per source, each with only that source's assigned chainIds.\n */\nfunction partitionChainsBySource(\n request: DataRequest,\n sources: BalanceSource[],\n): DataRequest[] {\n const { chainIds } = request;\n const assigned = new Set<ChainId>();\n\n return sources.map((source) => {\n const supported = new Set(source.getActiveChainsSync());\n const chainsForSource = chainIds.filter(\n (id) => supported.has(id) && !assigned.has(id),\n );\n chainsForSource.forEach((id) => assigned.add(id));\n\n return {\n ...request,\n chainIds: chainsForSource,\n };\n });\n}\n\n/**\n * Collect chain IDs that failed in the first round (present in response.errors).\n * Used to run a fallback round with remaining sources.\n *\n * @param requests - Partitioned requests, one per source (same order as results).\n * @param results - Results from each source; chain IDs in requests[i] that have errors in results[i].response.errors are considered failed.\n * @returns Set of chain IDs that had errors in the first round.\n */\nfunction getFailedChainIds(\n requests: DataRequest[],\n results: { response: DataResponse }[],\n): Set<ChainId> {\n const failed = new Set<ChainId>();\n for (let i = 0; i < results.length; i++) {\n const errors = results[i].response.errors ?? {};\n for (const chainId of requests[i].chainIds) {\n if (errors[chainId]) {\n failed.add(chainId);\n }\n }\n }\n return failed;\n}\n\n/**\n * Middleware that runs multiple balance data source middlewares in parallel,\n * with no chain overlap. Chains that fail (response.errors) are re-partitioned\n * and fetched again in a fallback round so lower-priority sources can try them.\n *\n * @param sources - Array of balance sources in priority order (each with getName(), getActiveChainsSync(), assetsMiddleware).\n * @returns A single middleware that runs all sources in parallel and merges responses.\n */\nexport function createParallelBalanceMiddleware(sources: BalanceSource[]): {\n getName(): string;\n assetsMiddleware: Middleware;\n} {\n return {\n getName(): string {\n return PARALLEL_BALANCE_MIDDLEWARE_NAME;\n },\n\n assetsMiddleware: async (context, next): Promise<Context> => {\n if (sources.length === 0) {\n return next(context);\n }\n\n const noopNext = async (ctx: typeof context): Promise<typeof context> =>\n ctx;\n const limit = pLimit(BALANCE_CONCURRENCY);\n\n // Round 1: partition chains (no overlap), run with limited concurrency\n const requests = partitionChainsBySource(context.request, sources);\n const round1Timed = await Promise.all(\n sources.map((source, i) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: requests[i],\n response: {},\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n const results = round1Timed.map((entry) => entry.result);\n\n const durationByDataSource: Record<string, number> = {\n ...context.durationByDataSource,\n };\n for (const { sourceName, durationMs } of round1Timed) {\n const key = `${PARALLEL_BALANCE_MIDDLEWARE_NAME}.${sourceName}`;\n durationByDataSource[key] =\n (durationByDataSource[key] ?? 0) + durationMs;\n }\n\n let mergedResponse = mergeDataResponses(\n results.map((result) => result.response),\n );\n\n // Fallback: chains that failed (in errors) get re-partitioned and tried again\n const failedChainIds = getFailedChainIds(requests, results);\n if (failedChainIds.size > 0) {\n const fallbackRequest: DataRequest = {\n ...context.request,\n chainIds: [...failedChainIds],\n };\n const fallbackRequests = partitionChainsBySource(\n fallbackRequest,\n sources,\n );\n const fallbackTimed = await Promise.all(\n sources.map((source, i) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: fallbackRequests[i],\n response: {},\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n const fallbackResults = fallbackTimed.map((entry) => entry.result);\n for (const { sourceName, durationMs } of fallbackTimed) {\n const key = `${PARALLEL_BALANCE_MIDDLEWARE_NAME}.${sourceName}`;\n durationByDataSource[key] =\n (durationByDataSource[key] ?? 0) + durationMs;\n }\n const fallbackMerged = mergeDataResponses(\n fallbackResults.map((result) => result.response),\n );\n mergedResponse = mergeDataResponses([mergedResponse, fallbackMerged]);\n // Remove errors for chains we successfully got balance for in fallback\n if (mergedResponse.errors && mergedResponse.assetsBalance) {\n const chainsWithBalance = new Set<ChainId>();\n for (const accountBalances of Object.values(\n mergedResponse.assetsBalance,\n )) {\n for (const assetId of Object.keys(accountBalances)) {\n const chainId = assetId.split('/')[0] as ChainId;\n chainsWithBalance.add(chainId);\n }\n }\n for (const chainId of failedChainIds) {\n if (chainsWithBalance.has(chainId)) {\n delete mergedResponse.errors[chainId];\n }\n }\n }\n }\n\n return next({\n ...context,\n response: mergeDataResponses([context.response, mergedResponse]),\n durationByDataSource,\n });\n },\n };\n}\n\n// ============================================================================\n// PARALLEL TOKEN/PRICE MIDDLEWARE\n// ============================================================================\n\nconst PARALLEL_MIDDLEWARE_NAME = 'ParallelMiddleware';\n\n/** Max concurrent token/price source calls. */\nconst CONCURRENCY = 2;\n\n/**\n * Middleware that runs multiple data source middlewares (e.g. TokenDataSource,\n * PriceDataSource) in parallel with the same request. Responses are merged so\n * that assetsInfo (token metadata) and assetsPrice are combined. Use this to\n * fetch token and price data concurrently instead of sequentially.\n *\n * @param sources - Array of named middleware sources (getName + assetsMiddleware).\n * @returns A single middleware that runs all sources in parallel and merges responses.\n */\nexport function createParallelMiddleware(sources: AssetsDataSource[]): {\n getName(): string;\n assetsMiddleware: Middleware;\n} {\n return {\n getName(): string {\n return PARALLEL_MIDDLEWARE_NAME;\n },\n\n assetsMiddleware: async (context, next): Promise<Context> => {\n if (sources.length === 0) {\n return next(context);\n }\n\n const noopNext = async (ctx: typeof context): Promise<typeof context> =>\n ctx;\n const limit = pLimit(CONCURRENCY);\n\n const timedResults = await Promise.all(\n sources.map((source) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: context.request,\n response: { ...context.response },\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n\n const results = timedResults.map((entry) => entry.result);\n const mergedResponse = mergeDataResponses(\n results.map((result) => result.response),\n );\n\n const durationByDataSource: Record<string, number> = {\n ...context.durationByDataSource,\n };\n for (const { sourceName, durationMs } of timedResults) {\n durationByDataSource[`${PARALLEL_MIDDLEWARE_NAME}.${sourceName}`] =\n durationMs;\n }\n\n return next({\n ...context,\n response: mergeDataResponses([context.response, mergedResponse]),\n durationByDataSource,\n });\n },\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ParallelMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/ParallelMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,SAAS,CAAC;AAW7B,+EA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;gBACnD,GAAG,OAAO,CAAC,oBAAoB;aAChC,CAAC;YACF,KAAK,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,YAAY,EAAE,CAAC;gBACtD,oBAAoB,CAAC,GAAG,wBAAwB,IAAI,UAAU,EAAE,CAAC;oBAC/D,UAAU,CAAC;YACf,CAAC;YAED,OAAO,IAAI,CAAC;gBACV,GAAG,OAAO;gBACV,QAAQ,EAAE,kBAAkB,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;gBAChE,oBAAoB;aACrB,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import pLimit from 'p-limit';\n\nimport type {\n ChainId,\n Context,\n DataRequest,\n DataResponse,\n Middleware,\n AssetsDataSource,\n} from '../types.js';\n\n// ============================================================================\n// MERGE HELPER\n// ============================================================================\n\n/**\n * Deep-merge multiple DataResponses into one.\n * Used when running balance data sources in parallel.\n *\n * @param responses - Array of DataResponse from each source.\n * @returns Single merged DataResponse.\n */\nexport function mergeDataResponses(responses: DataResponse[]): DataResponse {\n const merged: DataResponse = {};\n\n for (const response of responses) {\n if (response.assetsBalance) {\n merged.assetsBalance ??= {};\n for (const [accountId, accountBalances] of Object.entries(\n response.assetsBalance,\n )) {\n merged.assetsBalance[accountId] = {\n ...(merged.assetsBalance[accountId] ?? {}),\n ...accountBalances,\n };\n }\n }\n if (response.assetsInfo) {\n merged.assetsInfo = {\n ...(merged.assetsInfo ?? {}),\n ...response.assetsInfo,\n };\n }\n if (response.assetsPrice) {\n merged.assetsPrice = {\n ...(merged.assetsPrice ?? {}),\n ...response.assetsPrice,\n };\n }\n if (response.errors) {\n merged.errors = {\n ...(merged.errors ?? {}),\n ...response.errors,\n };\n }\n if (response.detectedAssets) {\n merged.detectedAssets = {\n ...(merged.detectedAssets ?? {}),\n ...response.detectedAssets,\n };\n }\n if (response.updateMode === 'full') {\n merged.updateMode = 'full';\n } else if (\n response.updateMode === 'merge' &&\n merged.updateMode !== 'full'\n ) {\n merged.updateMode = 'merge';\n }\n if (response.replaceCoveredChainBalances) {\n merged.replaceCoveredChainBalances = true;\n }\n }\n merged.updateMode ??= 'merge';\n\n return merged;\n}\n\n// ============================================================================\n// PARALLEL BALANCE MIDDLEWARE\n// ============================================================================\n\nconst PARALLEL_BALANCE_MIDDLEWARE_NAME = 'ParallelBalanceMiddleware';\n\n/** Max concurrent balance source calls (round 1 and fallback). */\nconst BALANCE_CONCURRENCY = 3;\n\nexport type BalanceSource = {\n getName(): string;\n /** Chains this source can fetch (e.g. from getActiveChainsSync()). Used to partition chains with no overlap. */\n getActiveChainsSync(): ChainId[];\n assetsMiddleware: Middleware;\n};\n\n/**\n * Partition request.chainIds so each chain is assigned to exactly one source\n * (by source order: first source that supports the chain gets it). Ensures no\n * chain overlap across data source calls.\n *\n * @param request - The data request with chainIds to partition.\n * @param sources - Balance sources in priority order (e.g. AccountsAPI, Snap, Rpc).\n * @returns Array of requests, one per source, each with only that source's assigned chainIds.\n */\nfunction partitionChainsBySource(\n request: DataRequest,\n sources: BalanceSource[],\n): DataRequest[] {\n const { chainIds } = request;\n const assigned = new Set<ChainId>();\n\n return sources.map((source) => {\n const supported = new Set(source.getActiveChainsSync());\n const chainsForSource = chainIds.filter(\n (id) => supported.has(id) && !assigned.has(id),\n );\n chainsForSource.forEach((id) => assigned.add(id));\n\n return {\n ...request,\n chainIds: chainsForSource,\n };\n });\n}\n\n/**\n * Collect chain IDs that failed in the first round (present in response.errors).\n * Used to run a fallback round with remaining sources.\n *\n * @param requests - Partitioned requests, one per source (same order as results).\n * @param results - Results from each source; chain IDs in requests[i] that have errors in results[i].response.errors are considered failed.\n * @returns Set of chain IDs that had errors in the first round.\n */\nfunction getFailedChainIds(\n requests: DataRequest[],\n results: { response: DataResponse }[],\n): Set<ChainId> {\n const failed = new Set<ChainId>();\n for (let i = 0; i < results.length; i++) {\n const errors = results[i].response.errors ?? {};\n for (const chainId of requests[i].chainIds) {\n if (errors[chainId]) {\n failed.add(chainId);\n }\n }\n }\n return failed;\n}\n\n/**\n * Middleware that runs multiple balance data source middlewares in parallel,\n * with no chain overlap. Chains that fail (response.errors) are re-partitioned\n * and fetched again in a fallback round so lower-priority sources can try them.\n *\n * @param sources - Array of balance sources in priority order (each with getName(), getActiveChainsSync(), assetsMiddleware).\n * @returns A single middleware that runs all sources in parallel and merges responses.\n */\nexport function createParallelBalanceMiddleware(sources: BalanceSource[]): {\n getName(): string;\n assetsMiddleware: Middleware;\n} {\n return {\n getName(): string {\n return PARALLEL_BALANCE_MIDDLEWARE_NAME;\n },\n\n assetsMiddleware: async (context, next): Promise<Context> => {\n if (sources.length === 0) {\n return next(context);\n }\n\n const noopNext = async (ctx: typeof context): Promise<typeof context> =>\n ctx;\n const limit = pLimit(BALANCE_CONCURRENCY);\n\n // Round 1: partition chains (no overlap), run with limited concurrency\n const requests = partitionChainsBySource(context.request, sources);\n const round1Timed = await Promise.all(\n sources.map((source, i) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: requests[i],\n response: {},\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n const results = round1Timed.map((entry) => entry.result);\n\n const durationByDataSource: Record<string, number> = {\n ...context.durationByDataSource,\n };\n for (const { sourceName, durationMs } of round1Timed) {\n const key = `${PARALLEL_BALANCE_MIDDLEWARE_NAME}.${sourceName}`;\n durationByDataSource[key] =\n (durationByDataSource[key] ?? 0) + durationMs;\n }\n\n let mergedResponse = mergeDataResponses(\n results.map((result) => result.response),\n );\n\n // Fallback: chains that failed (in errors) get re-partitioned and tried again\n const failedChainIds = getFailedChainIds(requests, results);\n if (failedChainIds.size > 0) {\n const fallbackRequest: DataRequest = {\n ...context.request,\n chainIds: [...failedChainIds],\n };\n const fallbackRequests = partitionChainsBySource(\n fallbackRequest,\n sources,\n );\n const fallbackTimed = await Promise.all(\n sources.map((source, i) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: fallbackRequests[i],\n response: {},\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n const fallbackResults = fallbackTimed.map((entry) => entry.result);\n for (const { sourceName, durationMs } of fallbackTimed) {\n const key = `${PARALLEL_BALANCE_MIDDLEWARE_NAME}.${sourceName}`;\n durationByDataSource[key] =\n (durationByDataSource[key] ?? 0) + durationMs;\n }\n const fallbackMerged = mergeDataResponses(\n fallbackResults.map((result) => result.response),\n );\n mergedResponse = mergeDataResponses([mergedResponse, fallbackMerged]);\n // Remove errors for chains we successfully got balance for in fallback\n if (mergedResponse.errors && mergedResponse.assetsBalance) {\n const chainsWithBalance = new Set<ChainId>();\n for (const accountBalances of Object.values(\n mergedResponse.assetsBalance,\n )) {\n for (const assetId of Object.keys(accountBalances)) {\n const chainId = assetId.split('/')[0] as ChainId;\n chainsWithBalance.add(chainId);\n }\n }\n for (const chainId of failedChainIds) {\n if (chainsWithBalance.has(chainId)) {\n delete mergedResponse.errors[chainId];\n }\n }\n }\n }\n\n return next({\n ...context,\n response: mergeDataResponses([context.response, mergedResponse]),\n durationByDataSource,\n });\n },\n };\n}\n\n// ============================================================================\n// PARALLEL TOKEN/PRICE MIDDLEWARE\n// ============================================================================\n\nconst PARALLEL_MIDDLEWARE_NAME = 'ParallelMiddleware';\n\n/** Max concurrent token/price source calls. */\nconst CONCURRENCY = 2;\n\n/**\n * Middleware that runs multiple data source middlewares (e.g. TokenDataSource,\n * PriceDataSource) in parallel with the same request. Responses are merged so\n * that assetsInfo (token metadata) and assetsPrice are combined. Use this to\n * fetch token and price data concurrently instead of sequentially.\n *\n * @param sources - Array of named middleware sources (getName + assetsMiddleware).\n * @returns A single middleware that runs all sources in parallel and merges responses.\n */\nexport function createParallelMiddleware(sources: AssetsDataSource[]): {\n getName(): string;\n assetsMiddleware: Middleware;\n} {\n return {\n getName(): string {\n return PARALLEL_MIDDLEWARE_NAME;\n },\n\n assetsMiddleware: async (context, next): Promise<Context> => {\n if (sources.length === 0) {\n return next(context);\n }\n\n const noopNext = async (ctx: typeof context): Promise<typeof context> =>\n ctx;\n const limit = pLimit(CONCURRENCY);\n\n const timedResults = await Promise.all(\n sources.map((source) =>\n limit(async () => {\n const start = Date.now();\n const result = await source.assetsMiddleware(\n {\n request: context.request,\n response: { ...context.response },\n getAssetsState: context.getAssetsState,\n },\n noopNext,\n );\n const durationMs = Date.now() - start;\n return { result, sourceName: source.getName(), durationMs };\n }),\n ),\n );\n\n const results = timedResults.map((entry) => entry.result);\n const mergedResponse = mergeDataResponses(\n results.map((result) => result.response),\n );\n\n const durationByDataSource: Record<string, number> = {\n ...context.durationByDataSource,\n };\n for (const { sourceName, durationMs } of timedResults) {\n durationByDataSource[`${PARALLEL_MIDDLEWARE_NAME}.${sourceName}`] =\n durationMs;\n }\n\n return next({\n ...context,\n response: mergeDataResponses([context.response, mergedResponse]),\n durationByDataSource,\n });\n },\n };\n}\n"]}
|
|
@@ -2,22 +2,24 @@ import type { AssetsDataSource, Middleware } from '../types.js';
|
|
|
2
2
|
export type RpcFallbackMiddlewareOptions = {
|
|
3
3
|
/** The RPC data source to use as a fallback. */
|
|
4
4
|
rpcDataSource: AssetsDataSource;
|
|
5
|
-
/**
|
|
6
|
-
* When true, recover `unprocessedCustomAssets` (Accounts API v6). When false,
|
|
7
|
-
* re-read stale tracked assets the v5 API omitted (legacy path).
|
|
8
|
-
*/
|
|
9
|
-
isBalanceV6Enabled?: () => boolean;
|
|
10
5
|
};
|
|
11
6
|
/**
|
|
12
|
-
* RpcFallbackMiddleware
|
|
7
|
+
* RpcFallbackMiddleware re-reads balances from the RPC data source in two
|
|
8
|
+
* cases:
|
|
13
9
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
10
|
+
* 1. Chains present in `response.errors` (network error, unprocessedNetworks,
|
|
11
|
+
* timeout, …). Successful RPC results are merged into the response and
|
|
12
|
+
* their entries are cleared from `response.errors`.
|
|
13
|
+
* 2. EVM assets tracked in state (`state.assetsBalance` or
|
|
14
|
+
* `state.customAssets`) that this balance response left empty. The Accounts
|
|
15
|
+
* API omits tokens it does not index, and a returned `0` is
|
|
16
|
+
* indistinguishable from "not indexed", so the `merge` state update would
|
|
17
|
+
* otherwise keep a stale amount forever. They are passed to RPC as
|
|
18
|
+
* `customAssets` so the balance fetcher includes them in its multicall.
|
|
16
19
|
*
|
|
17
|
-
*
|
|
18
|
-
* `
|
|
19
|
-
*
|
|
20
|
-
* imported or previously detected tokens on a failed chain).
|
|
20
|
+
* Place this immediately after `createParallelBalanceMiddleware` in the fast
|
|
21
|
+
* pipeline, after `CustomAssetGraduationMiddleware` so the custom assets RPC
|
|
22
|
+
* carries never trigger graduation.
|
|
21
23
|
*/
|
|
22
24
|
export declare class RpcFallbackMiddleware {
|
|
23
25
|
#private;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RpcFallbackMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/RpcFallbackMiddleware.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAEV,gBAAgB,
|
|
1
|
+
{"version":3,"file":"RpcFallbackMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/RpcFallbackMiddleware.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAEV,gBAAgB,EAKhB,UAAU,EACX,MAAM,aAAa,CAAC;AAOrB,MAAM,MAAM,4BAA4B,GAAG;IACzC,gDAAgD;IAChD,aAAa,EAAE,gBAAgB,CAAC;CACjC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,qBAAqB;;IAChC,QAAQ,CAAC,IAAI,2BAAmB;IAIhC,YAAY,OAAO,EAAE,4BAA4B,EAEhD;IAED,OAAO,IAAI,MAAM,CAEhB;IAED,IAAI,gBAAgB,IAAI,UAAU,CAoGjC;CACF"}
|
|
@@ -2,204 +2,124 @@ import { KnownCaipNamespace } from '@metamask/utils';
|
|
|
2
2
|
import { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js';
|
|
3
3
|
import { projectLogger, createModuleLogger } from '../logger.js';
|
|
4
4
|
import { forDataTypes } from '../types.js';
|
|
5
|
-
import { normalizeAssetId } from '../utils/index.js';
|
|
6
5
|
import { mergeDataResponses } from './ParallelMiddleware.js';
|
|
7
6
|
const CONTROLLER_NAME = 'RpcFallbackMiddleware';
|
|
8
7
|
const log = createModuleLogger(projectLogger, CONTROLLER_NAME);
|
|
9
|
-
const noopNext = async (ctx) => ctx;
|
|
10
8
|
/**
|
|
11
|
-
* RpcFallbackMiddleware
|
|
9
|
+
* RpcFallbackMiddleware re-reads balances from the RPC data source in two
|
|
10
|
+
* cases:
|
|
12
11
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* 1. Chains present in `response.errors` (network error, unprocessedNetworks,
|
|
13
|
+
* timeout, …). Successful RPC results are merged into the response and
|
|
14
|
+
* their entries are cleared from `response.errors`.
|
|
15
|
+
* 2. EVM assets tracked in state (`state.assetsBalance` or
|
|
16
|
+
* `state.customAssets`) that this balance response left empty. The Accounts
|
|
17
|
+
* API omits tokens it does not index, and a returned `0` is
|
|
18
|
+
* indistinguishable from "not indexed", so the `merge` state update would
|
|
19
|
+
* otherwise keep a stale amount forever. They are passed to RPC as
|
|
20
|
+
* `customAssets` so the balance fetcher includes them in its multicall.
|
|
15
21
|
*
|
|
16
|
-
*
|
|
17
|
-
* `
|
|
18
|
-
*
|
|
19
|
-
* imported or previously detected tokens on a failed chain).
|
|
22
|
+
* Place this immediately after `createParallelBalanceMiddleware` in the fast
|
|
23
|
+
* pipeline, after `CustomAssetGraduationMiddleware` so the custom assets RPC
|
|
24
|
+
* carries never trigger graduation.
|
|
20
25
|
*/
|
|
21
26
|
export class RpcFallbackMiddleware {
|
|
22
27
|
name = CONTROLLER_NAME;
|
|
23
28
|
#rpcDataSource;
|
|
24
|
-
#isBalanceV6Enabled;
|
|
25
29
|
constructor(options) {
|
|
26
30
|
this.#rpcDataSource = options.rpcDataSource;
|
|
27
|
-
this.#isBalanceV6Enabled =
|
|
28
|
-
options.isBalanceV6Enabled ?? (() => false);
|
|
29
31
|
}
|
|
30
32
|
getName() {
|
|
31
33
|
return this.name;
|
|
32
34
|
}
|
|
33
35
|
get assetsMiddleware() {
|
|
34
36
|
return forDataTypes(['balance'], async (ctx, next) => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
const erroredChains = new Set(Object.keys(ctx.response.errors ?? {}));
|
|
38
|
+
const staleAssets = collectStaleTrackedAssets(ctx);
|
|
39
|
+
const chainsToFetch = [
|
|
40
|
+
...new Set([
|
|
41
|
+
...ctx.request.chainIds.filter((id) => erroredChains.has(id)),
|
|
42
|
+
// Already restricted to requested chains. Their chain may not be
|
|
43
|
+
// errored: the Accounts API can answer for a chain while omitting a
|
|
44
|
+
// token it does not index.
|
|
45
|
+
...staleAssets.map((assetId) => assetId.split('/')[0]),
|
|
46
|
+
]),
|
|
47
|
+
];
|
|
48
|
+
if (chainsToFetch.length === 0) {
|
|
49
|
+
return next(ctx);
|
|
37
50
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
...ctx
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
for
|
|
77
|
-
|
|
78
|
-
|
|
51
|
+
log('Re-reading balances on RPC', {
|
|
52
|
+
erroredChains: [...erroredChains],
|
|
53
|
+
staleAssets,
|
|
54
|
+
chains: chainsToFetch,
|
|
55
|
+
});
|
|
56
|
+
const filteredRequest = {
|
|
57
|
+
...ctx.request,
|
|
58
|
+
chainIds: chainsToFetch,
|
|
59
|
+
customAssets: [
|
|
60
|
+
...new Set([...(ctx.request.customAssets ?? []), ...staleAssets]),
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
const noopNext = async (inner) => inner;
|
|
64
|
+
const rpcResult = await this.#rpcDataSource.assetsMiddleware({
|
|
65
|
+
...ctx,
|
|
66
|
+
request: filteredRequest,
|
|
67
|
+
response: {},
|
|
68
|
+
}, noopNext);
|
|
69
|
+
// A chain RPC itself failed on contributed nothing trustworthy: its
|
|
70
|
+
// balances are failure stubs (native 0) that would overwrite correct
|
|
71
|
+
// upstream amounts and, with replaceCoveredChainBalances, wipe the
|
|
72
|
+
// chain's token slice from state. Drop them before merging — this also
|
|
73
|
+
// keeps failed chains from counting as "recovered" below.
|
|
74
|
+
const rpcFailedChains = new Set(Object.keys(rpcResult.response.errors ?? {}));
|
|
75
|
+
const rpcAssetsBalance = filterOutChainBalances(rpcResult.response.assetsBalance, rpcFailedChains);
|
|
76
|
+
// RPC errors are kept only for chains that were already errored
|
|
77
|
+
// upstream. For chains fetched solely for stale tracked assets the
|
|
78
|
+
// upstream response succeeded and stays authoritative — the stale asset
|
|
79
|
+
// keeps its previous amount and is retried on the next pass.
|
|
80
|
+
const rpcErrors = Object.fromEntries(Object.entries(rpcResult.response.errors ?? {}).filter(([chainId]) => erroredChains.has(chainId)));
|
|
81
|
+
const merged = mergeDataResponses([
|
|
82
|
+
ctx.response,
|
|
83
|
+
{
|
|
84
|
+
...rpcResult.response,
|
|
85
|
+
assetsBalance: rpcAssetsBalance,
|
|
86
|
+
errors: rpcErrors,
|
|
87
|
+
},
|
|
88
|
+
]);
|
|
89
|
+
// Clear errors only for chains RPC actually recovered a balance for.
|
|
90
|
+
// We must inspect the (filtered) RPC balances — NOT merged — because
|
|
91
|
+
// merged also contains balances from the upstream sources (AccountsApi /
|
|
92
|
+
// Websocket / Staked). If those sources returned partial data for
|
|
93
|
+
// a chain that they also flagged as errored (e.g. via
|
|
94
|
+
// unprocessedNetworks), and RPC then failed for that same chain,
|
|
95
|
+
// looking at merged would incorrectly mark the error as recovered.
|
|
96
|
+
if (merged.errors && rpcAssetsBalance) {
|
|
97
|
+
const chainsRecoveredByRpc = new Set();
|
|
98
|
+
for (const accountBalances of Object.values(rpcAssetsBalance)) {
|
|
99
|
+
for (const assetId of Object.keys(accountBalances)) {
|
|
100
|
+
chainsRecoveredByRpc.add(assetId.split('/')[0]);
|
|
101
|
+
}
|
|
79
102
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
for (const chainId of erroredChains) {
|
|
104
|
+
if (chainsRecoveredByRpc.has(chainId)) {
|
|
105
|
+
delete merged.errors[chainId];
|
|
106
|
+
}
|
|
84
107
|
}
|
|
85
108
|
}
|
|
86
|
-
|
|
87
|
-
merged = clearRecoveredAssetIds(merged);
|
|
88
|
-
return next({ ...ctx, response: merged });
|
|
89
|
-
}
|
|
90
|
-
async #recoverV5(ctx, next) {
|
|
91
|
-
const erroredChains = new Set(Object.keys(ctx.response.errors ?? {}));
|
|
92
|
-
const staleAssets = collectStaleTrackedAssets(ctx);
|
|
93
|
-
const chainsToFetch = [
|
|
94
|
-
...new Set([
|
|
95
|
-
...ctx.request.chainIds.filter((id) => erroredChains.has(id)),
|
|
96
|
-
...staleAssets.map((assetId) => assetId.split('/')[0]),
|
|
97
|
-
]),
|
|
98
|
-
];
|
|
99
|
-
if (chainsToFetch.length === 0) {
|
|
100
|
-
return next(ctx);
|
|
101
|
-
}
|
|
102
|
-
log('Re-reading balances on RPC', {
|
|
103
|
-
erroredChains: [...erroredChains],
|
|
104
|
-
staleAssets,
|
|
105
|
-
chains: chainsToFetch,
|
|
109
|
+
return next({ ...ctx, response: merged });
|
|
106
110
|
});
|
|
107
|
-
const filteredRequest = {
|
|
108
|
-
...ctx.request,
|
|
109
|
-
chainIds: chainsToFetch,
|
|
110
|
-
customAssets: [
|
|
111
|
-
...new Set([...(ctx.request.customAssets ?? []), ...staleAssets]),
|
|
112
|
-
],
|
|
113
|
-
};
|
|
114
|
-
const rpcResult = await this.#rpcDataSource.assetsMiddleware({
|
|
115
|
-
...ctx,
|
|
116
|
-
request: filteredRequest,
|
|
117
|
-
response: {},
|
|
118
|
-
}, noopNext);
|
|
119
|
-
const rpcFailedChains = new Set(Object.keys(rpcResult.response.errors ?? {}));
|
|
120
|
-
const rpcAssetsBalance = filterOutChainBalances(rpcResult.response.assetsBalance, rpcFailedChains);
|
|
121
|
-
const rpcErrors = Object.fromEntries(Object.entries(rpcResult.response.errors ?? {}).filter(([chainId]) => erroredChains.has(chainId)));
|
|
122
|
-
const merged = mergeDataResponses([
|
|
123
|
-
ctx.response,
|
|
124
|
-
{
|
|
125
|
-
...rpcResult.response,
|
|
126
|
-
assetsBalance: rpcAssetsBalance,
|
|
127
|
-
errors: rpcErrors,
|
|
128
|
-
},
|
|
129
|
-
]);
|
|
130
|
-
if (merged.errors && rpcAssetsBalance) {
|
|
131
|
-
const chainsRecoveredByRpc = new Set();
|
|
132
|
-
for (const accountBalances of Object.values(rpcAssetsBalance)) {
|
|
133
|
-
for (const assetId of Object.keys(accountBalances)) {
|
|
134
|
-
chainsRecoveredByRpc.add(assetId.split('/')[0]);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const chainId of erroredChains) {
|
|
138
|
-
if (chainsRecoveredByRpc.has(chainId)) {
|
|
139
|
-
delete merged.errors[chainId];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return next({ ...ctx, response: merged });
|
|
144
111
|
}
|
|
145
112
|
}
|
|
146
|
-
function chainIdOfAsset(assetId) {
|
|
147
|
-
return assetId.split('/')[0];
|
|
148
|
-
}
|
|
149
113
|
/**
|
|
150
|
-
*
|
|
151
|
-
* retry of a failed chain re-reads the whole slice, not natives only. Hidden
|
|
152
|
-
* assets are left out, matching the `excludeAssetIds` the Accounts API is sent.
|
|
153
|
-
* Staking vaults belong to `StakedBalanceDataSource` and are skipped.
|
|
114
|
+
* Remove all balances that belong to the given chains.
|
|
154
115
|
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
116
|
+
* Used to discard results for chains the RPC source itself failed on, whose
|
|
117
|
+
* entries are failure stubs (native `0`) rather than real readings.
|
|
118
|
+
*
|
|
119
|
+
* @param assetsBalance - Balances by account from the RPC response.
|
|
120
|
+
* @param chainIds - Chains whose balances should be dropped.
|
|
121
|
+
* @returns The filtered balance map, without accounts left empty.
|
|
158
122
|
*/
|
|
159
|
-
function collectTrackedAssetsOnChains(ctx, chains) {
|
|
160
|
-
if (chains.size === 0) {
|
|
161
|
-
return [];
|
|
162
|
-
}
|
|
163
|
-
const { assetsBalance, customAssets: statePins, assetPreferences, } = ctx.getAssetsState();
|
|
164
|
-
const tracked = new Set();
|
|
165
|
-
for (const { account } of ctx.request.accountsWithSupportedChains) {
|
|
166
|
-
const balanceIds = Object.keys(assetsBalance[account.id] ?? {});
|
|
167
|
-
const pinIds = statePins?.[account.id] ?? [];
|
|
168
|
-
for (const assetId of [...balanceIds, ...pinIds]) {
|
|
169
|
-
if (chains.has(chainIdOfAsset(assetId)) &&
|
|
170
|
-
assetId.startsWith(`${KnownCaipNamespace.Eip155}:`) &&
|
|
171
|
-
!isStakingContractAssetId(assetId) &&
|
|
172
|
-
!assetPreferences?.[normalizeAssetId(assetId)]?.hidden) {
|
|
173
|
-
tracked.add(assetId);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return [...tracked];
|
|
178
|
-
}
|
|
179
|
-
function clearRecoveredAssetIds(response) {
|
|
180
|
-
if (!response.unprocessedCustomAssets ||
|
|
181
|
-
response.unprocessedCustomAssets.length === 0) {
|
|
182
|
-
return response;
|
|
183
|
-
}
|
|
184
|
-
const recovered = new Set();
|
|
185
|
-
for (const accountBalances of Object.values(response.assetsBalance ?? {})) {
|
|
186
|
-
for (const assetId of Object.keys(accountBalances)) {
|
|
187
|
-
recovered.add(normalizeAssetId(assetId));
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
const stillUnprocessed = response.unprocessedCustomAssets.filter((assetId) => !recovered.has(normalizeAssetId(assetId)));
|
|
191
|
-
if (stillUnprocessed.length === response.unprocessedCustomAssets.length) {
|
|
192
|
-
return response;
|
|
193
|
-
}
|
|
194
|
-
const next = { ...response };
|
|
195
|
-
if (stillUnprocessed.length === 0) {
|
|
196
|
-
delete next.unprocessedCustomAssets;
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
next.unprocessedCustomAssets = stillUnprocessed;
|
|
200
|
-
}
|
|
201
|
-
return next;
|
|
202
|
-
}
|
|
203
123
|
function filterOutChainBalances(assetsBalance, chainIds) {
|
|
204
124
|
if (!assetsBalance || chainIds.size === 0) {
|
|
205
125
|
return assetsBalance;
|
|
@@ -213,6 +133,17 @@ function filterOutChainBalances(assetsBalance, chainIds) {
|
|
|
213
133
|
}
|
|
214
134
|
return filtered;
|
|
215
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* EVM assets tracked in state that this balance response left empty and RPC
|
|
138
|
+
* should re-read.
|
|
139
|
+
*
|
|
140
|
+
* Limited to chains both requested and supported by the owning account:
|
|
141
|
+
* RpcDataSource fetches per account and skips chains outside its supported
|
|
142
|
+
* set, so anything else would be queued and then silently dropped.
|
|
143
|
+
*
|
|
144
|
+
* @param ctx - Pipeline context.
|
|
145
|
+
* @returns Asset IDs to hand to the RPC data source.
|
|
146
|
+
*/
|
|
216
147
|
function collectStaleTrackedAssets(ctx) {
|
|
217
148
|
const { assetsBalance: stateAssetsBalance, customAssets: stateCustomAssets } = ctx.getAssetsState();
|
|
218
149
|
const staleAssets = new Set();
|
|
@@ -226,6 +157,8 @@ function collectStaleTrackedAssets(ctx) {
|
|
|
226
157
|
const chainsForAccount = supportedChains.filter((chainId) => ctx.request.chainIds.includes(chainId));
|
|
227
158
|
for (const assetId of trackedAssetIds) {
|
|
228
159
|
if (isEvmAssetOnChains(assetId, chainsForAccount) &&
|
|
160
|
+
// Staked vault balances belong to StakedBalanceDataSource; an RPC
|
|
161
|
+
// ERC-20 read of the share token would clobber them.
|
|
229
162
|
!isStakingContractAssetId(assetId) &&
|
|
230
163
|
isBalanceEmpty(ctx.response.assetsBalance?.[accountId], assetId)) {
|
|
231
164
|
staleAssets.add(assetId);
|
|
@@ -234,6 +167,16 @@ function collectStaleTrackedAssets(ctx) {
|
|
|
234
167
|
}
|
|
235
168
|
return [...staleAssets];
|
|
236
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Whether a balance response carries no positive amount for an asset. A
|
|
172
|
+
* returned `0` cannot be distinguished from "not indexed", so both count as
|
|
173
|
+
* empty. Asset IDs are matched case-insensitively: state keys ERC-20 assets by
|
|
174
|
+
* checksummed address, while some data sources return them lower-cased.
|
|
175
|
+
*
|
|
176
|
+
* @param balances - Balance map for a single account from the response.
|
|
177
|
+
* @param assetId - Asset ID to check.
|
|
178
|
+
* @returns True when the response holds no positive amount for the asset.
|
|
179
|
+
*/
|
|
237
180
|
function isBalanceEmpty(balances, assetId) {
|
|
238
181
|
let amount = balances?.[assetId]?.amount;
|
|
239
182
|
if (amount === undefined && balances) {
|
|
@@ -242,6 +185,13 @@ function isBalanceEmpty(balances, assetId) {
|
|
|
242
185
|
}
|
|
243
186
|
return !(Number(amount) > 0);
|
|
244
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Whether an asset is an EVM asset on one of the given chains.
|
|
190
|
+
*
|
|
191
|
+
* @param assetId - CAIP-19 asset ID.
|
|
192
|
+
* @param chainIds - Chains to match against.
|
|
193
|
+
* @returns True for EVM assets whose chain is in `chainIds`.
|
|
194
|
+
*/
|
|
245
195
|
function isEvmAssetOnChains(assetId, chainIds) {
|
|
246
196
|
if (!assetId.startsWith(`${KnownCaipNamespace.Eip155}:`)) {
|
|
247
197
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RpcFallbackMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/RpcFallbackMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2CAA2C,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAY3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAEhD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAY/D,MAAM,QAAQ,GAAG,KAAK,EAAE,GAAY,EAAoB,EAAE,CAAC,GAAG,CAAC;AAE/D;;;;;;;;;;GAUG;AACH,MAAM,OAAO,qBAAqB;IACvB,IAAI,GAAG,eAAe,CAAC;IAEvB,cAAc,CAAmB;IAEjC,mBAAmB,CAAgB;IAE5C,YAAY,OAAqC;QAC/C,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;QAC5C,IAAI,CAAC,mBAAmB;YACtB,OAAO,CAAC,kBAAkB,IAAI,CAAC,GAAY,EAAE,CAAC,KAAK,CAAC,CAAC;IACzD,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,gBAAgB;QAClB,OAAO,YAAY,CAAC,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;YACnD,IAAI,IAAI,C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AAU,CAAC,GAAG,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAY,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import { KnownCaipNamespace } from '@metamask/utils';\n\nimport { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js';\nimport { projectLogger, createModuleLogger } from '../logger.js';\nimport { forDataTypes } from '../types.js';\nimport type {\n AssetBalance,\n AssetsDataSource,\n Caip19AssetId,\n ChainId,\n Context,\n DataRequest,\n DataResponse,\n Middleware,\n NextFunction,\n} from '../types.js';\nimport { normalizeAssetId } from '../utils/index.js';\nimport { mergeDataResponses } from './ParallelMiddleware.js';\n\nconst CONTROLLER_NAME = 'RpcFallbackMiddleware';\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\nexport type RpcFallbackMiddlewareOptions = {\n /** The RPC data source to use as a fallback. */\n rpcDataSource: AssetsDataSource;\n /**\n * When true, recover `unprocessedCustomAssets` (Accounts API v6). When false,\n * re-read stale tracked assets the v5 API omitted (legacy path).\n */\n isBalanceV6Enabled?: () => boolean;\n};\n\nconst noopNext = async (ctx: Context): Promise<Context> => ctx;\n\n/**\n * RpcFallbackMiddleware recovers what upstream sources left outstanding on RPC.\n *\n * Accounts API v5: chains in `response.errors`, plus EVM assets tracked in\n * state that this response left empty (the API omits tokens it does not index).\n *\n * Accounts API v6: one RPC retry. `customAssets` is the union of\n * `response.unprocessedCustomAssets` (trust the API) and every tracked\n * asset on chains in `response.errors` (so a `full` update cannot drop\n * imported or previously detected tokens on a failed chain).\n */\nexport class RpcFallbackMiddleware {\n readonly name = CONTROLLER_NAME;\n\n readonly #rpcDataSource: AssetsDataSource;\n\n readonly #isBalanceV6Enabled: () => boolean;\n\n constructor(options: RpcFallbackMiddlewareOptions) {\n this.#rpcDataSource = options.rpcDataSource;\n this.#isBalanceV6Enabled =\n options.isBalanceV6Enabled ?? ((): boolean => false);\n }\n\n getName(): string {\n return this.name;\n }\n\n get assetsMiddleware(): Middleware {\n return forDataTypes(['balance'], async (ctx, next) => {\n if (this.#isBalanceV6Enabled()) {\n return this.#recoverV6(ctx, next);\n }\n return this.#recoverV5(ctx, next);\n });\n }\n\n async #recoverV6(ctx: Context, next: NextFunction): Promise<Context> {\n const erroredChains = new Set<ChainId>(\n Object.keys(ctx.response.errors ?? {}) as ChainId[],\n );\n const unprocessedAssetsPins = [\n ...new Set(ctx.response.unprocessedCustomAssets ?? []),\n ];\n const assetsOnErroredChains = collectTrackedAssetsOnChains(\n ctx,\n erroredChains,\n );\n const customAssets = [\n ...new Set([...unprocessedAssetsPins, ...assetsOnErroredChains]),\n ];\n const chainsToFetch = [\n ...new Set([\n ...ctx.request.chainIds.filter((id) => erroredChains.has(id)),\n ...customAssets.map((assetId) => chainIdOfAsset(assetId)),\n ]),\n ];\n\n if (chainsToFetch.length === 0) {\n return next(ctx);\n }\n\n const rpcRequest: DataRequest = {\n ...ctx.request,\n chainIds: chainsToFetch,\n ...(customAssets.length > 0 ? { customAssets } : {}),\n };\n const rpcResult = await this.#rpcDataSource.assetsMiddleware(\n { ...ctx, request: rpcRequest, response: {} },\n noopNext,\n );\n\n const rpcFailedChains = new Set<ChainId>(\n Object.keys(rpcResult.response.errors ?? {}) as ChainId[],\n );\n const rpcAssetsBalance = filterOutChainBalances(\n rpcResult.response.assetsBalance,\n rpcFailedChains,\n );\n\n let merged = mergeDataResponses([\n ctx.response,\n {\n ...rpcResult.response,\n assetsBalance: rpcAssetsBalance,\n },\n ]);\n\n if (merged.errors && rpcAssetsBalance) {\n const chainsRecoveredByRpc = new Set<string>();\n for (const accountBalances of Object.values(rpcAssetsBalance)) {\n for (const assetId of Object.keys(accountBalances)) {\n chainsRecoveredByRpc.add(assetId.split('/')[0]);\n }\n }\n for (const chainId of erroredChains) {\n if (chainsRecoveredByRpc.has(chainId)) {\n delete merged.errors[chainId];\n }\n }\n }\n\n merged = clearRecoveredAssetIds(merged);\n\n return next({ ...ctx, response: merged });\n }\n\n async #recoverV5(ctx: Context, next: NextFunction): Promise<Context> {\n const erroredChains = new Set<ChainId>(\n Object.keys(ctx.response.errors ?? {}) as ChainId[],\n );\n const staleAssets = collectStaleTrackedAssets(ctx);\n\n const chainsToFetch = [\n ...new Set([\n ...ctx.request.chainIds.filter((id) => erroredChains.has(id)),\n ...staleAssets.map((assetId) => assetId.split('/')[0] as ChainId),\n ]),\n ];\n\n if (chainsToFetch.length === 0) {\n return next(ctx);\n }\n\n log('Re-reading balances on RPC', {\n erroredChains: [...erroredChains],\n staleAssets,\n chains: chainsToFetch,\n });\n\n const filteredRequest = {\n ...ctx.request,\n chainIds: chainsToFetch,\n customAssets: [\n ...new Set([...(ctx.request.customAssets ?? []), ...staleAssets]),\n ],\n };\n\n const rpcResult = await this.#rpcDataSource.assetsMiddleware(\n {\n ...ctx,\n request: filteredRequest,\n response: {},\n },\n noopNext,\n );\n\n const rpcFailedChains = new Set<ChainId>(\n Object.keys(rpcResult.response.errors ?? {}) as ChainId[],\n );\n const rpcAssetsBalance = filterOutChainBalances(\n rpcResult.response.assetsBalance,\n rpcFailedChains,\n );\n\n const rpcErrors = Object.fromEntries(\n Object.entries(rpcResult.response.errors ?? {}).filter(([chainId]) =>\n erroredChains.has(chainId as ChainId),\n ),\n );\n\n const merged: DataResponse = mergeDataResponses([\n ctx.response,\n {\n ...rpcResult.response,\n assetsBalance: rpcAssetsBalance,\n errors: rpcErrors,\n },\n ]);\n\n if (merged.errors && rpcAssetsBalance) {\n const chainsRecoveredByRpc = new Set<string>();\n for (const accountBalances of Object.values(rpcAssetsBalance)) {\n for (const assetId of Object.keys(accountBalances)) {\n chainsRecoveredByRpc.add(assetId.split('/')[0]);\n }\n }\n for (const chainId of erroredChains) {\n if (chainsRecoveredByRpc.has(chainId)) {\n delete merged.errors[chainId];\n }\n }\n }\n\n return next({ ...ctx, response: merged });\n }\n}\n\nfunction chainIdOfAsset(assetId: Caip19AssetId): ChainId {\n return assetId.split('/')[0] as ChainId;\n}\n\n/**\n * Every EVM asset already in state on `chains` — balances and pins — so an RPC\n * retry of a failed chain re-reads the whole slice, not natives only. Hidden\n * assets are left out, matching the `excludeAssetIds` the Accounts API is sent.\n * Staking vaults belong to `StakedBalanceDataSource` and are skipped.\n *\n * @param ctx - Pipeline context, for request accounts and assets state.\n * @param chains - Chains being recovered on RPC.\n * @returns Deduplicated asset IDs on those chains.\n */\nfunction collectTrackedAssetsOnChains(\n ctx: Context,\n chains: Set<ChainId>,\n): Caip19AssetId[] {\n if (chains.size === 0) {\n return [];\n }\n\n const {\n assetsBalance,\n customAssets: statePins,\n assetPreferences,\n } = ctx.getAssetsState();\n const tracked = new Set<Caip19AssetId>();\n\n for (const { account } of ctx.request.accountsWithSupportedChains) {\n const balanceIds = Object.keys(\n assetsBalance[account.id] ?? {},\n ) as Caip19AssetId[];\n const pinIds = statePins?.[account.id] ?? [];\n for (const assetId of [...balanceIds, ...pinIds]) {\n if (\n chains.has(chainIdOfAsset(assetId)) &&\n assetId.startsWith(`${KnownCaipNamespace.Eip155}:`) &&\n !isStakingContractAssetId(assetId) &&\n !assetPreferences?.[normalizeAssetId(assetId)]?.hidden\n ) {\n tracked.add(assetId);\n }\n }\n }\n\n return [...tracked];\n}\n\nfunction clearRecoveredAssetIds(response: DataResponse): DataResponse {\n if (\n !response.unprocessedCustomAssets ||\n response.unprocessedCustomAssets.length === 0\n ) {\n return response;\n }\n\n const recovered = new Set<Caip19AssetId>();\n for (const accountBalances of Object.values(response.assetsBalance ?? {})) {\n for (const assetId of Object.keys(accountBalances)) {\n recovered.add(normalizeAssetId(assetId as Caip19AssetId));\n }\n }\n\n const stillUnprocessed = response.unprocessedCustomAssets.filter(\n (assetId) => !recovered.has(normalizeAssetId(assetId)),\n );\n\n if (stillUnprocessed.length === response.unprocessedCustomAssets.length) {\n return response;\n }\n\n const next = { ...response };\n if (stillUnprocessed.length === 0) {\n delete next.unprocessedCustomAssets;\n } else {\n next.unprocessedCustomAssets = stillUnprocessed;\n }\n return next;\n}\n\nfunction filterOutChainBalances(\n assetsBalance: DataResponse['assetsBalance'],\n chainIds: Set<ChainId>,\n): DataResponse['assetsBalance'] {\n if (!assetsBalance || chainIds.size === 0) {\n return assetsBalance;\n }\n\n const filtered: NonNullable<DataResponse['assetsBalance']> = {};\n for (const [accountId, accountBalances] of Object.entries(assetsBalance)) {\n const kept = Object.fromEntries(\n Object.entries(accountBalances).filter(\n ([assetId]) => !chainIds.has(assetId.split('/')[0] as ChainId),\n ),\n ) as Record<Caip19AssetId, AssetBalance>;\n if (Object.keys(kept).length > 0) {\n filtered[accountId] = kept;\n }\n }\n return filtered;\n}\n\nfunction collectStaleTrackedAssets(ctx: Context): Caip19AssetId[] {\n const { assetsBalance: stateAssetsBalance, customAssets: stateCustomAssets } =\n ctx.getAssetsState();\n\n const staleAssets = new Set<Caip19AssetId>();\n\n for (const { account, supportedChains } of ctx.request\n .accountsWithSupportedChains) {\n const accountId = account.id;\n const trackedAssetIds = new Set<Caip19AssetId>([\n ...(Object.keys(stateAssetsBalance[accountId] ?? {}) as Caip19AssetId[]),\n ...(stateCustomAssets?.[accountId] ?? []),\n ]);\n\n const chainsForAccount = supportedChains.filter((chainId) =>\n ctx.request.chainIds.includes(chainId),\n );\n\n for (const assetId of trackedAssetIds) {\n if (\n isEvmAssetOnChains(assetId, chainsForAccount) &&\n !isStakingContractAssetId(assetId) &&\n isBalanceEmpty(ctx.response.assetsBalance?.[accountId], assetId)\n ) {\n staleAssets.add(assetId);\n }\n }\n }\n\n return [...staleAssets];\n}\n\nfunction isBalanceEmpty(\n balances: Record<string, AssetBalance> | undefined,\n assetId: Caip19AssetId,\n): boolean {\n let amount = balances?.[assetId]?.amount;\n if (amount === undefined && balances) {\n const lowerCasedAssetId = assetId.toLowerCase();\n amount = Object.entries(balances).find(\n ([id]) => id.toLowerCase() === lowerCasedAssetId,\n )?.[1]?.amount;\n }\n return !(Number(amount) > 0);\n}\n\nfunction isEvmAssetOnChains(\n assetId: Caip19AssetId,\n chainIds: ChainId[],\n): boolean {\n if (!assetId.startsWith(`${KnownCaipNamespace.Eip155}:`)) {\n return false;\n }\n return chainIds.includes(assetId.split('/')[0] as ChainId);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"RpcFallbackMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/RpcFallbackMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2CAA2C,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAU3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAEhD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAO/D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,qBAAqB;IACvB,IAAI,GAAG,eAAe,CAAC;IAEvB,cAAc,CAAmB;IAE1C,YAAY,OAAqC;QAC/C,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;IAC9C,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,gBAAgB;QAClB,OAAO,YAAY,CAAC,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;YACnD,MAAM,aAAa,GAAG,IAAI,GAAG,CAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,IAAI,EAAE,CAAc,CACpD,CAAC;YACF,MAAM,WAAW,GAAG,yBAAyB,CAAC,GAAG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AAC,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,CACpC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,WAAW,EAAE,KAAK,iBAAiB,CACjD,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC;IACD,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAC/B,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CACzB,OAAsB,EACtB,QAAmB;IAEnB,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAY,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import { KnownCaipNamespace } from '@metamask/utils';\n\nimport { isStakingContractAssetId } from '../data-sources/evm-rpc-services/index.js';\nimport { projectLogger, createModuleLogger } from '../logger.js';\nimport { forDataTypes } from '../types.js';\nimport type {\n AssetBalance,\n AssetsDataSource,\n Caip19AssetId,\n ChainId,\n Context,\n DataResponse,\n Middleware,\n} from '../types.js';\nimport { mergeDataResponses } from './ParallelMiddleware.js';\n\nconst CONTROLLER_NAME = 'RpcFallbackMiddleware';\n\nconst log = createModuleLogger(projectLogger, CONTROLLER_NAME);\n\nexport type RpcFallbackMiddlewareOptions = {\n /** The RPC data source to use as a fallback. */\n rpcDataSource: AssetsDataSource;\n};\n\n/**\n * RpcFallbackMiddleware re-reads balances from the RPC data source in two\n * cases:\n *\n * 1. Chains present in `response.errors` (network error, unprocessedNetworks,\n * timeout, …). Successful RPC results are merged into the response and\n * their entries are cleared from `response.errors`.\n * 2. EVM assets tracked in state (`state.assetsBalance` or\n * `state.customAssets`) that this balance response left empty. The Accounts\n * API omits tokens it does not index, and a returned `0` is\n * indistinguishable from \"not indexed\", so the `merge` state update would\n * otherwise keep a stale amount forever. They are passed to RPC as\n * `customAssets` so the balance fetcher includes them in its multicall.\n *\n * Place this immediately after `createParallelBalanceMiddleware` in the fast\n * pipeline, after `CustomAssetGraduationMiddleware` so the custom assets RPC\n * carries never trigger graduation.\n */\nexport class RpcFallbackMiddleware {\n readonly name = CONTROLLER_NAME;\n\n readonly #rpcDataSource: AssetsDataSource;\n\n constructor(options: RpcFallbackMiddlewareOptions) {\n this.#rpcDataSource = options.rpcDataSource;\n }\n\n getName(): string {\n return this.name;\n }\n\n get assetsMiddleware(): Middleware {\n return forDataTypes(['balance'], async (ctx, next) => {\n const erroredChains = new Set<ChainId>(\n Object.keys(ctx.response.errors ?? {}) as ChainId[],\n );\n const staleAssets = collectStaleTrackedAssets(ctx);\n\n const chainsToFetch = [\n ...new Set([\n ...ctx.request.chainIds.filter((id) => erroredChains.has(id)),\n // Already restricted to requested chains. Their chain may not be\n // errored: the Accounts API can answer for a chain while omitting a\n // token it does not index.\n ...staleAssets.map((assetId) => assetId.split('/')[0] as ChainId),\n ]),\n ];\n\n if (chainsToFetch.length === 0) {\n return next(ctx);\n }\n\n log('Re-reading balances on RPC', {\n erroredChains: [...erroredChains],\n staleAssets,\n chains: chainsToFetch,\n });\n\n const filteredRequest = {\n ...ctx.request,\n chainIds: chainsToFetch,\n customAssets: [\n ...new Set([...(ctx.request.customAssets ?? []), ...staleAssets]),\n ],\n };\n\n const noopNext = async (inner: typeof ctx): Promise<typeof ctx> => inner;\n const rpcResult = await this.#rpcDataSource.assetsMiddleware(\n {\n ...ctx,\n request: filteredRequest,\n response: {},\n },\n noopNext,\n );\n\n // A chain RPC itself failed on contributed nothing trustworthy: its\n // balances are failure stubs (native 0) that would overwrite correct\n // upstream amounts and, with replaceCoveredChainBalances, wipe the\n // chain's token slice from state. Drop them before merging — this also\n // keeps failed chains from counting as \"recovered\" below.\n const rpcFailedChains = new Set<ChainId>(\n Object.keys(rpcResult.response.errors ?? {}) as ChainId[],\n );\n const rpcAssetsBalance = filterOutChainBalances(\n rpcResult.response.assetsBalance,\n rpcFailedChains,\n );\n\n // RPC errors are kept only for chains that were already errored\n // upstream. For chains fetched solely for stale tracked assets the\n // upstream response succeeded and stays authoritative — the stale asset\n // keeps its previous amount and is retried on the next pass.\n const rpcErrors = Object.fromEntries(\n Object.entries(rpcResult.response.errors ?? {}).filter(([chainId]) =>\n erroredChains.has(chainId as ChainId),\n ),\n );\n\n const merged: DataResponse = mergeDataResponses([\n ctx.response,\n {\n ...rpcResult.response,\n assetsBalance: rpcAssetsBalance,\n errors: rpcErrors,\n },\n ]);\n\n // Clear errors only for chains RPC actually recovered a balance for.\n // We must inspect the (filtered) RPC balances — NOT merged — because\n // merged also contains balances from the upstream sources (AccountsApi /\n // Websocket / Staked). If those sources returned partial data for\n // a chain that they also flagged as errored (e.g. via\n // unprocessedNetworks), and RPC then failed for that same chain,\n // looking at merged would incorrectly mark the error as recovered.\n if (merged.errors && rpcAssetsBalance) {\n const chainsRecoveredByRpc = new Set<string>();\n for (const accountBalances of Object.values(rpcAssetsBalance)) {\n for (const assetId of Object.keys(accountBalances)) {\n chainsRecoveredByRpc.add(assetId.split('/')[0]);\n }\n }\n for (const chainId of erroredChains) {\n if (chainsRecoveredByRpc.has(chainId)) {\n delete merged.errors[chainId];\n }\n }\n }\n\n return next({ ...ctx, response: merged });\n });\n }\n}\n\n/**\n * Remove all balances that belong to the given chains.\n *\n * Used to discard results for chains the RPC source itself failed on, whose\n * entries are failure stubs (native `0`) rather than real readings.\n *\n * @param assetsBalance - Balances by account from the RPC response.\n * @param chainIds - Chains whose balances should be dropped.\n * @returns The filtered balance map, without accounts left empty.\n */\nfunction filterOutChainBalances(\n assetsBalance: DataResponse['assetsBalance'],\n chainIds: Set<ChainId>,\n): DataResponse['assetsBalance'] {\n if (!assetsBalance || chainIds.size === 0) {\n return assetsBalance;\n }\n\n const filtered: NonNullable<DataResponse['assetsBalance']> = {};\n for (const [accountId, accountBalances] of Object.entries(assetsBalance)) {\n const kept = Object.fromEntries(\n Object.entries(accountBalances).filter(\n ([assetId]) => !chainIds.has(assetId.split('/')[0] as ChainId),\n ),\n ) as Record<Caip19AssetId, AssetBalance>;\n if (Object.keys(kept).length > 0) {\n filtered[accountId] = kept;\n }\n }\n return filtered;\n}\n\n/**\n * EVM assets tracked in state that this balance response left empty and RPC\n * should re-read.\n *\n * Limited to chains both requested and supported by the owning account:\n * RpcDataSource fetches per account and skips chains outside its supported\n * set, so anything else would be queued and then silently dropped.\n *\n * @param ctx - Pipeline context.\n * @returns Asset IDs to hand to the RPC data source.\n */\nfunction collectStaleTrackedAssets(ctx: Context): Caip19AssetId[] {\n const { assetsBalance: stateAssetsBalance, customAssets: stateCustomAssets } =\n ctx.getAssetsState();\n\n const staleAssets = new Set<Caip19AssetId>();\n\n for (const { account, supportedChains } of ctx.request\n .accountsWithSupportedChains) {\n const accountId = account.id;\n const trackedAssetIds = new Set<Caip19AssetId>([\n ...(Object.keys(stateAssetsBalance[accountId] ?? {}) as Caip19AssetId[]),\n ...(stateCustomAssets?.[accountId] ?? []),\n ]);\n\n const chainsForAccount = supportedChains.filter((chainId) =>\n ctx.request.chainIds.includes(chainId),\n );\n\n for (const assetId of trackedAssetIds) {\n if (\n isEvmAssetOnChains(assetId, chainsForAccount) &&\n // Staked vault balances belong to StakedBalanceDataSource; an RPC\n // ERC-20 read of the share token would clobber them.\n !isStakingContractAssetId(assetId) &&\n isBalanceEmpty(ctx.response.assetsBalance?.[accountId], assetId)\n ) {\n staleAssets.add(assetId);\n }\n }\n }\n\n return [...staleAssets];\n}\n\n/**\n * Whether a balance response carries no positive amount for an asset. A\n * returned `0` cannot be distinguished from \"not indexed\", so both count as\n * empty. Asset IDs are matched case-insensitively: state keys ERC-20 assets by\n * checksummed address, while some data sources return them lower-cased.\n *\n * @param balances - Balance map for a single account from the response.\n * @param assetId - Asset ID to check.\n * @returns True when the response holds no positive amount for the asset.\n */\nfunction isBalanceEmpty(\n balances: Record<string, AssetBalance> | undefined,\n assetId: Caip19AssetId,\n): boolean {\n let amount = balances?.[assetId]?.amount;\n if (amount === undefined && balances) {\n const lowerCasedAssetId = assetId.toLowerCase();\n amount = Object.entries(balances).find(\n ([id]) => id.toLowerCase() === lowerCasedAssetId,\n )?.[1]?.amount;\n }\n return !(Number(amount) > 0);\n}\n\n/**\n * Whether an asset is an EVM asset on one of the given chains.\n *\n * @param assetId - CAIP-19 asset ID.\n * @param chainIds - Chains to match against.\n * @returns True for EVM assets whose chain is in `chainIds`.\n */\nfunction isEvmAssetOnChains(\n assetId: Caip19AssetId,\n chainIds: ChainId[],\n): boolean {\n if (!assetId.startsWith(`${KnownCaipNamespace.Eip155}:`)) {\n return false;\n }\n return chainIds.includes(assetId.split('/')[0] as ChainId);\n}\n"]}
|
|
@@ -34,14 +34,9 @@ export type FastFetchSources = {
|
|
|
34
34
|
* @param options - Lane options.
|
|
35
35
|
* @param options.isBasicFunctionality - When false, only the staking balance and
|
|
36
36
|
* detection run; no network-backed source is used.
|
|
37
|
-
* @param options.includeCustomAssetGraduation - `true` on the Accounts API v5
|
|
38
|
-
* lane. The v6 lane never graduates custom assets: it sends the pins to the
|
|
39
|
-
* endpoint as `includeAssetIds` and keeps the ones it could not resolve as
|
|
40
|
-
* `unprocessedCustomAssets`.
|
|
41
37
|
* @returns The composed source list, ready for `executeAssetsPipeline`.
|
|
42
38
|
*/
|
|
43
39
|
export declare function buildFastFetchSources(sources: FastFetchSources, options: {
|
|
44
40
|
isBasicFunctionality: boolean;
|
|
45
|
-
includeCustomAssetGraduation: boolean;
|
|
46
41
|
}): AssetsDataSource[];
|
|
47
42
|
//# sourceMappingURL=buildFastFetchSources.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildFastFetchSources.d.ts","sourceRoot":"","sources":["../../src/pipeline/buildFastFetchSources.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,qBAAqB,EAAE,aAAa,CAAC;IACrC,uBAAuB,EAAE,aAAa,CAAC;IACvC,+BAA+B,EAAE,gBAAgB,CAAC;IAClD,qBAAqB,EAAE,gBAAgB,CAAC;IACxC,mBAAmB,EAAE,gBAAgB,CAAC;IACtC,eAAe,EAAE,gBAAgB,CAAC;IAClC,eAAe,EAAE,gBAAgB,CAAC;CACnC,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"buildFastFetchSources.d.ts","sourceRoot":"","sources":["../../src/pipeline/buildFastFetchSources.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,qBAAqB,EAAE,aAAa,CAAC;IACrC,uBAAuB,EAAE,aAAa,CAAC;IACvC,+BAA+B,EAAE,gBAAgB,CAAC;IAClD,qBAAqB,EAAE,gBAAgB,CAAC;IACxC,mBAAmB,EAAE,gBAAgB,CAAC;IACtC,eAAe,EAAE,gBAAgB,CAAC;IAClC,eAAe,EAAE,gBAAgB,CAAC;CACnC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE;IAAE,oBAAoB,EAAE,OAAO,CAAA;CAAE,GACzC,gBAAgB,EAAE,CAyBpB"}
|
|
@@ -17,10 +17,6 @@ import { createParallelBalanceMiddleware, createParallelMiddleware, } from '../m
|
|
|
17
17
|
* @param options - Lane options.
|
|
18
18
|
* @param options.isBasicFunctionality - When false, only the staking balance and
|
|
19
19
|
* detection run; no network-backed source is used.
|
|
20
|
-
* @param options.includeCustomAssetGraduation - `true` on the Accounts API v5
|
|
21
|
-
* lane. The v6 lane never graduates custom assets: it sends the pins to the
|
|
22
|
-
* endpoint as `includeAssetIds` and keeps the ones it could not resolve as
|
|
23
|
-
* `unprocessedCustomAssets`.
|
|
24
20
|
* @returns The composed source list, ready for `executeAssetsPipeline`.
|
|
25
21
|
*/
|
|
26
22
|
export function buildFastFetchSources(sources, options) {
|
|
@@ -33,9 +29,7 @@ export function buildFastFetchSources(sources, options) {
|
|
|
33
29
|
accountsApiDataSource,
|
|
34
30
|
stakedBalanceDataSource,
|
|
35
31
|
]),
|
|
36
|
-
|
|
37
|
-
? [customAssetGraduationMiddleware]
|
|
38
|
-
: []),
|
|
32
|
+
customAssetGraduationMiddleware,
|
|
39
33
|
rpcFallbackMiddleware,
|
|
40
34
|
detectionMiddleware,
|
|
41
35
|
createParallelMiddleware([tokenDataSource, priceDataSource]),
|