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
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
defaultPermission: 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.send(account, 10_000n, tokenAccount, undefined, { account: tokenAccount }); // Distribute token amount to the liquidity account
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 },
);
// Increase (mint) total token supply and distribute the tokens
builder.modifyTokenSupply(10_000n, { account: tokenAccount });
builder.send(account, 10_000n, tokenAccount, undefined, { account: tokenAccount });
// Publish the transaction to the network
await builder.publish();
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