Publish a Hello World smart contract on SUI network with Sui Move

Andrei
3 min readSep 18, 2023

--

Hi everyone.

In this blog tutorial I will demonstrate how anyone can deploy a simple Hello World smart contract on SUI network with Move.

Move is a safe and reliable smart contract language originally created by developers that worked at Meta ( ex — Facebook ) and it is built in Rust but powered for blockchain usage.

First of all in order to deploy a smart contract on Sui Network you need to install Sui Move on your local machine or you can use MoveIDEStudio which is a similar web IDE as remix.ethereum.org

If you wonder how to install Sui Move on your computer you can check this tutorial : set up development Sui development environment

I’ll explain how to deploy a contract by supposing that you already have Sui Move installed on your local machine and later in this tutorial will also explain how to deploy using the web Sui Move IDE.

First of let’s initialize a Sui Move workspace environment ( I will name it hello-world ). Open your terminal and run the following command :

sui move new hello-world

You should see inside of the directory a file called Move.toml and a directory called sources/

Inside of sources/ directory create a Move file called Hello.move and start with the following code :

We create hello_world module and also import the dependencies that will help us to achieve our end goal.

module hello_world::hello_world {

use std::string;
use sui::object::{Self, UID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};


}

We define the object in our Hello.movefile as the following:

struct Hello has key, store {
id: UID,
text: string::String
}

struct is a custom type that is similar to a JSON / dictionary object and can have up to 4 properties:

For now, just know that there are four properties in Sui Move:

  • copy: value can be copied (or cloned by value)
  • drop: value can be dropped by the end of the scope
  • key: value can be used as a key for global storage operations
  • store: value can be stored inside global storage

Custom types that have the abilities key and store are considered to be assets in Sui Move. Assets are stored in global storage and can be transferred between accounts.

If your struct is using key property you must to include an ID field.

Now we create a public function that is also an entry function. By defining entry we would be able to call mint_hello_world right from the sui explorer.

// Mint an object that contains an ID and "Hello World" text
public entry fun mint_hello_world(ctx: &mut TxContext) {
let hello_object = Hello {
id: object::new(ctx),
text: string::utf8(b"Hello World!")
};
// Transfer the object to initializer address
transfer::public_transfer(hello_object, tx_context::sender(ctx));
}

Final source code should look like this :

module hello_world::hello_world {

use std::string;
use sui::object::{Self, UID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};

struct Hello has key, store {
id: UID,
text: string::String
}

public entry fun mint_hello_world(ctx: &mut TxContext) {
// Mint an object that contains an ID and "Hello World" text
let hello_object = Hello {
id: object::new(ctx),
text: string::utf8(b"Hello World!")
};
// Transfer the object to initializer address
transfer::public_transfer(hello_object, tx_context::sender(ctx));
}

}

Let’s build the code by running :

sui move build

If you see a newbuild/ directory and no errors in your terminal you are good to publish it :

sui move publish --gas-budget 10000000 .

Note : I am using devnet to publish the smart contract and I use 10000000 gas to publish it. You might increase the value if you will be deploy to mainnet or testnet

Note : I added a dot at the final of publishing command because I am already inside of the sources/ directory

The skeleton of Sui CLI publish command looks like this :

sui client publish [OPTIONS] --gas-budget <GAS_BUDGET> [package_path]

To see more Sui CLI commands ( how to add and switch to different networks you can have a look at Sui cookbook )

After running sui move publish command you should see a package id in your terminal. Copy and paste it into explorer and try your mint_and_transfer entry function :)

If you have any further questions contact me at andreid.dev@gmail.com

--

--