2

In the code below if I don't have token_program defined, I get compile time error. However, I am only initializing the ATA but not minting, transferring, approving, or burning the token mint which is handled by token program.

 #[account(
        init_if_needed,
        payer = buyer,
        associated_token::mint = mint,
        associated_token::authority = buyer
    )]
    pub buyer_mint: InterfaceAccount<'info,TokenAccount>,

    pub associated_token_program: Program<'info, AssociatedToken>
    pub token_program: Interface<'info, TokenInterface>,

But if I assume the ATA is already initialized below, I don't get any compile time errors if I omit both token program and atp which makes sense because the program assumes its an existing ata.

 #[account(
        mut,
        associated_token::mint = mint,
        associated_token::authority = buyer
    )]
    pub buyer_mint: InterfaceAccount<'info,TokenAccount>,

My question is what does token_program has to do in the first code base ?

1 Answer 1

4

TL;DR, solana needs to know all accounts that an instruction will interact with beforehand. That is why your program will panic if an account required by the #[derive(Accounts)] struct isn't provided

In your case,

   #[account(
        init_if_needed,
        payer = buyer,
        associated_token::mint = mint,
        associated_token::authority = buyer
    )]

this attribute macro will create the ATA if it doesn't exist because of the init_if_needed clause. As you've already guessed, you'll also need to pass in the associated_token_program responsible for creating this account.

In the second snippet

 #[account(
        mut,
        associated_token::mint = mint,
        associated_token::authority = buyer
    )]
    pub buyer_mint: InterfaceAccount<'info,TokenAccount>,

An ATA is simply a token account that's mapped to a particular address and that's why this is still correct.

I think that maybe an error might have been in the snippet because the account name is buyer_mint

Finally, specify the token_program because it's responsible for all SPL token operations.

2
  • Ahh.. So basically the token_program is needed to create the ATA if it doesn't exist and thats why we need it ?
    – WowBow
    Commented Sep 2 at 17:05
  • 1
    ATA is created by the ATA program. Token account by the token program. An ATA is a token account but not the other way around
    – Jimii
    Commented Sep 13 at 8:33

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.