Skip to main content

Check address balance

Once you've initialized a wallet for a specific currency, you can query its on-chain balance. For example, if you want to know how much ETH is available in your address, simply call getBalance().

Example usage​

import { initializeWallet } from 'chaingate'

async function checkBalance() {
// This phrase is just for demonstration; never share your real mnemonic!
const { wallet, phrase } = await initializeWallet.fromPhrase({
phrase: 'some bip39 phrase ...'
})

// Select "ethereum" from the wallet
const ethereum = wallet.currency('ethereum')

// Retrieve the balance
const balance = await ethereum.getBalance()

console.log('Confirmed balance:', balance.confirmed.str)
console.log('Unconfirmed balance:', balance.unconfirmed.str)

const confirmedUsd = await balance.confirmed.toFiat('usd')
console.log('Confirmed balance (USD):', confirmedUsd.toString())

// For example:
// Confirmed balance: 0.005 ETH
// Unconfirmed balance: 0 ETH
// Confirmed balance (USD): 15.27
}

Confirmed vs. unconfirmed​

  • confirmed: The fully confirmed (on-chain) portion of your funds. Generally considered spendable.
  • unconfirmed: Funds awaiting sufficient confirmations or still in the mempool. Not all blockchains differentiate this, so unconfirmed might be 0 in some networks.

Understanding the balance values​

The returned balance includes two objects: confirmed and unconfirmed. Each of them provides helpful properties:

  • str – A formatted string (e.g., "0.005 ETH").
  • baseAmount – The balance in standard units as a number-like object (e.g., 0.005).
  • minimalUnitAmount – The amount in the smallest unit (e.g., wei or satoshis).
  • baseSymbol / minimalUnitSymbol – The symbols for each unit (e.g., eth, wei).
  • toFiat(fiatCurrency) – (async) Converts the amount to the given fiat currency at current market rates.

Example​

console.log(balance.confirmed.str)
// "0.005 ETH"

console.log(balance.confirmed.baseAmount.toString())
// "0.005"

console.log(balance.confirmed.minimalUnitAmount.toString())
// "5000000000000000" (wei)

console.log(await balance.confirmed.toFiat('eur'))
// "14.89" (example)

You can also perform arithmetic on balances using .plus(), .minus(), etc., if you’re working with multiple amounts.

Supported fiat currencies​

You can convert balances into more than 120 fiat currencies using their standard 3-letter codes.

Common examples include:

  • usd – US Dollar
  • eur – Euro
  • gbp – British Pound
  • jpy – Japanese Yen
  • cny – Chinese Yuan
  • aud – Australian Dollar
  • cad – Canadian Dollar
  • chf – Swiss Franc
  • inr – Indian Rupee
  • brl – Brazilian Real
  • mxn – Mexican Peso
const amountInGbp = await balance.confirmed.toFiat('gbp')
console.log(amountInGbp.toString())
// e.g., "12.45"

Key points​

  • Confirmed & Unconfirmed: Some networks use a concept of β€œunconfirmed” funds. Others may report 0 for unconfirmed.
  • Single Wallet, Multiple Currencies: You can call getBalance() on any currency instance (e.g., bitcoin, litecoin) in the same wallet.
  • Easy to format: The returned balance objects make it easy to display values in a user-friendly or low-level format.
  • Live data: Each call to getBalance() queries the blockchain, so the data is always up to date.
  • Fiat conversion: Use .toFiat('usd'), .toFiat('eur'), etc., to display balance value in a chosen fiat currency.