4

Solana's Token 22 tutorial tells you how to upgrade to 2022, but most deployed programs need to support existing accounts that were made with the Token program.

Let's say we have an ix that makes a classic TokenAccount as a PDA, a very common implementation:

  pub some_mint: Account<'info, Mint>,
  #[account(
    init,
    // seeds, payer, mint, authority
  )]
  pub some_acc: Account<'info, TokenAccount>,
  pub token_program: Program<'info, Token>,

Now we want to support mints made with Token2022, which should open a token2022 account instead. We might change this to:

    #[account(
      // validate owner = token2022
    )]
    pub some_mint: InterfaceAccount<'info, MintInterface>,
    #[account(
      // same as before...
      owner = token22;
    )]
    pub some_acc: InterfaceAccount<'info, TokenAccountInterface>,
    pub token_program: Program<'info, Token2022>,

But what if we want to make a Token account if Mint uses the regular Token program, or a Token22 account if Mint uses the Token22 program? Do we have to make two instructions?

2 Answers 2

5

You can try replacing:

pub token_program: Program<'info, Token2022>,

With:

pub token_program: Interface<'info, TokenInterface>,

Which should allow for both for token program addresses.

https://github.com/coral-xyz/anchor/blob/master/lang/src/accounts/interface.rs

https://github.com/coral-xyz/anchor/blob/master/spl/src/token_interface.rs

1
  • This works great! Posting a longer version of this answer below if anybody wants the "complete" version.
    – Whiteseal
    Commented Apr 1 at 23:01
2

Expounding on @john's answer:

use anchor_spl::token_interface::{Mint as MintInterface, TokenAccount as TokenAccountInterface, TokenInterface};
 
    pub mint: InterfaceAccount<'info, MintInterface>,

    #[account(
      init,
      // seeds, payer, authority, etc
    )]
    pub vault: InterfaceAccount<'info, TokenAccountInterface>,

    pub token_program: Interface<'info, TokenInterface>,

Note that the owner = blah_blah in the InterfaceAccount init decorator must be omitted, Anchor will figure it out. If you include it, since it requires a static Pubkey, you can't allow both Token22 and Token programs as owner.

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.