Get Multical Balance (getMulticalBalance)
Check multiple ERC20 token balances
The getMulticalBalance function allows you to check multiple ERC20 token balances in one call for a wallet address on a specified blockchain network. (This is a read-only)
Import
import { getMulticalBalance } from 'dacc-js';Usage
import { getMulticalBalance } from 'dacc-js';
import { optimismSepolia } from "viem/chains"; // used `viem` - npm i viem
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: [
"0x5fd84259d66Cd46123540766Be93DFE6D43130D7",
"0x4200000000000000000000000000000000000006",
],
network: optimismSepolia,
// multicalAddress: "0xMulticalAddress...", // optional, only needed for chains that do not expose a multicall address in the chain config
});
console.log(balances);
console.log(balances.map((item) => `${item.symbol}: ${item.balanceFormatted}`));Arguments
| Parameter | Type | Description |
|---|---|---|
address | 0x${string} | The wallet address to check balances for. |
tokenAddress | 0x${string}[] | The ERC20 token contract addresses to query. |
network | Chain | The blockchain network object to query. |
multicalAddress (optional) | Address | Optional custom Multicall contract address for chains that do not expose one in the chain config. |
Return Value
The response result is an array of objects containing:
[
{
address: "0x123...",
tokenAddress: "0x5fd84259d66Cd46123540766Be93DFE6D43130D7",
chainId: 11155420,
balance: "1000000",
balanceFormatted: "1",
decimals: 6,
symbol: "USDC",
name: "USDC",
},
{
address: "0x123...",
tokenAddress: "0x4200000000000000000000000000000000000006",
chainId: 11155420,
balance: "10000000000000000",
balanceFormatted: "0.01",
decimals: 18,
symbol: "WETH",
name: "Wrapped Ether",
}
]
["USDC: 1", "WETH: 0.01"]Parameters
address
- Type:
0x${string}
The wallet address to check ERC20 token balances for.
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: ["0xUSDC...", "0xDAI..."],
network: optimismSepolia,
});tokenAddress
- Type:
0x${string}[]
An array of ERC20 token contract addresses to query in one multicall.
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: [
"0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
"0x4200000000000000000000000000000000000006",
],
network: optimism,
});network
- Type:
Chain
The blockchain network to query the token balances from.
import { mainnet, optimism } from "viem/chains";
const mainnetBalances = await getMulticalBalance({
address: "0x123...",
tokenAddress: ["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
network: mainnet,
});
const optimismBalances = await getMulticalBalance({
address: "0x123...",
tokenAddress: ["0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85"],
network: optimism,
});multicalAddress (optional)
- Type:
Address
Optional Multicall contract address for chains that need a manual override.
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: ["0xUSDC..."],
network: optimismSepolia,
multicalAddress: "0xca11bde05977b3631167028862be2a173976ca11",
});Examples
Check Multiple Token Balances on One Chain
import { getMulticalBalance } from 'dacc-js';
import { mainnet } from "viem/chains";
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: [
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
"0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT
"0x6B175474E89094C44Da98b954EedeAC495271d0F", // DAI
],
network: mainnet,
});
balances.forEach((balance) => {
console.log(`${balance.symbol}: ${balance.balanceFormatted}`);
});Use Custom Multicall Address
import { getMulticalBalance } from 'dacc-js';
import { defineChain } from "viem";
const customChain = defineChain({
id: 123456,
name: "Custom Chain",
nativeCurrency: { name: "Custom", symbol: "CST", decimals: 18 },
rpcUrls: {
default: { http: ["https://rpc.custom-chain.example"] },
},
});
const balances = await getMulticalBalance({
address: "0x123...",
tokenAddress: ["0xToken..."],
network: customChain,
multicalAddress: "0xca11bde05977b3631167028862be2a173976ca11",
});
console.log(balances);Use Result in UI
import { useEffect, useState } from "react";
import { getMulticalBalance } from 'dacc-js';
import { optimism } from "viem/chains";
function TokenBalances({ walletAddress, tokenAddress }) {
const [balances, setBalances] = useState([]);
useEffect(() => {
async function fetchBalances() {
const result = await getMulticalBalance({
address: walletAddress,
tokenAddress,
network: optimism,
});
setBalances(result);
}
fetchBalances();
}, [walletAddress, tokenAddress]);
return (
<div>
{balances.map((balance) => (
<p key={balance.tokenAddress}>
{balance.symbol}: {balance.balanceFormatted}
</p>
))}
</div>
);
}