Counter
The following sections describe how to create an example smart contract, which manages a counter
that can be initialized
, incremented
, decremented
, and queried
. Later on, you will be
writing tests for this smart contract using MultiTest
.
Counter smart contract is a simplified version of the contract created from the cw‑template (opens in a new tab). Check Setting up the contract for more details.
Specification
The example smart contract is a simple counter that allows users to initialize the counter, perform operations such as incrementing, decrementing, setting a specific value, and querying the current counter value. The primary purpose of this contract is to maintain and manipulate a counter value on the blockchain. This functionality can be utilized in various scenarios where a count or tally needs to be tracked, such as counting votes, tracking the number of actions performed, or maintaining a score in a game or competition.
Features:
- The counter can be initialized with a value of zero or any non-negative integer between 0 and 255.
- Users can increment, decrement, or set the counter to a new value within the range of 0 to 255.
- Boundary conditions are checked, preventing the counter from going below 0 or beyond 255.
- Users can query the current value of the counter at any time.
- The counter's value is persisted and limited to 8-bit unsigned integers [0..255].
Creating the counter project
Smart contracts written in Rust are developed as Rust libraries, so let's first create a Rust library named counter.
Change the working directory to your home directory:
cd ~
Create a dedicated directory to store your example smart contract:
mkdir my-contracts
Change the working directory to my-contracts
:
cd my-contracts
Create a new Rust library named counter:
cargo init --lib counter
Change the working directory to counter
:
cd counter
Newly created library contains the Cargo.toml file and lib.rs file in src
directory...
tree
...so the expected structure of the counter
directory is:
.
├── Cargo.toml
└── src
└── lib.rs
By convention, the source code of the smart contract is placed in the file named contract.rs,
and the messages processed by this contract are usually placed in file named msg.rs. Both files
should be stored in src
directory.
Let's create an empty contract.rs file...
touch src/contract.rs
...and empty msg.rs file:
touch src/msg.rs
The final structure of the smart contract project placed in the counter
directory should look like
this:
tree
.
├── Cargo.toml
└── src
├── contract.rs
├── lib.rs
└── msg.rs
Filling the content
In the previous section you have created a project structure for counter smart contract, but the source files are still empty. In the following chapters, we provide examples of two different implementations of the counter smart contract. The first one is using the pure CosmWasm libraries, and the second one is using the Sylvia framework.
Whether you're interested in developing smart contracts using pure CosmWasm libraries or exploring the Sylvia framework, the following examples will guide you through the basic content of the smart contract.
To better understand how the Sylvia framework can streamline and simplify smart contract development, we recommend creating and testing the counter smart contract in both ways.