Skip to content

๐Ÿช™ Token Operations โ€‹

Quest Objective: Create, transfer, and manage native tokens on Ergo Prerequisites: Completed First Transaction tutorial Time Required: ~45 minutes Difficulty: โญโญโญ Medium

๐ŸŽฏ What You'll Build โ€‹

By the end of this tutorial, you'll understand:

  • โœ… Ergo's native token model
  • โœ… How to transfer existing tokens
  • โœ… Token minting mechanics
  • โœ… Working with multiple tokens
mermaid
graph LR
    A[๐Ÿ“ฆ Box with Tokens] -->|Transfer| B[๐Ÿ“ฆ New Owner's Box]
    A -->|Mint| C[๐Ÿ“ฆ New Token Created]
    A -->|Burn| D[๐Ÿ”ฅ Token Destroyed]
    
    style A fill:#4CAF50
    style B fill:#2196F3
    style C fill:#FF9800
    style D fill:#f44336

๐Ÿ“‹ Understanding Ergo Tokens โ€‹

Key Concepts โ€‹

ConceptDescription
Native TokensFirst-class citizens, not smart contracts
Token ID64-character hex string (box ID of minting tx)
AmountBigInt, can be 1 (NFT) or billions
DecimalsDisplay precision (like cents for dollars)

Token vs NFT โ€‹

Token (Fungible)          NFT (Non-Fungible)
โ”œโ”€โ”€ Amount: 1,000,000     โ”œโ”€โ”€ Amount: 1
โ”œโ”€โ”€ Divisible: Yes        โ”œโ”€โ”€ Divisible: No
โ”œโ”€โ”€ Interchangeable       โ”œโ”€โ”€ Unique
โ””โ”€โ”€ Ex: SigUSD, ERG       โ””โ”€โ”€ Ex: Art, Collectibles

๐Ÿ’ป Example 1: Transfer Existing Tokens โ€‹

typescript
/**
 * โš”๏ธ QUEST: Token Transfer
 * 
 * Transfer tokens from one address to another
 */

import { 
  TransactionBuilder, 
  OutputBuilder,
  SAFE_MIN_BOX_VALUE,
  type Box
} from "@fleet-sdk/core";

// Token to transfer
const TOKEN_ID = "03faf2cb329f2e90d6d23b58d91bbb6c046aa143261cc21f52fbe2824bfcbf04";
const TOKEN_AMOUNT = 100n;

// Your input box that contains the token
const inputBox: Box<bigint> = {
  boxId: "abc123...",
  value: 1_000_000_000n,
  ergoTree: "0008cd...",
  creationHeight: 1_100_000,
  assets: [
    {
      tokenId: TOKEN_ID,
      amount: 500n  // You have 500 tokens
    }
  ],
  additionalRegisters: {},
  transactionId: "tx123...",
  index: 0
};

async function transferTokens() {
  console.log("๐Ÿช™ Transferring tokens...\n");

  // Create output with tokens
  const recipientOutput = new OutputBuilder(
    SAFE_MIN_BOX_VALUE,  // Minimum ERG required
    "9fRAWhdxEsTcdb8PhGNrZfwqa65zfkuYHAMmkQLcic1gdLSV5vA"
  ).addTokens({
    tokenId: TOKEN_ID,
    amount: TOKEN_AMOUNT
  });

  const tx = new TransactionBuilder(1_200_000)
    .from([inputBox])
    .to(recipientOutput)
    .sendChangeTo("9f4QF8AD1nQ3nJahQVkMj8hFSVVzVom77b52JU7EW71Zexg6N8v")
    .payMinFee()
    .build();

  console.log("โœ… Transaction built!");
  console.log(`   Sending: ${TOKEN_AMOUNT} tokens`);
  console.log(`   Token ID: ${TOKEN_ID.slice(0, 16)}...`);
  
  return tx;
}

transferTokens();

๐Ÿ’ป Example 2: Mint New Token โ€‹

typescript
/**
 * โš”๏ธ QUEST: Token Minting
 * 
 * Create a brand new fungible token
 */

import { 
  TransactionBuilder, 
  OutputBuilder,
  SAFE_MIN_BOX_VALUE
} from "@fleet-sdk/core";

async function mintToken() {
  console.log("๐Ÿญ Minting new token...\n");

  // The first input box ID becomes the token ID!
  const inputBox = {
    boxId: "e7b9c...",  // This becomes token ID
    value: 1_000_000_000n,
    ergoTree: "0008cd...",
    creationHeight: 1_100_000,
    assets: [],
    additionalRegisters: {},
    transactionId: "tx123...",
    index: 0
  };

  // Create output with new minted token
  const mintOutput = new OutputBuilder(
    SAFE_MIN_BOX_VALUE,
    "9f4QF8AD1nQ3nJahQVkMj8hFSVVzVom77b52JU7EW71Zexg6N8v"
  ).mintToken({
    amount: 1_000_000n,           // Total supply: 1 million
    name: "My Awesome Token",     // Display name
    decimals: 2,                  // 2 decimal places
    description: "A tutorial token created with Fleet SDK"
  });

  const tx = new TransactionBuilder(1_200_000)
    .from([inputBox])
    .to(mintOutput)
    .sendChangeTo("9f4QF8AD1nQ3nJahQVkMj8hFSVVzVom77b52JU7EW71Zexg6N8v")
    .payMinFee()
    .build();

  console.log("โœ… Token minted!");
  console.log(`   Name: My Awesome Token`);
  console.log(`   Supply: 1,000,000`);
  console.log(`   Token ID: ${inputBox.boxId}`);
  
  return tx;
}

mintToken();

๐Ÿ”ฅ Key Points โ€‹

Token ID = First Input Box ID

When you mint a token, its ID is automatically set to the box ID of your first input. This ensures unique token IDs!

Decimals Are Display Only

Decimals don't affect the actual amount stored. 1000 tokens with decimals: 2 displays as 10.00

๐Ÿ“Š Token Structure โ€‹

mermaid
graph TD
    subgraph "๐Ÿ“ฆ Box with Token"
        B[Box]
        V[value: 1000000000n]
        A[assets array]
        T1[Token 1]
        T2[Token 2]
    end
    
    B --> V
    B --> A
    A --> T1
    A --> T2
    
    subgraph "๐Ÿช™ Token Structure"
        TID[tokenId: string]
        TAM[amount: bigint]
    end
    
    T1 --> TID
    T1 --> TAM

๐Ÿš€ Next Quest โ€‹

Continue to NFT Minting โ†’ to create unique digital assets!

Released under the MIT License.