Creating your own coin on the Sui blockchain
One of the fundamentals of the blockchain are coins. This walkthrough will teach you how to create and mint your own coin on the Sui blockchain.
Prerequisite
Before we can build and deploy our cryptocurrency, make sure to have set up your Sui wallet. We recommend using Martian or the Sui wallet for this walkthrough. Other wallets such as Suiet wallet are currently not supported by the tools we use in this walkthrough.
The Sui standard package
Mysten labs, the company that created the Sui blockchain, has publish a package on the blockchain that contains all sorts of libraries and standards. One of the modules included in this package is the coin module, which is the standard used to create and manage cryptocurrencies. The Sui package is deployed at 0x02
, which we will need for later.
Creating the contract
Setting up the environment
Now to create our own package that will interact with the Sui package. We will be using the Move Studio IDE to write and deploy our package. Navigate to Move Studio to get started. Navigate to the build page and skip the tutorial (this walkthrough will cover what you need to know).
Once on the build page, create a new package called examples
. Select the new package, after you create it. Move Studio automatically adds the Sui package as one of the package’s dependency. The other dependency is this package. Enter mycoin
in the new module box and click add. Enter the code below into the editor for the mycoin module. Click compile to make sure the code has no syntax errors in it.
// This is an example module that creates a new cryptocurrency
// called MYCOIN. It uses the Sui coin standard to do so.
// This module code was provided by the Sui Move by Example book
// (https://examples.sui.io/samples/coin.html)
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))
}
}
What is the code doing?
The struct MYCOIN
is the type of the new coin we are creating. Then we have the init function which is called during the creation of the module. The init
calls three functions.
The coin::create_currency
function is the function provide by the coin module to create the new coin. The arguments it takes is the witness (coin type), the decimal precision of the coin, the symbol, the name, the description, the icon url, and txcontext. The function will output the TreasuryCap
and the CoinMetadata
. The TreasuryCap
is used to control the coin and the CoinMetadata
holds all of the info for the coin. Information on the coin::create_currency
function can be found in the docs.
The second function, transfer::freeze_object
is used to make the CoinMetadata
object immutable. More can be found in the docs.
The third function, transfer::transfer
transfers the TreasuryCap
object to the deployer of the package/module. More can be found in the docs.
Deploying the package.
Once the module is compiling, navigate to the deploy page of the studio. Connect your Sui or Martian wallet to the site.
Select the coinExample
package and hit deploy. Confirm the deployment with your wallet.
Now, we can see the coinExample
package, as well as the CoinMetadata
and TreasuryCap
objects. Clicking the box and arrow icons in the objects or package will show it on the Sui explorer page (if there is an error with the Sui explorer page, make sure you are on the right network). Using the Sui explorer, verify that the TreasuryCap is owned by your account.
Note, that if you get disconnected from Move Studio, you can re-add your contracts and objects to the page with the Add from transaction digest
section. Retrieve the transaction digest from your account’s previous transactions located in the Sui explorer.
Now lets try minting some tokens. We will call manually call the coin::mint_and_transfer
function from the coin module. Info on the function can be found in the docs. We need to add the Sui package by clicking the “add sui package” button. This will add the Sui package to our page. Feel free to remove all objects besides the Sui package and TreasuryCap as those two objects are the only ones required to mint and burn tokens.
View the package details on the Sui package and select coin::mint_and_transfer
under the entry functions section. This will open an interface where we can input the arguments and execute the function.
Based on the docs, we need to provide the type of the TreasuryCap
(the type argument of TreasuryCap
), the address of the TreasuryCap
, the number of tokens to mint (with proper decimal precision), and the address to send the coins to. Use the copy icon to make entering the long arguments easier. Hit execute and confirm the transaction.
Once the transaction has been executed, the minting can be confirmed in the wallet of the receiver address. We can also verify by checking the new total_supply
value in the TreasuryCap
object.
Conclusion
Now you know how to create your own coin on the Sui blockchain! You’ve practiced writing, deploying, and interacting with contracts using the Move Studio IDE as well as use the Sui documentation to view information on their standard modules.