You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I would like to ask how can I get balance of a native token in a wallet. Like the below code,
import {ContractCallContext, Multicall,} from 'ethereum-multicall';
import {ethers} from 'ethers';
import ERC20_ABI from './abi.json' assert {type: 'json'}
let provider = ethers.getDefaultProvider("https://polygon-rpc.com/");
try {
const walletAddress = "0xe7804c37c13166fF0b37F5aE0BB07A3aEbb6e245"
const tokenAddresses = ["0x0000000000000000000000000000000000000000"]
const network = await provider.getNetwork()
// you can use any ethers provider context here this example is
// just shows passing in a default provider, ethers hold providers in
// other context like wallet, signer etc all can be passed in as well.
const multicall = new Multicall({ ethersProvider: provider, tryAggregate: true });
const contractCallContext: ContractCallContext[] = tokenAddresses.map((it) => ({
reference: `${it}`,
contractAddress: it,
abi: ERC20_ABI,
calls: [{ reference: `callBalanceOf:${it}`, methodName: 'balanceOf', methodParameters: [walletAddress] }],
}))
const callResults = await multicall.call(contractCallContext)
const tokenBalancesMap = {} as Record<string, bigint>
for (const address in callResults.results) {
try {
tokenBalancesMap[address] = BigInt(callResults.results[address].callsReturnContext[0].returnValues[0].hex)
} catch (e) {
console.warn(`parsing token balance from multicall failed for token ${address} in chain ${network.chainId}`)
}
}
console.log(tokenBalancesMap)
}catch (e) {
console.error('!-- ERROR:', e)
console.log('Is there anything to inspect?')
} finally {
}
input: token address as null address
output: empty object. For more info, if I set tryAggregate to false, the error would be
ERROR: Error: data out-of-bounds (length=0, offset=32, code=BUFFER_OVERRUN, version=abi/5.7.0)
In the particular case is polygon chain, I can replace null address with matic token address 0x0000000000000000000000000000000000001010, and it works well. But if moving to another chain like Ethereum and BNB, there are no address like that but the null address.
How could I call get balance of a native currency for a wallet in such chains? On the other hand, in the library https://github.com/indexed-finance/multicall?tab=readme-ov-file#querying-token-balances, I could able to get balance with null address.
Thank you and have a great day.
P.S: Really appreciate your multicall package, and wishes you have awesome features in the future.
The text was updated successfully, but these errors were encountered:
Can I still use null address for ERC20 ABI like in the topic? If yes, how would I need to adjust the code?
If the original way does not work, could I not specify particular contractAddress and multicall contract's abi? Like the package already support these and I can reuse them?
I find out that there is public static ABI field of Multicall class, but it only has write functions, but not getEthBalance function
And there is function getContractBasedOnNetwork suits my needs but it is not published unfortunately. Hence, the work around would be duplicated that function into my own code.
Hi, I would like to ask how can I get balance of a native token in a wallet. Like the below code,
tryAggregate
to false, the error would beIn the particular case is polygon chain, I can replace null address with matic token address
0x0000000000000000000000000000000000001010
, and it works well. But if moving to another chain like Ethereum and BNB, there are no address like that but the null address.How could I call get balance of a native currency for a wallet in such chains? On the other hand, in the library https://github.com/indexed-finance/multicall?tab=readme-ov-file#querying-token-balances, I could able to get balance with null address.
Thank you and have a great day.
P.S: Really appreciate your multicall package, and wishes you have awesome features in the future.
The text was updated successfully, but these errors were encountered: