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.
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.mdIn order to run the subsequent commands change into the following directory /path/to/counter-script/<here>.
forc build --lockedBefore running the tests the programs must be compiled with the command above.
cargo test --lockedThe 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
countmethod is called and the returned value is stored in a variablecount countis asserted to be equal to 0, as that is the default value of thecountvariable- the
incrementmethod is called and the returned value is stored in the variablecount countis now asserted to be equal to 1, as after incrementing 0 by 1, the result will be 1.- the
incrementmethod is called again and the returned value is stored in the variablecount countis 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, theclearmethod is called. - the
countmethod is called and the returned value is stored in a variablecount - the
countvariable is returned, script execution is finished.
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.