5

Can you create a token account at a PDA that is not an "associated token account"? Meaning the PDA is derived with some arbitrary seeds that are different than what associated token accounts are derived with, i.e. ATAs are derived with the wallet address, token mint address, and token program id.

Say I want to create a token account at a PDA derived from seed1 and seed2 - how would I go about doing that in js/ts? I wanted to use the spl-token js SDK, but I haven't found a method that lets me do that. The "createAccount" method allows you to pass in a Keypair of the token account you want to create (presumably bc a signature is required to create the account) but I don't see anything in the SDK that allows for creating a token account with a PDA.

Obviously, the SDK supports creating associated token accounts, but the ATA is a different address than what I want.

2 Answers 2

7

Yes, you can use any address to create a token account. Non-ATA PDAs can also be used as token accounts. However, PDA is derived from a program. You can't derive PDA in the front-end just with the seeds without passing in a program.

When you are creating a token account, Token Program requires you to sign the transaction. If you use Keypair, the transaction is signed using private/public keypair whereas if you use PDA, the transaction is signed by the program. In the case of ATA, the transaction is signed using Associated Token Program.

But you can't use custom seeds with Associated Token Program. If you want to use custom seeds, create a program first. Then make a CPI call to Token Program from within your program and it will generate a token account for you with the required PDA.

5

The 1st answer explains well on why you cant do it from the SDK in client Side. So if you want to create a program an Do so this is how you could do so in an Anchor Program.

  #[account(
    init,
    seeds = [b"my-token-seed".as_ref()],
    bump,
    payer = authority,
    token::mint = mint,
    token::authority = authority,
)]
pub my_pda: Account<'info, TokenAccount>,

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.