Burn Tokens

Below is a minimal, end-to-end example that mints a token and then burns part of its supply. After creating a token account and minting 10,000 units, a burn operation reduces both the holder’s balance and the token’s total supply by 2,500. Burns are staged with a builder and become final once published.

How it works:

  • Create a token account, set basic info and default permission.

  • Mint an initial supply to the token account.

  • Start a new builder to burn: subtract the same amount from the account balance and the total supply.

  • Publish to commit the burn on-chain.

This pattern keeps the ledger consistent: every burned unit is removed from circulation and reflected immediately in both balance and supply.import * as KeetaNet from "@keetanetwork/keetanet-client";


async function main() {
    // Generate random seed for account creation
    const seed = KeetaNet.lib.Account.generateRandomSeed({ asString: true });
    console.log("seed =", seed);

    // Create account and user client
    const account = KeetaNet.lib.Account.fromSeed(seed, 0);
    const userClient = KeetaNet.UserClient.fromNetwork("test", account);

    // Create and mint new token
    const builderMint = userClient.initBuilder();
    const pendingTokenAccount = builderMint.generateIdentifier(KeetaNet.lib.Account.AccountKeyAlgorithm.TOKEN);
    await builderMint.computeBlocks();
    const tokenAccount = pendingTokenAccount.account;

    builderMint.setInfo(
        {
            name: '',
            description: '',
            metadata: '',
            defaultPermission: new KeetaNet.lib.Permissions(['ACCESS']),
        },
        { account: tokenAccount }
    );
    builderMint.modifyTokenSupply(10_000n, { account: tokenAccount });
    builderMint.modifyTokenBalance(tokenAccount, 10_000n);

    await userClient.publishBuilder(builderMint);

    console.log("Token account created and minted.");
    console.log("Balances after minting:", await userClient.allBalances());

    // Burn some tokens
    const builderBurn = userClient.initBuilder();
    builderBurn.modifyTokenBalance(tokenAccount, -2_500n);
    builderBurn.modifyTokenSupply(-2_500n, { account: tokenAccount });
    await userClient.publishBuilder(builderBurn);

    console.log("2,500 tokens burned from account", tokenAccount.publicKeyString.toString());
    console.log("Balances after burning:", await userClient.allBalances());
}

main()
    .then(() => {
        console.log("Done");
        process.exit(0);
    })
    .catch((err) => {
        console.error("Error:", err);
        process.exit(1);
    });

Last updated