Skip to main content

创建代币

在 Sui 上发布代币几乎与发布新类型一样简单。主要的区别在于创建代币时需要一次性见证人

module examples::mycoin {
use std::option;
use sui::coin;
use sui::transfer;
use sui::tx_context::{Self, TxContext};

/// The type identifier of coin. The coin will have a type
/// tag of kind: `Coin<package_object::mycoin::MYCOIN>`
/// Make sure that the name of the type matches the module's name.
struct MYCOIN has drop {}

/// Module initializer is called once on module publish. A treasury
/// cap is sent to the publisher, who then controls minting and burning
fun init(witness: MYCOIN, ctx: &mut TxContext) {
let (treasury, metadata) = coin::create_currency(witness, 6, b"MYCOIN", b"", b"", option::none(), ctx);
transfer::public_freeze_object(metadata);
transfer::public_transfer(treasury, tx_context::sender(ctx))
}
}

Coin<T> 是 Sui 上代币的泛型实现。TreasuryCap 的所有者对代币的铸造和销毁拥有控制权。可以使用 TreasuryCap 对象的授权直接将进一步的交易发送到 sui::coin::Coin