Solana CookbookTokens
How to Burn Tokens
You can burn tokens if you are the token account authority.
import { Connection, Keypair } from "@solana/web3.js";import {createMint,getOrCreateAssociatedTokenAccount,mintTo,burnChecked} from "@solana/spl-token";(async () => {// Connect to local Solana nodeconst connection = new Connection("http://127.0.0.1:8899", "confirmed");// Create a fee payer accountconst feePayer = Keypair.generate();// Request airdrop for fee payerconst airdropSig = await connection.requestAirdrop(feePayer.publicKey,1000000000);await connection.confirmTransaction(airdropSig);// Step 1: Create a new mintconst mintAuthority = feePayer;const freezeAuthority = feePayer;const decimals = 2;console.log("Creating mint...");const mint = await createMint(connection,feePayer,mintAuthority.publicKey,freezeAuthority.publicKey,decimals);console.log("Mint created:", mint.toBase58());// Step 2: Create Associated Token Accountconsole.log("Creating token account...");const tokenAccount = await getOrCreateAssociatedTokenAccount(connection,feePayer,mint,feePayer.publicKey);console.log("Token account:", tokenAccount.address.toBase58());// Step 3: Mint tokensconst amountToMint = 100;console.log("Minting tokens...");const mintTx = await mintTo(connection,feePayer,mint,tokenAccount.address,mintAuthority,amountToMint);console.log("Mint transaction:", mintTx);// Step 4: Burn tokensconst amountToBurn = 50;console.log("Burning tokens...");const burnTx = await burnChecked(connection,feePayer,tokenAccount.address,mint,feePayer,amountToBurn,decimals);console.log("Burn transaction:", burnTx);})();
Is this page helpful?