본문으로 건너뛰기

SUI Coin Deployment

May 23, 2023
Yoonsoo Jang
Software Engineer, DSRV

Introduction

This tutorial shows how to deploy your own coin.

정보

Please refer to here to get started.

Create Template

Create a simple example contract code written in Move. You can create a sample contract my_coin by selecting the template option and clicking the Create Template button.

01_template-code-sui-my-coin

Source Code

Note that init function is executed only once during deployment.

my_coin.move
module coin_sample::mycoin {
use std::option;
use sui::coin::{Self, Coin, TreasuryCap};
use sui::transfer;
use sui::tx_context::{Self, TxContext};

struct MYCOIN has drop {}

fun init(witness: MYCOIN, ctx: &mut TxContext) {
let (treasury_cap, metadata) = coin::create_currency<MYCOIN>(
witness,
2, // decimals
b"MC", // symbol
b"MYCOIN", // name
b"my coin", // description
option::none(),
ctx
);
transfer::public_freeze_object(metadata);
transfer::public_transfer(treasury_cap, tx_context::sender(ctx))
}

public entry fun mint(
treasury_cap: &mut TreasuryCap<MYCOIN>, amount:u64, recipient: address, ctx: &mut TxContext
) {
coin::mint_and_transfer(treasury_cap, amount, recipient, ctx)
}

public entry fun burn(treasury_cap: &mut TreasuryCap<MYCOIN>, coin: Coin<MYCOIN>) {
coin::burn(treasury_cap, coin);
}
}
Move.toml
[package]
name = "CoinSample"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir="crates/sui-framework/packages/sui-framework/", rev = "testnet" }

[addresses]
coin_sample = "0x0"

Compile The Source Code

Select the project you want to compile. For now, let's choose sui/my_coin and click Compile button.

02_project-to-compile-my-coin

Deployment

If the compilation succeed, you can see mv file mycoin.mv.

Click the Deploy button.

03_build-file-my-coin

and you can see wallet popup. Let's click Send button.

04_sui-my-coin-wallet-popup

Check Out Deployed Contract

After deployment, you can see mycoin module and functions.

05_sui-my-coin-deployed

Calling Contract Functions

  1. Select mint function. First parameter is TreasuryCap object id. You can get the object id in the terminal log which shows for above deployment transaction result. Second parameter is the amount of the minting coin. Third parameter is the receiver of the minting coin. For now, set the receiver to your account address.

    06_sui-my-coin-mint
  2. After mint transaction, check if MYCOIN was minted in SUI Explorer.

    07_sui-my-coin-minted

Try more...

  1. Call burn function and check it out in SUI Explorer.
  2. Read about One Time Witness (OTW).

Reference

https://examples.sui.io/basics/one-time-witness.html https://github.com/MystenLabs/sui/tree/main/sui_programmability/examples/fungible_tokens