Create amounts
Each NetworkDescriptor provides helper methods to create Amount instances for transfers and display. Amounts handle decimal precision automatically based on the network's native asset.
From native units
Use amount() to create an amount in native units (e.g., BTC, ETH):
import { ChainGate } from 'chaingate'
const cg = new ChainGate({ apiKey: 'your-api-key' })
// 0.001 BTC (= 100,000 satoshis)
const btcAmount = cg.networks.bitcoin.amount('0.001')
// 1.5 ETH (= 1,500,000,000,000,000,000 wei)
const ethAmount = cg.networks.ethereum.amount('1.5')
From fiat currency
Use amountFromCurrency() to create an amount from a fiat value at current market rates:
// $50 worth of ETH at current price
const ethAmount = await cg.networks.ethereum.amountFromCurrency('usd', 50)
// €100 worth of BTC at current price
const btcAmount = await cg.networks.bitcoin.amountFromCurrency('eur', 100)
Working with amounts
The Amount class provides:
.base()— Value in base units as aDecimal(e.g.,0.005ETH).min()— Value in minimal units as abigint(e.g.,5000000000000000wei).symbol— The 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
const amount = cg.networks.bitcoin.amount('0.005')
console.log(amount.base().toString()) // "0.005"
console.log(amount.min().toString()) // "500000"
console.log(amount.symbol) // "BTC"
// Convert to fiat
const usdValue = await amount.toCurrency('usd')
console.log(usdValue) // "485.23"
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()andamountFromCurrency().