đĸcounter
Last updated
Last updated
This tutorial guides you through creating a smart contract using the Concordium a default contract template that simply keeps a counter value in its state. It is a super simple, fundamental example contract that touches on the following points:
to be able to increase/decrease the counter value by the parameter given by the user if it is a positive number
view the current value
return a custom error when someone tries to increase it with a negative value (or vice versa)
all these operations have to be done by only the owner of the contract.
Once you have set up the tools needed you are ready to create your smart contract project. First, create a working directory, and run the command below in that directory. It will set up the initial project for you, including necessary rust dependencies.
The template repository contains short GIFs that show many of these commands.
Select the Default
option from the menu.
Then it will ask for a name and a description of your project. Fill them in. The result is a basic skeleton of a smart contract. Initially, it has a State
struct, an init
function for creating new instances, an Error
enum for custom errors, a view
function, and a receive
function.
Add the counter to the state and i8 for integer. Then add the values OwnerError
, IncrementError
, and DecrementError
to the Error
enum, and specify the counter initial value as zero in the init
function so the counter value starts from 0 when you create a new, fresh instance the contract. Your contract now looks like the example below.
Then change the update function as described below. Remember that input needs to be parsed without any errors. The value must be positive, otherwise you will get an Error::IncrementError
. The transaction must be triggered by the owner of the contract instance or it will throw Error::OwnerError
. And the function itself has to be a mutable function because you are going to change the state of the contract.
Add a new mutable function to implement decrement with a similar approach. It will also take an input parameter, but this time make sure that it is negative because a violation will be caused by an Error::DecrementError
. Like the other one, this can be triggered by only the owner of the contract,otherwise it will throw an Error::OwnerError
.
The view function will return only the counters value so you need to update its return value as i8 and return it from the host.state().
Create a dist
folder to keep the schema output file and Wasm compiled contract in and run the build command.
Deploy it with the command below.
Initialize it to create your contract instance, so you are ready to invoke the functions in the next section.
First, check the initial state of the contract.
Since you just initialized the contract it is 0.
Create a JSON file that holds your operator that will be given as input to the function and run the command below. Basically, you are saying to the contract instance âwith this transaction I will update your state from the increment entrypointâ which is your function name with this parameter.
Start by testing with your conditions. First, try another account other than the owner of the contract since you want that only the owner can call this function.
You get error code: -2. Check the developer portal of Concordium for information about custom errors. Basically, -2 means you are calling the second error code from your Error enum, which is OwnerError. So that means you have fulfilled the requirement that only the owner can call these functions. Update the state with number 2 now.
Now check the state once more.
Unsurprisingly, the state is 2. Now check the other requirement: that you cannot increment it with a negative number. Change the value in the JSON file to a negative number like -2.
You cannot do it because of error code -3 which is the third element in the enum: IncrementError
. That means the increment method operates as expected in your contract.
You can play with the decrement in the same way.