Mint Close Authority 扩展
MintCloseAuthority
扩展是一个 Mint
扩展,允许权限方关闭并从当前供应量为 0 的 Mint
账户中取回租金。
此扩展对于清理未使用的铸币账户并回收用于支付账户租金豁免的 SOL 非常有用。只有在没有代币流通时,铸币账户才能被关闭。
初始化铸币账户
要在 Mint
账户上初始化 MintCloseAuthority
扩展,我们需要使用 initializeMintCloseAuthority()
函数。
以下是如何创建带有 Mint Close 扩展的铸币账户:
import {
Keypair,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
createInitializeMintInstruction,
createInitializeMintCloseAuthorityInstruction,
getMintLen,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
const mint = Keypair.generate();
// Calculate the size needed for a Mint account with Transfer Fee extension
const mintLen = getMintLen([ExtensionType.MintCloseAuthority]);
// Calculate minimum lamports required for rent exemption
const lamports = await connection.getMinimumBalanceForRentExemption(mintLen);
// Create the account with the correct size and owner
const createAccountInstruction = SystemProgram.createAccount({
fromPubkey: keypair.publicKey,
newAccountPubkey: mint.publicKey,
space: mintLen,
lamports,
programId: TOKEN_2022_PROGRAM_ID,
});
// Initialize the Mint Close Authority extension
const initializeMintCloseAuthority = createInitializeMintCloseAuthorityInstruction(
mint.publicKey,
keypair.publicKey,
TOKEN_2022_PROGRAM_ID,
);
// Initialize the mint itself
const initializeMintInstruction = createInitializeMintInstruction(
mint.publicKey,
6,
keypair.publicKey,
null,
TOKEN_2022_PROGRAM_ID,
);
// Combine all instructions in the correct order
const transaction = new Transaction().add(
createAccountInstruction,
initializeMintCloseAuthority,
initializeMintInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, mint]);
console.log(`Mint created! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
关闭铸币账户
如果 Mint
账户的供应量为零,CloseMint Authority
可以通过使用 closeAccount
指令来回收该账户的租金,如下所示:
const closeMintInstruction = createCloseAccountInstruction(
mint.publicKey,
keypair.publicKey,
keypair.publicKey,
[],
TOKEN_2022_PROGRAM_ID,
);
const transaction = new Transaction().add(closeMintInstruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair], {skipPreflight: false});
console.log(`Mint closed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);