@metamask/assets-controller 4.0.0 → 5.0.1

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.
Files changed (108) hide show
  1. package/CHANGELOG.md +38 -1
  2. package/dist/AssetsController.cjs +90 -31
  3. package/dist/AssetsController.cjs.map +1 -1
  4. package/dist/AssetsController.d.cts +5 -5
  5. package/dist/AssetsController.d.cts.map +1 -1
  6. package/dist/AssetsController.d.mts +5 -5
  7. package/dist/AssetsController.d.mts.map +1 -1
  8. package/dist/AssetsController.mjs +90 -31
  9. package/dist/AssetsController.mjs.map +1 -1
  10. package/dist/data-sources/AccountsApiDataSource.cjs +1 -1
  11. package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
  12. package/dist/data-sources/AccountsApiDataSource.d.cts +1 -1
  13. package/dist/data-sources/AccountsApiDataSource.d.cts.map +1 -1
  14. package/dist/data-sources/AccountsApiDataSource.d.mts +1 -1
  15. package/dist/data-sources/AccountsApiDataSource.d.mts.map +1 -1
  16. package/dist/data-sources/AccountsApiDataSource.mjs +1 -1
  17. package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
  18. package/dist/data-sources/BackendWebsocketDataSource.cjs +1 -1
  19. package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
  20. package/dist/data-sources/BackendWebsocketDataSource.d.cts +2 -2
  21. package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
  22. package/dist/data-sources/BackendWebsocketDataSource.d.mts +2 -2
  23. package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
  24. package/dist/data-sources/BackendWebsocketDataSource.mjs +1 -1
  25. package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
  26. package/dist/data-sources/PriceDataSource.cjs +63 -38
  27. package/dist/data-sources/PriceDataSource.cjs.map +1 -1
  28. package/dist/data-sources/PriceDataSource.d.cts +1 -1
  29. package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
  30. package/dist/data-sources/PriceDataSource.d.mts +1 -1
  31. package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
  32. package/dist/data-sources/PriceDataSource.mjs +63 -38
  33. package/dist/data-sources/PriceDataSource.mjs.map +1 -1
  34. package/dist/data-sources/RpcDataSource.cjs +28 -61
  35. package/dist/data-sources/RpcDataSource.cjs.map +1 -1
  36. package/dist/data-sources/RpcDataSource.d.cts +3 -4
  37. package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
  38. package/dist/data-sources/RpcDataSource.d.mts +3 -4
  39. package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
  40. package/dist/data-sources/RpcDataSource.mjs +29 -62
  41. package/dist/data-sources/RpcDataSource.mjs.map +1 -1
  42. package/dist/data-sources/SnapDataSource.cjs +1 -1
  43. package/dist/data-sources/SnapDataSource.cjs.map +1 -1
  44. package/dist/data-sources/SnapDataSource.d.cts +2 -2
  45. package/dist/data-sources/SnapDataSource.d.cts.map +1 -1
  46. package/dist/data-sources/SnapDataSource.d.mts +2 -2
  47. package/dist/data-sources/SnapDataSource.d.mts.map +1 -1
  48. package/dist/data-sources/SnapDataSource.mjs +1 -1
  49. package/dist/data-sources/SnapDataSource.mjs.map +1 -1
  50. package/dist/data-sources/StakedBalanceDataSource.cjs +10 -3
  51. package/dist/data-sources/StakedBalanceDataSource.cjs.map +1 -1
  52. package/dist/data-sources/StakedBalanceDataSource.d.cts +2 -2
  53. package/dist/data-sources/StakedBalanceDataSource.d.cts.map +1 -1
  54. package/dist/data-sources/StakedBalanceDataSource.d.mts +2 -2
  55. package/dist/data-sources/StakedBalanceDataSource.d.mts.map +1 -1
  56. package/dist/data-sources/StakedBalanceDataSource.mjs +10 -3
  57. package/dist/data-sources/StakedBalanceDataSource.mjs.map +1 -1
  58. package/dist/data-sources/TokenDataSource.cjs +62 -31
  59. package/dist/data-sources/TokenDataSource.cjs.map +1 -1
  60. package/dist/data-sources/TokenDataSource.d.cts.map +1 -1
  61. package/dist/data-sources/TokenDataSource.d.mts.map +1 -1
  62. package/dist/data-sources/TokenDataSource.mjs +63 -32
  63. package/dist/data-sources/TokenDataSource.mjs.map +1 -1
  64. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.cjs +67 -0
  65. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.cjs.map +1 -0
  66. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.cts +23 -0
  67. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.cts.map +1 -0
  68. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.mts +23 -0
  69. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.d.mts.map +1 -0
  70. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.mjs +63 -0
  71. package/dist/data-sources/evm-rpc-services/clients/TokensApiClient.mjs.map +1 -0
  72. package/dist/data-sources/evm-rpc-services/clients/index.cjs +3 -1
  73. package/dist/data-sources/evm-rpc-services/clients/index.cjs.map +1 -1
  74. package/dist/data-sources/evm-rpc-services/clients/index.d.cts +1 -0
  75. package/dist/data-sources/evm-rpc-services/clients/index.d.cts.map +1 -1
  76. package/dist/data-sources/evm-rpc-services/clients/index.d.mts +1 -0
  77. package/dist/data-sources/evm-rpc-services/clients/index.d.mts.map +1 -1
  78. package/dist/data-sources/evm-rpc-services/clients/index.mjs +1 -0
  79. package/dist/data-sources/evm-rpc-services/clients/index.mjs.map +1 -1
  80. package/dist/data-sources/evm-rpc-services/index.cjs +2 -1
  81. package/dist/data-sources/evm-rpc-services/index.cjs.map +1 -1
  82. package/dist/data-sources/evm-rpc-services/index.d.cts +1 -1
  83. package/dist/data-sources/evm-rpc-services/index.d.cts.map +1 -1
  84. package/dist/data-sources/evm-rpc-services/index.d.mts +1 -1
  85. package/dist/data-sources/evm-rpc-services/index.d.mts.map +1 -1
  86. package/dist/data-sources/evm-rpc-services/index.mjs +1 -1
  87. package/dist/data-sources/evm-rpc-services/index.mjs.map +1 -1
  88. package/dist/data-sources/evm-rpc-services/services/TokenDetector.cjs +47 -50
  89. package/dist/data-sources/evm-rpc-services/services/TokenDetector.cjs.map +1 -1
  90. package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.cts +12 -9
  91. package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.cts.map +1 -1
  92. package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.mts +12 -9
  93. package/dist/data-sources/evm-rpc-services/services/TokenDetector.d.mts.map +1 -1
  94. package/dist/data-sources/evm-rpc-services/services/TokenDetector.mjs +47 -50
  95. package/dist/data-sources/evm-rpc-services/services/TokenDetector.mjs.map +1 -1
  96. package/dist/data-sources/evm-rpc-services/services/index.cjs.map +1 -1
  97. package/dist/data-sources/evm-rpc-services/services/index.d.cts +1 -1
  98. package/dist/data-sources/evm-rpc-services/services/index.d.cts.map +1 -1
  99. package/dist/data-sources/evm-rpc-services/services/index.d.mts +1 -1
  100. package/dist/data-sources/evm-rpc-services/services/index.d.mts.map +1 -1
  101. package/dist/data-sources/evm-rpc-services/services/index.mjs.map +1 -1
  102. package/dist/utils/formatStateForTransactionPay.cjs.map +1 -1
  103. package/dist/utils/formatStateForTransactionPay.d.cts +1 -1
  104. package/dist/utils/formatStateForTransactionPay.d.cts.map +1 -1
  105. package/dist/utils/formatStateForTransactionPay.d.mts +1 -1
  106. package/dist/utils/formatStateForTransactionPay.d.mts.map +1 -1
  107. package/dist/utils/formatStateForTransactionPay.mjs.map +1 -1
  108. package/package.json +21 -20
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/data-sources/evm-rpc-services/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAA8B,4BAAkB;AACxE,OAAO,EACL,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,2BAA2B,EAC3B,yBAAyB,EACzB,wBAAwB,EAQzB,6BAAmB;AACpB,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,0BAAgB","sourcesContent":["export type {\n Address,\n AssetFetchEntry,\n AssetsBalanceState,\n ChainId,\n GetProviderFunction,\n Provider,\n BalanceOfRequest,\n BalanceOfResponse,\n TokenListState,\n BalanceFetchResult,\n TokenDetectionResult,\n} from './types';\nexport { MulticallClient, type MulticallClientConfig } from './clients';\nexport {\n BalanceFetcher,\n TokenDetector,\n StakedBalanceFetcher,\n getSupportedStakingChainIds,\n getStakingContractAddress,\n isStakingContractAssetId,\n type BalancePollingInput,\n type DetectionPollingInput,\n type StakedBalancePollingInput,\n type StakedBalanceFetchResult,\n type OnBalanceUpdateCallback,\n type OnDetectionUpdateCallback,\n type OnStakedBalanceUpdateCallback,\n} from './services';\nexport { divideIntoBatches, reduceInBatchesSerially } from './utils';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/data-sources/evm-rpc-services/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,eAAe,EAEf,eAAe,EAEhB,4BAAkB;AACnB,OAAO,EACL,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,2BAA2B,EAC3B,yBAAyB,EACzB,wBAAwB,EAQzB,6BAAmB;AACpB,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,0BAAgB","sourcesContent":["export type {\n Address,\n AssetFetchEntry,\n AssetsBalanceState,\n ChainId,\n GetProviderFunction,\n Provider,\n BalanceOfRequest,\n BalanceOfResponse,\n TokenListState,\n BalanceFetchResult,\n TokenDetectionResult,\n} from './types';\nexport {\n MulticallClient,\n type MulticallClientConfig,\n TokensApiClient,\n type TokensApiClientConfig,\n} from './clients';\nexport {\n BalanceFetcher,\n TokenDetector,\n StakedBalanceFetcher,\n getSupportedStakingChainIds,\n getStakingContractAddress,\n isStakingContractAssetId,\n type BalancePollingInput,\n type DetectionPollingInput,\n type StakedBalancePollingInput,\n type StakedBalanceFetchResult,\n type OnBalanceUpdateCallback,\n type OnDetectionUpdateCallback,\n type OnStakedBalanceUpdateCallback,\n} from './services';\nexport { divideIntoBatches, reduceInBatchesSerially } from './utils';\n"]}
@@ -10,33 +10,36 @@ 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 _TokenDetector_instances, _TokenDetector_multicallClient, _TokenDetector_messenger, _TokenDetector_config, _TokenDetector_onDetectionUpdate, _TokenDetector_processBalanceResponses, _TokenDetector_formatBalance, _TokenDetector_getTokenMetadata, _TokenDetector_createAsset;
13
+ var _TokenDetector_instances, _TokenDetector_multicallClient, _TokenDetector_tokensApiClient, _TokenDetector_config, _TokenDetector_tokenListCache, _TokenDetector_onDetectionUpdate, _TokenDetector_fetchAndCacheTokenList, _TokenDetector_processBalanceResponses, _TokenDetector_formatBalance, _TokenDetector_getTokenMetadata, _TokenDetector_createAsset;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.TokenDetector = void 0;
16
16
  const polling_controller_1 = require("@metamask/polling-controller");
