Skip to content
On this page

getBalance

Returns the balance of an address in wei.

Import

ts
import { getBalance } from 'viem'

Usage

ts
import { getBalance } from 'viem'
import { publicClient } from '.'
 
const balance = await getBalance(publicClient, { 
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
// 10000000000000000000000n (wei)

Returns

bigint

The balance of the address in wei.

Parameters

address

  • Type: '0x${string}'

The address of the account.

ts
const balance = await getBalance(publicClient, {
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', 
})

blockNumber (optional)

  • Type: bigint

The balance of the account at a block number.

ts
const balance = await getBalance(publicClient, {
  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 getBalance(publicClient, {
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  blockTag: 'safe'  
})

Tips

  • You can convert the balance to ether units with formatEther.
ts
const balance = await getBalance(publicClient, {
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  blockTag: 'safe'
})
const balanceAsEther = formatEther(balance) 
// "6.942"

Released under the MIT License.