Sui.

Post

Share your knowledge.

article banner.
MoonBags.
Jun 30, 2025
Article

Illustration of Sui Move Standard Library - Core Token Package (balance, coin, pay)

This article analyzes the three core components of Sui Move tokens: balance (value storage layer), coin (token operation layer), and pay (payment abstraction layer). Through principle diagrams and code examples, the design principles of the three-layer architecture are revealed.

ModulesTiersFunctionScenario
sui::balanceValue storage layer (underlying storage)Manage the mapping relationship between addresses and token balancesBalance query, token ownership verification
sui::coinToken operation layer (basic operation)Focus on the life cycle management of the token itself; including: token creation, destruction and metadata management, etc.Custom token issuance and token metadata maintenance
sui::payPayment abstraction layer (high-level encapsulation)Provides compound operations for token payment including: single or batch token splitting, merging, transfer, etc.Batch transfer, split account logic, airdrop

Method diagram

The method is relatively simple, mainly calling cointhe method in balance. The method coinwill be called in, so this article focuses on cointhe core method illustrated.

The principles of regulatory currency will be introduced in a separate article and will be ignored in this article.

Code Examples

module cookbook::aig_token {
    use std::string::{Self, String};
    use std::ascii;
    use sui::coin::{Self, TreasuryCap};
    use sui::balance::{Self, Balance};
    use sui::url::{Self, Url};
    use sui::event;

    public struct EventMint has copy, drop {
        sender: address,
        amount: u64,
        coin_left: u64
    }

    public struct EventAirdrop has copy, drop {
        method: String,
        sender: address,
        amount: u64
    }

    public struct EventCoinMeta has copy, drop {
        decimals: u8,
        symbol: ascii::String,
        name: String,
        description: String,
        icon_url: Option<Url>,
    }

    public struct EventTotalSupply has copy, drop {
        total_supply: u64
    }

    public struct Vault has key {
        id: UID,
        balance: Balance<AIG_TOKEN>,
    }

    public struct AIG_TOKEN has drop {}

    fun init(
        witness: AIG_TOKEN,
        ctx: &mut TxContext
    ) {

        let decimals = 3;
        let symbol = b"AIG";
        let name = b"AIG Token";
        let description = b"AIG Token is a token that is used to incentivize the community to achieve the goals of the AI Goal.";
        let url = url::new_unsafe_from_bytes(b"https://ai-goal.vercel.app/");

        let (treasury_cap, metadata) = coin::create_currency<AIG_TOKEN>(
            witness,
            decimals,
            symbol,
            name,
            description,
            option::some(url),
            ctx
        );

        event::emit(
            EventCoinMeta {
                decimals: coin::get_decimals(&metadata),
                symbol: coin::get_symbol(&metadata),
                name: coin::get_name(&metadata),
                description: coin::get_description(&metadata),
                icon_url: option::some(url),
            }
        );

        transfer::public_freeze_object(metadata);
        transfer::public_transfer(treasury_cap, ctx.sender());

        transfer::share_object(
            Vault {
                id: object::new(ctx),
                balance: balance::zero(),
            }
        );
    }

    public(package) fun airdrop(
        vault: &mut Vault,
        amount: u64,
        method: vector<u8>,
        ctx: &mut TxContext
    ) {
        let sender = ctx.sender();

        let mut balance_drop = balance::split(&mut vault.balance, amount);
        let coin_drop = coin::take(&mut balance_drop, amount, ctx);
        transfer::public_transfer(coin_drop, sender);
        balance::destroy_zero(balance_drop);

        event::emit(
            EventAirdrop {
                method: string::utf8(method),
                sender,
                amount,
            }
        );
    }

    public fun mint_balance(
        treasury_cap: &mut TreasuryCap<AIG_TOKEN>,
        vault: &mut Vault,
        amount: u64,
        ctx: &mut TxContext
    ) {
        let balance_minted = coin::mint_balance(treasury_cap, amount);
        balance::join(&mut vault.balance, balance_minted);

        event::emit(
            EventMint {
                sender: ctx.sender(),
                amount: amount,
                coin_left: balance::value(&vault.balance)
            }
        );
    }

    #[allow(lint(self_transfer))]
    public fun mint_coin(
        treasury_cap: &mut TreasuryCap<AIG_TOKEN>,
        amount: u64,
        ctx: &mut TxContext
    ) {
        let coin_minted = coin::mint(treasury_cap, amount, ctx);
        transfer::public_transfer(coin_minted, ctx.sender());

        coin::mint_and_transfer(
            treasury_cap,
            amount,
            ctx.sender(),
            ctx
        );

        event::emit(
            EventTotalSupply {
                total_supply: coin::total_supply(treasury_cap)
            }
        )
    }
}

  • Sui
2
Share
Comments
.
0xduckmove.
Jun 30 2025, 06:14

You can write better to analyze the coin library

Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.

351Posts499Answers
Sui.X.Peera.

Earn Your Share of 1000 Sui

Gain Reputation Points & Get Rewards for Helping the Sui Community Grow.

Reward CampaignJuly