1

I have the following code that works fine with PDAs.

const escrow_pda = PublicKey.findProgramAddressSync([Buffer.from("escrow"), fundraiser.publicKey.toBytes()], program.programId)[0];
const esc = await program.account.campaignEscrow.fetch(escrow_pda);

However, when I use the same fetch method on the following ATA, it returns Error: Invalid account discriminator

const vault_ata = (await getOrCreateAssociatedTokenAccount(provider.connection, fundraiser, mint_token, escrow_pda, true)).address
    const esc = await program.account.campaignEscrow.fetch(vault_ata);

What methods do you use to fetch data from the associated token account in typescript ?

EDIT:

How can I also get the token balance in Rust ?

//TokenAccount
pub donor_ata: Account<'info, TokenAccount>,

1 Answer 1

2

program.account is used to help fetch and deserialize the custom accounts from your program.

To get the data for a Token Account in TS, use getAccount from @solana/spl-token:

import { getAccount } from "@solana/spl-token";

const TokenAccountData = await getAccount(connection, TokenAccountAddress)

You can get the Token Account data in Anchor using ctx.accounts.donor_ata.amount in your instruction logic.

1
  • I was able to figure it out this morning but forgot to update. Accepting answer! Thank you ser!
    – WowBow
    Commented Dec 14, 2023 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.