Solana CookbookTokens
How to Burn Tokens
You can burn tokens if you are the token account authority.
import { getCreateAccountInstruction } from "@solana-program/system";import {findAssociatedTokenPda,getBurnCheckedInstruction,getCreateAssociatedTokenInstructionAsync,getInitializeMintInstruction,getMintSize,getMintToCheckedInstruction,TOKEN_2022_PROGRAM_ADDRESS,} from "@solana-program/token-2022";import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners,} from "@solana/kit";const rpc = createSolanaRpc("http://127.0.0.1:8899");const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");/* constants */const MINT_AUTHORITY = await generateKeyPairSigner();const DECIMALS = 9;const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();let { mint, authorityATA } = await setup();const burnIx = getBurnCheckedInstruction({account: authorityATA,mint,authority: MINT_AUTHORITY.address,amount: 10_000_000_000n, // 10 tokensdecimals: DECIMALS,});const transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(MINT_AUTHORITY, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([burnIx], tx));const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction,{ commitment: "confirmed" });const txSignature = getSignatureFromTransaction(signedTransaction);console.log("Transaction Signature:", txSignature);/** The setup function initializes the mint and associated token accounts,* and mints tokens to said associated token account**/async function setup() {await airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: MINT_AUTHORITY.address,lamports: lamports(1_000_000_000n), // 1 SOLcommitment: "confirmed",});const mint = await generateKeyPairSigner();const space = BigInt(getMintSize());const rent = await rpc.getMinimumBalanceForRentExemption(space).send();// create & initialize mint accountconst createAccountInstruction = getCreateAccountInstruction({payer: MINT_AUTHORITY,newAccount: mint,lamports: rent,space,programAddress: TOKEN_2022_PROGRAM_ADDRESS,});const initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: DECIMALS,mintAuthority: MINT_AUTHORITY.address,});// create token accountconst [authorityATA] = await findAssociatedTokenPda({mint: mint.address,owner: MINT_AUTHORITY.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS,});const crateAuthorityATAInstruction =await getCreateAssociatedTokenInstructionAsync({payer: MINT_AUTHORITY,mint: mint.address,owner: MINT_AUTHORITY.address,});// mintTo token accountconst mintToInstruction = await getMintToCheckedInstruction({mint: mint.address,token: authorityATA,mintAuthority: MINT_AUTHORITY,amount: 1_000_000_000_000n, // 1000decimals: DECIMALS,});const instructions = [createAccountInstruction,initializeMintInstruction,crateAuthorityATAInstruction,mintToInstruction,];const transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(MINT_AUTHORITY, tx),(tx) =>setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions(instructions, tx));const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction,{ commitment: "confirmed" });return {mint: mint.address,authorityATA,};}
Is this page helpful?