Skip to main content

Blockchain utility methods

ChainGate provides a set of blockchain utility methods designed to help developers interact with all blockchains.

These utilities abstract low-level API calls and offer a consistent developer experience across chains. You can use them to:

  • Query balances and UTXOs
  • Broadcast transactions
  • Retrieve transaction and block data
  • Estimate gas and fees
  • Interact with smart contracts
  • Handle signing and address conversions
tip

These utilities are ideal for wallets, explorers, and blockchain tools that operate across multiple chains.

Initialization

To start using the utility methods, first initialize the utils interface:

import { initializeUtils } from 'chaingate'

const utils = await initializeUtils({ apiKey: 'your-api-key' })
const bitcoinUtils = utils.currency('bitcoin')

This provides access to all currency-specific utilities through a single entry point.


Usage per currency

After calling initializeUtils(), you can access the utilities for any supported currency using:

const polygonUtils = utils.currency('polygon')

Each currency provides its own relevant methods. For example:

  • bitcoin: addressUtxos(), transactionDetails(), latestBlock()
  • polygon: estimateGas(), callSmartContractRaw(), networkStatus()

Common methods

All currency utilities provide a shared set of methods, regardless of whether the chain is UTXO or EVM-based:

  • addressBalance(address) — Get confirmed and unconfirmed balances
  • publicKeyToAddress(publicKey) — Convert a public key to an address string
  • signMessage(message, privateKey) — Sign a message
  • verifySignedMessage(message, signature, address) — Verify a signed message
  • amount(value, unit) — Create an amount from a base or minimal unit
  • amountFiat(value, fiatCurrency) — Convert a fiat amount into a crypto amount
  • fiatRate(fiatCurrency) — Get the current rate for the crypto/fiat pair
  • latestBlock() — Get the latest block for the network
  • blockByHeight(height) — Fetch block data by block height
  • blockByHash(hash) — Fetch block data by block hash
  • transactionDetails(txId) — Retrieve full details of a transaction
  • broadcastTransaction(rawTx) — Broadcast raw transaction data

UTXO utilities

UtxoCurrencyUtils adds additional methods for managing unspent outputs and accessing blockchain history.

Available methods

  • addressUtxos(address) — List UTXOs associated with an address
  • addressHistory(address) — Get historical transactions
  • getFeeRate() — Get recommended fees based on network congestion
  • mempoolTransactions() — Get current mempool contents

EVM utilities

EvmCurrencyUtils includes additional methods tailored to EVM-compatible networks.

Available methods

  • addressTransactionCount(address) — Get transaction nonce
  • callSmartContractRaw(contract, data) — Low-level smart contract call
  • estimateGas(from, to, amount, nonce, data) — Estimate gas for a transaction
  • getFeeRate() — Retrieve current gas prices
  • networkStatus() — Returns real-time metrics about the network, including average block time, EIP-1559 fee details (base fee and predicted tips), network congestion, and estimated transaction costs.

Working with amounts

Whenever a utility method returns or requires an amount, it uses a specialized object that handles decimal precision, arithmetic operations, conversions, and formatted display.

This object provides methods like:

  • .str — Human-readable string with symbol (e.g., 0.00231 BTC)
  • .baseAmount — Decimal value in base unit
  • .minimalUnitAmount — Value in minimal unit (e.g., satoshis, wei)
  • .plus(amount) / .minus(amount) — Perform arithmetic
  • .toFiat(fiatCurrency) — Convert to fiat equivalent

This ensures safe calculations and consistent formatting across all blockchains.

Key points

  • Unified abstraction: The same API design across chains
  • Chain-aware methods: Adapts logic based on currency type
  • Smart contract support: For all EVM-based networks
  • Safe math: CurrencyAmount handles decimals correctly