Develop the counter smart-contract

In the Flipper smart contract tutorial, you were introduced to the fundamental process for creating and deploying a smart contract on a Substrate-based blockchain, starting with a basic project. In this tutorial, you will create a new smart contract designed to increase a counter value every time a function is executed.

Prerequisites

Before getting started, make sure you have the following ready:

  1. You are generally familiar with command-line interfaces (CLI).

  2. You have installed Rust and set up your development environment as described in one of the sources below:

About ink!

In the Flipper smart contract tutorial, you set up the cargo-contract package to gain command-line access to the ink! programming language. Ink! is an embedded domain-specific language tailored for writing WebAssembly-based smart contracts in Rust. It incorporates standard Rust conventions along with specialized #[ink(...)] attribute macros.

These macros help delineate various components of your smart contract, facilitating their conversion into WebAssembly bytecode that is compatible with Substrate.

Let's create a new incrementer counter smart contract

Smart contracts designed to operate on Substrate begin as projects, which are initiated through the use of cargo contract commands. In this tutorial, we will embark on creating a new project specifically for the incrementer smart contract. This process involves generating a new project directory and populating it with default starter files, also referred to as template files. These initial files will serve as the foundation that you will then alter to develop the smart contract's logic tailored for the incrementer project. Start the creation of your smart contract's new project:

  1. Open a terminal shell on your local computer, if you don’t already have one open.

  2. Create a new project named incrementer by running the following command:

    cargo contract new incrementer
  3. Change to the new project directory by running the following command:

    cd incrementer/
  4. Open the lib.rs file in a text editor.

    By default, the template lib.rs file contains the source code for the flipper smart contract with instances of the flipper contract name renamed incrementer.

  5. Replace the default template source code with new incrementer source code

  6. Save the changes to the lib.rs file, then close the file.

  7. Verify that the program compiles and passes the trivial test by running the following command:

    cargo test

    You can ignore any warnings because this template code is simply a skeleton. The command should display output similar to the following to indicate successful test completion:

    running 1 test
    test incrementer::tests::default_works ... ok
    
    test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  8. Verify that you can build the WebAssembly for the contract by running the following command:

    cargo contract build

    If the program compiles successfully, you are ready to start programming.

Storing basic values

This particular smart contract requires the storage of straightforward values. The code presented in this segment aims to showcase the capabilities of the ink! language. The specific code that will be utilized throughout the remainder of this tutorial is introduced in the following section: Update your smart contract. Simple values within a contract can be stored utilizing the #[ink(storage)] attribute macro:

Supported types

ink! smart contracts are compatible with a wide array of Rust's standard data types, such as booleans, unsigned and signed integers, strings, tuples, and arrays. These types are efficiently serialized and deserialized for network transmission via the Parity scale codec. Beyond these common Rust types, ink! also accommodates Substrate-specific types, including AccountId, Balance, and Hash, treating them akin to native types.

The code example below demonstrates the method for storing an AccountId and Balance within this contract:

Constructors

Every ink! smart contract is required to have a minimum of one constructor, which is executed at the time of contract creation. Nonetheless, it is possible for a smart contract to include several constructors if necessary. The code below provides an example of implementing multiple constructors:

Update your smart contract

Having familiarized yourself with the basics of storing simple values, defining data types, and utilizing constructors, you're now ready to enhance your smart contract's source code with the following implementations:

  • Establish a storage value named value with the data type i32.

  • Introduce a new Incrementer constructor, initializing value with init_value.

  • Implement an additional constructor function called default, which takes no arguments and instantiates a new Incrementer with value initialized to 0.

To proceed with the updates to your smart contract:

  1. Open the lib.rs file in a text editor.

  2. Replace the Storage Declaration comment by declaring the storage item named value with the data type of i32.

  3. Modify the Incrementer constructor to set its value to init_value.

  4. Add a second constructor function named default that creates a new Incrementer with its value set to 0.

  5. Save your changes and close the file.

  6. Try running the test subcommand again and you will see that the tests are now failing. This is because we need to update the get function and modify the tests to match the changes we implemented. We will do that in the next section.

Add a function to get a storage value

Having established and initialized a storage value, you're now set to update it through both public and private functions. In this tutorial, we'll introduce a public function that retrieves a storage value. It's important to note that all public functions are required to utilize the #[ink(message)] attribute macro.

To add the public function into your smart contract:

  1. Open the lib.rs file in a text editor.

  2. Update the get public function to return the data for the value storage item that has the i32 data type.

    Because this function only reads from the contract storage, it uses the &self parameter to access the contract functions and storage items.

    This function does not allow changes to the state of the value storage item.

    If the last expression in a function does not have a semicolon (;), Rust treats it as the return value.

  3. Replace the Test Your Contract comment in the private default_works function with code to test the get function.

  4. Save your changes and close the file.

  5. Check your work using the test subcommand, and you will see that it is still failing, because we need to update the it_works test and add a new public function to increment the value storage item.

Add a function to modify the storage value

Currently, the smart contract is configured in a way that prevents users from modifying the storage. To allow users to update storage items, it's necessary to designate value as a mutable variable. To add a for incrementing the stored value in your smart contract:

  1. Open the lib.rs file in a text editor.

  2. Add a new inc public function to increment the value stored using the by parameter that has data type of i32.

  3. Add a new test to the source code to verify this function.

  4. Save your changes and close the file.

  5. Check your work using the test subcommand:

    The command should display output similar to the following to indicate successful test completion:

Build the WebAssembly for the contract

Once you have tested the incrementer contract, you're prepared to compile this project into WebAssembly. To build the WebAssembly version of this smart contract:

  1. Open a terminal shell on your computer, if needed.

  2. Verify that you are in the incrementer project folder.

  3. Compile the incrementer smart contract by running the following command:

    The command displays output similar to the following:

Deploy and test the smart contract

You should have the substrate-contracts-node installed on your system, from the Flipper smart contract. You can start a local blockchain node specifically for your smart contract. Following this, cargo-contract can be utilized for deploying and testing your smart contract. To deploy it on the local node:

  1. Open a terminal shell on your computer, if needed.

  2. Start the contracts node in local development mode by running the following command:

  3. Upload and instantiate the contract

  4. Increment the value

  5. Get the current value

You should see the value retrieved from the contract: 42

Last updated