2

I am trying to call the create_associated_token_account function in my solana program. but I can't figure out what I am doing wrong.also when I run my program with my client, I get the error "invalid account data for this instruction"

pool_address = PDA account
tokenx_mint  = token mint address
last_ata     = same with my_ata_address, comes from the transaction accounts. 
token_program = token program 
let my_ata_address = spl_associated_token_account::get_associated_token_address(
                &pool_address, 
                tokenx_mint.key
            );
            
            invoke(&spl_associated_token_account::instruction::create_associated_token_account(
                    &my_ata_address, 
                    &pool_address, 
                    tokenx_mint.key, 
                    token_program.key
                ), &[
                    last_ata.clone(),
                    pool_account.clone(),
                    token_program.clone(),
                    tokenx_mint.clone()
                ]
            )?;

I add this to the keys array in my client (I was planning to make it dynamic another time):

{
                pubkey: new PublicKey("GENERATED_TOKEN_ATA_ADDRESS"),
                isWritable: true,
                isSigner: false
            }```
1
  • 1
    Can you also update the error message from compiler or error log?
    – wei1769
    Commented Jan 22 at 23:47

2 Answers 2

3

You've almost got it! Some of the parameters are incorrect for the call, so you should do:

            invoke(&spl_associated_token_account::instruction::create_associated_token_account(
                    &pool_address, 
                    &my_ata_address, 
                    tokenx_mint.key, 
                    token_program.key
                ), &[
                    pool_account.clone(),
                    last_ata.clone(),
                    tokenx_mint.clone(),
                    system_program.clone(),
                    token_program.clone(),
                ]
            )?;

And if pool_address is a PDA account for your program, you'll need to use invoke_signed along with the seeds used to derive the address.

For the instruction defined at https://github.com/solana-labs/solana-program-library/blob/65a92e6e0a4346920582d9b3893cacafd85bb017/associated-token-account/program/src/instruction.rs#L92

1

The create_associated_token_account return type of is wrong for invoking inside program.

The spl_associated_token_account crate is for client only.

You have to build the instruction with account info not account meta.

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.