Skip to content

Latest commit

 

History

History

counter-script

SwayApps Counter Script Logo

Overview

The script is an example which demonstrates how to call contracts in an arbitrary order and test them using the Rust SDK.

The script manipulates contract state through ABI calls.

More information can be found in the specification.

Project structure

The project consists of a smart contract.

counter-script
├── counter
│   └── src/main.sw
├── libraries
│   └── src/interface.sw
├── interaction_script
│   ├── src/main.sw
│   └── tests/src/harness.rs
├── README.md
└── SPECIFICATION.md

Running the project

In order to run the subsequent commands change into the following directory /path/to/counter-script/<here>.

Program compilation

forc build --locked

Running the tests

Before running the tests the programs must be compiled with the command above.

cargo test --locked

Walkthrough/Additional Information

Execution flow

The script calls all 3 methods of the contract in a specific order, along with asserting the retrieved values from the contract in order to demonstrate that the contract is behaving as expected.

Note: "asserted" refers to the use of the require function, which asserts, or requires, that the input condition be true. If the input condition is false, the script stops executing and reverts.

Following is the order of execution:

  • the count method is called and the returned value is stored in a variable count
  • count is asserted to be equal to 0, as that is the default value of the count variable
  • the increment method is called and the returned value is stored in the variable count
  • count is now asserted to be equal to 1, as after incrementing 0 by 1, the result will be 1.
  • the increment method is called again and the returned value is stored in the variable count
  • count is now asserted to be equal to 2, as after incrementing 1 by 1, the result will be 2.
  • If the value of the parameter clear, which was passed into the script main function, is true, the clear method is called.
  • the count method is called and the returned value is stored in a variable count
  • the count variable is returned, script execution is finished.

Overview of the Rust integration test

The rust integration test deploys the counter contract, and then executes the script. One of the tests is with the clear set to true, another test is with the clear set to false.