1

Please help i'm trying to create an Associated token account for a PDA onchain using

        let create_accounts = Create {
            payer: ctx.accounts.signer.to_account_info().clone(),
            associated_token: ctx.accounts.token_escrow.to_account_info().clone(),
            authority: ctx.accounts.another_pda.to_account_info().clone(),
            mint: ctx.accounts.token_mint.to_account_info().clone(),
            system_program: ctx.accounts.system_program.to_account_info().clone(),
            token_program: ctx.accounts.token_program.to_account_info().clone(),
            rent: ctx.accounts.rent.to_account_info().clone(),
        };

        let cpi_ctx = CpiContext::new(
            self.associated_token_program.to_account_info().clone(),
            create_accounts,
        );

        anchor_spl::associated_token::create(cpi_ctx)?;


But I'm getting this error

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: An account required by the instruction is missing
2
  • Could be this related with issue of the spl sdk and solana version mismatch (version higher to 1.0.5 needs Solana blockchain in version 1.10? See solana.stackexchange.com/questions/2034/…
    – chalda
    Commented Oct 3, 2022 at 9:54
  • 1
    thanks @chalda i've gotten the issue... i was mixing up the associated token seeds
    – iMEF
    Commented Oct 3, 2022 at 15:15

1 Answer 1

0

it turns out i was generating the Associated token account using the wrong seeds.

Here's the proper way to go about it

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

const SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID: PublicKey = new PublicKey(
  'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
);

export async function findAssociatedTokenAddress(
  ownerAddress: PublicKey,
  tokenMintAddress: PublicKey,
  programId: PublicKey = SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID
): Promise<PublicKey> {
  return (await PublicKey.findProgramAddress(
    [
      ownerAddress.toBuffer(),
      TOKEN_PROGRAM_ID.toBuffer(),
      tokenMintAddress.toBuffer(),
    ],
    programId
  ))[0];
}

3
  • Ok but this answer is not the "onchain" answer. It's a client example of finding the PDA
    – Henry E
    Commented Oct 4, 2022 at 12:45
  • yeah... the answer is already in the question @HenryE
    – iMEF
    Commented Oct 4, 2022 at 13:22
  • The issue is if someone searches for how to create an onchain associated token account, this will not be the correct answer for them. The answer goes in the answer, the question in the question
    – Henry E
    Commented Oct 5, 2022 at 13:22

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.