@metamask-previews/assets-controllers 73.0.1-preview-bf50b46b → 73.0.1-preview-e4e5ca5c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -13
- package/dist/AccountTrackerController.cjs +5 -167
- package/dist/AccountTrackerController.cjs.map +1 -1
- package/dist/AccountTrackerController.d.cts +2 -14
- package/dist/AccountTrackerController.d.cts.map +1 -1
- package/dist/AccountTrackerController.d.mts +2 -14
- package/dist/AccountTrackerController.d.mts.map +1 -1
- package/dist/AccountTrackerController.mjs +5 -167
- package/dist/AccountTrackerController.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +321 -267
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +93 -51
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +93 -51
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +320 -270
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +1 -13
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +0 -8
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +0 -8
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +1 -12
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/constants.cjs +1 -12
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +0 -1
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +0 -1
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +0 -11
- package/dist/constants.mjs.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs +1 -35
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts +0 -16
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts +0 -16
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs +0 -33
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.cts +0 -8
- package/dist/multi-chain-accounts-service/types.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/types.d.mts +0 -8
- package/dist/multi-chain-accounts-service/types.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/types.mjs.map +1 -1
- package/dist/multicall.cjs +22 -397
- package/dist/multicall.cjs.map +1 -1
- package/dist/multicall.d.cts +0 -39
- package/dist/multicall.d.cts.map +1 -1
- package/dist/multicall.d.mts +0 -39
- package/dist/multicall.d.mts.map +1 -1
- package/dist/multicall.mjs +21 -398
- package/dist/multicall.mjs.map +1 -1
- package/dist/selectors/balanceSelectors.cjs +275 -0
- package/dist/selectors/balanceSelectors.cjs.map +1 -0
- package/dist/selectors/balanceSelectors.d.cts +248 -0
- package/dist/selectors/balanceSelectors.d.cts.map +1 -0
- package/dist/selectors/balanceSelectors.d.mts +248 -0
- package/dist/selectors/balanceSelectors.d.mts.map +1 -0
- package/dist/selectors/balanceSelectors.mjs +268 -0
- package/dist/selectors/balanceSelectors.mjs.map +1 -0
- package/package.json +6 -3
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs +0 -98
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts +0 -28
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts +0 -28
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts.map +0 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs +0 -98
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.cjs +0 -128
- package/dist/rpc-service/rpc-balance-fetcher.cjs.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.cts +0 -34
- package/dist/rpc-service/rpc-balance-fetcher.d.cts.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.mts +0 -34
- package/dist/rpc-service/rpc-balance-fetcher.d.mts.map +0 -1
- package/dist/rpc-service/rpc-balance-fetcher.mjs +0 -124
- package/dist/rpc-service/rpc-balance-fetcher.mjs.map +0 -1
- package/dist/selectors.cjs +0 -64
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -51
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -51
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -61
- package/dist/selectors.mjs.map +0 -1
package/dist/multicall.cjs
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.multicallOrFallback = void 0;
|
|
7
4
|
const contracts_1 = require("@ethersproject/contracts");
|
|
8
|
-
const bn_js_1 = __importDefault(require("bn.js"));
|
|
9
|
-
const AssetsContractController_1 = require("./AssetsContractController.cjs");
|
|
10
5
|
const assetsUtil_1 = require("./assetsUtil.cjs");
|
|
11
6
|
// https://github.com/mds1/multicall/blob/main/deployments.json
|
|
12
7
|
const MULTICALL_CONTRACT_BY_CHAINID = {
|
|
@@ -135,7 +130,7 @@ const MULTICALL_CONTRACT_BY_CHAINID = {
|
|
|
135
130
|
'0x96f': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
136
131
|
'0x3cc5': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
137
132
|
'0x4571': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
138
|
-
'0xe99': '
|
|
133
|
+
'0xe99': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
139
134
|
'0x7d0': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
140
135
|
'0x1297': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
141
136
|
'0x1d5e': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
@@ -197,11 +192,30 @@ const MULTICALL_CONTRACT_BY_CHAINID = {
|
|
|
197
192
|
'0xa1337': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
198
193
|
'0x1f2b': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
199
194
|
'0xf63': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
200
|
-
'0x144': '
|
|
195
|
+
'0x144': '0xF9cda624FBC7e059355ce98a31693d299FACd963',
|
|
201
196
|
'0x118': '0xF9cda624FBC7e059355ce98a31693d299FACd963',
|
|
202
197
|
'0x12c': '0xF9cda624FBC7e059355ce98a31693d299FACd963',
|
|
203
198
|
'0x18995f': '0xF9cda624FBC7e059355ce98a31693d299FACd963',
|
|
204
199
|
'0x2b74': '0xF9cda624FBC7e059355ce98a31693d299FACd963',
|
|
200
|
+
'0xfc': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
201
|
+
'0x9da': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
202
|
+
'0x137': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
203
|
+
'0x13ed': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
204
|
+
'0x24b1': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
205
|
+
'0xba9302': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
206
|
+
'0x7c8': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
207
|
+
'0x138d5': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
208
|
+
'0x6d': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
209
|
+
'0x343b': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
210
|
+
'0x34a1': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
211
|
+
'0x3109': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
212
|
+
'0x91b': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
213
|
+
'0xa96': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
214
|
+
'0x22c3': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
215
|
+
'0x2be3': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
216
|
+
'0xbf03': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
217
|
+
'0x1b254': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
218
|
+
'0xa7b14': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
205
219
|
'0x2276': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
206
220
|
'0x1b9e': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
207
221
|
'0x6a63bb8': '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
@@ -271,70 +285,6 @@ const multicallAbi = [
|
|
|
271
285
|
],
|
|
272
286
|
},
|
|
273
287
|
];
|
|
274
|
-
// Multicall3 ABI for aggregate3 function
|
|
275
|
-
const multicall3Abi = [
|
|
276
|
-
{
|
|
277
|
-
name: 'aggregate3',
|
|
278
|
-
type: 'function',
|
|
279
|
-
stateMutability: 'payable',
|
|
280
|
-
inputs: [
|
|
281
|
-
{
|
|
282
|
-
name: 'calls',
|
|
283
|
-
type: 'tuple[]',
|
|
284
|
-
components: [
|
|
285
|
-
{ name: 'target', type: 'address' },
|
|
286
|
-
{ name: 'allowFailure', type: 'bool' },
|
|
287
|
-
{ name: 'callData', type: 'bytes' },
|
|
288
|
-
],
|
|
289
|
-
},
|
|
290
|
-
],
|
|
291
|
-
outputs: [
|
|
292
|
-
{
|
|
293
|
-
name: 'returnData',
|
|
294
|
-
type: 'tuple[]',
|
|
295
|
-
components: [
|
|
296
|
-
{ name: 'success', type: 'bool' },
|
|
297
|
-
{ name: 'returnData', type: 'bytes' },
|
|
298
|
-
],
|
|
299
|
-
},
|
|
300
|
-
],
|
|
301
|
-
},
|
|
302
|
-
];
|
|
303
|
-
// Constants for encoded strings and addresses
|
|
304
|
-
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
305
|
-
const BALANCE_OF_FUNCTION = 'balanceOf(address)';
|
|
306
|
-
const GET_ETH_BALANCE_FUNCTION = 'getEthBalance';
|
|
307
|
-
const GET_SHARES_FUNCTION = 'getShares';
|
|
308
|
-
// ERC20 balanceOf ABI
|
|
309
|
-
const ERC20_BALANCE_OF_ABI = [
|
|
310
|
-
{
|
|
311
|
-
name: 'balanceOf',
|
|
312
|
-
type: 'function',
|
|
313
|
-
inputs: [{ name: 'account', type: 'address' }],
|
|
314
|
-
outputs: [{ name: '', type: 'uint256' }],
|
|
315
|
-
stateMutability: 'view',
|
|
316
|
-
},
|
|
317
|
-
];
|
|
318
|
-
// Multicall3 getEthBalance ABI
|
|
319
|
-
const MULTICALL3_GET_ETH_BALANCE_ABI = [
|
|
320
|
-
{
|
|
321
|
-
name: 'getEthBalance',
|
|
322
|
-
type: 'function',
|
|
323
|
-
inputs: [{ name: 'addr', type: 'address' }],
|
|
324
|
-
outputs: [{ name: 'balance', type: 'uint256' }],
|
|
325
|
-
stateMutability: 'view',
|
|
326
|
-
},
|
|
327
|
-
];
|
|
328
|
-
// Staking contract getShares ABI
|
|
329
|
-
const STAKING_GET_SHARES_ABI = [
|
|
330
|
-
{
|
|
331
|
-
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
|
|
332
|
-
name: 'getShares',
|
|
333
|
-
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|
334
|
-
stateMutability: 'view',
|
|
335
|
-
type: 'function',
|
|
336
|
-
},
|
|
337
|
-
];
|
|
338
288
|
const multicall = async (calls, multicallAddress, provider, maxCallsPerMulticall) => {
|
|
339
289
|
const multicallContract = new contracts_1.Contract(multicallAddress, multicallAbi, provider);
|
|
340
290
|
return await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
@@ -410,329 +360,4 @@ const multicallOrFallback = async (calls, chainId, provider, maxCallsPerMultical
|
|
|
410
360
|
return await fallback(calls, maxCallsParallel);
|
|
411
361
|
};
|
|
412
362
|
exports.multicallOrFallback = multicallOrFallback;
|
|
413
|
-
/**
|
|
414
|
-
* Execute multiple contract calls using Multicall3's aggregate3 function.
|
|
415
|
-
* This allows for more efficient batch calls with individual failure handling.
|
|
416
|
-
*
|
|
417
|
-
* @param calls - Array of calls to execute via aggregate3
|
|
418
|
-
* @param chainId - The hexadecimal chain id
|
|
419
|
-
* @param provider - An ethers rpc provider
|
|
420
|
-
* @returns Promise resolving to array of results from aggregate3
|
|
421
|
-
*/
|
|
422
|
-
const aggregate3 = async (calls, chainId, provider) => {
|
|
423
|
-
if (calls.length === 0) {
|
|
424
|
-
return [];
|
|
425
|
-
}
|
|
426
|
-
const multicall3Address = MULTICALL_CONTRACT_BY_CHAINID[chainId];
|
|
427
|
-
const multicall3Contract = new contracts_1.Contract(multicall3Address, multicall3Abi, provider);
|
|
428
|
-
return await multicall3Contract.callStatic.aggregate3(calls);
|
|
429
|
-
};
|
|
430
|
-
exports.aggregate3 = aggregate3;
|
|
431
|
-
/**
|
|
432
|
-
* Processes and decodes balance results from aggregate3 calls
|
|
433
|
-
*
|
|
434
|
-
* @param results - Array of results from aggregate3 calls
|
|
435
|
-
* @param callMapping - Array mapping call indices to token and user addresses
|
|
436
|
-
* @param chainId - The hexadecimal chain id
|
|
437
|
-
* @param provider - An ethers rpc provider
|
|
438
|
-
* @param includeStaked - Whether to include staked balances
|
|
439
|
-
* @returns Map of token address to map of user address to balance
|
|
440
|
-
*/
|
|
441
|
-
const processBalanceResults = (results, callMapping, chainId, provider, includeStaked) => {
|
|
442
|
-
const balanceMap = {};
|
|
443
|
-
const stakedBalanceMap = {};
|
|
444
|
-
// Create contract instances for decoding
|
|
445
|
-
const erc20Contract = new contracts_1.Contract(ZERO_ADDRESS, ERC20_BALANCE_OF_ABI, provider);
|
|
446
|
-
const multicall3Address = MULTICALL_CONTRACT_BY_CHAINID[chainId];
|
|
447
|
-
const multicall3Contract = new contracts_1.Contract(multicall3Address, MULTICALL3_GET_ETH_BALANCE_ABI, provider);
|
|
448
|
-
const stakingContractAddress = AssetsContractController_1.STAKING_CONTRACT_ADDRESS_BY_CHAINID[chainId];
|
|
449
|
-
const stakingContract = stakingContractAddress
|
|
450
|
-
? new contracts_1.Contract(stakingContractAddress, STAKING_GET_SHARES_ABI, provider)
|
|
451
|
-
: null;
|
|
452
|
-
results.forEach((result, index) => {
|
|
453
|
-
if (result.success) {
|
|
454
|
-
const { tokenAddress, userAddress, callType } = callMapping[index];
|
|
455
|
-
let balance;
|
|
456
|
-
if (callType === 'native') {
|
|
457
|
-
// For native token, decode the getEthBalance result
|
|
458
|
-
balance = multicall3Contract.interface.decodeFunctionResult(GET_ETH_BALANCE_FUNCTION, result.returnData)[0];
|
|
459
|
-
if (!balanceMap[tokenAddress]) {
|
|
460
|
-
balanceMap[tokenAddress] = {};
|
|
461
|
-
}
|
|
462
|
-
balanceMap[tokenAddress][userAddress] = balance;
|
|
463
|
-
}
|
|
464
|
-
else if (callType === 'staking') {
|
|
465
|
-
// For staking contract, decode the getShares result
|
|
466
|
-
if (stakingContract) {
|
|
467
|
-
balance = stakingContract.interface.decodeFunctionResult(GET_SHARES_FUNCTION, result.returnData)[0];
|
|
468
|
-
stakedBalanceMap[userAddress] = balance;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
else {
|
|
472
|
-
// For ERC20 tokens, decode the balanceOf result
|
|
473
|
-
balance = erc20Contract.interface.decodeFunctionResult(BALANCE_OF_FUNCTION, result.returnData)[0];
|
|
474
|
-
if (!balanceMap[tokenAddress]) {
|
|
475
|
-
balanceMap[tokenAddress] = {};
|
|
476
|
-
}
|
|
477
|
-
balanceMap[tokenAddress][userAddress] = balance;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
const result = { tokenBalances: balanceMap };
|
|
482
|
-
if (includeStaked && Object.keys(stakedBalanceMap).length > 0) {
|
|
483
|
-
result.stakedBalances = stakedBalanceMap;
|
|
484
|
-
}
|
|
485
|
-
return result;
|
|
486
|
-
};
|
|
487
|
-
/**
|
|
488
|
-
* Fallback function to get native token balances using individual eth_getBalance calls
|
|
489
|
-
* when Multicall3 is not supported on the chain.
|
|
490
|
-
*
|
|
491
|
-
* @param userAddresses - Array of user addresses to check balances for
|
|
492
|
-
* @param provider - An ethers rpc provider
|
|
493
|
-
* @param maxCallsParallel - Maximum number of parallel calls (default: 20)
|
|
494
|
-
* @returns Promise resolving to map of user address to balance
|
|
495
|
-
*/
|
|
496
|
-
const getNativeBalancesFallback = async (userAddresses, provider, maxCallsParallel = 20) => {
|
|
497
|
-
const balanceMap = {};
|
|
498
|
-
await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
499
|
-
values: userAddresses,
|
|
500
|
-
batchSize: maxCallsParallel,
|
|
501
|
-
initialResult: undefined,
|
|
502
|
-
eachBatch: async (_, batch) => {
|
|
503
|
-
const results = await Promise.allSettled(batch.map(async (userAddress) => {
|
|
504
|
-
const balance = await provider.getBalance(userAddress);
|
|
505
|
-
return {
|
|
506
|
-
success: true,
|
|
507
|
-
balance: new bn_js_1.default(balance.toString()),
|
|
508
|
-
userAddress,
|
|
509
|
-
};
|
|
510
|
-
}));
|
|
511
|
-
results.forEach((result) => {
|
|
512
|
-
if (result.status === 'fulfilled' &&
|
|
513
|
-
result.value.success &&
|
|
514
|
-
result.value.balance !== null) {
|
|
515
|
-
balanceMap[result.value.userAddress] = result.value.balance;
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
},
|
|
519
|
-
});
|
|
520
|
-
return balanceMap;
|
|
521
|
-
};
|
|
522
|
-
/**
|
|
523
|
-
* Fallback function to get token balances using individual calls
|
|
524
|
-
* when Multicall3 is not supported or when aggregate3 calls fail.
|
|
525
|
-
*
|
|
526
|
-
* @param tokenAddresses - Array of ERC20 token contract addresses
|
|
527
|
-
* @param userAddresses - Array of user addresses to check balances for
|
|
528
|
-
* @param provider - An ethers rpc provider
|
|
529
|
-
* @param includeNative - Whether to include native token balances (default: true)
|
|
530
|
-
* @param maxCallsParallel - Maximum number of parallel calls (default: 20)
|
|
531
|
-
* @returns Promise resolving to map of token address to map of user address to balance
|
|
532
|
-
*/
|
|
533
|
-
const getTokenBalancesFallback = async (tokenAddresses, userAddresses, provider, includeNative, maxCallsParallel) => {
|
|
534
|
-
const balanceMap = {};
|
|
535
|
-
// Handle ERC20 token balances using the existing fallback function
|
|
536
|
-
if (tokenAddresses.length > 0) {
|
|
537
|
-
const erc20Calls = [];
|
|
538
|
-
const callMapping = [];
|
|
539
|
-
tokenAddresses.forEach((tokenAddress) => {
|
|
540
|
-
userAddresses.forEach((userAddress) => {
|
|
541
|
-
const contract = new contracts_1.Contract(tokenAddress, ERC20_BALANCE_OF_ABI, provider);
|
|
542
|
-
erc20Calls.push({
|
|
543
|
-
contract,
|
|
544
|
-
functionSignature: BALANCE_OF_FUNCTION,
|
|
545
|
-
arguments: [userAddress],
|
|
546
|
-
});
|
|
547
|
-
callMapping.push({ tokenAddress, userAddress });
|
|
548
|
-
});
|
|
549
|
-
});
|
|
550
|
-
const erc20Results = await fallback(erc20Calls, maxCallsParallel);
|
|
551
|
-
erc20Results.forEach((result, index) => {
|
|
552
|
-
if (result.success) {
|
|
553
|
-
const { tokenAddress, userAddress } = callMapping[index];
|
|
554
|
-
if (!balanceMap[tokenAddress]) {
|
|
555
|
-
balanceMap[tokenAddress] = {};
|
|
556
|
-
}
|
|
557
|
-
balanceMap[tokenAddress][userAddress] = result.value;
|
|
558
|
-
}
|
|
559
|
-
});
|
|
560
|
-
}
|
|
561
|
-
// Handle native token balances using the native fallback function
|
|
562
|
-
if (includeNative) {
|
|
563
|
-
const nativeBalances = await getNativeBalancesFallback(userAddresses, provider, maxCallsParallel);
|
|
564
|
-
if (Object.keys(nativeBalances).length > 0) {
|
|
565
|
-
balanceMap[ZERO_ADDRESS] = nativeBalances;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
return balanceMap;
|
|
569
|
-
};
|
|
570
|
-
/**
|
|
571
|
-
* Fallback function to get staked balances using individual calls
|
|
572
|
-
* when Multicall3 is not supported or when aggregate3 calls fail.
|
|
573
|
-
*
|
|
574
|
-
* @param userAddresses - Array of user addresses to check staked balances for
|
|
575
|
-
* @param chainId - The hexadecimal chain id
|
|
576
|
-
* @param provider - An ethers rpc provider
|
|
577
|
-
* @param maxCallsParallel - Maximum number of parallel calls (default: 20)
|
|
578
|
-
* @returns Promise resolving to map of user address to staked balance
|
|
579
|
-
*/
|
|
580
|
-
const getStakedBalancesFallback = async (userAddresses, chainId, provider, maxCallsParallel) => {
|
|
581
|
-
const stakedBalanceMap = {};
|
|
582
|
-
const stakingContractAddress = AssetsContractController_1.STAKING_CONTRACT_ADDRESS_BY_CHAINID[chainId];
|
|
583
|
-
if (!stakingContractAddress) {
|
|
584
|
-
// No staking support for this chain
|
|
585
|
-
return stakedBalanceMap;
|
|
586
|
-
}
|
|
587
|
-
const stakingCalls = [];
|
|
588
|
-
const callMapping = [];
|
|
589
|
-
userAddresses.forEach((userAddress) => {
|
|
590
|
-
const contract = new contracts_1.Contract(stakingContractAddress, STAKING_GET_SHARES_ABI, provider);
|
|
591
|
-
stakingCalls.push({
|
|
592
|
-
contract,
|
|
593
|
-
functionSignature: GET_SHARES_FUNCTION,
|
|
594
|
-
arguments: [userAddress],
|
|
595
|
-
});
|
|
596
|
-
callMapping.push({ userAddress });
|
|
597
|
-
});
|
|
598
|
-
const stakingResults = await fallback(stakingCalls, maxCallsParallel);
|
|
599
|
-
stakingResults.forEach((result, index) => {
|
|
600
|
-
if (result.success) {
|
|
601
|
-
const { userAddress } = callMapping[index];
|
|
602
|
-
stakedBalanceMap[userAddress] = result.value;
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
return stakedBalanceMap;
|
|
606
|
-
};
|
|
607
|
-
/**
|
|
608
|
-
* Get token balances (both ERC20 and native) for multiple addresses using aggregate3.
|
|
609
|
-
* This is more efficient than individual balanceOf calls for multiple addresses and tokens.
|
|
610
|
-
* Native token balances are mapped to the zero address (0x0000000000000000000000000000000000000000).
|
|
611
|
-
*
|
|
612
|
-
* @param accountTokenGroups - Array of objects containing account addresses and their associated token addresses
|
|
613
|
-
* @param chainId - The hexadecimal chain id
|
|
614
|
-
* @param provider - An ethers rpc provider
|
|
615
|
-
* @param includeNative - Whether to include native token balances (default: true)
|
|
616
|
-
* @param includeStaked - Whether to include staked balances from supported staking contracts (default: false)
|
|
617
|
-
* @returns Promise resolving to object containing tokenBalances map and optional stakedBalances map
|
|
618
|
-
*/
|
|
619
|
-
const getTokenBalancesForMultipleAddresses = async (accountTokenGroups, chainId, provider, includeNative, includeStaked) => {
|
|
620
|
-
// Return early if no groups provided
|
|
621
|
-
if (accountTokenGroups.length === 0 && !includeNative && !includeStaked) {
|
|
622
|
-
return { tokenBalances: {} };
|
|
623
|
-
}
|
|
624
|
-
// Extract unique token addresses and user addresses from groups
|
|
625
|
-
const uniqueTokenAddresses = Array.from(new Set(accountTokenGroups.flatMap((group) => group.tokenAddresses))).filter((tokenAddress) => tokenAddress !== ZERO_ADDRESS); // Exclude native token from ERC20 calls
|
|
626
|
-
const uniqueUserAddresses = Array.from(new Set(accountTokenGroups.map((group) => group.accountAddress)));
|
|
627
|
-
// Check if Multicall3 is supported on this chain
|
|
628
|
-
if (!MULTICALL_CONTRACT_BY_CHAINID[chainId]) {
|
|
629
|
-
// Fallback to individual balance calls when Multicall3 is not supported
|
|
630
|
-
const tokenBalances = await getTokenBalancesFallback(uniqueTokenAddresses, uniqueUserAddresses, provider, includeNative, 20);
|
|
631
|
-
const result = { tokenBalances };
|
|
632
|
-
// Handle staked balances fallback if requested
|
|
633
|
-
if (includeStaked) {
|
|
634
|
-
const stakedBalances = await getStakedBalancesFallback(uniqueUserAddresses, chainId, provider, 20);
|
|
635
|
-
if (Object.keys(stakedBalances).length > 0) {
|
|
636
|
-
result.stakedBalances = stakedBalances;
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
return result;
|
|
640
|
-
}
|
|
641
|
-
try {
|
|
642
|
-
// Create calls directly from pairs
|
|
643
|
-
const allCalls = [];
|
|
644
|
-
const allCallMapping = [];
|
|
645
|
-
// Create a temporary ERC20 contract for encoding
|
|
646
|
-
const tempERC20Contract = new contracts_1.Contract(ZERO_ADDRESS, ERC20_BALANCE_OF_ABI, provider);
|
|
647
|
-
// Create ERC20 balance calls for all account-token combinations
|
|
648
|
-
accountTokenGroups.forEach((group) => {
|
|
649
|
-
group.tokenAddresses
|
|
650
|
-
.filter((tokenAddress) => tokenAddress !== ZERO_ADDRESS)
|
|
651
|
-
.forEach((tokenAddress) => {
|
|
652
|
-
allCalls.push({
|
|
653
|
-
target: tokenAddress,
|
|
654
|
-
allowFailure: true,
|
|
655
|
-
callData: tempERC20Contract.interface.encodeFunctionData(BALANCE_OF_FUNCTION, [group.accountAddress]),
|
|
656
|
-
});
|
|
657
|
-
allCallMapping.push({
|
|
658
|
-
tokenAddress,
|
|
659
|
-
userAddress: group.accountAddress,
|
|
660
|
-
callType: 'erc20',
|
|
661
|
-
});
|
|
662
|
-
});
|
|
663
|
-
});
|
|
664
|
-
// Add native token balance calls if requested
|
|
665
|
-
if (includeNative) {
|
|
666
|
-
const multicall3Address = MULTICALL_CONTRACT_BY_CHAINID[chainId];
|
|
667
|
-
const multicall3TempContract = new contracts_1.Contract(multicall3Address, MULTICALL3_GET_ETH_BALANCE_ABI, provider);
|
|
668
|
-
uniqueUserAddresses.forEach((userAddress) => {
|
|
669
|
-
allCalls.push({
|
|
670
|
-
target: multicall3Address,
|
|
671
|
-
allowFailure: true,
|
|
672
|
-
callData: multicall3TempContract.interface.encodeFunctionData(GET_ETH_BALANCE_FUNCTION, [userAddress]),
|
|
673
|
-
});
|
|
674
|
-
allCallMapping.push({
|
|
675
|
-
tokenAddress: ZERO_ADDRESS,
|
|
676
|
-
userAddress,
|
|
677
|
-
callType: 'native',
|
|
678
|
-
});
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
// Add staking balance calls if requested
|
|
682
|
-
if (includeStaked) {
|
|
683
|
-
const stakingContractAddress = AssetsContractController_1.STAKING_CONTRACT_ADDRESS_BY_CHAINID[chainId];
|
|
684
|
-
if (stakingContractAddress) {
|
|
685
|
-
const stakingContract = new contracts_1.Contract(stakingContractAddress, STAKING_GET_SHARES_ABI, provider);
|
|
686
|
-
uniqueUserAddresses.forEach((userAddress) => {
|
|
687
|
-
allCalls.push({
|
|
688
|
-
target: stakingContractAddress,
|
|
689
|
-
allowFailure: true,
|
|
690
|
-
callData: stakingContract.interface.encodeFunctionData(GET_SHARES_FUNCTION, [userAddress]),
|
|
691
|
-
});
|
|
692
|
-
allCallMapping.push({
|
|
693
|
-
tokenAddress: stakingContractAddress,
|
|
694
|
-
userAddress,
|
|
695
|
-
callType: 'staking',
|
|
696
|
-
});
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
// Execute all calls in batches
|
|
701
|
-
const maxCallsPerBatch = 300; // Limit calls per batch to avoid gas/size limits
|
|
702
|
-
const allResults = [];
|
|
703
|
-
await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
704
|
-
values: allCalls,
|
|
705
|
-
batchSize: maxCallsPerBatch,
|
|
706
|
-
initialResult: undefined,
|
|
707
|
-
eachBatch: async (_, batch) => {
|
|
708
|
-
const batchResults = await (0, exports.aggregate3)(batch, chainId, provider);
|
|
709
|
-
allResults.push(...batchResults);
|
|
710
|
-
},
|
|
711
|
-
});
|
|
712
|
-
// Process and return results
|
|
713
|
-
return processBalanceResults(allResults, allCallMapping, chainId, provider, includeStaked);
|
|
714
|
-
}
|
|
715
|
-
catch (error) {
|
|
716
|
-
// Fallback only on revert
|
|
717
|
-
// https://docs.ethers.org/v5/troubleshooting/errors/#help-CALL_EXCEPTION
|
|
718
|
-
if (!error ||
|
|
719
|
-
typeof error !== 'object' ||
|
|
720
|
-
!('code' in error) ||
|
|
721
|
-
error.code !== 'CALL_EXCEPTION') {
|
|
722
|
-
throw error;
|
|
723
|
-
}
|
|
724
|
-
// Fallback to individual balance calls when aggregate3 fails
|
|
725
|
-
const tokenBalances = await getTokenBalancesFallback(uniqueTokenAddresses, uniqueUserAddresses, provider, includeNative, 20);
|
|
726
|
-
const result = { tokenBalances };
|
|
727
|
-
// Handle staked balances fallback if requested
|
|
728
|
-
if (includeStaked) {
|
|
729
|
-
const stakedBalances = await getStakedBalancesFallback(uniqueUserAddresses, chainId, provider, 20);
|
|
730
|
-
if (Object.keys(stakedBalances).length > 0) {
|
|
731
|
-
result.stakedBalances = stakedBalances;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
return result;
|
|
735
|
-
}
|
|
736
|
-
};
|
|
737
|
-
exports.getTokenBalancesForMultipleAddresses = getTokenBalancesForMultipleAddresses;
|
|
738
363
|
//# sourceMappingURL=multicall.cjs.map
|