UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

51 lines 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUnifiedERC20Balance = getUnifiedERC20Balance; const viem_1 = require("viem"); async function getUnifiedERC20Balance(parameters) { const { mcToken, account: account_ } = parameters; const relevantTokensByChain = Array.from(mcToken.deployments).filter(([chainId]) => account_.deployments.some((account) => account.client.chain?.id === chainId)); const balances = await Promise.all(relevantTokensByChain.map(async ([chainId, address]) => { const { publicClient, address: accountAddress } = account_.deploymentOn(chainId, true); const tokenContract = (0, viem_1.getContract)({ abi: viem_1.erc20Abi, address, client: publicClient }); const [balance, decimals] = await Promise.all([ tokenContract.read.balanceOf([accountAddress]), tokenContract.read.decimals() ]); return { balance, decimals, chainId }; })); return { ...balances .map((balance) => { return { balance: balance.balance, decimals: balance.decimals }; }) .reduce((curr, acc) => { if (curr.decimals !== acc.decimals) { throw Error(` Error while trying to fetch a unified ERC20 balance. The addresses provided in the mapping don't have the same number of decimals across all chains. The function can't fetch a unified balance for token mappings with differing decimals. `); } return { balance: curr.balance + acc.balance, decimals: curr.decimals }; }), breakdown: balances, mcToken }; } //# sourceMappingURL=getUnifiedERC20Balance.js.map