Skip to main content

Working with amounts

Whenever an explorer method returns a value (balances, fees, token amounts), it uses the Amount class. This class handles decimal precision, arithmetic, and formatted display across all blockchains.

Amount properties

  • .base() — Value in base units as a Decimal (e.g., 0.005 ETH)
  • .min() — Value in minimal units as a bigint (e.g., 5000000000000000 wei)
  • .symbol — Currency symbol (e.g., 'ETH', 'BTC')
  • .name — Human-readable name (e.g., 'Ether', 'Bitcoin')
  • .network — Network identifier
  • .toCurrency(fiat)(async) Convert to fiat at current market rates
  • .isToken / .isNFT — Whether the amount represents a token or NFT

Example

import { ChainGate } from 'chaingate'

const cg = new ChainGate({ apiKey: 'your-api-key' })
const btc = cg.explore(cg.networks.bitcoin)

const balance = await btc.getAddressBalance('bc1q...')

console.log(balance.confirmed.base().toString()) // "0.005"
console.log(balance.confirmed.min().toString()) // "500000"
console.log(balance.confirmed.symbol) // "BTC"

Fiat conversion

Convert any amount to a fiat currency at current market rates:

const usdValue = await balance.confirmed.toCurrency('usd')
console.log(usdValue) // "485.23"

const eurValue = await balance.confirmed.toCurrency('eur')
console.log(eurValue) // "442.10"

Key points

  • Precision-safe — Decimal math avoids floating-point errors common with cryptocurrency values.
  • Network-aware — Each amount knows its decimals, symbol, and network.
  • Fiat conversion — Built-in market rate lookup via .toCurrency().
  • Consistent — The same Amount class is used across explorers, connectors, and transfers.