17
+ const logger_1 = require("../../../logger.cjs");
17
18
  const utils_1 = require("../utils/index.cjs");
19
+ const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'TokenDetector');
18
20
  const DEFAULT_DETECTION_INTERVAL = 180000; // 3 minutes
19
21
  /**
20
22
  * TokenDetector - Detects tokens with non-zero balances via multicall.
23
+ * Fetches the token list from the Tokens API and uses multicall to check balances.
21
24
  * Extends StaticIntervalPollingControllerOnly for built-in polling support.
22
25
  */
23
26
  class TokenDetector extends (0, polling_controller_1.StaticIntervalPollingControllerOnly)() {
24
- constructor(multicallClient, messenger, config) {
27
+ constructor(multicallClient, tokensApiClient, config) {
25
28
  super();
26
29
  _TokenDetector_instances.add(this);
27
30
  _TokenDetector_multicallClient.set(this, void 0);
28
- _TokenDetector_messenger.set(this, void 0);
31
+ _TokenDetector_tokensApiClient.set(this, void 0);
29
32
  _TokenDetector_config.set(this, void 0);
33
+ _TokenDetector_tokenListCache.set(this, new Map());
30
34
  _TokenDetector_onDetectionUpdate.set(this, void 0);
31
35
  __classPrivateFieldSet(this, _TokenDetector_multicallClient, multicallClient, "f");
32
- __classPrivateFieldSet(this, _TokenDetector_messenger, messenger, "f");
36
+ __classPrivateFieldSet(this, _TokenDetector_tokensApiClient, tokensApiClient, "f");
33
37
  __classPrivateFieldSet(this, _TokenDetector_config, {
34
38
  tokenDetectionEnabled: config?.tokenDetectionEnabled ?? (() => true),
35
39
  useExternalService: config?.useExternalService ?? (() => true),
36
40
  defaultBatchSize: config?.defaultBatchSize ?? 300,
37
41
  defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,
38
42
  }, "f");
39
- // Set the polling interval
40
43
  this.setIntervalLength(config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL);
41
44
  }
42
45
  /**
@@ -54,35 +57,26 @@ class TokenDetector extends (0, polling_controller_1.StaticIntervalPollingContro
54
57
  * @param input - The polling input.
55
58
  */
56
59
  async _executePoll(input) {
57
- // Check if token list is available for this chain
58
- const tokensToCheck = this.getTokensToCheck(input.chainId);
59
- if (tokensToCheck.length === 0) {
60
- // No tokens in list for chain, will retry on next poll
61
- return;
60
+ try {
61
+ const result = await this.detectTokens(input.chainId, input.accountId, input.accountAddress);
62
+ if (__classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f") && result.detectedAssets.length > 0) {
63
+ __classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f").call(this, result);
64
+ }
62
65
  }
63
- const result = await this.detectTokens(input.chainId, input.accountId, input.accountAddress);
64
- if (__classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f") && result.detectedAssets.length > 0) {
65
- __classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f").call(this, result);
66
+ catch (error) {
67
+ log('Token detection poll failed', { chainId: input.chainId, error });
66
68
  }
67
69
  }
68
- getTokensToCheck(chainId) {
69
- const tokenListState = __classPrivateFieldGet(this, _TokenDetector_messenger, "f").call('TokenListController:getState');
70
- // Defensive check for tokensChainsCache
71
- if (!tokenListState?.tokensChainsCache) {
72
- return [];
73
- }
74
- // Try direct lookup first
75
- let chainCacheEntry = tokenListState.tokensChainsCache[chainId];
76
- // If not found, try normalizing the chain ID (e.g., 0x0a -> 0xa)
77
- if (!chainCacheEntry) {
78
- const normalizedChainId = `0x${parseInt(chainId, 16).toString(16)}`;
79
- chainCacheEntry = tokenListState.tokensChainsCache[normalizedChainId];
80
- }
81
- const chainTokenList = chainCacheEntry?.data;
82
- if (!chainTokenList) {
83
- return [];
84
- }
85
- return Object.keys(chainTokenList);
70
+ /**
71
+ * Fetch the list of token addresses to check for the given chain.
72
+ * Calls the Tokens API and caches the result for metadata lookups.
73
+ *
74
+ * @param chainId - Chain ID in hex format.
75
+ * @returns Array of token contract addresses.
76
+ */
77
+ async getTokensToCheck(chainId) {
78
+ const tokenList = await __classPrivateFieldGet(this, _TokenDetector_instances, "m", _TokenDetector_fetchAndCacheTokenList).call(this, chainId);
79
+ return tokenList.map((entry) => entry.address);
86
80
  }
87
81
  async detectTokens(chainId, accountId, accountAddress, options) {
88
82
  const tokenDetectionEnabled = options?.tokenDetectionEnabled ?? __classPrivateFieldGet(this, _TokenDetector_config, "f").tokenDetectionEnabled();
@@ -101,7 +95,7 @@ class TokenDetector extends (0, polling_controller_1.StaticIntervalPollingContro
101
95
  }
102
96
  const batchSize = options?.batchSize ?? __classPrivateFieldGet(this, _TokenDetector_config, "f").defaultBatchSize;
103
97
  const timestamp = Date.now();
104
- const tokensToCheck = this.getTokensToCheck(chainId);
98
+ const tokensToCheck = await this.getTokensToCheck(chainId);
105
99
  if (tokensToCheck.length === 0) {
106
100
  return {
107
101
  chainId,
@@ -142,7 +136,22 @@ class TokenDetector extends (0, polling_controller_1.StaticIntervalPollingContro
142
136
  }
143
137
  }
144
138
  exports.TokenDetector = TokenDetector;
145
- _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_messenger = new WeakMap(), _TokenDetector_config = new WeakMap(), _TokenDetector_onDetectionUpdate = new WeakMap(), _TokenDetector_instances = new WeakSet(), _TokenDetector_processBalanceResponses = function _TokenDetector_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp) {
139
+ _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_tokensApiClient = new WeakMap(), _TokenDetector_config = new WeakMap(), _TokenDetector_tokenListCache = new WeakMap(), _TokenDetector_onDetectionUpdate = new WeakMap(), _TokenDetector_instances = new WeakSet(), _TokenDetector_fetchAndCacheTokenList = async function _TokenDetector_fetchAndCacheTokenList(chainId) {
140
+ try {
141
+ const list = await __classPrivateFieldGet(this, _TokenDetector_tokensApiClient, "f").fetchTokenList(chainId);
142
+ __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").set(chainId, list);
143
+ return list;
144
+ }
145
+ catch (error) {
146
+ const cached = __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").get(chainId);
147
+ log('Failed to fetch token list; using stale cache', {
148
+ chainId,
149
+ cachedCount: cached?.length ?? 0,
150
+ error,
151
+ });
152
+ return cached ?? [];
153
+ }
154
+ }, _TokenDetector_processBalanceResponses = function _TokenDetector_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp) {
146
155
  const { detectedAssets, detectedBalances, zeroBalanceAddresses, failedAddresses, } = accumulator;
147
156
  for (const response of responses) {
148
157
  if (!response.success) {
@@ -195,25 +204,13 @@ _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_messenger = new W
195
204
  return rawBalance;
196
205
  }
197
206
  }, _TokenDetector_getTokenMetadata = function _TokenDetector_getTokenMetadata(chainId, tokenAddress) {
198
- const tokenListState = __classPrivateFieldGet(this, _TokenDetector_messenger, "f").call('TokenListController:getState');
199
- if (!tokenListState?.tokensChainsCache) {
200
- return undefined;
201
- }
202
- const chainCacheEntry = tokenListState.tokensChainsCache[chainId];
203
- const chainTokenList = chainCacheEntry?.data;
204
- if (!chainTokenList) {
205
- return undefined;
206
- }
207
- if (chainTokenList[tokenAddress]) {
208
- return chainTokenList[tokenAddress];
209
- }
207
+ const list = __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").get(chainId) ?? [];
210
208
  const lowerAddress = tokenAddress.toLowerCase();
211
- for (const [address, metadata] of Object.entries(chainTokenList)) {
212
- if (address.toLowerCase() === lowerAddress) {
213
- return metadata;
214
- }
209
+ const exact = list.find((entry) => entry.address === tokenAddress);
210
+ if (exact) {
211
+ return exact;
215
212
  }
216
- return undefined;
213
+ return list.find((entry) => entry.address.toLowerCase() === lowerAddress);
217
214
  }, _TokenDetector_createAsset = function _TokenDetector_createAsset(chainId, tokenAddress, metadata) {
218
215
  const chainIdDecimal = parseInt(chainId, 16);
219
216
  const assetId = `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}`;
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetector.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qEAAmF;AAiBnF,8CAAmD;AAEnD,MAAM,0BAA0B,GAAG,MAAO,CAAC,CAAC,YAAY;AAqCxD;;;GAGG;AACH,MAAa,aAAc,SAAQ,IAAA,wDAAmC,GAAyB;IAS7F,YACE,eAAgC,EAChC,SAAiC,EACjC,MAA4B;QAE5B,KAAK,EAAE,CAAC;;QAbD,iDAAkC;QAElC,2CAAmC;QAEnC,wCAAgE;QAEzE,mDAA0D;QAQxD,uBAAA,IAAI,kCAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,yBAAW;YACb,qBAAqB,EACnB,MAAM,EAAE,qBAAqB,IAAI,CAAC,GAAY,EAAE,CAAC,IAAI,CAAC;YACxD,kBAAkB,EAAE,MAAM,EAAE,kBAAkB,IAAI,CAAC,GAAY,EAAE,CAAC,IAAI,CAAC;YACvE,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,GAAG;YACjD,gB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AAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAE7C,MAAM,OAAO,GACX,UAAU,cAAc,UAAU,YAAY,CAAC,WAAW,EAAE,EAAmB,CAAC;IAElF,OAAO;QACL,OAAO;QACP,OAAO;QACP,OAAO,EAAE,YAAY;QACrB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ,EAAE,MAAM;QACxB,IAAI,EAAE,QAAQ,EAAE,IAAI;QACpB,QAAQ,EAAE,QAAQ,EAAE,QAAQ;QAC5B,KAAK,EAAE,QAAQ,EAAE,OAAO;QACxB,QAAQ,EAAE,KAAK;QACf,WAAW,EAAE,QAAQ,EAAE,WAAW;KACnC,CAAC;AACJ,CAAC","sourcesContent":["import { StaticIntervalPollingControllerOnly } from '@metamask/polling-controller';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { MulticallClient } from '../clients';\nimport type {\n AccountId,\n Address,\n Asset,\n AssetBalance,\n BalanceOfRequest,\n BalanceOfResponse,\n ChainId,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenListEntry,\n TokenListState,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst DEFAULT_DETECTION_INTERVAL = 180_000; // 3 minutes\n\n/**\n * Minimal messenger interface for TokenDetector.\n */\nexport type TokenDetectorMessenger = {\n call: (action: 'TokenListController:getState') => TokenListState;\n};\n\nexport type TokenDetectorConfig = {\n /** Function returning whether token detection is enabled (avoids stale value) */\n tokenDetectionEnabled?: () => boolean;\n /** Function returning whether external services are allowed (avoids stale value; default: () => true) */\n useExternalService?: () => boolean;\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n /** Polling interval in ms (default: 3 minutes) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for TokenDetector - identifies what to poll for.\n */\nexport type DetectionPollingInput = {\n /** Chain ID (hex format) */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n};\n\n/**\n * Callback type for token detection updates.\n */\nexport type OnDetectionUpdateCallback = (result: TokenDetectionResult) => void;\n\n/**\n * TokenDetector - Detects tokens with non-zero balances via multicall.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n */\nexport class TokenDetector extends StaticIntervalPollingControllerOnly<DetectionPollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #messenger: TokenDetectorMessenger;\n\n readonly #config: Required<Omit<TokenDetectorConfig, 'pollingInterval'>>;\n\n #onDetectionUpdate: OnDetectionUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n messenger: TokenDetectorMessenger,\n config?: TokenDetectorConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#messenger = messenger;\n this.#config = {\n tokenDetectionEnabled:\n config?.tokenDetectionEnabled ?? ((): boolean => true),\n useExternalService: config?.useExternalService ?? ((): boolean => true),\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n };\n\n // Set the polling interval\n this.setIntervalLength(\n config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL,\n );\n }\n\n /**\n * Set the callback to receive detection updates during polling.\n *\n * @param callback - Function to call with detection results.\n */\n setOnDetectionUpdate(callback: OnDetectionUpdateCallback): void {\n this.#onDetectionUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Detects tokens and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: DetectionPollingInput): Promise<void> {\n // Check if token list is available for this chain\n const tokensToCheck = this.getTokensToCheck(input.chainId);\n\n if (tokensToCheck.length === 0) {\n // No tokens in list for chain, will retry on next poll\n return;\n }\n\n const result = await this.detectTokens(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onDetectionUpdate && result.detectedAssets.length > 0) {\n this.#onDetectionUpdate(result);\n }\n }\n\n getTokensToCheck(chainId: ChainId): Address[] {\n const tokenListState = this.#messenger.call('TokenListController:getState');\n\n // Defensive check for tokensChainsCache\n if (!tokenListState?.tokensChainsCache) {\n return [];\n }\n\n // Try direct lookup first\n let chainCacheEntry = tokenListState.tokensChainsCache[chainId];\n\n // If not found, try normalizing the chain ID (e.g., 0x0a -> 0xa)\n if (!chainCacheEntry) {\n const normalizedChainId: ChainId = `0x${parseInt(chainId, 16).toString(\n 16,\n )}`;\n chainCacheEntry = tokenListState.tokensChainsCache[normalizedChainId];\n }\n\n const chainTokenList = chainCacheEntry?.data;\n\n if (!chainTokenList) {\n return [];\n }\n\n return Object.keys(chainTokenList) as Address[];\n }\n\n async detectTokens(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n options?: TokenDetectionOptions,\n ): Promise<TokenDetectionResult> {\n const tokenDetectionEnabled =\n options?.tokenDetectionEnabled ?? this.#config.tokenDetectionEnabled();\n const useExternalService =\n options?.useExternalService ?? this.#config.useExternalService();\n if (!tokenDetectionEnabled || !useExternalService) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp: Date.now(),\n };\n }\n const batchSize = options?.batchSize ?? this.#config.defaultBatchSize;\n const timestamp = Date.now();\n\n const tokensToCheck = this.getTokensToCheck(chainId);\n\n if (tokensToCheck.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n const balanceRequests: BalanceOfRequest[] = tokensToCheck.map(\n (tokenAddress) => ({\n tokenAddress,\n accountAddress,\n }),\n );\n\n type DetectionAccumulator = {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n DetectionAccumulator\n >({\n values: balanceRequests,\n batchSize,\n initialResult: {\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n },\n eachBatch: async (workingResult, batch) => {\n const responses = await this.#multicallClient.batchBalanceOf(\n chainId,\n batch,\n );\n\n return this.#processBalanceResponses(\n responses,\n workingResult as DetectionAccumulator,\n chainId,\n accountId,\n timestamp,\n );\n },\n });\n\n return {\n chainId,\n accountId,\n accountAddress,\n ...result,\n timestamp,\n };\n }\n\n #processBalanceResponses(\n responses: BalanceOfResponse[],\n accumulator: {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n ): {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n } {\n const {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n } = accumulator;\n\n for (const response of responses) {\n if (!response.success) {\n failedAddresses.push(response.tokenAddress);\n continue;\n }\n\n const balance = response.balance ?? '0';\n\n if (balance === '0' || balance === '') {\n zeroBalanceAddresses.push(response.tokenAddress);\n continue;\n }\n\n const tokenMetadata = this.#getTokenMetadata(\n chainId,\n response.tokenAddress,\n );\n\n const asset = this.#createAsset(\n chainId,\n response.tokenAddress,\n tokenMetadata,\n );\n detectedAssets.push(asset);\n\n if (tokenMetadata?.decimals === undefined) {\n continue;\n }\n\n const { decimals } = tokenMetadata;\n const formattedBalance = this.#formatBalance(balance, decimals);\n\n detectedBalances.push({\n assetId: asset.assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n decimals,\n timestamp,\n });\n }\n\n return {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n try {\n const balanceBigInt = BigInt(rawBalance);\n const divisor = BigInt(10 ** decimals);\n\n const integerPart = balanceBigInt / divisor;\n const remainder = balanceBigInt % divisor;\n const fractionalStr = remainder.toString().padStart(decimals, '0');\n const trimmedFractional = fractionalStr.replace(/0+$/u, '');\n\n if (trimmedFractional === '') {\n return integerPart.toString();\n }\n\n return `${integerPart}.${trimmedFractional}`;\n } catch {\n return rawBalance;\n }\n }\n\n #getTokenMetadata(\n chainId: ChainId,\n tokenAddress: Address,\n ): TokenListEntry | undefined {\n const tokenListState = this.#messenger.call('TokenListController:getState');\n if (!tokenListState?.tokensChainsCache) {\n return undefined;\n }\n\n const chainCacheEntry = tokenListState.tokensChainsCache[chainId];\n const chainTokenList = chainCacheEntry?.data;\n if (!chainTokenList) {\n return undefined;\n }\n\n if (chainTokenList[tokenAddress]) {\n return chainTokenList[tokenAddress];\n }\n\n const lowerAddress = tokenAddress.toLowerCase();\n for (const [address, metadata] of Object.entries(chainTokenList)) {\n if (address.toLowerCase() === lowerAddress) {\n return metadata;\n }\n }\n\n return undefined;\n }\n\n #createAsset(\n chainId: ChainId,\n tokenAddress: Address,\n metadata: TokenListEntry | undefined,\n ): Asset {\n const chainIdDecimal = parseInt(chainId, 16);\n\n const assetId =\n `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}` as CaipAssetType;\n\n return {\n assetId,\n chainId,\n address: tokenAddress,\n type: 'erc20',\n symbol: metadata?.symbol,\n name: metadata?.name,\n decimals: metadata?.decimals,\n image: metadata?.iconUrl,\n isNative: false,\n aggregators: metadata?.aggregators,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"TokenDetector.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qEAAmF;AAGnF,gDAAoE;AAepE,8CAAmD;AAEnD,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,eAAe,CAAC,CAAC;AAE/D,MAAM,0BAA0B,GAAG,MAAO,CAAC,CAAC,YAAY;AA8BxD;;;;GAIG;AACH,MAAa,aAAc,SAAQ,IAAA,wDAAmC,GAAyB;IAW7F,YACE,eAAgC,EAChC,eAAgC,EAChC,MAA4B;QAE5B,KAAK,EAAE,CAAC;;Q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AI,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,YAAY,CAAC,CAAC;AAC5E,CAAC,mEAGC,OAAgB,EAChB,YAAqB,EACrB,QAAoC;IAEpC,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAE7C,MAAM,OAAO,GACX,UAAU,cAAc,UAAU,YAAY,CAAC,WAAW,EAAE,EAAmB,CAAC;IAElF,OAAO;QACL,OAAO;QACP,OAAO;QACP,OAAO,EAAE,YAAY;QACrB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ,EAAE,MAAM;QACxB,IAAI,EAAE,QAAQ,EAAE,IAAI;QACpB,QAAQ,EAAE,QAAQ,EAAE,QAAQ;QAC5B,KAAK,EAAE,QAAQ,EAAE,OAAO;QACxB,QAAQ,EAAE,KAAK;QACf,WAAW,EAAE,QAAQ,EAAE,WAAW;KACnC,CAAC;AACJ,CAAC","sourcesContent":["import { StaticIntervalPollingControllerOnly } from '@metamask/polling-controller';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from '../../../logger';\nimport type { MulticallClient } from '../clients';\nimport type { TokensApiClient } from '../clients/TokensApiClient';\nimport type {\n AccountId,\n Address,\n Asset,\n AssetBalance,\n BalanceOfRequest,\n BalanceOfResponse,\n ChainId,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenListEntry,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst log = createModuleLogger(projectLogger, 'TokenDetector');\n\nconst DEFAULT_DETECTION_INTERVAL = 180_000; // 3 minutes\n\nexport type TokenDetectorConfig = {\n /** Function returning whether token detection is enabled (avoids stale value) */\n tokenDetectionEnabled?: () => boolean;\n /** Function returning whether external services are allowed (avoids stale value; default: () => true) */\n useExternalService?: () => boolean;\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n /** Polling interval in ms (default: 3 minutes) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for TokenDetector - identifies what to poll for.\n */\nexport type DetectionPollingInput = {\n /** Chain ID (hex format) */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n};\n\n/**\n * Callback type for token detection updates.\n */\nexport type OnDetectionUpdateCallback = (result: TokenDetectionResult) => void;\n\n/**\n * TokenDetector - Detects tokens with non-zero balances via multicall.\n * Fetches the token list from the Tokens API and uses multicall to check balances.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n */\nexport class TokenDetector extends StaticIntervalPollingControllerOnly<DetectionPollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #tokensApiClient: TokensApiClient;\n\n readonly #config: Required<Omit<TokenDetectorConfig, 'pollingInterval'>>;\n\n readonly #tokenListCache: Map<ChainId, TokenListEntry[]> = new Map();\n\n #onDetectionUpdate: OnDetectionUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n tokensApiClient: TokensApiClient,\n config?: TokenDetectorConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#tokensApiClient = tokensApiClient;\n this.#config = {\n tokenDetectionEnabled:\n config?.tokenDetectionEnabled ?? ((): boolean => true),\n useExternalService: config?.useExternalService ?? ((): boolean => true),\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n };\n\n this.setIntervalLength(\n config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL,\n );\n }\n\n /**\n * Set the callback to receive detection updates during polling.\n *\n * @param callback - Function to call with detection results.\n */\n setOnDetectionUpdate(callback: OnDetectionUpdateCallback): void {\n this.#onDetectionUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Detects tokens and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: DetectionPollingInput): Promise<void> {\n try {\n const result = await this.detectTokens(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onDetectionUpdate && result.detectedAssets.length > 0) {\n this.#onDetectionUpdate(result);\n }\n } catch (error) {\n log('Token detection poll failed', { chainId: input.chainId, error });\n }\n }\n\n /**\n * Fetch the list of token addresses to check for the given chain.\n * Calls the Tokens API and caches the result for metadata lookups.\n *\n * @param chainId - Chain ID in hex format.\n * @returns Array of token contract addresses.\n */\n async getTokensToCheck(chainId: ChainId): Promise<Address[]> {\n const tokenList = await this.#fetchAndCacheTokenList(chainId);\n return tokenList.map((entry) => entry.address as Address);\n }\n\n async detectTokens(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n options?: TokenDetectionOptions,\n ): Promise<TokenDetectionResult> {\n const tokenDetectionEnabled =\n options?.tokenDetectionEnabled ?? this.#config.tokenDetectionEnabled();\n const useExternalService =\n options?.useExternalService ?? this.#config.useExternalService();\n if (!tokenDetectionEnabled || !useExternalService) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp: Date.now(),\n };\n }\n const batchSize = options?.batchSize ?? this.#config.defaultBatchSize;\n const timestamp = Date.now();\n\n const tokensToCheck = await this.getTokensToCheck(chainId);\n\n if (tokensToCheck.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n const balanceRequests: BalanceOfRequest[] = tokensToCheck.map(\n (tokenAddress) => ({\n tokenAddress,\n accountAddress,\n }),\n );\n\n type DetectionAccumulator = {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n DetectionAccumulator\n >({\n values: balanceRequests,\n batchSize,\n initialResult: {\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n },\n eachBatch: async (workingResult, batch) => {\n const responses = await this.#multicallClient.batchBalanceOf(\n chainId,\n batch,\n );\n\n return this.#processBalanceResponses(\n responses,\n workingResult as DetectionAccumulator,\n chainId,\n accountId,\n timestamp,\n );\n },\n });\n\n return {\n chainId,\n accountId,\n accountAddress,\n ...result,\n timestamp,\n };\n }\n\n async #fetchAndCacheTokenList(chainId: ChainId): Promise<TokenListEntry[]> {\n try {\n const list = await this.#tokensApiClient.fetchTokenList(chainId);\n this.#tokenListCache.set(chainId, list);\n return list;\n } catch (error) {\n const cached = this.#tokenListCache.get(chainId);\n log('Failed to fetch token list; using stale cache', {\n chainId,\n cachedCount: cached?.length ?? 0,\n error,\n });\n return cached ?? [];\n }\n }\n\n #processBalanceResponses(\n responses: BalanceOfResponse[],\n accumulator: {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n ): {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n } {\n const {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n } = accumulator;\n\n for (const response of responses) {\n if (!response.success) {\n failedAddresses.push(response.tokenAddress);\n continue;\n }\n\n const balance = response.balance ?? '0';\n\n if (balance === '0' || balance === '') {\n zeroBalanceAddresses.push(response.tokenAddress);\n continue;\n }\n\n const tokenMetadata = this.#getTokenMetadata(\n chainId,\n response.tokenAddress,\n );\n\n const asset = this.#createAsset(\n chainId,\n response.tokenAddress,\n tokenMetadata,\n );\n detectedAssets.push(asset);\n\n if (tokenMetadata?.decimals === undefined) {\n continue;\n }\n\n const { decimals } = tokenMetadata;\n const formattedBalance = this.#formatBalance(balance, decimals);\n\n detectedBalances.push({\n assetId: asset.assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n decimals,\n timestamp,\n });\n }\n\n return {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n try {\n const balanceBigInt = BigInt(rawBalance);\n const divisor = BigInt(10 ** decimals);\n\n const integerPart = balanceBigInt / divisor;\n const remainder = balanceBigInt % divisor;\n const fractionalStr = remainder.toString().padStart(decimals, '0');\n const trimmedFractional = fractionalStr.replace(/0+$/u, '');\n\n if (trimmedFractional === '') {\n return integerPart.toString();\n }\n\n return `${integerPart}.${trimmedFractional}`;\n } catch {\n return rawBalance;\n }\n }\n\n #getTokenMetadata(\n chainId: ChainId,\n tokenAddress: Address,\n ): TokenListEntry | undefined {\n const list = this.#tokenListCache.get(chainId) ?? [];\n const lowerAddress = tokenAddress.toLowerCase();\n\n const exact = list.find((entry) => entry.address === tokenAddress);\n if (exact) {\n return exact;\n }\n\n return list.find((entry) => entry.address.toLowerCase() === lowerAddress);\n }\n\n #createAsset(\n chainId: ChainId,\n tokenAddress: Address,\n metadata: TokenListEntry | undefined,\n ): Asset {\n const chainIdDecimal = parseInt(chainId, 16);\n\n const assetId =\n `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}` as CaipAssetType;\n\n return {\n assetId,\n chainId,\n address: tokenAddress,\n type: 'erc20',\n symbol: metadata?.symbol,\n name: metadata?.name,\n decimals: metadata?.decimals,\n image: metadata?.iconUrl,\n isNative: false,\n aggregators: metadata?.aggregators,\n };\n }\n}\n"]}
@@ -1,11 +1,6 @@
1
1
  import type { MulticallClient } from "../clients/index.cjs";
2
- import type { AccountId, Address, ChainId, TokenDetectionOptions, TokenDetectionResult, TokenListState } from "../types/index.cjs";
3
- /**
4
- * Minimal messenger interface for TokenDetector.
5
- */
6
- export type TokenDetectorMessenger = {
7
- call: (action: 'TokenListController:getState') => TokenListState;
8
- };
2
+ import type { TokensApiClient } from "../clients/TokensApiClient.cjs";
3
+ import type { AccountId, Address, ChainId, TokenDetectionOptions, TokenDetectionResult } from "../types/index.cjs";
9
4
  export type TokenDetectorConfig = {
10
5
  /** Function returning whether token detection is enabled (avoids stale value) */
11
6
  tokenDetectionEnabled?: () => boolean;
@@ -50,11 +45,12 @@ declare const TokenDetector_base: (abstract new (...args: any[]) => {
50
45
  };
51
46
  /**
52
47
  * TokenDetector - Detects tokens with non-zero balances via multicall.
48
+ * Fetches the token list from the Tokens API and uses multicall to check balances.
53
49
  * Extends StaticIntervalPollingControllerOnly for built-in polling support.
54
50
  */
55
51
  export declare class TokenDetector extends TokenDetector_base {
56
52
  #private;
57
- constructor(multicallClient: MulticallClient, messenger: TokenDetectorMessenger, config?: TokenDetectorConfig);
53
+ constructor(multicallClient: MulticallClient, tokensApiClient: TokensApiClient, config?: TokenDetectorConfig);
58
54
  /**
59
55
  * Set the callback to receive detection updates during polling.
60
56
  *
@@ -68,7 +64,14 @@ export declare class TokenDetector extends TokenDetector_base {
68
64
  * @param input - The polling input.
69
65
  */
70
66
  _executePoll(input: DetectionPollingInput): Promise<void>;
71
- getTokensToCheck(chainId: ChainId): Address[];
67
+ /**
68
+ * Fetch the list of token addresses to check for the given chain.
69
+ * Calls the Tokens API and caches the result for metadata lookups.
70
+ *
71
+ * @param chainId - Chain ID in hex format.
72
+ * @returns Array of token contract addresses.
73
+ */
74
+ getTokensToCheck(chainId: ChainId): Promise<Address[]>;
72
75
  detectTokens(chainId: ChainId, accountId: AccountId, accountAddress: Address, options?: TokenDetectionOptions): Promise<TokenDetectionResult>;
73
76
  }
74
77
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetector.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,6BAAmB;AAClD,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EAKP,OAAO,EACP,qBAAqB,EACrB,oBAAoB,EAEpB,cAAc,EACf,2BAAiB;AAKlB;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,CAAC,MAAM,EAAE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AE,SAAS,EACpB,cAAc,EAAE,OAAO,EACvB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,oBAAoB,CAAC;CA2OjC"}
@@ -1,11 +1,6 @@
1
1
  import type { MulticallClient } from "../clients/index.mjs";
2
- import type { AccountId, Address, ChainId, TokenDetectionOptions, TokenDetectionResult, TokenListState } from "../types/index.mjs";
3
- /**
4
- * Minimal messenger interface for TokenDetector.
5
- */
6
- export type TokenDetectorMessenger = {
7
- call: (action: 'TokenListController:getState') => TokenListState;
8
- };
2
+ import type { TokensApiClient } from "../clients/TokensApiClient.mjs";
3
+ import type { AccountId, Address, ChainId, TokenDetectionOptions, TokenDetectionResult } from "../types/index.mjs";
9
4
  export type TokenDetectorConfig = {
10
5
  /** Function returning whether token detection is enabled (avoids stale value) */
11
6
  tokenDetectionEnabled?: () => boolean;
@@ -50,11 +45,12 @@ declare const TokenDetector_base: (abstract new (...args: any[]) => {
50
45
  };
51
46
  /**
52
47
  * TokenDetector - Detects tokens with non-zero balances via multicall.
48
+ * Fetches the token list from the Tokens API and uses multicall to check balances.
53
49
  * Extends StaticIntervalPollingControllerOnly for built-in polling support.
54
50
  */
55
51
  export declare class TokenDetector extends TokenDetector_base {
56
52
  #private;
57
- constructor(multicallClient: MulticallClient, messenger: TokenDetectorMessenger, config?: TokenDetectorConfig);
53
+ constructor(multicallClient: MulticallClient, tokensApiClient: TokensApiClient, config?: TokenDetectorConfig);
58
54
  /**
59
55
  * Set the callback to receive detection updates during polling.
60
56
  *
@@ -68,7 +64,14 @@ export declare class TokenDetector extends TokenDetector_base {
68
64
  * @param input - The polling input.
69
65
  */
70
66
  _executePoll(input: DetectionPollingInput): Promise<void>;
71
- getTokensToCheck(chainId: ChainId): Address[];
67
+ /**
68
+ * Fetch the list of token addresses to check for the given chain.
69
+ * Calls the Tokens API and caches the result for metadata lookups.
70
+ *
71
+ * @param chainId - Chain ID in hex format.
72
+ * @returns Array of token contract addresses.
73
+ */
74
+ getTokensToCheck(chainId: ChainId): Promise<Address[]>;
72
75
  detectTokens(chainId: ChainId, accountId: AccountId, accountAddress: Address, options?: TokenDetectionOptions): Promise<TokenDetectionResult>;
73
76
  }
74
77
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetector.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,6BAAmB;AAClD,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EAKP,OAAO,EACP,qBAAqB,EACrB,oBAAoB,EAEpB,cAAc,EACf,2BAAiB;AAKlB;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,CAAC,MAAM,EAAE,8BAA8B,KAAK,cAAc,CAAC;CAClE,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,iFAAiF;IACjF,qBAAqB,CAAC,EAAE,MAAM,OAAO,CAAC;IACtC,yGAAyG;IACzG,kBAAkB,CAAC,EAAE,MAAM,OAAO,CAAC;IACnC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,kDAAkD;IAClD,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,4BAA4B;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,sBAAsB;IACtB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,CAAC,MAAM,EAAE,oBAAoB,KAAK,IAAI,CAAC;;;;;;;;;;;;;;;;;;AAE/E;;;GAGG;AACH,qBAAa,aAAc,SAAQ,kBAA4D;;gBAU3F,eAAe,EAAE,eAAe,EAChC,SAAS,EAAE,sBAAsB,EACjC,MAAM,CAAC,EAAE,mBAAmB;IAmB9B;;;;OAIG;IACH,oBAAoB,CAAC,QAAQ,EAAE,yBAAyB,GAAG,IAAI;IAI/D;;;;;OAKG;IACG,YAAY,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAoB/D,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,EAAE;IA4BvC,YAAY,CAChB,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,EACpB,cAAc,EAAE,OAAO,EACvB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,oBAAoB,CAAC;CAyOjC"}
1
+ {"version":3,"file":"TokenDetector.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,6BAAmB;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAmC;AAClE,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EAKP,OAAO,EACP,qBAAqB,EACrB,oBAAoB,EAErB,2BAAiB;AAOlB,MAAM,MAAM,mBAAmB,GAAG;IAChC,iFAAiF;IACjF,qBAAqB,CAAC,EAAE,MAAM,OAAO,CAAC;IACtC,yGAAyG;IACzG,kBAAkB,CAAC,EAAE,MAAM,OAAO,CAAC;IACnC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,kDAAkD;IAClD,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,4BAA4B;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,sBAAsB;IACtB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,CAAC,MAAM,EAAE,oBAAoB,KAAK,IAAI,CAAC;;;;;;;;;;;;;;;;;;AAE/E;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,kBAA4D;;gBAY3F,eAAe,EAAE,eAAe,EAChC,eAAe,EAAE,eAAe,EAChC,MAAM,CAAC,EAAE,mBAAmB;IAkB9B;;;;OAIG;IACH,oBAAoB,CAAC,QAAQ,EAAE,yBAAyB,GAAG,IAAI;IAI/D;;;;;OAKG;IACG,YAAY,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB/D;;;;;;OAMG;IACG,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAKtD,YAAY,CAChB,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,EACpB,cAAc,EAAE,OAAO,EACvB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,oBAAoB,CAAC;CA2OjC"}
@@ -9,31 +9,34 @@ 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 _TokenDetector_instances, _TokenDetector_multicallClient, _TokenDetector_messenger, _TokenDetector_config, _TokenDetector_onDetectionUpdate, _TokenDetector_processBalanceResponses, _TokenDetector_formatBalance, _TokenDetector_getTokenMetadata, _TokenDetector_createAsset;
12
+ var _TokenDetector_instances, _TokenDetector_multicallClient, _TokenDetector_tokensApiClient, _TokenDetector_config, _TokenDetector_tokenListCache, _TokenDetector_onDetectionUpdate, _TokenDetector_fetchAndCacheTokenList, _TokenDetector_processBalanceResponses, _TokenDetector_formatBalance, _TokenDetector_getTokenMetadata, _TokenDetector_createAsset;
13
13
  import { StaticIntervalPollingControllerOnly } from "@metamask/polling-controller";
14
+ import { projectLogger, createModuleLogger } from "../../../logger.mjs";
14
15
  import { reduceInBatchesSerially } from "../utils/index.mjs";
16
+ const log = createModuleLogger(projectLogger, 'TokenDetector');
15
17
  const DEFAULT_DETECTION_INTERVAL = 180000; // 3 minutes
16
18
  /**
17
19
  * TokenDetector - Detects tokens with non-zero balances via multicall.
20
+ * Fetches the token list from the Tokens API and uses multicall to check balances.
18
21
  * Extends StaticIntervalPollingControllerOnly for built-in polling support.
19
22
  */
20
23
  export class TokenDetector extends StaticIntervalPollingControllerOnly() {
21
- constructor(multicallClient, messenger, config) {
24
+ constructor(multicallClient, tokensApiClient, config) {
22
25
  super();
23
26
  _TokenDetector_instances.add(this);
24
27
  _TokenDetector_multicallClient.set(this, void 0);
25
- _TokenDetector_messenger.set(this, void 0);
28
+ _TokenDetector_tokensApiClient.set(this, void 0);
26
29
  _TokenDetector_config.set(this, void 0);
30
+ _TokenDetector_tokenListCache.set(this, new Map());
27
31
  _TokenDetector_onDetectionUpdate.set(this, void 0);
28
32
  __classPrivateFieldSet(this, _TokenDetector_multicallClient, multicallClient, "f");
29
- __classPrivateFieldSet(this, _TokenDetector_messenger, messenger, "f");
33
+ __classPrivateFieldSet(this, _TokenDetector_tokensApiClient, tokensApiClient, "f");
30
34
  __classPrivateFieldSet(this, _TokenDetector_config, {
31
35
  tokenDetectionEnabled: config?.tokenDetectionEnabled ?? (() => true),
32
36
  useExternalService: config?.useExternalService ?? (() => true),
33
37
  defaultBatchSize: config?.defaultBatchSize ?? 300,
34
38
  defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,
35
39
  }, "f");
36
- // Set the polling interval
37
40
  this.setIntervalLength(config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL);
38
41
  }
39
42
  /**
@@ -51,35 +54,26 @@ export class TokenDetector extends StaticIntervalPollingControllerOnly() {
51
54
  * @param input - The polling input.
52
55
  */
53
56
  async _executePoll(input) {
54
- // Check if token list is available for this chain
55
- const tokensToCheck = this.getTokensToCheck(input.chainId);
56
- if (tokensToCheck.length === 0) {
57
- // No tokens in list for chain, will retry on next poll
58
- return;
57
+ try {
58
+ const result = await this.detectTokens(input.chainId, input.accountId, input.accountAddress);
59
+ if (__classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f") && result.detectedAssets.length > 0) {
60
+ __classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f").call(this, result);
61
+ }
59
62
  }
60
- const result = await this.detectTokens(input.chainId, input.accountId, input.accountAddress);
61
- if (__classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f") && result.detectedAssets.length > 0) {
62
- __classPrivateFieldGet(this, _TokenDetector_onDetectionUpdate, "f").call(this, result);
63
+ catch (error) {
64
+ log('Token detection poll failed', { chainId: input.chainId, error });
63
65
  }
64
66
  }
65
- getTokensToCheck(chainId) {
66
- const tokenListState = __classPrivateFieldGet(this, _TokenDetector_messenger, "f").call('TokenListController:getState');
67
- // Defensive check for tokensChainsCache
68
- if (!tokenListState?.tokensChainsCache) {
69
- return [];
70
- }
71
- // Try direct lookup first
72
- let chainCacheEntry = tokenListState.tokensChainsCache[chainId];
73
- // If not found, try normalizing the chain ID (e.g., 0x0a -> 0xa)
74
- if (!chainCacheEntry) {
75
- const normalizedChainId = `0x${parseInt(chainId, 16).toString(16)}`;
76
- chainCacheEntry = tokenListState.tokensChainsCache[normalizedChainId];
77
- }
78
- const chainTokenList = chainCacheEntry?.data;
79
- if (!chainTokenList) {
80
- return [];
81
- }
82
- return Object.keys(chainTokenList);
67
+ /**
68
+ * Fetch the list of token addresses to check for the given chain.
69
+ * Calls the Tokens API and caches the result for metadata lookups.
70
+ *
71
+ * @param chainId - Chain ID in hex format.
72
+ * @returns Array of token contract addresses.
73
+ */
74
+ async getTokensToCheck(chainId) {
75
+ const tokenList = await __classPrivateFieldGet(this, _TokenDetector_instances, "m", _TokenDetector_fetchAndCacheTokenList).call(this, chainId);
76
+ return tokenList.map((entry) => entry.address);
83
77
  }
84
78
  async detectTokens(chainId, accountId, accountAddress, options) {
85
79
  const tokenDetectionEnabled = options?.tokenDetectionEnabled ?? __classPrivateFieldGet(this, _TokenDetector_config, "f").tokenDetectionEnabled();
@@ -98,7 +92,7 @@ export class TokenDetector extends StaticIntervalPollingControllerOnly() {
98
92
  }
99
93
  const batchSize = options?.batchSize ?? __classPrivateFieldGet(this, _TokenDetector_config, "f").defaultBatchSize;
100
94
  const timestamp = Date.now();
101
- const tokensToCheck = this.getTokensToCheck(chainId);
95
+ const tokensToCheck = await this.getTokensToCheck(chainId);
102
96
  if (tokensToCheck.length === 0) {
103
97
  return {
104
98
  chainId,
@@ -138,7 +132,22 @@ export class TokenDetector extends StaticIntervalPollingControllerOnly() {
138
132
  };
139
133
  }
140
134
  }
141
- _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_messenger = new WeakMap(), _TokenDetector_config = new WeakMap(), _TokenDetector_onDetectionUpdate = new WeakMap(), _TokenDetector_instances = new WeakSet(), _TokenDetector_processBalanceResponses = function _TokenDetector_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp) {
135
+ _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_tokensApiClient = new WeakMap(), _TokenDetector_config = new WeakMap(), _TokenDetector_tokenListCache = new WeakMap(), _TokenDetector_onDetectionUpdate = new WeakMap(), _TokenDetector_instances = new WeakSet(), _TokenDetector_fetchAndCacheTokenList = async function _TokenDetector_fetchAndCacheTokenList(chainId) {
136
+ try {
137
+ const list = await __classPrivateFieldGet(this, _TokenDetector_tokensApiClient, "f").fetchTokenList(chainId);
138
+ __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").set(chainId, list);
139
+ return list;
140
+ }
141
+ catch (error) {
142
+ const cached = __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").get(chainId);
143
+ log('Failed to fetch token list; using stale cache', {
144
+ chainId,
145
+ cachedCount: cached?.length ?? 0,
146
+ error,
147
+ });
148
+ return cached ?? [];
149
+ }
150
+ }, _TokenDetector_processBalanceResponses = function _TokenDetector_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp) {
142
151
  const { detectedAssets, detectedBalances, zeroBalanceAddresses, failedAddresses, } = accumulator;
143
152
  for (const response of responses) {
144
153
  if (!response.success) {
@@ -191,25 +200,13 @@ _TokenDetector_multicallClient = new WeakMap(), _TokenDetector_messenger = new W
191
200
  return rawBalance;
192
201
  }
193
202
  }, _TokenDetector_getTokenMetadata = function _TokenDetector_getTokenMetadata(chainId, tokenAddress) {
194
- const tokenListState = __classPrivateFieldGet(this, _TokenDetector_messenger, "f").call('TokenListController:getState');
195
- if (!tokenListState?.tokensChainsCache) {
196
- return undefined;
197
- }
198
- const chainCacheEntry = tokenListState.tokensChainsCache[chainId];
199
- const chainTokenList = chainCacheEntry?.data;
200
- if (!chainTokenList) {
201
- return undefined;
202
- }
203
- if (chainTokenList[tokenAddress]) {
204
- return chainTokenList[tokenAddress];
205
- }
203
+ const list = __classPrivateFieldGet(this, _TokenDetector_tokenListCache, "f").get(chainId) ?? [];
206
204
  const lowerAddress = tokenAddress.toLowerCase();
207
- for (const [address, metadata] of Object.entries(chainTokenList)) {
208
- if (address.toLowerCase() === lowerAddress) {
209
- return metadata;
210
- }
205
+ const exact = list.find((entry) => entry.address === tokenAddress);
206
+ if (exact) {
207
+ return exact;
211
208
  }
212
- return undefined;
209
+ return list.find((entry) => entry.address.toLowerCase() === lowerAddress);
213
210
  }, _TokenDetector_createAsset = function _TokenDetector_createAsset(chainId, tokenAddress, metadata) {
214
211
  const chainIdDecimal = parseInt(chainId, 16);
215
212
  const assetId = `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}`;
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetector.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mCAAmC,EAAE,qCAAqC;AAiBnF,OAAO,EAAE,uBAAuB,EAAE,2BAAiB;AAEnD,MAAM,0BAA0B,GAAG,MAAO,CAAC,CAAC,YAAY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c,EAAE,CAAC;QACpB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC;QACjC,OAAO,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,CAAC;IAED,MAAM,YAAY,GAAG,YAAY,CAAC,WAAW,EAAE,CAAC;IAChD,KAAK,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,CAAC;QACjE,IAAI,OAAO,CAAC,WAAW,EAAE,KAAK,YAAY,EAAE,CAAC;YAC3C,OAAO,QAAQ,CAAC;QAClB,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,mEAGC,OAAgB,EAChB,YAAqB,EACrB,QAAoC;IAEpC,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAE7C,MAAM,OAAO,GACX,UAAU,cAAc,UAAU,YAAY,CAAC,WAAW,EAAE,EAAmB,CAAC;IAElF,OAAO;QACL,OAAO;QACP,OAAO;QACP,OAAO,EAAE,YAAY;QACrB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ,EAAE,MAAM;QACxB,IAAI,EAAE,QAAQ,EAAE,IAAI;QACpB,QAAQ,EAAE,QAAQ,EAAE,QAAQ;QAC5B,KAAK,EAAE,QAAQ,EAAE,OAAO;QACxB,QAAQ,EAAE,KAAK;QACf,WAAW,EAAE,QAAQ,EAAE,WAAW;KACnC,CAAC;AACJ,CAAC","sourcesContent":["import { StaticIntervalPollingControllerOnly } from '@metamask/polling-controller';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport type { MulticallClient } from '../clients';\nimport type {\n AccountId,\n Address,\n Asset,\n AssetBalance,\n BalanceOfRequest,\n BalanceOfResponse,\n ChainId,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenListEntry,\n TokenListState,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst DEFAULT_DETECTION_INTERVAL = 180_000; // 3 minutes\n\n/**\n * Minimal messenger interface for TokenDetector.\n */\nexport type TokenDetectorMessenger = {\n call: (action: 'TokenListController:getState') => TokenListState;\n};\n\nexport type TokenDetectorConfig = {\n /** Function returning whether token detection is enabled (avoids stale value) */\n tokenDetectionEnabled?: () => boolean;\n /** Function returning whether external services are allowed (avoids stale value; default: () => true) */\n useExternalService?: () => boolean;\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n /** Polling interval in ms (default: 3 minutes) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for TokenDetector - identifies what to poll for.\n */\nexport type DetectionPollingInput = {\n /** Chain ID (hex format) */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n};\n\n/**\n * Callback type for token detection updates.\n */\nexport type OnDetectionUpdateCallback = (result: TokenDetectionResult) => void;\n\n/**\n * TokenDetector - Detects tokens with non-zero balances via multicall.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n */\nexport class TokenDetector extends StaticIntervalPollingControllerOnly<DetectionPollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #messenger: TokenDetectorMessenger;\n\n readonly #config: Required<Omit<TokenDetectorConfig, 'pollingInterval'>>;\n\n #onDetectionUpdate: OnDetectionUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n messenger: TokenDetectorMessenger,\n config?: TokenDetectorConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#messenger = messenger;\n this.#config = {\n tokenDetectionEnabled:\n config?.tokenDetectionEnabled ?? ((): boolean => true),\n useExternalService: config?.useExternalService ?? ((): boolean => true),\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n };\n\n // Set the polling interval\n this.setIntervalLength(\n config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL,\n );\n }\n\n /**\n * Set the callback to receive detection updates during polling.\n *\n * @param callback - Function to call with detection results.\n */\n setOnDetectionUpdate(callback: OnDetectionUpdateCallback): void {\n this.#onDetectionUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Detects tokens and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: DetectionPollingInput): Promise<void> {\n // Check if token list is available for this chain\n const tokensToCheck = this.getTokensToCheck(input.chainId);\n\n if (tokensToCheck.length === 0) {\n // No tokens in list for chain, will retry on next poll\n return;\n }\n\n const result = await this.detectTokens(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onDetectionUpdate && result.detectedAssets.length > 0) {\n this.#onDetectionUpdate(result);\n }\n }\n\n getTokensToCheck(chainId: ChainId): Address[] {\n const tokenListState = this.#messenger.call('TokenListController:getState');\n\n // Defensive check for tokensChainsCache\n if (!tokenListState?.tokensChainsCache) {\n return [];\n }\n\n // Try direct lookup first\n let chainCacheEntry = tokenListState.tokensChainsCache[chainId];\n\n // If not found, try normalizing the chain ID (e.g., 0x0a -> 0xa)\n if (!chainCacheEntry) {\n const normalizedChainId: ChainId = `0x${parseInt(chainId, 16).toString(\n 16,\n )}`;\n chainCacheEntry = tokenListState.tokensChainsCache[normalizedChainId];\n }\n\n const chainTokenList = chainCacheEntry?.data;\n\n if (!chainTokenList) {\n return [];\n }\n\n return Object.keys(chainTokenList) as Address[];\n }\n\n async detectTokens(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n options?: TokenDetectionOptions,\n ): Promise<TokenDetectionResult> {\n const tokenDetectionEnabled =\n options?.tokenDetectionEnabled ?? this.#config.tokenDetectionEnabled();\n const useExternalService =\n options?.useExternalService ?? this.#config.useExternalService();\n if (!tokenDetectionEnabled || !useExternalService) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp: Date.now(),\n };\n }\n const batchSize = options?.batchSize ?? this.#config.defaultBatchSize;\n const timestamp = Date.now();\n\n const tokensToCheck = this.getTokensToCheck(chainId);\n\n if (tokensToCheck.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n const balanceRequests: BalanceOfRequest[] = tokensToCheck.map(\n (tokenAddress) => ({\n tokenAddress,\n accountAddress,\n }),\n );\n\n type DetectionAccumulator = {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n DetectionAccumulator\n >({\n values: balanceRequests,\n batchSize,\n initialResult: {\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n },\n eachBatch: async (workingResult, batch) => {\n const responses = await this.#multicallClient.batchBalanceOf(\n chainId,\n batch,\n );\n\n return this.#processBalanceResponses(\n responses,\n workingResult as DetectionAccumulator,\n chainId,\n accountId,\n timestamp,\n );\n },\n });\n\n return {\n chainId,\n accountId,\n accountAddress,\n ...result,\n timestamp,\n };\n }\n\n #processBalanceResponses(\n responses: BalanceOfResponse[],\n accumulator: {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n ): {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n } {\n const {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n } = accumulator;\n\n for (const response of responses) {\n if (!response.success) {\n failedAddresses.push(response.tokenAddress);\n continue;\n }\n\n const balance = response.balance ?? '0';\n\n if (balance === '0' || balance === '') {\n zeroBalanceAddresses.push(response.tokenAddress);\n continue;\n }\n\n const tokenMetadata = this.#getTokenMetadata(\n chainId,\n response.tokenAddress,\n );\n\n const asset = this.#createAsset(\n chainId,\n response.tokenAddress,\n tokenMetadata,\n );\n detectedAssets.push(asset);\n\n if (tokenMetadata?.decimals === undefined) {\n continue;\n }\n\n const { decimals } = tokenMetadata;\n const formattedBalance = this.#formatBalance(balance, decimals);\n\n detectedBalances.push({\n assetId: asset.assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n decimals,\n timestamp,\n });\n }\n\n return {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n try {\n const balanceBigInt = BigInt(rawBalance);\n const divisor = BigInt(10 ** decimals);\n\n const integerPart = balanceBigInt / divisor;\n const remainder = balanceBigInt % divisor;\n const fractionalStr = remainder.toString().padStart(decimals, '0');\n const trimmedFractional = fractionalStr.replace(/0+$/u, '');\n\n if (trimmedFractional === '') {\n return integerPart.toString();\n }\n\n return `${integerPart}.${trimmedFractional}`;\n } catch {\n return rawBalance;\n }\n }\n\n #getTokenMetadata(\n chainId: ChainId,\n tokenAddress: Address,\n ): TokenListEntry | undefined {\n const tokenListState = this.#messenger.call('TokenListController:getState');\n if (!tokenListState?.tokensChainsCache) {\n return undefined;\n }\n\n const chainCacheEntry = tokenListState.tokensChainsCache[chainId];\n const chainTokenList = chainCacheEntry?.data;\n if (!chainTokenList) {\n return undefined;\n }\n\n if (chainTokenList[tokenAddress]) {\n return chainTokenList[tokenAddress];\n }\n\n const lowerAddress = tokenAddress.toLowerCase();\n for (const [address, metadata] of Object.entries(chainTokenList)) {\n if (address.toLowerCase() === lowerAddress) {\n return metadata;\n }\n }\n\n return undefined;\n }\n\n #createAsset(\n chainId: ChainId,\n tokenAddress: Address,\n metadata: TokenListEntry | undefined,\n ): Asset {\n const chainIdDecimal = parseInt(chainId, 16);\n\n const assetId =\n `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}` as CaipAssetType;\n\n return {\n assetId,\n chainId,\n address: tokenAddress,\n type: 'erc20',\n symbol: metadata?.symbol,\n name: metadata?.name,\n decimals: metadata?.decimals,\n image: metadata?.iconUrl,\n isNative: false,\n aggregators: metadata?.aggregators,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"TokenDetector.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/TokenDetector.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mCAAmC,EAAE,qCAAqC;AAGnF,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAepE,OAAO,EAAE,uBAAuB,EAAE,2BAAiB;AAEnD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D,MAAM,0BAA0B,GAAG,MAAO,CAAC,CAAC,YAAY;AA8BxD;;;;GAIG;AACH,MAAM,OAAO,aAAc,SAAQ,mCAAmC,EAAyB;IAW7F,YACE,eAAgC,EAChC,eAAgC,EAChC,MAA4B;QAE5B,KAAK,EAAE,CAAC;;QAfD,iDAAkC;QAElC,iDAAkC;QAElC,wCAAgE;QAEhE,wCAAkD,IAAI,GAAG,EAAE,EAAC;QAErE,mDAA0D;QAQxD,uBAAA,IAAI,kCAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,kCAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,yBAAW;YACb,qBAAqB,EACnB,MAAM,EAAE,qBAAqB,IAAI,CAAC,GAAY,EAAE,CAAC,IAAI,CAAC;YACxD,kBAAkB,EAAE,MAAM,EAAE,kBAAkB,IAAI,CAAC,GAAY,EAAE,CAAC,IAAI,CAAC;YACvE,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,GAAG;YACjD,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,KAAK;SACpD,MAAA,CAAC;QAEF,IAAI,CAAC,iBAAiB,CACpB,MAAM,EAAE,eAAe,IAAI,0BAA0B,CACtD,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,oBAAoB,CAAC,QAAm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ollingControllerOnly } from '@metamask/polling-controller';\nimport type { CaipAssetType } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from '../../../logger';\nimport type { MulticallClient } from '../clients';\nimport type { TokensApiClient } from '../clients/TokensApiClient';\nimport type {\n AccountId,\n Address,\n Asset,\n AssetBalance,\n BalanceOfRequest,\n BalanceOfResponse,\n ChainId,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenListEntry,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst log = createModuleLogger(projectLogger, 'TokenDetector');\n\nconst DEFAULT_DETECTION_INTERVAL = 180_000; // 3 minutes\n\nexport type TokenDetectorConfig = {\n /** Function returning whether token detection is enabled (avoids stale value) */\n tokenDetectionEnabled?: () => boolean;\n /** Function returning whether external services are allowed (avoids stale value; default: () => true) */\n useExternalService?: () => boolean;\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n /** Polling interval in ms (default: 3 minutes) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for TokenDetector - identifies what to poll for.\n */\nexport type DetectionPollingInput = {\n /** Chain ID (hex format) */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n};\n\n/**\n * Callback type for token detection updates.\n */\nexport type OnDetectionUpdateCallback = (result: TokenDetectionResult) => void;\n\n/**\n * TokenDetector - Detects tokens with non-zero balances via multicall.\n * Fetches the token list from the Tokens API and uses multicall to check balances.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n */\nexport class TokenDetector extends StaticIntervalPollingControllerOnly<DetectionPollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #tokensApiClient: TokensApiClient;\n\n readonly #config: Required<Omit<TokenDetectorConfig, 'pollingInterval'>>;\n\n readonly #tokenListCache: Map<ChainId, TokenListEntry[]> = new Map();\n\n #onDetectionUpdate: OnDetectionUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n tokensApiClient: TokensApiClient,\n config?: TokenDetectorConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#tokensApiClient = tokensApiClient;\n this.#config = {\n tokenDetectionEnabled:\n config?.tokenDetectionEnabled ?? ((): boolean => true),\n useExternalService: config?.useExternalService ?? ((): boolean => true),\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n };\n\n this.setIntervalLength(\n config?.pollingInterval ?? DEFAULT_DETECTION_INTERVAL,\n );\n }\n\n /**\n * Set the callback to receive detection updates during polling.\n *\n * @param callback - Function to call with detection results.\n */\n setOnDetectionUpdate(callback: OnDetectionUpdateCallback): void {\n this.#onDetectionUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Detects tokens and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: DetectionPollingInput): Promise<void> {\n try {\n const result = await this.detectTokens(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onDetectionUpdate && result.detectedAssets.length > 0) {\n this.#onDetectionUpdate(result);\n }\n } catch (error) {\n log('Token detection poll failed', { chainId: input.chainId, error });\n }\n }\n\n /**\n * Fetch the list of token addresses to check for the given chain.\n * Calls the Tokens API and caches the result for metadata lookups.\n *\n * @param chainId - Chain ID in hex format.\n * @returns Array of token contract addresses.\n */\n async getTokensToCheck(chainId: ChainId): Promise<Address[]> {\n const tokenList = await this.#fetchAndCacheTokenList(chainId);\n return tokenList.map((entry) => entry.address as Address);\n }\n\n async detectTokens(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n options?: TokenDetectionOptions,\n ): Promise<TokenDetectionResult> {\n const tokenDetectionEnabled =\n options?.tokenDetectionEnabled ?? this.#config.tokenDetectionEnabled();\n const useExternalService =\n options?.useExternalService ?? this.#config.useExternalService();\n if (!tokenDetectionEnabled || !useExternalService) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp: Date.now(),\n };\n }\n const batchSize = options?.batchSize ?? this.#config.defaultBatchSize;\n const timestamp = Date.now();\n\n const tokensToCheck = await this.getTokensToCheck(chainId);\n\n if (tokensToCheck.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n const balanceRequests: BalanceOfRequest[] = tokensToCheck.map(\n (tokenAddress) => ({\n tokenAddress,\n accountAddress,\n }),\n );\n\n type DetectionAccumulator = {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n DetectionAccumulator\n >({\n values: balanceRequests,\n batchSize,\n initialResult: {\n detectedAssets: [],\n detectedBalances: [],\n zeroBalanceAddresses: [],\n failedAddresses: [],\n },\n eachBatch: async (workingResult, batch) => {\n const responses = await this.#multicallClient.batchBalanceOf(\n chainId,\n batch,\n );\n\n return this.#processBalanceResponses(\n responses,\n workingResult as DetectionAccumulator,\n chainId,\n accountId,\n timestamp,\n );\n },\n });\n\n return {\n chainId,\n accountId,\n accountAddress,\n ...result,\n timestamp,\n };\n }\n\n async #fetchAndCacheTokenList(chainId: ChainId): Promise<TokenListEntry[]> {\n try {\n const list = await this.#tokensApiClient.fetchTokenList(chainId);\n this.#tokenListCache.set(chainId, list);\n return list;\n } catch (error) {\n const cached = this.#tokenListCache.get(chainId);\n log('Failed to fetch token list; using stale cache', {\n chainId,\n cachedCount: cached?.length ?? 0,\n error,\n });\n return cached ?? [];\n }\n }\n\n #processBalanceResponses(\n responses: BalanceOfResponse[],\n accumulator: {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n ): {\n detectedAssets: Asset[];\n detectedBalances: AssetBalance[];\n zeroBalanceAddresses: Address[];\n failedAddresses: Address[];\n } {\n const {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n } = accumulator;\n\n for (const response of responses) {\n if (!response.success) {\n failedAddresses.push(response.tokenAddress);\n continue;\n }\n\n const balance = response.balance ?? '0';\n\n if (balance === '0' || balance === '') {\n zeroBalanceAddresses.push(response.tokenAddress);\n continue;\n }\n\n const tokenMetadata = this.#getTokenMetadata(\n chainId,\n response.tokenAddress,\n );\n\n const asset = this.#createAsset(\n chainId,\n response.tokenAddress,\n tokenMetadata,\n );\n detectedAssets.push(asset);\n\n if (tokenMetadata?.decimals === undefined) {\n continue;\n }\n\n const { decimals } = tokenMetadata;\n const formattedBalance = this.#formatBalance(balance, decimals);\n\n detectedBalances.push({\n assetId: asset.assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n decimals,\n timestamp,\n });\n }\n\n return {\n detectedAssets,\n detectedBalances,\n zeroBalanceAddresses,\n failedAddresses,\n };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n try {\n const balanceBigInt = BigInt(rawBalance);\n const divisor = BigInt(10 ** decimals);\n\n const integerPart = balanceBigInt / divisor;\n const remainder = balanceBigInt % divisor;\n const fractionalStr = remainder.toString().padStart(decimals, '0');\n const trimmedFractional = fractionalStr.replace(/0+$/u, '');\n\n if (trimmedFractional === '') {\n return integerPart.toString();\n }\n\n return `${integerPart}.${trimmedFractional}`;\n } catch {\n return rawBalance;\n }\n }\n\n #getTokenMetadata(\n chainId: ChainId,\n tokenAddress: Address,\n ): TokenListEntry | undefined {\n const list = this.#tokenListCache.get(chainId) ?? [];\n const lowerAddress = tokenAddress.toLowerCase();\n\n const exact = list.find((entry) => entry.address === tokenAddress);\n if (exact) {\n return exact;\n }\n\n return list.find((entry) => entry.address.toLowerCase() === lowerAddress);\n }\n\n #createAsset(\n chainId: ChainId,\n tokenAddress: Address,\n metadata: TokenListEntry | undefined,\n ): Asset {\n const chainIdDecimal = parseInt(chainId, 16);\n\n const assetId =\n `eip155:${chainIdDecimal}/erc20:${tokenAddress.toLowerCase()}` as CaipAssetType;\n\n return {\n assetId,\n chainId,\n address: tokenAddress,\n type: 'erc20',\n symbol: metadata?.symbol,\n name: metadata?.name,\n decimals: metadata?.decimals,\n image: metadata?.iconUrl,\n isNative: false,\n aggregators: metadata?.aggregators,\n };\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/index.ts"],"names":[],"mappings":";;;AAAA,qDAMyB;AALvB,8GAAA,aAAa,OAAA;AAMf,uDAM0B;AALxB,gHAAA,cAAc,OAAA;AAMhB,mEASgC;AAR9B,4HAAA,oBAAoB,OAAA;AACpB,mIAAA,2BAA2B,OAAA;AAC3B,iIAAA,yBAAyB,OAAA;AACzB,gIAAA,wBAAwB,OAAA","sourcesContent":["export {\n TokenDetector,\n type TokenDetectorConfig,\n type TokenDetectorMessenger,\n type DetectionPollingInput,\n type OnDetectionUpdateCallback,\n} from './TokenDetector';\nexport {\n BalanceFetcher,\n type BalanceFetcherConfig,\n type BalanceFetcherMessenger,\n type BalancePollingInput,\n type OnBalanceUpdateCallback,\n} from './BalanceFetcher';\nexport {\n StakedBalanceFetcher,\n getSupportedStakingChainIds,\n getStakingContractAddress,\n isStakingContractAssetId,\n type StakedBalanceFetcherConfig,\n type StakedBalancePollingInput,\n type StakedBalanceFetchResult,\n type OnStakedBalanceUpdateCallback,\n} from './StakedBalanceFetcher';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/index.ts"],"names":[],"mappings":";;;AAAA,qDAKyB;AAJvB,8GAAA,aAAa,OAAA;AAKf,uDAM0B;AALxB,gHAAA,cAAc,OAAA;AAMhB,mEASgC;AAR9B,4HAAA,oBAAoB,OAAA;AACpB,mIAAA,2BAA2B,OAAA;AAC3B,iIAAA,yBAAyB,OAAA;AACzB,gIAAA,wBAAwB,OAAA","sourcesContent":["export {\n TokenDetector,\n type TokenDetectorConfig,\n type DetectionPollingInput,\n type OnDetectionUpdateCallback,\n} from './TokenDetector';\nexport {\n BalanceFetcher,\n type BalanceFetcherConfig,\n type BalanceFetcherMessenger,\n type BalancePollingInput,\n type OnBalanceUpdateCallback,\n} from './BalanceFetcher';\nexport {\n StakedBalanceFetcher,\n getSupportedStakingChainIds,\n getStakingContractAddress,\n isStakingContractAssetId,\n type StakedBalanceFetcherConfig,\n type StakedBalancePollingInput,\n type StakedBalanceFetchResult,\n type OnStakedBalanceUpdateCallback,\n} from './StakedBalanceFetcher';\n"]}