0

I understand the differences between token mint accounts and token associated accounts. As I'm building a web-app allowing people to mint spl tokens on our website, we are facing a question about who gets to create the ATA accounts for users? I thought if init_if_needed is specified for token_ata, the rust program will handle it automatically, but I've tried a few times, and the program complains about the lack of ATA accounts.

That said, my question is if a user wants to interact with a function on our website, do we need to make it a two-step process, one is to create an ATA account for this user and the other is to interact with the function with the newly generated ATA account?

Although the generation of ATA account address is quick and easy, the actual creation of such an account on Solana network does take a transaction to complete? Maybe several instructions including the one for creating an ATA account can be bundled together?

Thanks for sharing valueable experiences or insights if there is any.

#[derive(Accounts)]
pub struct MintToken<'info> {
    #[account(mut)]
    pub wallet: Signer<'info>,
    #[account(
        seeds = [b"config"],
        bump = config.bump
    )]
    pub config: Account<'info, Config>,
    #[account(mut)]
    pub token_mint: Account<'info, Mint>,
    #[account(
        init_if_needed,
        payer = wallet,
        associated_token::authority = wallet,
        associated_token::mint = token_mint
    )]
    pub token_ata: Account<'info, TokenAccount>,

1 Answer 1

1

There's a few questions to sort through here.

If you want your example to work, you'll also need to pass the associated token account program and the token program, ie:

#[derive(Accounts)]
pub struct MintToken<'info> {
    #[account(mut)]
    pub wallet: Signer<'info>,
    #[account(
        seeds = [b"config"],
        bump = config.bump
    )]
    pub config: Account<'info, Config>,
    #[account(mut)]
    pub token_mint: Account<'info, Mint>,
    #[account(
        init_if_needed,
        payer = wallet,
        associated_token::authority = wallet,
        associated_token::mint = token_mint
    )]
    pub token_ata: Account<'info, TokenAccount>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}

Lifted from this example: https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/associated_token

Otherwise, there are many ways to create the associated token account. You can do it inside of your program, as a separate instruction in a transaction, or as a separate transaction.

Typically it's simpler and cleaner to do the work on the client side by adding a separate instruction in a transaction to create the account if needed, and avoid requiring all of the accounts and logic in your program. But it's up to you!

Multiple transactions is not a great user experience, since they'll need to sign and send the transactions separately, and wait for the first to land, which can take time. You should aim to bundle all of the work in one transaction as much as possible.

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.