Anchor
使用 Anchor 的 Token2022

使用 Anchor 的 Token2022

Mint Close Authority 扩展

MintCloseAuthority 扩展是一个 Mint 扩展,允许权限方关闭并从当前供应量为 0 的 Mint 账户中取回租金。

此扩展对于清理未使用的铸币账户并回收用于支付账户租金豁免的 SOL 非常有用。只有当没有代币在流通时,铸币账户才能被关闭。

初始化铸币账户

要在 Mint 账户上初始化 MintCloseAuthority 扩展,我们可以简单地使用 Anchor 为我们创建的宏。

以下是如何使用 Mint Close 扩展创建铸币账户:

#[derive(Accounts)]
pub struct CreateMint<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(
        init,
        payer = signer,
        mint::decimals = 6,
        mint::authority = signer.key(),
        mint::token_program = token_program
        extensions::close_authority::authority = signer,
    )]
    pub mint: InterfaceAccount<'info, Mint>,
    pub system_program: Program<'info, System>,
    pub token_program: Interface<'info, TokenInterface>,
}

关闭铸币账户

如果 Mint 账户的供应量为零,CloseMint Authority 可以通过使用 close_account() 指令来回收该账户的租金,如下所示:

use anchor_lang::prelude::*;
use anchor_lang::system_program::{create_account, CreateAccount};
use anchor_spl::{
    token_2022::{close_account, CloseAccount},
    token_interface::{
        spl_token_2022::{
            extension::{
                mint_close_authority::MintCloseAuthority
            },
        },
        Mint, Token2022,
    },
};
 
#[derive(Accounts)]
pub struct Close<'info> {
    #[account(mut)]
    pub authority: Signer<'info>,
 
    #[account(
        mut,
        extensions::close_authority::authority = authority,
    )]
    pub mint_account: InterfaceAccount<'info, Mint>,
    pub token_program: Program<'info, Token2022>,
}
 
pub fn close(ctx: Context<Close>) -> Result<()> {
    // cpi to token extensions programs to close mint account
    // alternatively, this can also be done in the client
    close_account(CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        CloseAccount {
            account: ctx.accounts.mint_account.to_account_info(),
            destination: ctx.accounts.authority.to_account_info(),
            authority: ctx.accounts.authority.to_account_info(),
        },
    ))?;
    Ok(())
}
Blueshift © 2025Commit: fd080b2