Skip to main content

Assets

The SDK provides simple methods to query balances and asset information across all chains.

Get Wallet Balances

Get all balances for a wallet across all chains:
const balances = await wallet.getBalances();

balances.forEach(balance => {
  console.log(`${balance.symbol}: ${balance.formattedBalance}`);
  console.log(`Chain: ${balance.chainId}`);
  console.log(`USD Value: $${balance.usdValue}`);
});

Get Balances for Chain

Get balances for a specific chain:
const balances = await wallet.getBalances({
  chainId: 'eip155:8453' // Base
});

balances.forEach(balance => {
  console.log(`${balance.symbol}: ${balance.formattedBalance}`);
});

Get Address Balances

Get balances for a specific address:
const balances = await wallet.getAddressBalances({
  address: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
  chainId: 'eip155:1',
  includeTokens: true, // Include ERC-20 tokens
  includeNFTs: false // Exclude NFTs
});

console.log('Native balance:', balances.nativeBalance);
console.log('Token balances:', balances.tokenBalances);
console.log('Total USD value:', balances.totalUsdValue);

Get Token Info

Get detailed information about a token:
const token = await axonVault.getTokenInfo({
  chainId: 'eip155:1',
  contractAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC
});

console.log('Name:', token.name);
console.log('Symbol:', token.symbol);
console.log('Decimals:', token.decimals);
console.log('Price:', token.price.usd);
console.log('Logo:', token.logoUrl);

Get Native Token Info

Get information about the native token (ETH, SOL, etc.):
const nativeToken = await axonVault.getTokenInfo({
  chainId: 'eip155:1',
  contractAddress: 'native' // or null
});

console.log('Name:', nativeToken.name); // Ethereum
console.log('Symbol:', nativeToken.symbol); // ETH
console.log('Decimals:', nativeToken.decimals); // 18

Watch Balance Changes

Listen for balance updates:
// Watch wallet balances
wallet.on('balance:updated', (balances) => {
  console.log('Balances updated:', balances);
  // Update UI
});

// Watch specific address
wallet.on('address:balance:updated', (data) => {
  console.log('Address balance updated:', data.address, data.balance);
});

Refresh Balances

Manually refresh balances:
// Refresh all wallet balances
await wallet.refreshBalances();

// Refresh specific chain
await wallet.refreshBalances({
  chainId: 'eip155:8453'
});

Balance Formatting

The SDK provides formatted balances for display:
const balances = await wallet.getBalances();

balances.forEach(balance => {
  // Raw balance (string to preserve precision)
  console.log('Raw:', balance.balance); // "1500000000000000000"
  
  // Formatted balance (human-readable)
  console.log('Formatted:', balance.formattedBalance); // "1.5"
  
  // USD value
  console.log('USD:', balance.usdValue); // "3750.00"
  
  // Symbol
  console.log('Symbol:', balance.symbol); // "ETH"
});

Error Handling

import { AssetError } from '@axonvault/embedded-wallet-sdk';

try {
  const balances = await wallet.getBalances();
} catch (error) {
  if (error instanceof AssetError) {
    switch (error.code) {
      case 'CHAIN_NOT_SUPPORTED':
        console.error('Chain not supported');
        break;
      case 'INVALID_ADDRESS':
        console.error('Invalid address');
        break;
      case 'RPC_ERROR':
        console.error('RPC error:', error.message);
        break;
      default:
        console.error('Asset error:', error.message);
    }
  }
}

Best Practices

  • SDK caches balances automatically
  • Use event listeners for real-time updates
  • Refresh balances only when needed
  • Always use string for balance calculations
  • Use formattedBalance for display only
  • Use BigInt for arithmetic operations
  • Show loading states while fetching balances
  • Display USD values when available
  • Update balances after transactions