Tutup Mint

Cara mengaktifkan ekstensi MintCloseAuthority

Ekstensi MintCloseAuthority memungkinkan mint account untuk ditutup dan rent-nya diklaim kembali. Ekstensi ini harus diinisialisasi saat mint dibuat.

Hanya close authority yang ditunjuk yang dapat menutup mint account. Mirip dengan bagaimana token account hanya dapat ditutup ketika saldo token adalah nol, mint account dengan ekstensi ini hanya dapat ditutup ketika seluruh pasokan token telah berkurang menjadi nol.

Typescript

import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "@solana/kit";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
getInitializeMintInstruction,
getMintSize,
TOKEN_2022_PROGRAM_ADDRESS,
extension,
getInitializeMintCloseAuthorityInstruction,
getCloseAccountInstruction
} from "@solana-program/token-2022";
// Create Connection, local validator in this example
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
// Generate the authority for the mint, (will also act as the fee payer)
// and destination to receive recovered SOL after closing the mint
const authority = await generateKeyPairSigner();
const destination = await generateKeyPairSigner();
// Fund authority/fee payer
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: authority.address,
lamports: lamports(5_000_000_000n), // 5 SOL
commitment: "confirmed"
});
// Generate keypair to use as address of mint
const mint = await generateKeyPairSigner();
// And a mint close authority extension.
const mintCloseAuthorityExtension = extension("MintCloseAuthority", {
closeAuthority: authority.address
});
// Get default mint account size (in bytes), with the close mint extension
const space = BigInt(getMintSize([mintCloseAuthorityExtension]));
// Get minimum balance for rent exemption
const rent = await rpc.getMinimumBalanceForRentExemption(space).send();
// Get latest blockhash to include in transaction
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
// Instruction to create new account for mint (token program)
// Invokes the system program
const createAccountInstruction = getCreateAccountInstruction({
payer: authority,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
});
// Instruction to initialize the close mint authority extension
const initializeCloseMintInstruction =
getInitializeMintCloseAuthorityInstruction({
mint: mint.address,
closeAuthority: mintCloseAuthorityExtension.closeAuthority
});
// Instruction to initialize mint account data
// Invokes the token program
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 9,
mintAuthority: authority.address
});
const instructions = [
createAccountInstruction,
initializeCloseMintInstruction,
initializeMintInstruction
];
// Create transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(authority, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instructions, tx)
);
// Sign transaction message with all required signers
const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed", skipPreflight: true }
);
// // Get transaction signature
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Mint Address:", mint.address.toString());
console.log("Transaction Signature:", transactionSignature);
// Get a fresh blockhash for the close mint extension instruction
const { value: latestBlockhash2 } = await rpc.getLatestBlockhash().send();
const closeMintInstruction = getCloseAccountInstruction({
account: mint.address,
destination: destination.address,
owner: authority
});
// Create transaction message for close mint extension
const closeMintMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(authority, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash2, tx),
(tx) => appendTransactionMessageInstructions([closeMintInstruction], tx)
);
// Sign transaction message with all required signers
const signedCloseMintTx =
await signTransactionMessageWithSigners(closeMintMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedCloseMintTx,
{ commitment: "confirmed" }
);
// Get transaction signature
const closeMintTxSig = getSignatureFromTransaction(signedCloseMintTx);
console.log("\nDestination Address:", destination.address.toString());
console.log("Transaction Signature:", closeMintTxSig);
Console
Click to execute the code.

Rust

Rust
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction::create_account;
use spl_token_2022_interface::{
extension::{
mint_close_authority::MintCloseAuthority, BaseStateWithExtensions, ExtensionType,
StateWithExtensions,
},
instruction::{close_account, initialize_mint, initialize_mint_close_authority},
state::Mint,
ID as TOKEN_2022_PROGRAM_ID,
};
#[tokio::main]
async fn main() -> Result<()> {
// Create connection to local validator
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let latest_blockhash = client.get_latest_blockhash().await?;
// Generate a new keypair for the fee payer
let fee_payer = Keypair::new();
// Generate a keypair for the destination
let destination = Keypair::new();
// Airdrop 5 SOL to fee payer
let airdrop_signature = client
.request_airdrop(&fee_payer.pubkey(), 5_000_000_000)
.await?;
loop {
let confirmed = client.confirm_transaction(&airdrop_signature).await?;
if confirmed {
break;
}
}
// Generate keypair to use as address of mint
let mint = Keypair::new();
// Get default mint account size (in bytes),
let mint_space =
ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::MintCloseAuthority])?;
// get the rent in lamports for the mint account
let mint_rent = client
.get_minimum_balance_for_rent_exemption(mint_space)
.await?;
// Instruction to create new account for mint
let create_account_instruction = create_account(
&fee_payer.pubkey(), // payer
&mint.pubkey(), // new account (mint)
mint_rent, // lamports
mint_space as u64, // space
&TOKEN_2022_PROGRAM_ID, // program id
);
let initialize_close_mint_instruction = initialize_mint_close_authority(
&TOKEN_2022_PROGRAM_ID, // token program
&mint.pubkey(), // mint
Some(&fee_payer.pubkey()), // close authority
)?;
// Instruction to initialize mint account data
let initialize_mint_instruction = initialize_mint(
&TOKEN_2022_PROGRAM_ID, // token program
&mint.pubkey(), // mint
&fee_payer.pubkey(), // mint authority
Some(&fee_payer.pubkey()), // freeze authority
9, // decimals
)?;
// Create transaction and add instructions
let transaction = Transaction::new_signed_with_payer(
&[
create_account_instruction,
initialize_close_mint_instruction,
initialize_mint_instruction,
],
Some(&fee_payer.pubkey()),
&[&fee_payer, &mint],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
println!("Mint Address: {}", mint.pubkey());
// Fetch mint account
let mint_account = client.get_account(&mint.pubkey()).await?;
// Deserialize the mint account with extensions
let mint_state = StateWithExtensions::<Mint>::unpack(&mint_account.data)?;
// Get all extension types enabled on this mint
let mint_extension_types = mint_state.get_extension_types()?;
println!("\nMint extensions enabled: {:?}", mint_extension_types);
// Deserialize the MintCloseAuthority extension data
let mint_close_authority = mint_state.get_extension::<MintCloseAuthority>()?;
println!("\n{:#?}", mint_close_authority);
let close_mint_instruction = close_account(
&TOKEN_2022_PROGRAM_ID, // token program
&mint.pubkey(), // mint
&destination.pubkey(), // destination
&fee_payer.pubkey(), // authority
&[&fee_payer.pubkey()], // signer keys
)?;
// Create transaction for closing the mint account
let transaction = Transaction::new_signed_with_payer(
&[close_mint_instruction],
Some(&fee_payer.pubkey()),
&[&fee_payer],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
// Try to fetch the mint account (should fail)
match client.get_account(&mint.pubkey()).await {
Ok(_) => println!("\nMint account still exists (unexpected)"),
Err(e) => println!("\nMint account successfully closed: {:#?}", e),
}
Ok(())
}
Console
Click to execute the code.

Is this page helpful?

Daftar Isi

Edit Halaman

Dikelola oleh

© 2025 Yayasan Solana.
Semua hak dilindungi.