Skip to main content
Get token balances for any address with account-specific balance information.
The DZap SDK allows you to retrieve token balances when providing an account address, giving you access to user-specific balance data along with token information.

Get Tokens with Balances

Get all supported tokens with user balance information:
import { DZapClient } from "@dzapio/sdk";

const dZap = DZapClient.getInstance();

// Get tokens with account balances
const tokensWithBalances = await dZap.getAllTokens(
  42161, // chainId
  userAccount // account address
);

console.log("Tokens with balances:", tokensWithBalances);

// Access specific token balance
const usdcToken = tokensWithBalances["0xaf88d065e77c8cC2239327C5EDb3A432268e5831"];
console.log("USDC Balance:", usdcToken.balance);
console.log("USDC Balance in USD:", usdcToken.balanceInUsd);

Get Specific Token Details with Balance

Get detailed information for a specific token including balance:
// Get detailed information for a specific token with balance
const tokenDetails = await dZap.getTokenDetails(
  "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC address
  1, // Ethereum mainnet
  userAccount, // account (required for balance)
  true, // includeBalance
  false // includePrice (optional)
);

console.log("Token Details with Balance:", tokenDetails);
console.log("User Balance:", tokenDetails.balance);
console.log("Balance in USD:", tokenDetails.balanceInUsd);

Calculate Portfolio Value

Sum up the USD value of all holdings on a chain:
const tokensWithBalances = await dZap.getAllTokens(42161, userAccount);

// Filter tokens with a non-zero balance and calculate total value
const holdings = Object.values(tokensWithBalances).filter(
  (token) => token.balance && token.balance !== "0"
);

const totalValueUsd = holdings.reduce(
  (sum, token) => sum + (token.balanceInUsd ?? 0),
  0
);

console.log(`Tokens held: ${holdings.length}`);
holdings.forEach((token) => {
  console.log(
    `  ${token.symbol}: ${token.balance} ($${token.balanceInUsd?.toFixed(2) ?? "N/A"})`
  );
});
console.log(`Total portfolio value: $${totalValueUsd.toFixed(2)}`);

Balance Information Structure

When an account is provided, each token includes balance information:
type TokenInfoWithBalance = {
  contract: string; // Contract address
  symbol: string; // Token symbol (e.g., "USDC")
  decimals: number; // Number of decimals
  name: string; // Full name (e.g., "USD Coin")
  balance: string; // User's balance in token units
  chainId: number; // Chain ID where token exists
  balanceInUsd?: number | null; // USD value of user's balance
  price?: string; // Current price in USD (if available)
  isDisabledOnSwapBridge?: {
    source: boolean; // Disabled as source token
    destination: boolean; // Disabled as destination token
  };
  isDisabledOnZap?: {
    source: boolean; // Disabled for zap operations
    destination: boolean;
  };
};

Next Steps