getBalance
Returns the balance of an address in wei.
Usage
example.ts
import { publicClient } from './client'
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
> 10000000000000000000000n (wei)Returns
bigint
The balance of the address in wei.
Parameters
address
- Type:
Address
The address of the account.
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})blockNumber (optional)
- Type:
bigint
The balance of the account at a block number.
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.
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
blockTag: 'safe'
})Tips
- You can convert the balance to ether units with
formatEther.
import { formatEther } from 'viem'
const balance = await publicClient.getBalance({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
blockTag: 'safe'
})
const balanceAsEther = formatEther(balance)
// "6.942"