Tokens verbranden
Tokens verbranden
De
BurnChecked
instructie vernietigt tokens permanent door het saldo in een token account te
verminderen. Verbrande tokens worden uit circulatie gehaald en verminderen de
supply
die wordt bijgehouden op het mint account. Alleen de eigenaar van het
token account kan tokens uit hun account verbranden.
Het Token Program en Token Extension Program delen vergelijkbare implementaties om dezelfde functionaliteit te bereiken.
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 {getCreateAssociatedTokenInstructionAsync,getInitializeMintInstruction,getMintSize,TOKEN_PROGRAM_ADDRESS,findAssociatedTokenPda,getMintToInstruction,getBurnCheckedInstruction,fetchToken} 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 payerconst feePayer = 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});// Create instruction to mint tokensconst mintToInstruction = getMintToInstruction({mint: mint.address,token: associatedTokenAddress,mintAuthority: feePayer.address,amount: 1000_000_000_000n // 1000.0 tokens with 9 decimals});const instructions = [createAccountInstruction,initializeMintInstruction,createAtaInstruction,mintToInstruction];// 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("Transaction Signature:", transactionSignature);console.log("Successfully minted 1000.0 tokens");// Fetch token account to check balance before burnconst tokenAccountBefore = await fetchToken(rpc, associatedTokenAddress);console.log("\nToken balance before burn:",tokenAccountBefore.data.amount / 10n ** 9n,"tokens");// Get a fresh blockhash for the burn transactionconst { value: burnBlockhash } = await rpc.getLatestBlockhash().send();// Create instruction to burn tokensconst burnInstruction = getBurnCheckedInstruction({account: associatedTokenAddress,mint: mint.address,authority: feePayer.address,amount: 10_000_000_000n, // 10.0 tokens with 9 decimalsdecimals: 9});// Create transaction message for burningconst burnTxMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(burnBlockhash, tx),(tx) => appendTransactionMessageInstructions([burnInstruction], tx));// Sign transaction message with all required signersconst signedBurnTx = await signTransactionMessageWithSigners(burnTxMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedBurnTx,{ commitment: "confirmed" });// Get transaction signatureconst transactionSignature2 = getSignatureFromTransaction(signedBurnTx);// Fetch token account to check balance after burnconst tokenAccountAfter = await fetchToken(rpc, associatedTokenAddress);console.log("Token balance after burn:",tokenAccountAfter.data.amount / 10n ** 9n,"tokens");console.log("\nSuccessfully burned 10.0 tokens");console.log("Transaction Signature:", transactionSignature2);
Console
Click to execute the code.
Rust
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_sdk::{commitment_config::CommitmentConfig,program_pack::Pack,signature::{Keypair, Signer},system_instruction::create_account,transaction::Transaction,};use spl_associated_token_account::{get_associated_token_address_with_program_id, instruction::create_associated_token_account,};use spl_token::{id as token_program_id,instruction::{initialize_mint, mint_to, burn_checked},state::{Mint, Account as TokenAccount},};#[tokio::main]async fn main() -> Result<()> {// Create connection to local validatorlet client = RpcClient::new_with_commitment(String::from("http://localhost:8899"),CommitmentConfig::confirmed(),);let latestBlockhash = 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();// 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 authority9, // decimals)?;// Calculate the associated token account address for fee_payerlet associated_token_address = get_associated_token_address_with_program_id(&fee_payer.pubkey(), // owner&mint.pubkey(), // mint&token_program_id(), // program_id);// 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);// Amount of tokens to mint (1000 tokens with 9 decimals)let amount = 1000_000_000_000;// Create mint_to instruction to mint tokens to the associated token accountlet mint_to_instruction = mint_to(&token_program_id(),&mint.pubkey(), // mint&associated_token_address, // destination&fee_payer.pubkey(), // authority&[&fee_payer.pubkey()], // signeramount, // amount)?;// Create transaction and add instructionslet transaction = Transaction::new_signed_with_payer(&[create_account_instruction,initialize_mint_instruction,create_ata_instruction,mint_to_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint],latestBlockhash,);// Send and confirm transactionlet transaction_signature = client.send_and_confirm_transaction(&transaction).await?;println!("Mint Address: {}", mint.pubkey());println!("Associated Token Account Address: {}",associated_token_address);println!("Transaction Signature: {}", transaction_signature);println!("Successfully minted 1000.0 tokens");// Fetch token account to check balance before burnlet token_account_data = client.get_account(&associated_token_address).await?;let token_account = TokenAccount::unpack(&token_account_data.data)?;println!("\nToken balance before burn: {} tokens", token_account.amount as f64 / 1_000_000_000.0);// Get the latest blockhash for the burn transactionlet latestBlockhash = client.get_latest_blockhash().await?;// Amount of tokens to burn (10 tokens with 9 decimals)let burn_amount = 10_000_000_000;// Create burn_checked instructionlet burn_instruction = burn_checked(&token_program_id(), // program id&associated_token_address, // token account&mint.pubkey(), // mint&fee_payer.pubkey(), // owner&[&fee_payer.pubkey()], // signersburn_amount, // amount9, // decimals)?;// Create transaction for burning tokenslet transaction = Transaction::new_signed_with_payer(&[burn_instruction],Some(&fee_payer.pubkey()),&[&fee_payer],latestBlockhash,);// Send and confirm transactionlet transaction_signature = client.send_and_confirm_transaction(&transaction).await?;// Fetch token account to check balance after burnlet token_account_data = client.get_account(&associated_token_address).await?;let token_account = TokenAccount::unpack(&token_account_data.data)?;println!("Token balance after burn: {} tokens", token_account.amount as f64 / 1_000_000_000.0);println!("\nSuccessfully burned 10.0 tokens");println!("Transaction Signature: {}", transaction_signature);Ok(())}
Console
Click to execute the code.
Is this page helpful?