Manage Tokens
This guide provides step-by-step instructions for managing fungible tokens using the OCAP Client. You will learn how to set up a token factory, which acts as a blueprint for your token, and then use it to mint (create new tokens) and burn (destroy existing tokens). These operations are fundamental to creating and managing custom economies within your application.
Once you have minted tokens, you can transfer them between accounts. For more details on that process, please see the Transfer Tokens and NFTs guide.
Create a Token Factory#
A token factory is a smart contract that defines the properties and rules of a fungible token, such as its name, symbol, and supply mechanics. It also governs the process of minting and burning. Creating a factory is the first step before any new tokens can be brought into circulation.
The createTokenFactory method deploys a new token factory to the blockchain.
Parameters#
Returns#
Returns a promise that resolves to an array containing the transaction hash and the address of the newly created token factory.
Example#
Create a Token Factory
import Client from '@ocap/client';
import Wallet from '@ocap/wallet';
const endpoint = 'https://beta.abtnetwork.io/api';
const client = new Client(endpoint);
const wallet = Wallet.fromRandom();
// First, ensure the wallet has funds. You can get test tokens from a faucet:
// https://faucet.abtnetwork.io/
async function createFactory() {
try {
const [hash, factoryAddress] = await client.createTokenFactory({
wallet,
token: {
name: 'My Game Coin',
symbol: 'MGC',
decimal: 18,
description: 'The official currency for My Awesome Game.',
maxTotalSupply: 1000000,
},
feeRate: 100, // 1% fee
});
console.log('Token factory created successfully!');See all 9 lines
Mint Tokens#
Minting is the process of creating new tokens and adding them to the total supply. This is done through a token factory. If the factory was configured with a bonding curve, minting will require a payment in the reserve token.
The mintToken method initiates a transaction to mint a specified amount of tokens from a factory.
Parameters#
Returns#
Returns a promise that resolves to the transaction hash.
Example#
Mint Tokens from a Factory
async function mintNewTokens(factoryAddress) {
try {
const hash = await client.mintToken({
wallet,
tokenFactory: factoryAddress,
amount: 5000,
receiver: wallet.address, // Mint tokens to our own wallet
maxReserve: 10, // Max reserve token to pay. Adjust based on bonding curve price.
});
console.log('Tokens minted successfully!');
console.log('Transaction Hash:', hash);
} catch (error) {
console.error('Error minting tokens:', error);
}
}
// Assuming `factoryAddress` is available from the createFactory example
// const factoryAddress = '...';
// mintNewTokens(factoryAddress);Burn Tokens#
Burning is the opposite of minting; it permanently removes tokens from circulation. If the token factory uses a bonding curve, burning tokens will return a proportional amount of the reserve currency to the user.
The burnToken method initiates this process.
Parameters#
Returns#
Returns a promise that resolves to the transaction hash.
Example#
Burn Tokens
async function burnExistingTokens(factoryAddress) {
try {
const hash = await client.burnToken({
wallet,
tokenFactory: factoryAddress,
amount: 1000,
receiver: wallet.address, // Receive reserve tokens back to our own wallet
minReserve: 1, // Min reserve token to receive. Adjust based on bonding curve price.
});
console.log('Tokens burned successfully!');
console.log('Transaction Hash:', hash);
} catch (error) {
console.error('Error burning tokens:', error);
}
}
// Assuming `factoryAddress` is available from the createFactory example
// const factoryAddress = '...';
// burnExistingTokens(factoryAddress);Summary#
In this guide, you've learned the complete lifecycle for managing fungible tokens: creating a factory, minting new tokens into existence, and burning them to reduce the supply. These powerful primitives allow you to build sophisticated economic systems on the OCAP platform.
Now that you know how to create tokens, the next logical step is to learn how to move them around. Head over to the Transfer Tokens and NFTs guide to see how it's done.