4

can someone please help me understand the difference between associated_token::mint and token::mint. I've seen both used interchangeably in anchor account contexts when setting constraints for token accounts.

For example:

    ...
    nft_mint: Account<'info, Mint>,
    #[account(
        token::mint=nft_mint,
        token::authority=signer
    )]
    nft_token_account: Account<'info, TokenAccount>,

Or

    ...
    nft_mint: Account<'info, Mint>,
    #[account(
        associated_token::mint=nft_mint,
        associated_token::authority=signer
    )]
    nft_token_account: Account<'info, TokenAccount>,

I've also seen this used too

    ...
    nft_mint: Account<'info, Mint>,
    #[account(
        constraint=nft_token_account.owner == signer.key(),
        constraint=nft_token_account.mint == nft_mint.key()
    )]
    nft_token_account: Box<Account<'info, TokenAccount>>,

1 Answer 1

7

Tokenaccounts of a publickey that have a corresponding private key are usually Associated Token Accounts(ATA). An ATA is a PDA that uses the publickey and token mint as seeds. However, to generate a token account that is a PDA, you generate the PDA from your custom seeds and then instruct the Token Program to create a token account with your specified token_mint and specified authority.

associated_token::mint = mint and associated_token::authority = signer enforces that the token account generated uses the specified mint and signer as seeds alongside enforcing that the token account corresponds to the specified mint and the token account authority corresponds to the signer while token::mint = mint and token::authority = signer only enforce that the token account corresponds to the specified mint and the token account authority corresponds to the specified signer.

Hence, using associated_token:: would not work for token accounts that are PDAs, token:: should be used instead. Consequently, this means that multiple token accounts for a publickey that has a private key can be generated but only one of them can be an ATA.

    constraint = nft_token_account.owner == signer.key(),
    constraint = nft_token_account.mint == nft_mint.key()

These constraints above just check the mint and authority of a token account(works for either PDA or non-PDA token account if the right constraints are passed). The above constraints are an alternative to either using token:: or associated_token:: and only work for an already existing token account.

1
  • While trying to answer your question, I discovered something when testing my assumptions, I think you should also check this out. Commented Sep 27, 2022 at 9:48

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.