@metamask-previews/assets-controller 3.1.0-preview-1189e6b40 → 3.1.0-preview-2d89a26
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/dist/data-sources/RpcDataSource.cjs +22 -18
- package/dist/data-sources/RpcDataSource.cjs.map +1 -1
- package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
- package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
- package/dist/data-sources/RpcDataSource.mjs +22 -18
- package/dist/data-sources/RpcDataSource.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.cjs +6 -3
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.mjs +4 -1
- package/dist/data-sources/evm-rpc-services/clients/MulticallClient.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.cts +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.mts +1 -1
- package/dist/data-sources/evm-rpc-services/index.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/index.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.cjs +76 -87
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.d.cts +8 -20
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.d.mts +8 -20
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.mjs +74 -85
- package/dist/data-sources/evm-rpc-services/services/BalanceFetcher.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.d.cts +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.d.mts +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/index.mjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.cjs.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.d.cts +17 -9
- package/dist/data-sources/evm-rpc-services/types/services.d.cts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.d.mts +17 -9
- package/dist/data-sources/evm-rpc-services/types/services.d.mts.map +1 -1
- package/dist/data-sources/evm-rpc-services/types/services.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/utils/constants.cjs +0 -5
- package/dist/utils/constants.cjs.map +0 -1
- package/dist/utils/constants.d.cts +0 -2
- package/dist/utils/constants.d.cts.map +0 -1
- package/dist/utils/constants.d.mts +0 -2
- package/dist/utils/constants.d.mts.map +0 -1
- package/dist/utils/constants.mjs +0 -2
- package/dist/utils/constants.mjs.map +0 -1
|
@@ -9,20 +9,14 @@ 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 _BalanceFetcher_instances, _BalanceFetcher_multicallClient, _BalanceFetcher_messenger, _BalanceFetcher_config, _BalanceFetcher_onBalanceUpdate,
|
|
12
|
+
var _BalanceFetcher_instances, _BalanceFetcher_multicallClient, _BalanceFetcher_messenger, _BalanceFetcher_config, _BalanceFetcher_onBalanceUpdate, _BalanceFetcher_processBalanceResponses, _BalanceFetcher_formatBalance;
|
|
13
13
|
import { StaticIntervalPollingControllerOnly } from "@metamask/polling-controller";
|
|
14
|
-
import { parseCaipAssetType } from "@metamask/utils";
|
|
15
|
-
import { ZERO_ADDRESS } from "../../../utils/constants.mjs";
|
|
16
14
|
import { reduceInBatchesSerially } from "../utils/index.mjs";
|
|
17
15
|
const DEFAULT_BALANCE_INTERVAL = 30000; // 30 seconds
|
|
16
|
+
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
18
17
|
/**
|
|
19
18
|
* BalanceFetcher - Fetches token balances via multicall.
|
|
20
19
|
* Extends StaticIntervalPollingControllerOnly for built-in polling support.
|
|
21
|
-
*
|
|
22
|
-
* Callers provide CAIP-19 asset IDs; the fetcher extracts on-chain addresses
|
|
23
|
-
* (or uses the zero address for native assets) and maps multicall responses
|
|
24
|
-
* back to the original asset IDs. This ensures the returned balance entries
|
|
25
|
-
* always carry the correct identifier regardless of chain.
|
|
26
20
|
*/
|
|
27
21
|
export class BalanceFetcher extends StaticIntervalPollingControllerOnly() {
|
|
28
22
|
constructor(multicallClient, messenger, config) {
|
|
@@ -37,6 +31,7 @@ export class BalanceFetcher extends StaticIntervalPollingControllerOnly() {
|
|
|
37
31
|
__classPrivateFieldSet(this, _BalanceFetcher_config, {
|
|
38
32
|
defaultBatchSize: config?.defaultBatchSize ?? 300,
|
|
39
33
|
defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,
|
|
34
|
+
includeNativeByDefault: config?.includeNativeByDefault ?? true,
|
|
40
35
|
}, "f");
|
|
41
36
|
// Set the polling interval
|
|
42
37
|
this.setIntervalLength(config?.pollingInterval ?? DEFAULT_BALANCE_INTERVAL);
|
|
@@ -56,36 +51,69 @@ export class BalanceFetcher extends StaticIntervalPollingControllerOnly() {
|
|
|
56
51
|
* @param input - The polling input.
|
|
57
52
|
*/
|
|
58
53
|
async _executePoll(input) {
|
|
59
|
-
const result = await
|
|
54
|
+
const result = await this.fetchBalances(input.chainId, input.accountId, input.accountAddress);
|
|
60
55
|
if (__classPrivateFieldGet(this, _BalanceFetcher_onBalanceUpdate, "f") && result.balances.length > 0) {
|
|
61
56
|
await __classPrivateFieldGet(this, _BalanceFetcher_onBalanceUpdate, "f").call(this, result);
|
|
62
57
|
}
|
|
63
58
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
59
|
+
getTokensToFetch(chainId, accountId) {
|
|
60
|
+
const state = __classPrivateFieldGet(this, _BalanceFetcher_messenger, "f").call('AssetsController:getState');
|
|
61
|
+
if (!state?.assetsBalance) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const accountBalances = state.assetsBalance[accountId];
|
|
65
|
+
if (!accountBalances) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
// Convert hex chainId to decimal for CAIP-2 matching
|
|
69
|
+
const chainIdDecimal = parseInt(chainId, 16);
|
|
70
|
+
const caipChainPrefix = `eip155:${chainIdDecimal}/`;
|
|
71
|
+
const tokenMap = new Map();
|
|
72
|
+
for (const assetId of Object.keys(accountBalances)) {
|
|
73
|
+
// Only process ERC20 tokens on the current chain
|
|
74
|
+
if (assetId.startsWith(caipChainPrefix) && assetId.includes('/erc20:')) {
|
|
75
|
+
// Parse token address from CAIP-19: eip155:1/erc20:0x...
|
|
76
|
+
const tokenAddress = assetId.split('/erc20:')[1];
|
|
77
|
+
if (tokenAddress) {
|
|
78
|
+
const lowerAddress = tokenAddress.toLowerCase();
|
|
79
|
+
if (!tokenMap.has(lowerAddress)) {
|
|
80
|
+
tokenMap.set(lowerAddress, {
|
|
81
|
+
address: tokenAddress,
|
|
82
|
+
symbol: '',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return Array.from(tokenMap.values());
|
|
89
|
+
}
|
|
90
|
+
async fetchBalances(chainId, accountId, accountAddress, options) {
|
|
91
|
+
const tokens = this.getTokensToFetch(chainId, accountId);
|
|
92
|
+
const tokenAddresses = tokens.map((token) => token.address);
|
|
93
|
+
return this.fetchBalancesForTokens(chainId, accountId, accountAddress, tokenAddresses, options, tokens);
|
|
94
|
+
}
|
|
95
|
+
async fetchBalancesForTokens(chainId, accountId, accountAddress, tokenAddresses, options, tokenInfos) {
|
|
96
|
+
const batchSize = options?.batchSize ?? __classPrivateFieldGet(this, _BalanceFetcher_config, "f").defaultBatchSize;
|
|
97
|
+
const includeNative = options?.includeNative ?? __classPrivateFieldGet(this, _BalanceFetcher_config, "f").includeNativeByDefault;
|
|
77
98
|
const timestamp = Date.now();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
for (const entry of assets) {
|
|
83
|
-
const lowerAddress = entry.address.toLowerCase();
|
|
84
|
-
if (entryByAddress.has(lowerAddress)) {
|
|
85
|
-
continue; // deduplicate
|
|
99
|
+
const tokenInfoMap = new Map();
|
|
100
|
+
if (tokenInfos) {
|
|
101
|
+
for (const info of tokenInfos) {
|
|
102
|
+
tokenInfoMap.set(info.address.toLowerCase(), info);
|
|
86
103
|
}
|
|
87
|
-
|
|
88
|
-
|
|
104
|
+
}
|
|
105
|
+
const balanceRequests = [];
|
|
106
|
+
if (includeNative) {
|
|
107
|
+
balanceRequests.push({
|
|
108
|
+
tokenAddress: ZERO_ADDRESS,
|
|
109
|
+
accountAddress,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
for (const tokenAddress of tokenAddresses) {
|
|
113
|
+
balanceRequests.push({
|
|
114
|
+
tokenAddress,
|
|
115
|
+
accountAddress,
|
|
116
|
+
});
|
|
89
117
|
}
|
|
90
118
|
if (balanceRequests.length === 0) {
|
|
91
119
|
return {
|
|
@@ -99,14 +127,14 @@ export class BalanceFetcher extends StaticIntervalPollingControllerOnly() {
|
|
|
99
127
|
}
|
|
100
128
|
const result = await reduceInBatchesSerially({
|
|
101
129
|
values: balanceRequests,
|
|
102
|
-
batchSize
|
|
130
|
+
batchSize,
|
|
103
131
|
initialResult: {
|
|
104
132
|
balances: [],
|
|
105
133
|
failedAddresses: [],
|
|
106
134
|
},
|
|
107
135
|
eachBatch: async (workingResult, batch) => {
|
|
108
136
|
const responses = await __classPrivateFieldGet(this, _BalanceFetcher_multicallClient, "f").batchBalanceOf(chainId, batch);
|
|
109
|
-
return __classPrivateFieldGet(this, _BalanceFetcher_instances, "m", _BalanceFetcher_processBalanceResponses).call(this, responses, workingResult, chainId, accountId, timestamp,
|
|
137
|
+
return __classPrivateFieldGet(this, _BalanceFetcher_instances, "m", _BalanceFetcher_processBalanceResponses).call(this, responses, workingResult, chainId, accountId, timestamp, tokenInfoMap);
|
|
110
138
|
},
|
|
111
139
|
});
|
|
112
140
|
return {
|
|
@@ -118,76 +146,37 @@ export class BalanceFetcher extends StaticIntervalPollingControllerOnly() {
|
|
|
118
146
|
};
|
|
119
147
|
}
|
|
120
148
|
}
|
|
121
|
-
_BalanceFetcher_multicallClient = new WeakMap(), _BalanceFetcher_messenger = new WeakMap(), _BalanceFetcher_config = new WeakMap(), _BalanceFetcher_onBalanceUpdate = new WeakMap(), _BalanceFetcher_instances = new WeakSet(),
|
|
122
|
-
const state = __classPrivateFieldGet(this, _BalanceFetcher_messenger, "f").call('AssetsController:getState');
|
|
123
|
-
const accountBalances = state?.assetsBalance?.[accountId];
|
|
124
|
-
if (!accountBalances) {
|
|
125
|
-
return [];
|
|
126
|
-
}
|
|
127
|
-
// Convert hex chainId to decimal for CAIP-2 matching
|
|
128
|
-
// This is safe because we are filtring with an accountId that is for evm balances only
|
|
129
|
-
const chainIdDecimal = parseInt(chainId, 16).toString();
|
|
130
|
-
const assetsToFetch = new Map();
|
|
131
|
-
for (const assetId of Object.keys(accountBalances)) {
|
|
132
|
-
const { chain: { reference: chainReference }, assetNamespace, assetReference, } = parseCaipAssetType(assetId);
|
|
133
|
-
if (chainReference === chainIdDecimal) {
|
|
134
|
-
const assetIdLowerCase = assetId.toLowerCase();
|
|
135
|
-
if (assetsToFetch.has(assetIdLowerCase)) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
const isNative = assetNamespace === 'slip44';
|
|
139
|
-
const tokenAddress = isNative
|
|
140
|
-
? ZERO_ADDRESS
|
|
141
|
-
: assetReference.toLowerCase();
|
|
142
|
-
assetsToFetch.set(assetIdLowerCase, {
|
|
143
|
-
assetId,
|
|
144
|
-
address: tokenAddress,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return Array.from(assetsToFetch.values());
|
|
149
|
-
}, _BalanceFetcher_fetchBalances =
|
|
150
|
-
/**
|
|
151
|
-
* Fetch balances for assets already tracked in state for the given
|
|
152
|
-
* account and chain.
|
|
153
|
-
*
|
|
154
|
-
* @param chainId - Hex chain ID.
|
|
155
|
-
* @param accountId - Account UUID.
|
|
156
|
-
* @param accountAddress - On-chain address of the account.
|
|
157
|
-
* @returns Balance fetch result.
|
|
158
|
-
*/
|
|
159
|
-
async function _BalanceFetcher_fetchBalances(chainId, accountId, accountAddress) {
|
|
160
|
-
const assets = __classPrivateFieldGet(this, _BalanceFetcher_instances, "m", _BalanceFetcher_getAssetsToFetch).call(this, chainId, accountId);
|
|
161
|
-
return this.fetchBalancesForAssets(chainId, accountId, accountAddress, assets);
|
|
162
|
-
}, _BalanceFetcher_processBalanceResponses = function _BalanceFetcher_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp, entryByAddress) {
|
|
149
|
+
_BalanceFetcher_multicallClient = new WeakMap(), _BalanceFetcher_messenger = new WeakMap(), _BalanceFetcher_config = new WeakMap(), _BalanceFetcher_onBalanceUpdate = new WeakMap(), _BalanceFetcher_instances = new WeakSet(), _BalanceFetcher_processBalanceResponses = function _BalanceFetcher_processBalanceResponses(responses, accumulator, chainId, accountId, timestamp, tokenInfoMap) {
|
|
163
150
|
const { balances, failedAddresses } = accumulator;
|
|
164
151
|
for (const response of responses) {
|
|
165
152
|
if (!response.success) {
|
|
166
153
|
failedAddresses.push(response.tokenAddress);
|
|
167
154
|
continue;
|
|
168
155
|
}
|
|
169
|
-
const lowerAddress = response.tokenAddress.toLowerCase();
|
|
170
|
-
const entry = entryByAddress.get(lowerAddress);
|
|
171
|
-
if (!entry) {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
156
|
const balance = response.balance ?? '0';
|
|
175
|
-
const
|
|
157
|
+
const chainIdDecimal = parseInt(chainId, 16);
|
|
158
|
+
const isNative = response.tokenAddress.toLowerCase() === ZERO_ADDRESS.toLowerCase();
|
|
159
|
+
const tokenInfo = tokenInfoMap.get(response.tokenAddress.toLowerCase());
|
|
160
|
+
// Native uses 18. ERC-20: format when decimals are known; otherwise pass raw
|
|
161
|
+
// `balance` through so RpcDataSource can resolve decimals (state → list → RPC).
|
|
176
162
|
let decimals;
|
|
177
163
|
let formattedBalance;
|
|
178
164
|
if (isNative) {
|
|
179
165
|
decimals = 18;
|
|
180
166
|
formattedBalance = __classPrivateFieldGet(this, _BalanceFetcher_instances, "m", _BalanceFetcher_formatBalance).call(this, balance, decimals);
|
|
181
167
|
}
|
|
182
|
-
else if (
|
|
168
|
+
else if (tokenInfo?.decimals === undefined) {
|
|
183
169
|
formattedBalance = balance;
|
|
184
170
|
}
|
|
185
171
|
else {
|
|
186
|
-
decimals =
|
|
172
|
+
decimals = tokenInfo.decimals;
|
|
187
173
|
formattedBalance = __classPrivateFieldGet(this, _BalanceFetcher_instances, "m", _BalanceFetcher_formatBalance).call(this, balance, decimals);
|
|
188
174
|
}
|
|
175
|
+
const assetId = isNative
|
|
176
|
+
? `eip155:${chainIdDecimal}/slip44:60`
|
|
177
|
+
: `eip155:${chainIdDecimal}/erc20:${response.tokenAddress.toLowerCase()}`;
|
|
189
178
|
const balanceEntry = {
|
|
190
|
-
assetId
|
|
179
|
+
assetId,
|
|
191
180
|
accountId,
|
|
192
181
|
chainId,
|
|
193
182
|
balance,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BalanceFetcher.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/BalanceFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mCAAmC,EAAE,qCAAqC;AACnF,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,YAAY,EAAE,qCAAiC;AAcxD,OAAO,EAAE,uBAAuB,EAAE,2BAAiB;AAEnD,MAAM,wBAAwB,GAAG,KAAM,CAAC,CAAC,aAAa;AAmCtD;;;;;;;;GAQG;AACH,MAAM,OAAO,cAAe,SAAQ,mCAAmC,EAAuB;IAS5F,YACE,eAAgC,EAChC,SAAkC,EAClC,MAA6B;QAE7B,KAAK,EAAE,CAAC;;QAbD,kDAAkC;QAElC,4CAAoC;QAEpC,yCAAiE;QAE1E,kDAAsD;QAQpD,uBAAA,IAAI,mCAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,6BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,0BAAW;YACb,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,GAAG;YACjD,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,KAAK;SACpD,MAAA,CAAC;QAEF,2BAA2B;QAC3B,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,eAAe,IAAI,wBAAwB,CAAC,CAAC;IAC9E,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,QAAiC;QAClD,uBAAA,IAAI,mCAAoB,QAAQ,MAAA,CAAC;IACnC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,YAAY,CAAC,KAA0B;QAC3C,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,gEAAe,MAAnB,IAAI,EACvB,KAAK,CAAC,OAAO,EACb,KAAK,CAAC,SAAS,E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AS,GAAG,aAAa,GAAG,OAAO,CAAC;QAC1C,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QACnE,MAAM,iBAAiB,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAE5D,IAAI,iBAAiB,KAAK,EAAE,EAAE,CAAC;YAC7B,OAAO,WAAW,CAAC,QAAQ,EAAE,CAAC;QAChC,CAAC;QAED,OAAO,GAAG,WAAW,IAAI,iBAAiB,EAAE,CAAC;IAC/C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,UAAU,CAAC;IACpB,CAAC;AACH,CAAC","sourcesContent":["import { StaticIntervalPollingControllerOnly } from '@metamask/polling-controller';\nimport { parseCaipAssetType } from '@metamask/utils';\n\nimport { ZERO_ADDRESS } from '../../../utils/constants';\nimport type { MulticallClient } from '../clients';\nimport type {\n AccountId,\n Address,\n AssetBalance,\n AssetFetchEntry,\n AssetsBalanceState,\n BalanceFetchResult,\n BalanceOfRequest,\n BalanceOfResponse,\n CaipAssetType,\n ChainId,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst DEFAULT_BALANCE_INTERVAL = 30_000; // 30 seconds\n\n/**\n * Minimal messenger interface for BalanceFetcher.\n */\nexport type BalanceFetcherMessenger = {\n call: (action: 'AssetsController:getState') => AssetsBalanceState;\n};\n\nexport type BalanceFetcherConfig = {\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n /** Polling interval in ms (default: 30s) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for BalanceFetcher - identifies what to poll for.\n */\nexport type BalancePollingInput = {\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 balance updates.\n */\nexport type OnBalanceUpdateCallback = (\n result: BalanceFetchResult,\n) => void | Promise<void>;\n\n/**\n * BalanceFetcher - Fetches token balances via multicall.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n *\n * Callers provide CAIP-19 asset IDs; the fetcher extracts on-chain addresses\n * (or uses the zero address for native assets) and maps multicall responses\n * back to the original asset IDs. This ensures the returned balance entries\n * always carry the correct identifier regardless of chain.\n */\nexport class BalanceFetcher extends StaticIntervalPollingControllerOnly<BalancePollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #messenger: BalanceFetcherMessenger;\n\n readonly #config: Required<Omit<BalanceFetcherConfig, 'pollingInterval'>>;\n\n #onBalanceUpdate: OnBalanceUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n messenger: BalanceFetcherMessenger,\n config?: BalanceFetcherConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#messenger = messenger;\n this.#config = {\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n };\n\n // Set the polling interval\n this.setIntervalLength(config?.pollingInterval ?? DEFAULT_BALANCE_INTERVAL);\n }\n\n /**\n * Set the callback to receive balance updates during polling.\n *\n * @param callback - Function to call with balance results.\n */\n setOnBalanceUpdate(callback: OnBalanceUpdateCallback): void {\n this.#onBalanceUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Fetches balances and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: BalancePollingInput): Promise<void> {\n const result = await this.#fetchBalances(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onBalanceUpdate && result.balances.length > 0) {\n await this.#onBalanceUpdate(result);\n }\n }\n\n /**\n * Return asset fetch entries tracked in state for the given account and\n * chain. Both native (`slip44:`) and ERC-20 (`erc20:`) entries are included.\n *\n * @param chainId - Hex chain ID (e.g. \"0x1\").\n * @param accountId - Account UUID.\n * @returns Array of asset fetch entries from state for the requested chain.\n */\n #getAssetsToFetch(chainId: ChainId, accountId: AccountId): AssetFetchEntry[] {\n const state = this.#messenger.call('AssetsController:getState');\n\n const accountBalances = state?.assetsBalance?.[accountId];\n if (!accountBalances) {\n return [];\n }\n\n // Convert hex chainId to decimal for CAIP-2 matching\n // This is safe because we are filtring with an accountId that is for evm balances only\n const chainIdDecimal = parseInt(chainId, 16).toString();\n\n const assetsToFetch = new Map<string, AssetFetchEntry>();\n\n for (const assetId of Object.keys(accountBalances) as CaipAssetType[]) {\n const {\n chain: { reference: chainReference },\n assetNamespace,\n assetReference,\n } = parseCaipAssetType(assetId);\n\n if (chainReference === chainIdDecimal) {\n const assetIdLowerCase = assetId.toLowerCase();\n if (assetsToFetch.has(assetIdLowerCase)) {\n continue;\n }\n\n const isNative = assetNamespace === 'slip44';\n const tokenAddress = isNative\n ? ZERO_ADDRESS\n : (assetReference.toLowerCase() as Address);\n\n assetsToFetch.set(assetIdLowerCase, {\n assetId,\n address: tokenAddress,\n });\n }\n }\n\n return Array.from(assetsToFetch.values());\n }\n\n /**\n * Fetch balances for assets already tracked in state for the given\n * account and chain.\n *\n * @param chainId - Hex chain ID.\n * @param accountId - Account UUID.\n * @param accountAddress - On-chain address of the account.\n * @returns Balance fetch result.\n */\n async #fetchBalances(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n ): Promise<BalanceFetchResult> {\n const assets = this.#getAssetsToFetch(chainId, accountId);\n\n return this.fetchBalancesForAssets(\n chainId,\n accountId,\n accountAddress,\n assets,\n );\n }\n\n /**\n * Fetch balances for the given assets via multicall.\n *\n * Each entry bundles a CAIP-19 asset ID with its on-chain address and\n * optional decimals.\n *\n * @param chainId - Hex chain ID.\n * @param accountId - Account UUID.\n * @param accountAddress - On-chain address of the account.\n * @param assets - Asset fetch entries to fetch balances for.\n * @returns Balance fetch result.\n */\n async fetchBalancesForAssets(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n assets: AssetFetchEntry[],\n ): Promise<BalanceFetchResult> {\n const timestamp = Date.now();\n\n // Build a single map keyed by lowercase address that holds all info\n // needed to match multicall responses back to their original entries.\n const balanceRequests: BalanceOfRequest[] = [];\n const entryByAddress = new Map<string, AssetFetchEntry>();\n\n for (const entry of assets) {\n const lowerAddress = entry.address.toLowerCase();\n if (entryByAddress.has(lowerAddress)) {\n continue; // deduplicate\n }\n\n entryByAddress.set(lowerAddress, entry);\n balanceRequests.push({ tokenAddress: entry.address, accountAddress });\n }\n\n if (balanceRequests.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n balances: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n type FetchAccumulator = {\n balances: AssetBalance[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n FetchAccumulator\n >({\n values: balanceRequests,\n batchSize: this.#config.defaultBatchSize,\n initialResult: {\n balances: [],\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 FetchAccumulator,\n chainId,\n accountId,\n timestamp,\n entryByAddress,\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 balances: AssetBalance[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n entryByAddress: Map<string, AssetFetchEntry>,\n ): {\n balances: AssetBalance[];\n failedAddresses: Address[];\n } {\n const { balances, failedAddresses } = accumulator;\n\n for (const response of responses) {\n if (!response.success) {\n failedAddresses.push(response.tokenAddress);\n continue;\n }\n\n const lowerAddress = response.tokenAddress.toLowerCase();\n const entry = entryByAddress.get(lowerAddress);\n if (!entry) {\n continue;\n }\n\n const balance = response.balance ?? '0';\n const isNative = lowerAddress === ZERO_ADDRESS.toLowerCase();\n\n let decimals: number | undefined;\n let formattedBalance: string;\n if (isNative) {\n decimals = 18;\n formattedBalance = this.#formatBalance(balance, decimals);\n } else if (entry.decimals === undefined) {\n formattedBalance = balance;\n } else {\n decimals = entry.decimals;\n formattedBalance = this.#formatBalance(balance, decimals);\n }\n\n const balanceEntry: AssetBalance = {\n assetId: entry.assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n timestamp,\n };\n if (typeof decimals === 'number') {\n balanceEntry.decimals = decimals;\n }\n balances.push(balanceEntry);\n }\n\n return { balances, failedAddresses };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n if (rawBalance === '0') {\n return '0';\n }\n\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"]}
|
|
1
|
+
{"version":3,"file":"BalanceFetcher.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/services/BalanceFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mCAAmC,EAAE,qCAAqC;AAgBnF,OAAO,EAAE,uBAAuB,EAAE,2BAAiB;AAEnD,MAAM,wBAAwB,GAAG,KAAM,CAAC,CAAC,aAAa;AAEtD,MAAM,YAAY,GAChB,4CAAuD,CAAC;AAoC1D;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,mCAAmC,EAAuB;IAS5F,YACE,eAAgC,EAChC,SAAkC,EAClC,MAA6B;QAE7B,KAAK,EAAE,CAAC;;QAbD,kDAAkC;QAElC,4CAAoC;QAEpC,yCAAiE;QAE1E,kDAAsD;QAQpD,uBAAA,IAAI,mCAAoB,eAAe,MAAA,CAAC;QACxC,uBAAA,IAAI,6BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,0BAAW;YACb,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,I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BAAiB,KAAK,EAAE,EAAE,CAAC;YAC7B,OAAO,WAAW,CAAC,QAAQ,EAAE,CAAC;QAChC,CAAC;QAED,OAAO,GAAG,WAAW,IAAI,iBAAiB,EAAE,CAAC;IAC/C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,UAAU,CAAC;IACpB,CAAC;AACH,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 AssetBalance,\n AssetsBalanceState,\n BalanceFetchOptions,\n BalanceFetchResult,\n BalanceOfRequest,\n BalanceOfResponse,\n ChainId,\n TokenFetchInfo,\n} from '../types';\nimport { reduceInBatchesSerially } from '../utils';\n\nconst DEFAULT_BALANCE_INTERVAL = 30_000; // 30 seconds\n\nconst ZERO_ADDRESS: Address =\n '0x0000000000000000000000000000000000000000' as Address;\n\n/**\n * Minimal messenger interface for BalanceFetcher.\n */\nexport type BalanceFetcherMessenger = {\n call: (action: 'AssetsController:getState') => AssetsBalanceState;\n};\n\nexport type BalanceFetcherConfig = {\n defaultBatchSize?: number;\n defaultTimeoutMs?: number;\n includeNativeByDefault?: boolean;\n /** Polling interval in ms (default: 30s) */\n pollingInterval?: number;\n};\n\n/**\n * Polling input for BalanceFetcher - identifies what to poll for.\n */\nexport type BalancePollingInput = {\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 balance updates.\n */\nexport type OnBalanceUpdateCallback = (\n result: BalanceFetchResult,\n) => void | Promise<void>;\n\n/**\n * BalanceFetcher - Fetches token balances via multicall.\n * Extends StaticIntervalPollingControllerOnly for built-in polling support.\n */\nexport class BalanceFetcher extends StaticIntervalPollingControllerOnly<BalancePollingInput>() {\n readonly #multicallClient: MulticallClient;\n\n readonly #messenger: BalanceFetcherMessenger;\n\n readonly #config: Required<Omit<BalanceFetcherConfig, 'pollingInterval'>>;\n\n #onBalanceUpdate: OnBalanceUpdateCallback | undefined;\n\n constructor(\n multicallClient: MulticallClient,\n messenger: BalanceFetcherMessenger,\n config?: BalanceFetcherConfig,\n ) {\n super();\n this.#multicallClient = multicallClient;\n this.#messenger = messenger;\n this.#config = {\n defaultBatchSize: config?.defaultBatchSize ?? 300,\n defaultTimeoutMs: config?.defaultTimeoutMs ?? 30000,\n includeNativeByDefault: config?.includeNativeByDefault ?? true,\n };\n\n // Set the polling interval\n this.setIntervalLength(config?.pollingInterval ?? DEFAULT_BALANCE_INTERVAL);\n }\n\n /**\n * Set the callback to receive balance updates during polling.\n *\n * @param callback - Function to call with balance results.\n */\n setOnBalanceUpdate(callback: OnBalanceUpdateCallback): void {\n this.#onBalanceUpdate = callback;\n }\n\n /**\n * Execute a poll cycle (required by base class).\n * Fetches balances and calls the update callback.\n *\n * @param input - The polling input.\n */\n async _executePoll(input: BalancePollingInput): Promise<void> {\n const result = await this.fetchBalances(\n input.chainId,\n input.accountId,\n input.accountAddress,\n );\n\n if (this.#onBalanceUpdate && result.balances.length > 0) {\n await this.#onBalanceUpdate(result);\n }\n }\n\n getTokensToFetch(chainId: ChainId, accountId: AccountId): TokenFetchInfo[] {\n const state = this.#messenger.call('AssetsController:getState');\n\n if (!state?.assetsBalance) {\n return [];\n }\n\n const accountBalances = state.assetsBalance[accountId];\n if (!accountBalances) {\n return [];\n }\n\n // Convert hex chainId to decimal for CAIP-2 matching\n const chainIdDecimal = parseInt(chainId, 16);\n const caipChainPrefix = `eip155:${chainIdDecimal}/`;\n\n const tokenMap = new Map<string, TokenFetchInfo>();\n\n for (const assetId of Object.keys(accountBalances)) {\n // Only process ERC20 tokens on the current chain\n if (assetId.startsWith(caipChainPrefix) && assetId.includes('/erc20:')) {\n // Parse token address from CAIP-19: eip155:1/erc20:0x...\n const tokenAddress = assetId.split('/erc20:')[1] as Address;\n if (tokenAddress) {\n const lowerAddress = tokenAddress.toLowerCase();\n if (!tokenMap.has(lowerAddress)) {\n tokenMap.set(lowerAddress, {\n address: tokenAddress,\n symbol: '',\n });\n }\n }\n }\n }\n\n return Array.from(tokenMap.values());\n }\n\n async fetchBalances(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n options?: BalanceFetchOptions,\n ): Promise<BalanceFetchResult> {\n const tokens = this.getTokensToFetch(chainId, accountId);\n const tokenAddresses = tokens.map((token) => token.address);\n\n return this.fetchBalancesForTokens(\n chainId,\n accountId,\n accountAddress,\n tokenAddresses,\n options,\n tokens,\n );\n }\n\n async fetchBalancesForTokens(\n chainId: ChainId,\n accountId: AccountId,\n accountAddress: Address,\n tokenAddresses: Address[],\n options?: BalanceFetchOptions,\n tokenInfos?: TokenFetchInfo[],\n ): Promise<BalanceFetchResult> {\n const batchSize = options?.batchSize ?? this.#config.defaultBatchSize;\n const includeNative =\n options?.includeNative ?? this.#config.includeNativeByDefault;\n const timestamp = Date.now();\n\n const tokenInfoMap = new Map<string, TokenFetchInfo>();\n if (tokenInfos) {\n for (const info of tokenInfos) {\n tokenInfoMap.set(info.address.toLowerCase(), info);\n }\n }\n\n const balanceRequests: BalanceOfRequest[] = [];\n\n if (includeNative) {\n balanceRequests.push({\n tokenAddress: ZERO_ADDRESS,\n accountAddress,\n });\n }\n\n for (const tokenAddress of tokenAddresses) {\n balanceRequests.push({\n tokenAddress,\n accountAddress,\n });\n }\n\n if (balanceRequests.length === 0) {\n return {\n chainId,\n accountId,\n accountAddress,\n balances: [],\n failedAddresses: [],\n timestamp,\n };\n }\n\n type FetchAccumulator = {\n balances: AssetBalance[];\n failedAddresses: Address[];\n };\n\n const result = await reduceInBatchesSerially<\n BalanceOfRequest,\n FetchAccumulator\n >({\n values: balanceRequests,\n batchSize,\n initialResult: {\n balances: [],\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 FetchAccumulator,\n chainId,\n accountId,\n timestamp,\n tokenInfoMap,\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 balances: AssetBalance[];\n failedAddresses: Address[];\n },\n chainId: ChainId,\n accountId: AccountId,\n timestamp: number,\n tokenInfoMap: Map<string, TokenFetchInfo>,\n ): {\n balances: AssetBalance[];\n failedAddresses: Address[];\n } {\n const { balances, failedAddresses } = 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 const chainIdDecimal = parseInt(chainId, 16);\n const isNative =\n response.tokenAddress.toLowerCase() === ZERO_ADDRESS.toLowerCase();\n const tokenInfo = tokenInfoMap.get(response.tokenAddress.toLowerCase());\n\n // Native uses 18. ERC-20: format when decimals are known; otherwise pass raw\n // `balance` through so RpcDataSource can resolve decimals (state → list → RPC).\n let decimals: number | undefined;\n let formattedBalance: string;\n if (isNative) {\n decimals = 18;\n formattedBalance = this.#formatBalance(balance, decimals);\n } else if (tokenInfo?.decimals === undefined) {\n formattedBalance = balance;\n } else {\n decimals = tokenInfo.decimals;\n formattedBalance = this.#formatBalance(balance, decimals);\n }\n const assetId: CaipAssetType = isNative\n ? (`eip155:${chainIdDecimal}/slip44:60` as CaipAssetType)\n : (`eip155:${chainIdDecimal}/erc20:${response.tokenAddress.toLowerCase()}` as CaipAssetType);\n\n const balanceEntry: AssetBalance = {\n assetId,\n accountId,\n chainId,\n balance,\n formattedBalance,\n timestamp,\n };\n if (typeof decimals === 'number') {\n balanceEntry.decimals = decimals;\n }\n balances.push(balanceEntry);\n }\n\n return { balances, failedAddresses };\n }\n\n #formatBalance(rawBalance: string, decimals: number): string {\n if (rawBalance === '0') {\n return '0';\n }\n\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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"","sourcesContent":["// Core types\nexport type {\n AccountId,\n Address,\n AssetType,\n CaipAssetType,\n ChainId,\n} from './core';\n\n// Asset types\nexport type { Asset, AssetBalance } from './assets';\n\n// State types\nexport type {\n AssetsBalanceState,\n TokenChainsCacheEntry,\n TokenListEntry,\n TokenListState,\n} from './state';\n\n// Config types\nexport type { GetProviderFunction, Provider } from './config';\n\n// Multicall types\nexport type { BalanceOfRequest, BalanceOfResponse } from './multicall';\n\n// Service types\nexport type {\n
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"","sourcesContent":["// Core types\nexport type {\n AccountId,\n Address,\n AssetType,\n CaipAssetType,\n ChainId,\n} from './core';\n\n// Asset types\nexport type { Asset, AssetBalance } from './assets';\n\n// State types\nexport type {\n AssetsBalanceState,\n TokenChainsCacheEntry,\n TokenListEntry,\n TokenListState,\n} from './state';\n\n// Config types\nexport type { GetProviderFunction, Provider } from './config';\n\n// Multicall types\nexport type { BalanceOfRequest, BalanceOfResponse } from './multicall';\n\n// Service types\nexport type {\n BalanceFetchOptions,\n BalanceFetchResult,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenFetchInfo,\n} from './services';\n"]}
|
|
@@ -3,5 +3,5 @@ export type { Asset, AssetBalance } from "./assets.cjs";
|
|
|
3
3
|
export type { AssetsBalanceState, TokenChainsCacheEntry, TokenListEntry, TokenListState, } from "./state.cjs";
|
|
4
4
|
export type { GetProviderFunction, Provider } from "./config.cjs";
|
|
5
5
|
export type { BalanceOfRequest, BalanceOfResponse } from "./multicall.cjs";
|
|
6
|
-
export type {
|
|
6
|
+
export type { BalanceFetchOptions, BalanceFetchResult, TokenDetectionOptions, TokenDetectionResult, TokenFetchInfo, } from "./services.cjs";
|
|
7
7
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,OAAO,EACP,SAAS,EACT,aAAa,EACb,OAAO,GACR,mBAAe;AAGhB,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAGpD,YAAY,EACV,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,EACd,cAAc,GACf,oBAAgB;AAGjB,YAAY,EAAE,mBAAmB,EAAE,QAAQ,EAAE,qBAAiB;AAG9D,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,wBAAoB;AAGvE,YAAY,EACV,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,OAAO,EACP,SAAS,EACT,aAAa,EACb,OAAO,GACR,mBAAe;AAGhB,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAGpD,YAAY,EACV,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,EACd,cAAc,GACf,oBAAgB;AAGjB,YAAY,EAAE,mBAAmB,EAAE,QAAQ,EAAE,qBAAiB;AAG9D,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,wBAAoB;AAGvE,YAAY,EACV,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,cAAc,GACf,uBAAmB"}
|
|
@@ -3,5 +3,5 @@ export type { Asset, AssetBalance } from "./assets.mjs";
|
|
|
3
3
|
export type { AssetsBalanceState, TokenChainsCacheEntry, TokenListEntry, TokenListState, } from "./state.mjs";
|
|
4
4
|
export type { GetProviderFunction, Provider } from "./config.mjs";
|
|
5
5
|
export type { BalanceOfRequest, BalanceOfResponse } from "./multicall.mjs";
|
|
6
|
-
export type {
|
|
6
|
+
export type { BalanceFetchOptions, BalanceFetchResult, TokenDetectionOptions, TokenDetectionResult, TokenFetchInfo, } from "./services.mjs";
|
|
7
7
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,OAAO,EACP,SAAS,EACT,aAAa,EACb,OAAO,GACR,mBAAe;AAGhB,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAGpD,YAAY,EACV,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,EACd,cAAc,GACf,oBAAgB;AAGjB,YAAY,EAAE,mBAAmB,EAAE,QAAQ,EAAE,qBAAiB;AAG9D,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,wBAAoB;AAGvE,YAAY,EACV,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,SAAS,EACT,OAAO,EACP,SAAS,EACT,aAAa,EACb,OAAO,GACR,mBAAe;AAGhB,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAGpD,YAAY,EACV,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,EACd,cAAc,GACf,oBAAgB;AAGjB,YAAY,EAAE,mBAAmB,EAAE,QAAQ,EAAE,qBAAiB;AAG9D,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,wBAAoB;AAGvE,YAAY,EACV,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,cAAc,GACf,uBAAmB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"","sourcesContent":["// Core types\nexport type {\n AccountId,\n Address,\n AssetType,\n CaipAssetType,\n ChainId,\n} from './core';\n\n// Asset types\nexport type { Asset, AssetBalance } from './assets';\n\n// State types\nexport type {\n AssetsBalanceState,\n TokenChainsCacheEntry,\n TokenListEntry,\n TokenListState,\n} from './state';\n\n// Config types\nexport type { GetProviderFunction, Provider } from './config';\n\n// Multicall types\nexport type { BalanceOfRequest, BalanceOfResponse } from './multicall';\n\n// Service types\nexport type {\n
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/index.ts"],"names":[],"mappings":"","sourcesContent":["// Core types\nexport type {\n AccountId,\n Address,\n AssetType,\n CaipAssetType,\n ChainId,\n} from './core';\n\n// Asset types\nexport type { Asset, AssetBalance } from './assets';\n\n// State types\nexport type {\n AssetsBalanceState,\n TokenChainsCacheEntry,\n TokenListEntry,\n TokenListState,\n} from './state';\n\n// Config types\nexport type { GetProviderFunction, Provider } from './config';\n\n// Multicall types\nexport type { BalanceOfRequest, BalanceOfResponse } from './multicall';\n\n// Service types\nexport type {\n BalanceFetchOptions,\n BalanceFetchResult,\n TokenDetectionOptions,\n TokenDetectionResult,\n TokenFetchInfo,\n} from './services';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"","sourcesContent":["import type { Asset, AssetBalance } from './assets';\nimport type { AccountId, Address,
|
|
1
|
+
{"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"","sourcesContent":["import type { Asset, AssetBalance } from './assets';\nimport type { AccountId, Address, ChainId } from './core';\n\n/**\n * Token detection result.\n */\nexport type TokenDetectionResult = {\n /** Chain ID */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address used for detection */\n accountAddress: Address;\n /** Newly detected tokens with non-zero balances */\n detectedAssets: Asset[];\n /** Balances for newly detected tokens */\n detectedBalances: AssetBalance[];\n /** Tokens that were checked but had zero balance */\n zeroBalanceAddresses: Address[];\n /** Tokens that failed to be checked */\n failedAddresses: Address[];\n /** Timestamp of detection */\n timestamp: number;\n};\n\n/**\n * Token detection options.\n */\nexport type TokenDetectionOptions = {\n /** Whether token detection is enabled */\n tokenDetectionEnabled?: boolean;\n /** Whether external services (e.g. token list API) are allowed; detection stops when false */\n useExternalService?: boolean;\n /** Maximum number of tokens to check per batch */\n batchSize?: number;\n /** Timeout for detection in milliseconds */\n timeout?: number;\n};\n\n/**\n * Balance fetch result.\n */\nexport type BalanceFetchResult = {\n /** Chain ID */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n /** Fetched balances */\n balances: AssetBalance[];\n /** Token addresses that failed to fetch */\n failedAddresses: Address[];\n /** Block number when balances were fetched */\n blockNumber?: number;\n /** Timestamp of fetch */\n timestamp: number;\n};\n\n/**\n * Balance fetch options.\n */\nexport type BalanceFetchOptions = {\n /** Maximum number of tokens to fetch per batch */\n batchSize?: number;\n /** Timeout for fetch in milliseconds */\n timeout?: number;\n /** Include native token balance */\n includeNative?: boolean;\n};\n\n/**\n * Token info for balance fetching.\n */\nexport type TokenFetchInfo = {\n /** Token contract address */\n address: Address;\n /** Token decimals (omit when unknown — balance fetcher returns raw balance for RpcDataSource to resolve). */\n decimals?: number;\n /** Token symbol (optional) */\n symbol?: string;\n};\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Asset, AssetBalance } from "./assets.cjs";
|
|
2
|
-
import type { AccountId, Address,
|
|
2
|
+
import type { AccountId, Address, ChainId } from "./core.cjs";
|
|
3
3
|
/**
|
|
4
4
|
* Token detection result.
|
|
5
5
|
*/
|
|
@@ -54,17 +54,25 @@ export type BalanceFetchResult = {
|
|
|
54
54
|
timestamp: number;
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
* Bundles the CAIP-19 asset ID with the on-chain address (zero address for
|
|
59
|
-
* native assets even when the chain has a native asset with a non-zero address)
|
|
60
|
-
* and optional metadata
|
|
57
|
+
* Balance fetch options.
|
|
61
58
|
*/
|
|
62
|
-
export type
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
/**
|
|
59
|
+
export type BalanceFetchOptions = {
|
|
60
|
+
/** Maximum number of tokens to fetch per batch */
|
|
61
|
+
batchSize?: number;
|
|
62
|
+
/** Timeout for fetch in milliseconds */
|
|
63
|
+
timeout?: number;
|
|
64
|
+
/** Include native token balance */
|
|
65
|
+
includeNative?: boolean;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Token info for balance fetching.
|
|
69
|
+
*/
|
|
70
|
+
export type TokenFetchInfo = {
|
|
71
|
+
/** Token contract address */
|
|
66
72
|
address: Address;
|
|
67
73
|
/** Token decimals (omit when unknown — balance fetcher returns raw balance for RpcDataSource to resolve). */
|
|
68
74
|
decimals?: number;
|
|
75
|
+
/** Token symbol (optional) */
|
|
76
|
+
symbol?: string;
|
|
69
77
|
};
|
|
70
78
|
//# sourceMappingURL=services.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"AAAA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H,MAAM,MAAM,mBAAmB,GAAG;IAChC,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mCAAmC;IACnC,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,6BAA6B;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,6GAA6G;IAC7G,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Asset, AssetBalance } from "./assets.mjs";
|
|
2
|
-
import type { AccountId, Address,
|
|
2
|
+
import type { AccountId, Address, ChainId } from "./core.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Token detection result.
|
|
5
5
|
*/
|
|
@@ -54,17 +54,25 @@ export type BalanceFetchResult = {
|
|
|
54
54
|
timestamp: number;
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
* Bundles the CAIP-19 asset ID with the on-chain address (zero address for
|
|
59
|
-
* native assets even when the chain has a native asset with a non-zero address)
|
|
60
|
-
* and optional metadata
|
|
57
|
+
* Balance fetch options.
|
|
61
58
|
*/
|
|
62
|
-
export type
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
/**
|
|
59
|
+
export type BalanceFetchOptions = {
|
|
60
|
+
/** Maximum number of tokens to fetch per batch */
|
|
61
|
+
batchSize?: number;
|
|
62
|
+
/** Timeout for fetch in milliseconds */
|
|
63
|
+
timeout?: number;
|
|
64
|
+
/** Include native token balance */
|
|
65
|
+
includeNative?: boolean;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Token info for balance fetching.
|
|
69
|
+
*/
|
|
70
|
+
export type TokenFetchInfo = {
|
|
71
|
+
/** Token contract address */
|
|
66
72
|
address: Address;
|
|
67
73
|
/** Token decimals (omit when unknown — balance fetcher returns raw balance for RpcDataSource to resolve). */
|
|
68
74
|
decimals?: number;
|
|
75
|
+
/** Token symbol (optional) */
|
|
76
|
+
symbol?: string;
|
|
69
77
|
};
|
|
70
78
|
//# sourceMappingURL=services.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,qBAAiB;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,mBAAe;AAE1D;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,eAAe;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,yCAAyC;IACzC,cAAc,EAAE,OAAO,CAAC;IACxB,mDAAmD;IACnD,cAAc,EAAE,KAAK,EAAE,CAAC;IACxB,yCAAyC;IACzC,gBAAgB,EAAE,YAAY,EAAE,CAAC;IACjC,oDAAoD;IACpD,oBAAoB,EAAE,OAAO,EAAE,CAAC;IAChC,uCAAuC;IACvC,eAAe,EAAE,OAAO,EAAE,CAAC;IAC3B,6BAA6B;IAC7B,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,yCAAyC;IACzC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8FAA8F;IAC9F,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4CAA4C;IAC5C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,eAAe;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,iBAAiB;IACjB,SAAS,EAAE,SAAS,CAAC;IACrB,sBAAsB;IACtB,cAAc,EAAE,OAAO,CAAC;IACxB,uBAAuB;IACvB,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,2CAA2C;IAC3C,eAAe,EAAE,OAAO,EAAE,CAAC;IAC3B,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,yBAAyB;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,kDAAkD;IAClD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mCAAmC;IACnC,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,6BAA6B;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,6GAA6G;IAC7G,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"","sourcesContent":["import type { Asset, AssetBalance } from './assets';\nimport type { AccountId, Address,
|
|
1
|
+
{"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../../src/data-sources/evm-rpc-services/types/services.ts"],"names":[],"mappings":"","sourcesContent":["import type { Asset, AssetBalance } from './assets';\nimport type { AccountId, Address, ChainId } from './core';\n\n/**\n * Token detection result.\n */\nexport type TokenDetectionResult = {\n /** Chain ID */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address used for detection */\n accountAddress: Address;\n /** Newly detected tokens with non-zero balances */\n detectedAssets: Asset[];\n /** Balances for newly detected tokens */\n detectedBalances: AssetBalance[];\n /** Tokens that were checked but had zero balance */\n zeroBalanceAddresses: Address[];\n /** Tokens that failed to be checked */\n failedAddresses: Address[];\n /** Timestamp of detection */\n timestamp: number;\n};\n\n/**\n * Token detection options.\n */\nexport type TokenDetectionOptions = {\n /** Whether token detection is enabled */\n tokenDetectionEnabled?: boolean;\n /** Whether external services (e.g. token list API) are allowed; detection stops when false */\n useExternalService?: boolean;\n /** Maximum number of tokens to check per batch */\n batchSize?: number;\n /** Timeout for detection in milliseconds */\n timeout?: number;\n};\n\n/**\n * Balance fetch result.\n */\nexport type BalanceFetchResult = {\n /** Chain ID */\n chainId: ChainId;\n /** Account ID */\n accountId: AccountId;\n /** Account address */\n accountAddress: Address;\n /** Fetched balances */\n balances: AssetBalance[];\n /** Token addresses that failed to fetch */\n failedAddresses: Address[];\n /** Block number when balances were fetched */\n blockNumber?: number;\n /** Timestamp of fetch */\n timestamp: number;\n};\n\n/**\n * Balance fetch options.\n */\nexport type BalanceFetchOptions = {\n /** Maximum number of tokens to fetch per batch */\n batchSize?: number;\n /** Timeout for fetch in milliseconds */\n timeout?: number;\n /** Include native token balance */\n includeNative?: boolean;\n};\n\n/**\n * Token info for balance fetching.\n */\nexport type TokenFetchInfo = {\n /** Token contract address */\n address: Address;\n /** Token decimals (omit when unknown — balance fetcher returns raw balance for RpcDataSource to resolve). */\n decimals?: number;\n /** Token symbol (optional) */\n symbol?: string;\n};\n"]}
|
package/package.json
CHANGED
package/dist/utils/constants.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GACvB,4CAAqD,CAAC","sourcesContent":["export const ZERO_ADDRESS =\n '0x0000000000000000000000000000000000000000' as const;\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,8CAC8B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,8CAC8B,CAAC"}
|
package/dist/utils/constants.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,YAAY,GACvB,4CAAqD,CAAC","sourcesContent":["export const ZERO_ADDRESS =\n '0x0000000000000000000000000000000000000000' as const;\n"]}
|