0

I was unable to determine from the documentation if the process of creating Associated Token Accounts is idempotent:

Is the best practice when transferring tokens to always assume that the ADA does not exist and create the ATA right before transferring the tokens? Or will that result in some kind of error?

1 Answer 1

1

You can call:

https://solana-labs.github.io/solana-program-library/token/js/modules.html#getOrCreateAssociatedTokenAccount before to ensure it exist.

Or if you want to be more efficient, check if it exists and if not, prepend the instructions to create it before you transfer instructions.

That's usually what I do, I got a getOrCreateAtaInstruction that returns null or the TransactionInstruction.

3
  • What about race conditions? What if two parties are trying to send the same token at the same time to a wallet that never had the token before? If both ask the system if it exists at the same time and both see that it does not exist, one of them creates it ok and the other one attempts to create it after it already exists from the first one. It's a rare problem that could be handled special I guess. :D
    – OCDev
    Commented Sep 13, 2022 at 2:25
  • 1
    Can use init_if_needed in Anchor , but feels like an overkill for this use case
    – Ohad Dahan
    Commented Sep 13, 2022 at 3:24
  • I agree, it is overkill. :)
    – OCDev
    Commented Sep 13, 2022 at 21:45

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.