Create a Coin
Publishing a coin is Sui is almost as simple as publishing a new type. However it is a bit tricky as it requires using a One Time Witness.
module examples::mycoin {
use sui::coin;
/// 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.
public 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, // decimals
b"MYC", // symbol
b"My Coin", // name
b"Don't ask why", // description
option::none(), // icon url
ctx
);
// transfer the `TreasuryCap` to the sender, so they can mint and burn
transfer::public_transfer(treasury, ctx.sender());
// metadata is typically frozen after creation
transfer::public_freeze_object(metadata);
}
}
The Coin<T>
is a generic implementation of a Coin on Sui. Owner of the TreasuryCap
gets control over the minting and burning of coins. Further transactions can be sent directly to the sui::coin::Coin
with TreasuryCap
object as authorization.