admin管理员组

文章数量:1278789

I I've been able to get token balances for sol and uscd plus tokens like Pyth Network using @solana.web3js. However the same code fails when retrieving the balance for ai16z.

Simplified code looks like:

import { Connection } from "@solana/web3.js";
import { getAssociatedTokenAddressSync } from "@solana/spl-token";

private usdcMint = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
private ai16zMint = new PublicKey("HeLp6NuQkmYB4pYWo2zYs22mESHXPQYzXbB8n4V98jwC");

usdcTokenAccount = getAssociatedTokenAddressSync(usdcMint, publicKey);
ai16zTokenAccount = getAssociatedTokenAddressSync(ai16zMint, wallet.publicKey);

const usdcBalanceResult = solanaConnection.getTokenAccountBalance(usdcTokenAccount)
const ai16zBalanceResult = solanaConnection.getTokenAccountBalance(ai16zTokenAccount)

usdcBalance = usdcBalanceResult.value.value.uiAmount ?? 0;
ai16zBalance = ai16zBalanceResult.value.value.uiAmount ?? 0;

The pseudo-code above works for usdc but not for ai16z. For usdc, I always get this:

usdcBalance.status = 'fulfilled'

When for ai16z, I always get

ai16zBalanceResult.status = 'rejected'

With this error:

"failed to get token account balance: Invalid param: could not find account"

HeLp6NuQkmYB4pYWo2zYs22mESHXPQYzXbB8n4V98jwC is the correct account for ai16z. ai16z: usdc:

I I've been able to get token balances for sol and uscd plus tokens like Pyth Network using @solana.web3js. However the same code fails when retrieving the balance for ai16z.

Simplified code looks like:

import { Connection } from "@solana/web3.js";
import { getAssociatedTokenAddressSync } from "@solana/spl-token";

private usdcMint = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
private ai16zMint = new PublicKey("HeLp6NuQkmYB4pYWo2zYs22mESHXPQYzXbB8n4V98jwC");

usdcTokenAccount = getAssociatedTokenAddressSync(usdcMint, publicKey);
ai16zTokenAccount = getAssociatedTokenAddressSync(ai16zMint, wallet.publicKey);

const usdcBalanceResult = solanaConnection.getTokenAccountBalance(usdcTokenAccount)
const ai16zBalanceResult = solanaConnection.getTokenAccountBalance(ai16zTokenAccount)

usdcBalance = usdcBalanceResult.value.value.uiAmount ?? 0;
ai16zBalance = ai16zBalanceResult.value.value.uiAmount ?? 0;

The pseudo-code above works for usdc but not for ai16z. For usdc, I always get this:

usdcBalance.status = 'fulfilled'

When for ai16z, I always get

ai16zBalanceResult.status = 'rejected'

With this error:

"failed to get token account balance: Invalid param: could not find account"

HeLp6NuQkmYB4pYWo2zYs22mESHXPQYzXbB8n4V98jwC is the correct account for ai16z. ai16z: https://solscan.io/token/HeLp6NuQkmYB4pYWo2zYs22mESHXPQYzXbB8n4V98jwC usdc: https://solscan.io/token/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v

Share Improve this question edited Feb 24 at 18:50 Biosopher asked Feb 24 at 3:58 BiosopherBiosopher 6166 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found my answer here: https://solana.stackexchange/questions/8549/how-to-getprogramaccounts-for-token-2022-program

Plus this helped with a more simplified set of code to follow: https://www.quicknode/guides/solana-development/spl-tokens/how-to-get-all-tokens-held-by-a-wallet-in-solana

Turns out that ai16z tokens are part of the 2022 token program so must be looked up in a different manner.

本文标签: Cannot access spltoken account balance for ai16z using solanaweb3jsStack Overflow