Skip to main content
The Balance Service provides functions for fetching token balances across chains, with built-in caching, validation, and error handling.

Exports


Functions

getBalance(chainId, tokenAddress, walletAddress, cache?)

Get token balance for a specific wallet address.
Returns: Balance Throws:
  • InvalidAddressError if the wallet address is invalid
  • BalanceFetchError if the API fails and no cache available
Example:

getBalances(walletAddress, chainIds, tokenAddresses?, cache?)

Get token balances across multiple chains in parallel.
Returns: BalancesResponse Example:

getChainBalances(walletAddress, chainId, cache?)

Get all supported token balances for a specific chain.
Returns: Array of BalanceWithMetadata with extended information. Example:

getBalanceWithMetadata(chainId, tokenAddress, walletAddress, cache?)

Get balance with additional metadata like USD value and price.
Returns: BalanceWithMetadata Example:

validateBalance(quote, walletAddress, cache?)

Validate that a user has sufficient balance to execute a quote.
Returns: BalanceValidation BalanceWarning Types: Example:

checkBalance(chainId, tokenAddress, walletAddress, amount, cache?)

Quick check if a user has sufficient balance for a specific amount.
Returns: BalanceCheckResult Example:

Error Types

BalanceFetchError

Thrown when balance fetching fails.
Example:

InvalidAddressError

Thrown when an invalid wallet address is provided.
Example:

BalanceCache Class

The BalanceCache class manages caching for balance data with a 30-second TTL.

Methods

Example Usage


Types

Balance

BalanceWithMetadata

BalanceValidation

BalanceWarning

BalanceCheckResult