5

how can i use Token-2022 in anchor program? There is anchor_spl crate but i dont see any reference to Token-2022. There are examples only with javascript but no for anchor program https://spl.solana.com/token-2022/extensions.

The functionality and structures in Token-2022 are a strict superset of Token

This means that i can use anchor_spl crate to work with accounts like with old spl token program without compatibility issues? How can i call functions in Token-2022 from anchor? My goal is create tokens which user cannot transfer until anchor program allow it

4 Answers 4

4

Giving more context to using token-2022 with Anchor:

Token-2022 is a different token program, and with the a different program id. The program also includes a number of extensions that you can use with the token program, namely:

Closing Mint Accounts
Transfer Fees
Default Account States
Immutable Owner
Non-Transferable Tokens
Interest-bearing Mints
Required Memo on Transfer

In order for Anchor to be fully compatible to token-2022, it needs to either update or add a similar crate to the anchor-spl crate.

At the current time of writing, this is not implemented.

2

Hey there with anchor you can import - token_interface.

Under the hood its importing the token_2022 - pub use crate::token_2022::*;

A few examples a priori and a posteriori of the Anchor 0.28.0 version.

Importing

pre v0.28

use anchor_spl::{token::{Mint, TokenAccount, Token,
MintTo, mint_to,
CloseAccount, close_account,
Transfer, transfer as transfer_spl,}, associated_token::AssociatedToken};

pos v0.28

use anchor_spl::{
token_interface::{TokenAccount, Mint, MintTo, mint_to,
CloseAccount, close_account,   
TokenInterface, TransferChecked, transfer_checked}, 
associated_token::AssociatedToken

};

Token Accounts:

pre v0.28

ata: Account<'info, TokenAccount>,

pos v0.28

ata: InterfaceAccount<'info, TokenAccount>

Mint Accounts:

pre v0.28

mint: Account<'info, Mint>,

pos v0.28

mint: InterfaceAccount<'info, Mint>,

Token Program

pre v0.28

token_program: Program<'info, Token>,

pos v0.28

token_program: Interface<'info, TokenInterface>,

CPIS TransferSPL:

pre v0.28

let accounts = TransferSpl {
        from: self.owner_ata.to_account_info(),
        to: self.stake_ata.to_account_info(),
        authority: self.owner.to_account_info()
    };

    let ctx = CpiContext::new(
        self.token_program.to_account_info(),
        accounts
    );
    transfer_spl(ctx, amount)

pos v0.28

let accounts = TransferChecked {
        from: self.owner_ata.to_account_info(),
        to: self.stake_ata.to_account_info(),
        authority: self.owner.to_account_info(),
        mint: self.mint.to_account_info(),

    };

    let ctx = CpiContext::new(
        self.token_program.to_account_info(),
        accounts
    );
    transfer_checked(ctx, amount, self.mint.decimals)
}

The MintTo and CloseAccount remain unchanged.

1
  • This answer definitely deserves more upvote!!
    – Yan
    Mar 13 at 8:42
1

Actually it is very compatible; this has been around in anchor since v0.28

You have to use InterfaceAccount instead of account for mints and token accounts, also everything works pretty much the same, except for the fact that you're gonna need to write some cpi wrappers for your token 2022 instructions that aren't already on the old token program.

Take a look at the bottom examples in anchor docs: https://docs.rs/anchor-lang/latest/anchor_lang/derive.Accounts.html

0

Well token-2022 instructions are not anchor compatible yet(at the time of this writing). Idk if someone is already working on it or not but happy to start if no one is

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.