إغلاق حساب التوكن
إغلاق الحساب
تعليمة
CloseAccount
تقوم بإغلاق حساب التوكن بشكل دائم وتحويل جميع عملات SOL المتبقية (الإيجار) إلى
حساب الوجهة المحدد. يجب أن يكون رصيد حساب التوكن صفرًا قبل الإغلاق. فقط مالك
حساب التوكن أو سلطة الإغلاق المعينة يمكنهم تنفيذ هذه التعليمة.
يشترك برنامج التوكن وبرنامج Token Extensions في تنفيذات متشابهة لتحقيق نفس الوظيفة.
تايبسكريبت
import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners} from "@solana/kit";import { getCreateAccountInstruction } from "@solana-program/system";import {getCreateAssociatedTokenInstructionAsync,getInitializeMintInstruction,getMintSize,TOKEN_PROGRAM_ADDRESS,findAssociatedTokenPda,getCloseAccountInstruction} from "@solana-program/token";// Create Connection, local validator in this exampleconst rpc = createSolanaRpc("http://localhost:8899");const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");// Generate keypairs for fee payer and destinationconst feePayer = await generateKeyPairSigner();const destination = await generateKeyPairSigner();// Fund fee payerawait airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: feePayer.address,lamports: lamports(1_000_000_000n),commitment: "confirmed"});// Generate keypair to use as address of mintconst mint = await generateKeyPairSigner();// Get default mint account size (in bytes), no extensions enabledconst space = BigInt(getMintSize());// Get minimum balance for rent exemptionconst rent = await rpc.getMinimumBalanceForRentExemption(space).send();// Get latest blockhash to include in transactionconst { value: latestBlockhash } = await rpc.getLatestBlockhash().send();// Instruction to create new account for mint (token program)// Invokes the system programconst createAccountInstruction = getCreateAccountInstruction({payer: feePayer,newAccount: mint,lamports: rent,space,programAddress: TOKEN_PROGRAM_ADDRESS});// Instruction to initialize mint account data// Invokes the token programconst initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: 9,mintAuthority: feePayer.address});// Use findAssociatedTokenPda to derive the ATA addressconst [associatedTokenAddress] = await findAssociatedTokenPda({mint: mint.address,owner: feePayer.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});// Create instruction to create the associated token accountconst createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({payer: feePayer,mint: mint.address,owner: feePayer.address});const instructions = [createAccountInstruction,initializeMintInstruction,createAtaInstruction];// Create transaction messageconst transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions(instructions, tx));// Sign transaction message with all required signersconst signedTransaction =await signTransactionMessageWithSigners(transactionMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction,{ commitment: "confirmed" });// Get transaction signatureconst transactionSignature = getSignatureFromTransaction(signedTransaction);console.log("Mint Address:", mint.address.toString());console.log("Associated Token Account Address:",associatedTokenAddress.toString());console.log("Destination Address:", destination.address.toString());console.log("Transaction Signature:", transactionSignature);// Get a fresh blockhash for the close transactionconst { value: closeBlockhash } = await rpc.getLatestBlockhash().send();// Create instruction to close the token accountconst closeAccountInstruction = getCloseAccountInstruction({account: associatedTokenAddress,destination: destination.address,owner: feePayer});// Create transaction message for closingconst closeTxMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(closeBlockhash, tx),(tx) => appendTransactionMessageInstructions([closeAccountInstruction], tx));// Sign transaction message with all required signersconst signedCloseTx = await signTransactionMessageWithSigners(closeTxMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedCloseTx,{ commitment: "confirmed" });// Get transaction signatureconst transactionSignature2 = getSignatureFromTransaction(signedCloseTx);console.log("\nSuccessfully closed the token account");console.log("Transaction Signature:", transactionSignature2);
Console
Click to execute the code.
راست
Rust
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_commitment_config::CommitmentConfig;use solana_sdk::{program_pack::Pack,signature::{Keypair, Signer},transaction::Transaction,};use solana_system_interface::instruction::create_account;use spl_associated_token_account_interface::{address::get_associated_token_address, instruction::create_associated_token_account,};use spl_token_interface::{id as token_program_id,instruction::{close_account, initialize_mint},state::Mint,};#[tokio::main]async fn main() -> Result<()> {// Create connection to local validatorlet 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 payerlet fee_payer = Keypair::new();// Airdrop 1 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 1_000_000_000).await?;client.confirm_transaction(&airdrop_signature).await?;loop {let confirmed = client.confirm_transaction(&airdrop_signature).await?;if confirmed {break;}}// Generate keypair to use as address of mintlet mint = Keypair::new();// Set decimals for mintlet decimals = 2;// Get default mint account size (in bytes), no extensions enabledlet mint_space = Mint::LEN;let mint_rent = client.get_minimum_balance_for_rent_exemption(mint_space).await?;// Instruction to create new account for mint (token program)let create_account_instruction = create_account(&fee_payer.pubkey(), // payer&mint.pubkey(), // new account (mint)mint_rent, // lamportsmint_space as u64, // space&token_program_id(), // program id);// Instruction to initialize mint account datalet initialize_mint_instruction = initialize_mint(&token_program_id(),&mint.pubkey(), // mint&fee_payer.pubkey(), // mint authoritySome(&fee_payer.pubkey()), // freeze authoritydecimals, // decimals)?;// Calculate the associated token account address for fee_payerlet associated_token_address = get_associated_token_address(&fee_payer.pubkey(), // owner&mint.pubkey(), // mint);// Instruction to create associated token accountlet create_ata_instruction = create_associated_token_account(&fee_payer.pubkey(), // funding address&fee_payer.pubkey(), // wallet address&mint.pubkey(), // mint address&token_program_id(), // program id);// Create transaction and add instructionslet transaction = Transaction::new_signed_with_payer(&[create_account_instruction,initialize_mint_instruction,create_ata_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint],latest_blockhash,);// Send and confirm transactionclient.send_and_confirm_transaction(&transaction).await?;let token_account = client.get_token_account(&associated_token_address).await?;println!("Associated Token Account Address: {}",associated_token_address);if let Some(token_account) = token_account {println!("Account Created");println!("{:#?}", token_account);}// Create close account instructionlet close_instruction = close_account(&token_program_id(),&associated_token_address, // Token account to close&fee_payer.pubkey(), // Destination to receive SOL&fee_payer.pubkey(), // Owner of token account&[&fee_payer.pubkey()],)?;// Create transaction for closing token accountlet transaction = Transaction::new_signed_with_payer(&[close_instruction],Some(&fee_payer.pubkey()),&[&fee_payer],latest_blockhash,);// Send and confirm transactionclient.send_and_confirm_transaction(&transaction).await?;match client.get_account(&associated_token_address).await {Ok(token_account) => println!("{:#?}", token_account),Err(e) => eprintln!("\nAccount Closed: \n{:#?}", e),}Ok(())}
Console
Click to execute the code.
بايثون
Python
#!/usr/bin/env python3import asynciofrom solana.rpc.async_api import AsyncClientfrom solders.keypair import Keypairfrom solders.pubkey import Pubkeyfrom solders.transaction import VersionedTransactionfrom solders.message import MessageV0from spl.token.instructions import close_account, CloseAccountParamsfrom spl.token.constants import TOKEN_PROGRAM_IDasync def main():rpc = AsyncClient("http://localhost:8899")# Example keypairs and addressespayer = Keypair()owner = Keypair()token_account = Pubkey.from_string("GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ")# Account to receive the remaining lamports (usually the owner)destination = owner.pubkey()async with rpc:# Create close account instructionclose_instruction = close_account(CloseAccountParams(program_id=TOKEN_PROGRAM_ID,account=token_account,dest=destination,owner=owner.pubkey()))# Get latest blockhashrecent_blockhash = await rpc.get_latest_blockhash()# Create messagemessage = MessageV0.try_compile(payer=payer.pubkey(),instructions=[close_instruction],address_lookup_table_accounts=[],recent_blockhash=recent_blockhash.value.blockhash)# Create transactiontransaction = VersionedTransaction(message, [payer, owner])print(f"Token Account: {token_account}")print(f"Owner: {owner.pubkey()}")print(f"Destination: {destination}")print(f"Close token account transaction created successfully")if __name__ == "__main__":asyncio.run(main())
Console
Click to execute the code.
Is this page helpful?