Create a Token with Sui Move

Create a Token with Sui Move

We will be building our very own token in Move programming language and then deploying it on Sui blockchain. This article is important as it equips you with the necessary knowledge to begin your exciting journey of coding in Sui using Move.

Let’s look at how you will be interacting with the token by the end of the course. You will mint your token and view it in your wallet. How exciting!

Why Sui?

  • Meta’s top executives created this Layer-1 blockchain.

  • It is another name for security. It offers secure contracts, assets, transactions, and networks.

  • It handles fee spikes even when demand is higher.

  • It supports parallel processing which makes it super fast and it doesn’t sacrifice security.

Learning Objectives

We will:

  1. Develop an understanding of Sui blockchain and Move

  2. Set up Sui Wallet and online development environment

  3. Deploy the Move smart contract on Sui

Who exactly should take this course?
This article is for beginner web3 developers who wish to learn about Sui blockchain and how they can deploy the smart contracts on it. You should have a basic knowledge of programming concepts like object-oriented programming, data structures, and algorithms. These concepts will help you have a quick start to this project.

Into the World of Sui

Sui Blockchain is changing the game in decentralized finance. You know how blockchain tech is always evolving, right? Well, Sui Blockchain is on a whole new level. It's all about revolutionizing the world of decentralized finance (DeFi) and giving users like us the power to rock a secure, transparent, and super efficient financial ecosystem. Sui involves third party audits to ensure highest level of security possible. Sui keeps things cool with its staking program, using a proof-of-stake (PoS) consensus mechanism which is more energy-efficient than proof-of-work mechanisms used by other blockchains like Bitcoin and Ethereum. Here's the deal: if you hold and stake your SUI tokens, you can help secure and validate transactions on the network. It's not like those proof-of-work (PoW) systems that rely on raw computational power. With PoS, it's all about how many tokens you've got and are willing to stake. And guess what? Stakers get rewarded with more SUI tokens for being active and keeping the network stable. It's all about being energy-efficient and making staking a smooth ride for all of us.

What is Sui’s language?

When it comes to programming in Sui, they have developed their own unique language known as Move, which emphasizes distinctiveness and robustness. Move was specifically crafted to address the shortcomings found in other popular smart contract languages, such as Solidity. Instead of conforming to existing norms, they opted for their own approach.

Unlike compiled languages that operate swiftly, Move, functioning as an interpreter, operates at a slower pace. However, this deliberate absence of a compiler and the elimination of associated compiler bugs render Move exceptionally secure, even more so than Solidity.

Move is primarily centered on implementing innovative concepts like objects and parallel execution in the most efficient manner conceivable. It's akin to bestowing developers with extraordinary abilities to construct projects that transcend ordinary boundaries. What's more, Move places a strong emphasis on data composability, granting developers remarkable flexibility when it comes to manipulating data structures like structs. These structures act as building blocks that can be rearranged and manipulated to craft remarkable creations!

What are Sui tokens?

Sui uses SUI token. Another name used is Mysten Labs coin too. They've got a total supply of a whopping ten billion tokens! Now, let me break down a few things you can do with those SUI tokens.

  • First up, you can stake and get in on that PoS consensus action by helping secure and validate transactions on the network.

  • Next, you can use those SUI tokens to pay the gas fees. Yeah, like when you make transactions or do other cool stuff on the blockchain.

Sui is a Layer 1 blockchain that deals on the PROOF OF STAKE CONSENSUS (POS)

Setting up the Development Environment

Let’s install Sui wallet so you can deploy your contract.

You can add it easily using a browser extension. For chrome, just go to this link and then add to chrome. Once you add Sui wallet extension to Chrome, you will be redirected to new page. Click on Get Started. Create a new wallet and then add your password. Make sure to save your recovery phrase.

Getting some tokens

Here's a simple step-by-step guide:

  • Open your Sui wallet.

  • Change the network to Testnet. Click on the hamburger icon on top right corner. Click on Network and then select Sui Testnet.

  • Go back and Request SUI tokens. After a while you will receive SUI token in your wallet.

Write Your Token Smart Contract in Move

You need no installations. Just go to this online Move Studio and let’s start building there.

First, create a new package. Name it anything you want. Then select the created package from the left panel. Now you are ready to create your file.

Now it’s time to create a file, just write any name in module name and the file is ready to edit. For this article, we’re going to use doge.

Write code

Let's go through the code line by line that you will be writing:

