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
0in 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 Dollareurβ Eurogbpβ British Poundjpyβ Japanese Yencnyβ Chinese Yuanaudβ Australian Dollarcadβ Canadian Dollarchfβ Swiss Francinrβ Indian Rupeebrlβ Brazilian Realmxnβ 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
0for 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.