Check wallet balances

The Swing SDK requires a connected wallet provider to initiate signing, sending transactions and checking balances. If you haven't already, check out our SDK guide on connecting a wallet;

Get balance for a single token

import type { ChainSlug, TokenSymbol } from '@swing.xyz/sdk';
 
const chainSlug: ChainSlug = 'ethereum';
const tokenSymbol: TokenSymbol = 'USDC';
const walletAddress = '0xabc...';
 
// Optionally check to make sure the wallet address is connected
await swingSDK.wallet.isConnected(walletAddress, chainSlug);
 
// Get the current balance for the chain-token pair
const balance = await swingSDK.wallet.getBalance(
  chainSlug,
  tokenSymbol,
  walletAddress,
);

Get balance for multiple tokens

Following the same instructions as above, you can get the balance for multiple tokens by executing a Promise.all. Note that fetching balances for all tokens on a chain is not recommended, since you will most likely hit RPC rate limits.

const tokensForChain = swingSDK.getTokensForChain(chainSlug);
 
const tokenBalances = await Promise.all(
  tokensForChain.map((token) =>
    swingSDK.wallet
      .getBalance(chainSlug, token.symbol, walletAddress)
      .then((balance) => ({ balance, token })),
  ),
);