Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt

Use this file to discover all available pages before exploring further.

You can get the token balances for a wallet account by calling the getBalances function. This returns native and token balances for a single network, unlike getMultichainBalances which queries across multiple chains. If you only need the native token balance, use getBalance instead.

Usage

import { getBalances } from '@dynamic-labs-sdk/client';

const balances = await getBalances({ walletAccount });

With options

import { getBalances } from '@dynamic-labs-sdk/client';

const balances = await getBalances({
  walletAccount,
  networkId: 137,
  includePrices: true,
  includeNative: true,
  filterSpamTokens: true,
  forceRefresh: true,
  whitelistedContracts: ['0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174'],
});

Parameters

ParameterTypeRequiredDescription
walletAccountWalletAccountYesThe wallet account to get the balances for
networkIdnumberNoThe network ID to query. Defaults to the active network
includePricesbooleanNoInclude token prices in the response
includeNativebooleanNoInclude the native token balance
filterSpamTokensbooleanNoFilter out spam tokens (default: true)
forceRefreshbooleanNoForce a refresh instead of using cached values
whitelistedContractsstring[]NoOnly return balances for these contract addresses

React

Fetch balances in a useEffect and refresh when the wallet account changes:
import { getBalances } from '@dynamic-labs-sdk/client';
import { useEffect, useState } from 'react';

function TokenBalances({ walletAccount }) {
  const [balances, setBalances] = useState([]);

  useEffect(() => {
    if (!walletAccount) return;
    getBalances({ walletAccount, includePrices: true }).then(setBalances);
  }, [walletAccount]);

  return (
    <ul>
      {balances.map((token) => (
        <li key={token.address ?? 'native'}>
          {token.symbol}: {token.balance}
        </li>
      ))}
    </ul>
  );
}

Response

Returns a Promise<TokenBalance[]> — an array of token balance objects.