1
async function ata() {
  const secretKey = Uint8Array.from(JSON.parse(process.env.PRIVATE_KEY));
  const keypair = Keypair.fromSecretKey(secretKey);
  const owner = new PublicKey(keypair.publicKey);
  console.log(owner);
  const tokenMint = new PublicKey(
    "So11111111111111111111111111111111111111112"
  );
  const TOKEN_PROGRAM_ID = new PublicKey(
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
  );
  const SYSVAR_RENT_PUBKEY = new PublicKey(SystemProgram.programId.toString());

  const keys = [
    { pubkey: owner, isSigner: true, isWritable: true },
    { pubkey: owner, isSigner: false, isWritable: true },
    { pubkey: tokenMint, isSigner: false, isWritable: false },
    { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
    { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  ];
  const instruction = new TransactionInstruction({
    keys,
    programId: ASSOCIATED_TOKEN_PROGRAM_ID,
    data: Buffer.from([]),
  });
  let transaction = new Transaction().add(instruction);
  let blockhash = await connection.getLatestBlockhash("finalized");

  transaction.feePayer = keypair.publicKey;
  transaction.recentBlockhash = blockhash;
  const txid = await sendAndConfirmTransaction(connection, transaction, [
    keypair,
  ]);
  console.log(txid);
}
ata();

hello, what should I change in code to avoid an error in javascript like this:

  logs: [
    'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]',
    'Program log: Create',
    'Program log: Error: Associated address does not match seed derivation',
    'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 3542 of 200000 compute units',
    'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL failed: Provided seeds do not result in a valid address'
  ]
}
SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Provided seeds do not result in a valid address
```?

1 Answer 1

2

I do not see you passing in the ATA at all in your function, you need to obtain the ATA and then pass it in.

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

  // Determine the correct Associated Token Account address
  const associatedTokenAccount = await getAssociatedTokenAddress(
    tokenMint,
    owner,
  );

  const keys = [
    { pubkey: owner, isSigner: true, isWritable: true },
    { pubkey: associatedTokenAccount, isSigner: false, isWritable: true },
    { pubkey: tokenMint, isSigner: false, isWritable: false },
    { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
    { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  ];

Now apart from that there is a much easier and cleaner process to create the instructions and that is utilizing createAssociatedTokenAccountInstruction

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

  // Determine the correct Associated Token Account address
  const associatedTokenAccount = await getAssociatedTokenAddress(
    tokenMint,
    owner,
  );

  let ataTransaction = new Transaction()
    .add(
      createAssociatedTokenAccountInstruction(
        owner,
        associatedTokenAccount,
        owner,
        tokenMint
      )
    );

https://solana-labs.github.io/solana-program-library/token/js/functions/getAssociatedTokenAddress.html

https://solana-labs.github.io/solana-program-library/token/js/functions/createAssociatedTokenAccountInstruction.html

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.