# 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](./how-to-guides-transfer-tokens-and-nfts.md) 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

<x-field-group>
  <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet object of the factory owner, used to sign the transaction."></x-field>
  <x-field data-name="token" data-type="object" data-required="true" data-desc="An object defining the properties of the token to be created.">
    <x-field data-name="name" data-type="string" data-required="true" data-desc="The full name of the token (e.g., 'My Awesome Token')."></x-field>
    <x-field data-name="symbol" data-type="string" data-required="true" data-desc="The token's ticker symbol (e.g., 'MAT')."></x-field>
    <x-field data-name="decimal" data-type="number" data-required="true" data-desc="The number of decimal places the token supports."></x-field>
    <x-field data-name="description" data-type="string" data-required="false" data-desc="A brief description of the token."></x-field>
    <x-field data-name="icon" data-type="string" data-required="false" data-desc="URL to an icon for the token."></x-field>
    <x-field data-name="maxTotalSupply" data-type="number" data-required="false" data-desc="The maximum total supply that can ever be minted."></x-field>
  </x-field>
  <x-field data-name="curve" data-type="object" data-required="false" data-desc="Configuration for a bonding curve, which programmatically controls the token's price. If omitted, minting/burning is not tied to a reserve token.">
    <x-field data-name="basePrice" data-type="number" data-required="false" data-desc="The base price for the token in the reserve currency."></x-field>
    <x-field data-name="fixedPrice" data-type="number" data-required="false" data-desc="A fixed price for the token, if not using a dynamic curve."></x-field>
    <x-field data-name="slope" data-type="number" data-required="false" data-desc="The slope of the bonding curve, determining its steepness."></x-field>
  </x-field>
  <x-field data-name="feeRate" data-type="number" data-default="0" data-required="false" data-desc="The fee rate (in basis points) for minting and burning operations."></x-field>
  <x-field data-name="data" data-type="object" data-required="false" data-desc="Optional custom data to attach to the token factory."></x-field>
</x-field-group>

### Returns

Returns a promise that resolves to an array containing the transaction hash and the address of the newly created token factory.

<x-field-group>
  <x-field data-name="[0]" data-type="string" data-desc="The transaction hash for the factory creation."></x-field>
  <x-field data-name="[1]" data-type="string" data-desc="The address of the new token factory."></x-field>
</x-field-group>

### Example

```javascript Create a Token Factory icon=logos:javascript
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

<x-field-group>
  <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet funding the mint operation and signing the transaction."></x-field>
  <x-field data-name="tokenFactory" data-type="string" data-required="true" data-desc="The address of the token factory to mint from."></x-field>
  <x-field data-name="amount" data-type="number" data-required="true" data-desc="The quantity of tokens to mint."></x-field>
  <x-field data-name="receiver" data-type="string" data-required="true" data-desc="The address that will receive the newly minted tokens."></x-field>
  <x-field data-name="maxReserve" data-type="number" data-required="true" data-desc="The maximum amount of the reserve token the wallet is willing to spend. This acts as a slippage protection mechanism."></x-field>
  <x-field data-name="data" data-type="object" data-required="false" data-desc="Optional custom data to attach to the mint transaction."></x-field>
</x-field-group>

### Returns

Returns a promise that resolves to the transaction hash.

<x-field data-name="hash" data-type="string" data-desc="The transaction hash for the mint operation."></x-field>

### Example

```javascript Mint Tokens from a Factory icon=logos:javascript
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

<x-field-group>
  <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet that holds the tokens to be burned and will sign the transaction."></x-field>
  <x-field data-name="tokenFactory" data-type="string" data-required="true" data-desc="The address of the token factory."></x-field>
  <x-field data-name="amount" data-type="number" data-required="true" data-desc="The quantity of tokens to burn."></x-field>
  <x-field data-name="receiver" data-type="string" data-required="true" data-desc="The address that will receive the reserve tokens in return."></x-field>
  <x-field data-name="minReserve" data-type="number" data-required="true" data-desc="The minimum amount of the reserve token the wallet expects to receive. This protects against price slippage."></x-field>
  <x-field data-name="data" data-type="object" data-required="false" data-desc="Optional custom data to attach to the burn transaction."></x-field>
</x-field-group>

### Returns

Returns a promise that resolves to the transaction hash.

<x-field data-name="hash" data-type="string" data-desc="The transaction hash for the burn operation."></x-field>

### Example

```javascript Burn Tokens icon=logos:javascript
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](./how-to-guides-transfer-tokens-and-nfts.md) guide to see how it's done.
