Forking lets you interact with contracts and run tests as if on a real network. Learn how to fork Hedera Testnet on the latest block and test your contracts with the latest state of the network.
If the output contains text similar to command not found, please install that item.
Get started
Set up project
To follow along, start with the main branch, which is the default branch of this repository. This gives you the initial state from which you can follow along with the steps as described in the tutorial.
forge manages dependencies by using git submodules. Clone the following project and pass --recurse-submodules to the git clone command to automatically initialize and update the submodule in the repository.
Your hex encoded private key must be prefixed with `0x`.
You should see output similar to the following:
[⠒] Compiling...
[⠔] Compiling 22 files with 0.8.23
[⠑] Solc 0.8.23 finished in 3.44s
Compiler run successful!
No files changed, compilation skipped
Deployer: 0xdfAb7899aFaBd146732c84eD83250889C40d6A00
Deployed to: 0xc1E551Eb1B3430A8D373C43e8804561fca5ce90D
Transaction hash: 0x8709443db7b60df7b563c83514ce8b03e54c341a5fe9844e01c72b05fc50950e
Open the Hashscan link to your deployed contract by copying the Deployed to EVM address and replacing <Deployed_Contract_EVM_Address> in the link below:
You should see numberOftodos has incremented by 1.
cast send signs/publishes a transaction and changes the state.
cast call performs a call without changing state. Essentially a read transaction.
Write your test
An almost-complete test has already been prepared for you. It's located at test/TodoList.t.sol. You will only need to make a few modifications (outlined below) for it to run successfully.
Look for a comment in the code to locate the specific lines of code which you will need to edit. For example, in this step, look for this: // Step (1) in the accompanying tutorial You will need to delete the inline comment that looks like this: /* ... */. Replace it with the correct code.
Step 1: Target your deployed contract
Open the TodoList.t.sol file and copy and paste the line below.
Replace "Deployed_Contract_EVM_Address" with your deployed contract's EVM address.
TodoList public todoList =
TodoList("Deployed_Contract_EVM_Address");
Step 2: Perform an assertion
This test saves the current number of todos as numberOfTodos, then executes createTodo(), and finally performs an assertion to check that the new number of todos has increased.
Write a statement which asserts that the todoCountAfterCreate is equal to the numberOfTodos + 1.
Copy the below line and paste it in TodoList.t.sol
Using the --fork-url flag you will run your test against a forked Hedera Testnet environment at the latest block.
In your terminal, replace "RPC_URL" with a Tesnet URL
forge test --fork-url "RPC_URL" -vvvv
You should see output similar to the following:
[⠢] Compiling...
[⠰] Compiling 1 files with 0.8.23
[⠔] Solc 0.8.23 finished in 1.15s
Compiler run successful!
Running 1 test for test/TodoList.t.sol:TodoListTest
[PASS] test_createTodo_returnsNumberOfTodosIncrementedByOne() (gas: 59188)
Traces:
[59188] TodoListTest::test_createTodo_returnsNumberOfTodosIncrementedByOne()
├─ [2325] 0xc1E551Eb1B3430A8D373C43e8804561fca5ce90D::getNumberOfTodos() [staticcall]
│ └─ ← 1
├─ [51026] 0xc1E551Eb1B3430A8D373C43e8804561fca5ce90D::createTodo("A new todo for you!")
│ └─ ← 2
└─ ← ()
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 2.13s
Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
Your output will show you the state of numberOfTodos before you created a new todo and after. It also shows whether the test passed, failed or was skipped.
If you'd like to test a contract deployed on mainnet use a Mainnet RPC URL. Currently fork testing at the latest block is only supported. Be aware everytime you run your test it is against the latest state of the network.
Complete
Congratulations, on completing the tutorial on how to fork Hedera Testnet on the latest block.
You have learned how to:
Grab your ECDSA hex encoded private key by logging into .
If you need to create a Hedera account, follow the tutorial.
Next, Replace "RPC_URL" with a Tesnet URL by choosing one of the options over at