Mint Close Authority Extension
Phần mở rộng MintCloseAuthority
là một Mint
extension cho phép authority đóng và lấy lại phí thuê từ Mint
account có cung hiện tại là 0.
Extension này hữu ích để dọn dẹp các mint không sử dụng và lấy lại SOL đã được sử dụng để trả cho phí thuê của account. Mint chỉ có thể được đóng khi không có token nào đang lưu thông.
Khởi tạo Mint Account
Để khởi tạo phần mở rộng MintCloseAuthority
trên Mint
account, chúng ta sẽ cần hàm initializeMintCloseAuthority()
.
Đây là cách tạo mint với phần mở rộng 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`);
Đóng Mint Account
Nếu Mint
account có supply bằng không, CloseMint Authority
có thể lấy lại phí thuê trên account đó bằng cách sử dụng instruction closeAccount
như thế này:
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`);