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)
// For example:
// Confirmed balance: 0.005 ETH
// Unconfirmed balance: 0 ETH
}
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
).
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)
You can also perform arithmetic on balances using .plus()
, .minus()
, etc., if youβre working with multiple amounts.
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.