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
- wallet
WalletObject(required) — The wallet object of the factory owner, used to sign the transaction. - token
object(required) — An object defining the properties of the token to be created.- name
string(required) — The full name of the token (e.g., 'My Awesome Token'). - symbol
string(required) — The token's ticker symbol (e.g., 'MAT'). - decimal
number(required) — The number of decimal places the token supports. - description
string— A brief description of the token. - icon
string— URL to an icon for the token. - maxTotalSupply
number— The maximum total supply that can ever be minted.
- name
- curve
object— Configuration for a bonding curve, which programmatically controls the token's price. If omitted, minting/burning is not tied to a reserve token.- basePrice
number— The base price for the token in the reserve currency. - fixedPrice
number— A fixed price for the token, if not using a dynamic curve. - slope
number— The slope of the bonding curve, determining its steepness.
- basePrice
- feeRate
number(default:0) — The fee rate (in basis points) for minting and burning operations. - data
object— Optional custom data to attach to the token factory.
Returns
Returns a promise that resolves to an array containing the transaction hash and the address of the newly created token factory.
- [0]
string— The transaction hash for the factory creation. - [1]
string— The address of the new 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!');
console.log('Transaction Hash:', hash);
console.log('Factory Address:', factoryAddress);
return factoryAddress;
} catch (error) {
console.error('Error creating token factory:', error);
}
}
createFactory();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
- wallet
WalletObject(required) — The wallet funding the mint operation and signing the transaction. - tokenFactory
string(required) — The address of the token factory to mint from. - amount
number(required) — The quantity of tokens to mint. - receiver
string(required) — The address that will receive the newly minted tokens. - maxReserve
number(required) — The maximum amount of the reserve token the wallet is willing to spend. This acts as a slippage protection mechanism. - data
object— Optional custom data to attach to the mint transaction.
Returns
Returns a promise that resolves to the transaction hash.
- hash
string— The transaction hash for the mint operation.
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
- wallet
WalletObject(required) — The wallet that holds the tokens to be burned and will sign the transaction. - tokenFactory
string(required) — The address of the token factory. - amount
number(required) — The quantity of tokens to burn. - receiver
string(required) — The address that will receive the reserve tokens in return. - minReserve
number(required) — The minimum amount of the reserve token the wallet expects to receive. This protects against price slippage. - data
object— Optional custom data to attach to the burn transaction.
Returns
Returns a promise that resolves to the transaction hash.
- hash
string— The transaction hash for the burn operation.
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.