Tutorial on end-to-end testing of transferFrom user flow
Objectives:
Install the CLI and setup environment
Successfully upload your first smart contract to Workbench.
Visually design an end-to-end test scenario.
Generate the state space and a test suite.
Analyze and validate contract behavior.
Export a test case to Hardhat and Foundry.
Run the init
command from the main folder of your Foundry or Hardhat project to create the state-space.toml
config file.
It should look like this:
Add the workbench.new
property to automatically create a Workbench project upon submission. Your settings should like this:
Authenticate using the same credentials you created in Step 1. After you authenticate, you will see your project appear in the browser Workbench dasboard.
Use the browser to login to State Space, and select the My Contract
Workbench project we just uploaded.
Expand the folder tree to view your code as visual interactive components.
Each component represents an externally visible function, with the following identifiers:
Blue = CALL
- function call
Purple = VIEW
- view function
Green = CREATE
- constructor
Gray = DEPLOY
- deployment script
Build an end-to-end test scenario for the transferFrom(...)
flow using the following sequence of transactions:
Deployment
→ constructor(...)
→ approve(...)
→ transferFrom(...)
Since our project did not use a custom deployment script, we can use the provided deployment template to quickly setup a few EOAs.
From the bottom of the folder tree, drag an Empty Deployment
component to the canvas and rename it to “ERC20 Deployment”.
Reduce the EOA count to two (2) by deleting Claire
(keeping Alice
and Bob
). You can add more EOA accounts by selecting the +
icon from inside the node.
constructor
, approve
, and transferFrom
function calls onto the canvas and connect them in sequence, mimicing the end-to-end user flow.It should look like this:
transferFrom workflow
Transaction and function call parameters can be set to behave as symbolic values or concrete inputs.
When set to symbolic, State Space will automatically calculate the possible unique input values that will lead to an undiscovered path.
Learn more about how structured dynamic inputs are generated.
Alice
deploys the contractAlice
approves Bob
as the _spender
Bob
calls transferFrom(...)
Leave all other fields blank to act symbolically.
It should look like this:
transferFrom workflow with symbolic and concrete parameters
Select the icon to start.
Discovered execution paths will appear dynamically during execution. Overall coverage metrics will be displayed once the state space exploration is completed.
Contact us if you experience any issues.
Upon completion, you should see a total 39 test cases (execution paths) with a 37% health score.
For a more in-depth coverage analysis, open the coverage view in a new tab by selecting the run number under “Run History”. Select your contract on the left navigation menu and view the covered lines under the source
tab.
Feel free to experiement with different parameter settings and sequences to analyze behavior and maximize coverage.
Scroll through and locate the test case where:
Bob
approves Aice
to spend an allowance of uint MAX
tokens, ANDAlice
successfully transfers 1 token from Bob's
account.It should look like this:
Successful transaction
Transaction highlighted with green indicates a successful transaction occurred. Transactions ending with a red highlight indicate a reverting path.
Here is an example of a reverted execution path (expected behavior) where State Space autonomously tested a transferFrom amount greater than the umax
spending allowance:
Reverted transaction
Test case state analysis
History
tab.Select the “Export Test” button, and choose your framework (Hardhat or Foundry) to copy or download the test.
Check the “Events” box to include assertions for event emissions.
Export Test Case to Foundry or Hardhat
Tutorial on end-to-end testing of transferFrom user flow
Objectives:
Install the CLI and setup environment
Successfully upload your first smart contract to Workbench.
Visually design an end-to-end test scenario.
Generate the state space and a test suite.
Analyze and validate contract behavior.
Export a test case to Hardhat and Foundry.
Run the init
command from the main folder of your Foundry or Hardhat project to create the state-space.toml
config file.
It should look like this:
Add the workbench.new
property to automatically create a Workbench project upon submission. Your settings should like this:
Authenticate using the same credentials you created in Step 1. After you authenticate, you will see your project appear in the browser Workbench dasboard.
Use the browser to login to State Space, and select the My Contract
Workbench project we just uploaded.
Expand the folder tree to view your code as visual interactive components.
Each component represents an externally visible function, with the following identifiers:
Blue = CALL
- function call
Purple = VIEW
- view function
Green = CREATE
- constructor
Gray = DEPLOY
- deployment script
Build an end-to-end test scenario for the transferFrom(...)
flow using the following sequence of transactions:
Deployment
→ constructor(...)
→ approve(...)
→ transferFrom(...)
Since our project did not use a custom deployment script, we can use the provided deployment template to quickly setup a few EOAs.
From the bottom of the folder tree, drag an Empty Deployment
component to the canvas and rename it to “ERC20 Deployment”.
Reduce the EOA count to two (2) by deleting Claire
(keeping Alice
and Bob
). You can add more EOA accounts by selecting the +
icon from inside the node.
constructor
, approve
, and transferFrom
function calls onto the canvas and connect them in sequence, mimicing the end-to-end user flow.It should look like this:
transferFrom workflow
Transaction and function call parameters can be set to behave as symbolic values or concrete inputs.
When set to symbolic, State Space will automatically calculate the possible unique input values that will lead to an undiscovered path.
Learn more about how structured dynamic inputs are generated.
Alice
deploys the contractAlice
approves Bob
as the _spender
Bob
calls transferFrom(...)
Leave all other fields blank to act symbolically.
It should look like this:
transferFrom workflow with symbolic and concrete parameters
Select the icon to start.
Discovered execution paths will appear dynamically during execution. Overall coverage metrics will be displayed once the state space exploration is completed.
Contact us if you experience any issues.
Upon completion, you should see a total 39 test cases (execution paths) with a 37% health score.
For a more in-depth coverage analysis, open the coverage view in a new tab by selecting the run number under “Run History”. Select your contract on the left navigation menu and view the covered lines under the source
tab.
Feel free to experiement with different parameter settings and sequences to analyze behavior and maximize coverage.
Scroll through and locate the test case where:
Bob
approves Aice
to spend an allowance of uint MAX
tokens, ANDAlice
successfully transfers 1 token from Bob's
account.It should look like this:
Successful transaction
Transaction highlighted with green indicates a successful transaction occurred. Transactions ending with a red highlight indicate a reverting path.
Here is an example of a reverted execution path (expected behavior) where State Space autonomously tested a transferFrom amount greater than the umax
spending allowance:
Reverted transaction
Test case state analysis
History
tab.Select the “Export Test” button, and choose your framework (Hardhat or Foundry) to copy or download the test.
Check the “Events” box to include assertions for event emissions.
Export Test Case to Foundry or Hardhat