DeveloperBreeze

Fetch Balance Development Tutorials, Guides & Insights

Unlock 1+ expert-curated fetch balance tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your fetch balance skills on DeveloperBreeze.

Fetching Address Details from Solana

Tutorial August 09, 2024
javascript json

Finally, let's fetch the token holdings of the wallet:

async function getTokenHoldings(walletAddress) {
  try {
    const publicKey = new solanaWeb3.PublicKey(walletAddress);
    const tokenAccounts = await connection.getParsedTokenAccountsByOwner(publicKey, {
      programId: solanaWeb3.TOKEN_PROGRAM_ID,
    });

    console.log('Token Holdings:');
    tokenAccounts.value.forEach((tokenAccount) => {
      const tokenAmount = tokenAccount.account.data.parsed.info.tokenAmount.uiAmount;
      const tokenMint = tokenAccount.account.data.parsed.info.mint;
      console.log(`- Token Mint: ${tokenMint}`);
      console.log(`  Amount: ${tokenAmount}`);
    });
  } catch (error) {
    console.error('Error fetching token holdings:', error);
  }
}

getTokenHoldings(walletAddress);