UNPKG

@biconomy/sdk

Version:

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

53 lines 1.96 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]) => { return account_.deployments.some((account) => account.client.chain?.id === chainId); }); const balances = await Promise.all(relevantTokensByChain.map(async ([chainId, address]) => { const account = account_.deployments.filter((account) => account.client.chain?.id === chainId)[0]; const tokenContract = (0, viem_1.getContract)({ abi: viem_1.erc20Abi, address, client: account.client }); const [balance, decimals] = await Promise.all([ tokenContract.read.balanceOf([account.address]), 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