Mint Tokens

This guide explains how to create and mint a token using the KeetaNet SDK. You'll learn how to build and publish a token account, set its supply, and adjust balances.

Step-by-Step

1

Initialize the Transaction Builder

Start by creating a transaction builder from your user client. This builder is used to queue up all operations before publishing to the network.

const builder = userClient.initBuilder();
2

Generate the Token Account

Use the builder to create a new token account:

const pendingTokenAccount = builder.generateIdentifier(KeetaNet.lib.Account.AccountKeyAlgorithm.TOKEN);
await builder.computeBlocks();
const tokenAccount = pendingTokenAccount.account;
console.log("tokenAccount.publicKey =", tokenAccount.publicKeyString.toString());
3

Set Token Information & Permissions

Configure the basic information and default permissions for your token:

builder.setInfo(
    {
        name: '',                 // Token name
        description: '',          // Short description
        metadata: '',             // Arbitrary metadata
        default    Permission: new KeetaNet.lib.Permissions([
            'ACCESS', // Public token
        ]),
    },
    { account: tokenAccount },
);
4

Mint Token Supply

Define the total supply for the new token and mint tokens into the liquidity account:

builder.modifyTokenSupply(10_000n, { account: tokenAccount }); // Set total supply
builder.modifyTokenBalance(tokenAccount, 10_000n);             // Mint token amount to the liquidity account
5

Publish the Operation

Send the queued operations to the network:

await userClient.publishBuilder(builder);
console.log("Token account created and minted.");

Full Code Example

import * as KeetaNet from "@keetanetwork/keetanet-client";

// Function to create and mint a new token
async function createToken(userClient: KeetaNet.UserClient) {
    const builder = userClient.initBuilder();

    // Generate the token account identifier
    const pendingTokenAccount = builder.generateIdentifier(KeetaNet.lib.Account.AccountKeyAlgorithm.TOKEN);
    await builder.computeBlocks();
    const tokenAccount = pendingTokenAccount.account;

    // Set token info and permissions
    builder.setInfo(
        {
            name: '', // Token name (add as needed)
            description: '', // Description (add as needed)
            metadata: '', // Metadata (add as needed)
            defaultPermission: new KeetaNet.lib.Permissions([
                'ACCESS', // Public token
            ]),
        },
        { account: tokenAccount },
    );

    // Set total token supply and mint the tokens
    builder.modifyTokenSupply(10_000n, { account: tokenAccount });
    builder.modifyTokenBalance(tokenAccount, 10_000n);

    // Publish the transaction to the network
    await userClient.publishBuilder(builder);

    console.log("Token account created and minted.");
    return tokenAccount;
}

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

    // Create a liquidity provider account from the seed
    const liquidityProviderAccount = KeetaNet.lib.Account.fromSeed(seed, 0);

    // Instantiate a user client connected to the test network
    const liquidityProviderClient = KeetaNet.UserClient.fromNetwork("test", liquidityProviderAccount);

    // Create and mint the token
    const tokenAccount = await createToken(liquidityProviderClient);

    // Log the token identifier that was created
    console.log("Token Account =", tokenAccount.publicKeyString.get());

    // Log balances of the liquidity provider
    console.log("liquidityProviderClient.balances[] =", await liquidityProviderClient.allBalances());
}

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

Last updated