fetchBalance
Action for fetching balance information for Ethereum or ERC-20 tokens.
This is a wrapper around viem's getBalance
.
import { fetchBalance } from '@wagmi/core'
Usage
import { fetchBalance } from '@wagmi/core'
const balance = await fetchBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
Return Value
{
decimals: number
formatted: string
symbol: string
value: bigint
}
Configuration
address
Address to fetch balance for.
import { fetchBalance } from '@wagmi/core'
const balance = await fetchBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
chainId (optional)
Force a specific chain id for the request. The @wagmi/core Client
's publicClient
must be set up as a chain-aware function for this to work correctly.
import { fetchBalance } from '@wagmi/core'
const balance = await fetchBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
chainId: 1,
})
formatUnits (optional)
Formats balance. Defaults to ether
or token
's decimal value.
import { fetchBalance } from '@wagmi/core'
const balance = await fetchBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
formatUnits: 'gwei',
})
token (optional)
Address for ERC-20 token. If token
is provided, action fetches token balance instead of Ethereum balance. For example, we can fetch 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
's current $UNI balance.
import { fetchBalance } from '@wagmi/core'
const balance = await fetchBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
})