First you will add the package name, so make sure to replace gbracademy with the package name you opted for:

module gbracademy::doge {
  • This line defines a module named gbracademy::pepe, which will contain the Move token implementation. A module is a way to organize code and group related functionality together.
    use std::option;
    use sui::coin;
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};
  • Import modules (std::option, sui::coin, sui::transfer, and sui::tx_context) that contain pre-built functions and types to be used in the code.
    struct DOGE has drop {}
  • Define a new struct called DOGEwith has drop attribute.
    fun init(witness: DOGE, ctx: &mut TxContext) {
        let (treasury, metadata) = coin::create_currency(witness, 9, b"DO", b"DOGE", b"", option::none(), ctx);
        transfer::public_freeze_object(metadata);
        transfer::public_transfer(treasury, tx_context::sender(ctx))
    }
  • Define a function called init, which is the module initializer.

  • The init function takes the following:

    • Move utilizes a witness design pattern that states that a type passed can only be initiated once. The witness resource needs to be consumed or discarded right away to prevent creating multiple instances of the given object. So we passed DOGE struct that has drop functionality.

    • We also passed a mutable reference to TxContext.

  • Inside the function, call the coin::create_currency function to create a new DOGE currency:

    • Pass the DOGE witness as the first argument.

    • The desired decimal places as second parameter. For example, for 9 as decimal value and suppose we want to mint 100 token, so we need to pass 100*10^9.

    • The symbol of our token is DO.

    • The token name is DOGE which is forth argument.

    • Next an empty icon URL is added and an optional metadata too.

  • The coin::create_currency function returns a tuple containing treasury and metadata that are TreasuryCap and CoinMetadata objects respectively.

    • The TreasuryCap is like a manager that controls access to the mint and burn methods.
  • Call transfer::public_freeze_object to freeze the metadata so that no one can change the coin metadata once it's published

  • Finally, transfer the treasury to the sender of the transaction using transfer::public_transfer.

    public entry fun mint(
        treasury: &mut coin::TreasuryCap<DOGE>, amount: u64, recipient: address, ctx: &mut TxContext
    ) {
        coin::mint_and_transfer(treasury, amount, recipient, ctx)
    }
}
  • Define a public entry function called mint.

  • The mint function takes a mutable reference to coin::TreasuryCap<DOGE>, an amount of type u64, a recipient of type address, and a mutable reference to TxContext as arguments.

  • Inside the function, call the coin::mint_and_transfer function to mint new tokens and transfer them to the specified recipient.

Complete code

The complete code for this is below, you can paste it into the file created in Move Studio.

module gbracademy::doge {
    use std::option;
    use sui::coin;
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};

    // Name matches the module name, but in UPPERCASE
    struct DOGE 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: DOGE, ctx: &mut TxContext) {
        let (treasury, metadata) = coin::create_currency(witness, 9, b"DO", b"DOGE", b"", option::none(), ctx);
        transfer::public_freeze_object(metadata);
        transfer::public_transfer(treasury, tx_context::sender(ctx))
    }

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

Deploy your contract

Click on the icon on the upper left corner and go to Deploy. Then click on Sui Wallet to connect to your wallet. (The Sui wallet can ask you to Connect so click on it to proceed.)

Now select your package from Deploy package, in my case it is gbracademy. Click on icon on the right and approve the transaction when prompted for and then wait until it is successfully published.

After successful deployment, you will see a window like this:

  • One tab with your package name has the address of your contract.

  • UpgradeCap has the details of your TreasuryCap and package address.

  • The TreasuryCap is like a manager that controls access to the mint and burn methods.

  • CoinMetadata has the metadata of your coin.

Now click on the 1st icon of your package name and you will be taken to Sui explorer.

Now expand the mint function and connect your Sui wallet.

Now go back to the Move studio and copy the first address from TreasuryCap as this is the first parameter of mint function.

Now complete the following steps to execute the mint function.

  • Add the copied TreasuryCap address in Arg0.

  • Add 100 or any other value as the amount to be minted as second parameter in Arg1.

  • Add Sui wallet’s account address in Arg2. You can open the wallet, copy it from there and paste it.

  • Click on Execute button.

  • Approve the transaction.

You can see the minted token in your wallet.

In this tutorial, you successfully launched a fungible token on the Sui blockchain. From here on out, you have the freedom to author and deploy contracts on Sui without any hindrance. Best of luck with your forthcoming deployments!