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);
This function uses the getParsedTokenAccountsByOwner
method to retrieve token accounts owned by the wallet address. It then logs the token mint address and the amount held.