The Mint Close Authority Extension
The MintCloseAuthority
extension is a Mint
extension that allows the authority to close and retrieve the rent from a Mint
account that has a current supply of 0.
This extension is useful for cleaning up unused mints and reclaiming the SOL that was used to pay for the account's rent exemption. The mint can only be closed when no tokens are in circulation.
Initializing the Mint Account
To initialie the MintCloseAuthority
extension on a Mint
account we can simply use the macro that Anchor
created for us.
Here's how to create a mint with the Mint Close extension:
#[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>,
}
Closing the Mint Account
If a Mint
account has a supply of zero, the CloseMint Authority
can reclaim the rent on that account by usingin the close_account()
instruction like so:
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(())
}