getBalance โ
Returns the balance of an address in wei.
Usage โ
ts
import { publicClient } from './client'
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
// 10000000000000000000000n (wei)
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})
Returns โ
bigint
The balance of the address in wei.
Parameters โ
address โ
- Type:
Address
The address of the account.
ts
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
blockNumber (optional) โ
- Type:
bigint
The balance of the account at a block number.
ts
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
blockNumber: 69420n
})
blockTag (optional) โ
- Type:
'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'
The balance of the account at a block tag.
ts
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
blockTag: 'safe'
})
Tips โ
- You can convert the balance to ether units with
formatEther
.
ts
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
blockTag: 'safe'
})
const balanceAsEther = formatEther(balance)
// "6.942"