1

Im getting this error "Transaction simulation failed: Error processing Instruction 0: incorrect program id for instruction" I think its because Im passing in the same account twice one as signer and the other time as one of the seeds for the associated account. Is there any way to make it work?

 public static TransactionInstruction createAssociatedTokenAccount(PublicKey funder, PublicKey receiver) throws Exception {
        PublicKey.ProgramDerivedAddress assoc = getAssociatedTokenAddress(receiver,funder);
        byte[]b= new byte[Integer.valueOf(0).byteValue()];
        PublicKey assocTokenProgram=new PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");

        List<AccountMeta> metas= new ArrayList<>();
        metas.add(new AccountMeta(funder,true,true));
        metas.add(new AccountMeta(assoc.getAddress(),false,true));
        metas.add(new AccountMeta(receiver,false,false));
        metas.add(new AccountMeta(funder,false,false));
        metas.add(new AccountMeta(SystemProgram.PROGRAM_ID,false,false));
        metas.add(new AccountMeta(new PublicKey(TOKEN_PROGRAM_ID),false,false));
        metas.add(new AccountMeta(Sysvar.SYSVAR_RENT_ADDRESS,false,false));
        TransactionInstruction instruction= new TransactionInstruction(assocTokenProgram,metas,b);
        return instruction;


    }
2
  • Kindly include your code. Difficult to give an helpful solution without it
    – Ademola
    Oct 4, 2022 at 18:37
  • @Ademola I included the code. This code works when the funder/signer is not passed in as one of the accounts to be used to create the associated account
    – Noe Trejo
    Oct 5, 2022 at 2:10

1 Answer 1

2

The fourth account provided must be the mint, and not the funder. So instead, you should do:

        List<AccountMeta> metas= new ArrayList<>();
        metas.add(new AccountMeta(funder,true,true));
        metas.add(new AccountMeta(assoc.getAddress(),false,true));
        metas.add(new AccountMeta(receiver,false,false));
        metas.add(new AccountMeta(mint,false,false));
        metas.add(new AccountMeta(SystemProgram.PROGRAM_ID,false,false));
        metas.add(new AccountMeta(new PublicKey(TOKEN_PROGRAM_ID),false,false));
        TransactionInstruction instruction= new TransactionInstruction(assocTokenProgram,metas,b);

You also don't need the rent, so I omitted it.

Here's the reference JS implementation: https://github.com/solana-labs/solana-program-library/blob/c5a6d0ae31e9b651d3255340f85853bb7d166283/token/js/src/instructions/associatedTokenAccount.ts#L80

1
  • I've realized now that the SystemProgram.createAccountWithSeed is more suitable for my use case than a PDA your answer is correct tho
    – Noe Trejo
    Oct 11, 2022 at 21:08

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.