Sui Move Language - Counter Contract

Sui Move Language - Counter Contract

Write your first smart contract on Sui

·

3 min read

Hello Everyone..

Welcome to another day of exploring Web3 Engineering. In our series, we are exploring the Sui Move language and it's features. In today's article, let us write our first smart contract. So, without any further ado, lets get started.

Let us write a Counter contract which gives user a counter object and the value in the object can be incremented and decrement as per requirements.

First create a new sui move project with the command

sui move new counter

This will create a new folder named counter in our current directory which has the following folder structure.

We will the code in the counters.move file in the sources folder. And the test cases will go into the counters_tests.move file in the tests folder.

Let us create a Counter object on the code with one attribute called count and the type of the attribute is u64 and also since our object needs to be transferred it must have key ability.

public struct Counter has key, store {
    id: UID,
    val: u64
}

Now let us create a function to create this Counter object. This function will create a new Counter object and will return it to the caller.

public fun new(ctx: &mut TxContext): Counter {
    Counter {
        id: object::new(ctx),
        val:0
    }
}

The increment function will take the mutable reference of the counter object and will update the value in it.

public fun increment(c: &mut Counter) {
    c.val = c.val + 1
}

Since we are only taking taking a reference to the object, the contract don't own the object and hence no need to return it to user.

Now, let us write the decrement function. Here we have to make sure that we are not trying to decrement the value to a negative number. So that we have added an assert condition for that.

const ENegativeDecrement: u64 = 0;

public fun decrement(c: &mut Counter) {
    assert!(c.val > 0, ENegativeDecrement);
    c.val = c.val - 1
}

We also declare a error type on the contract to specify the error reason.

Now the full contract should look like below

/// Module: counter
module counter::counter {

    const ENegativeDecrement: u64 = 0;

    public struct Counter has key,store{
        id: UID,
        val: u64
    }

    public fun new(ctx: &mut TxContext): Counter {
        Counter {
            id: object::new(ctx),
            val:0
        }
    }

    public fun increment(c: &mut Counter) {
        c.val = c.val + 1
    }

    public fun decrement(c: &mut Counter) {
        assert!(c.val > 0, ENegativeDecrement);
        c.val = c.val - 1
    }

}

We can compile the contract using the command

sui move build

And then we will publish the package with the following command

sui move publish sources/counter.move --gas-budget 10000000000

NOTE: Before trying to publish the package, don't forget to have an active wallet and the environment.

The full code can be found here: https://github.com/jveer634/sui_contracts.git