2

Is it possible to create associated token account and transfer token to that token account at the same time in one transaction?

4

1 Answer 1

3

Yes

In Solana, a transaction is a list of instructions.

allocating data is an instruction, token transfer is an instruction etc...

In this case you need an

  • associated token account instruction
  • transfer token instruction

example with @solana/web3.js

let tx = new Transaction().add(

  // create nonce account
   Token.createAssociatedTokenAccountInstruction(
        mintToken.associatedProgramId,
        mintToken.programId,
        mintPublicKey,
        associatedDestinationTokenAddr,
        destPublicKey,
        wallet.publicKey
      )


  // init nonce account
   Token.createTransferInstruction(
      TOKEN_PROGRAM_ID,
      fromTokenAccount.address,
      associatedDestinationTokenAddr,
      wallet.publicKey,
      [],
      amount
    )
  );

  console.log(
    `txhash: ${await connection.sendTransaction(tx, [feePayer, 
  nonceAccount])}`
);

You can find the example here

1
  • 1
    What's the associatedDestinationTokenAddr defined as? Commented Aug 5, 2022 at 21:51

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.