How to Fork Testnet on Latest Block
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.
What you will accomplish
Prerequisites
Before you begin, you should be familiar with the following:
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.
git clone --recurse-submodules [email protected]:hedera-dev/fork-hedera-testnet.git
Install the submodule dependencies
forge install
Copy Your ECDSA Hex Encoded Private Key
Grab your ECDSA hex encoded private key by logging into Hedera Portal.
Deploy your contract to Hedera Testnet
In your terminal, replace the value of "HEX_Encoded_Private_Key"
with your ECDSA
account's private key in the command below:
Next, Replace "RPC_URL" with a Tesnet URL by choosing one of the options over at How to Connect to Hedera Networks Over RPC
forge create --rpc-url "RPC_URL" --private-key "HEX_Encoded_Private_Key" src/TodoList.sol:TodoList
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:
https://hashscan.io/testnet/contract/<Deployed_Contract_EVM_Address>
Example: https://hashscan.io/testnet/contract/0xc1E551Eb1B3430A8D373C43e8804561fca5ce90D
Execute a contract call and create a new todo
Use Foundry's cast send
command to sign and publish a transaction to Hedera Tesnet.
In your terminal, you will need to replace the following items in the command below:
Replace
"deployed-contract-EVM-address"
with your deployed contracts EVM addressReplace
"HEX_Encoded_Private_Key"
with your hex encoded private key.Replace
"RPC_URL"
with a Tesnet URL.
cast send "deployed-contract-EVM-address" --private-key "HEX_Encoded_Private_Key" "createTodo(string)(uint256)" "Buy camping supplies" --rpc-url "RPC_URL"
You should see output similar to the following:
blockHash 0x958f246cc010aa074c81eae1988abc16c16bc12d2246397320d163b5ea748a89
blockNumber 7261495
contractAddress 0xc1E551Eb1B3430A8D373C43e8804561fca5ce90D
cumulativeGasUsed 91838
effectiveGasPrice 1050000000000
gasUsed 91838
logs []
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status 1
transactionHash 0x18286c79ae735dd08fccbe676ee2d7335147d7a1a6c7f2d0e99099c2802c397c
transactionIndex 2
type 2
Use cast call
to execute TodoList.sol
's numberOfTodos()
.
cast call "deployed-contract-EVM-address" "numberOfTodos()(uint256)" --rpc-url "RPC_URL"
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
assertEq(todoCountAfterCreate, (numberOfTodos + 1));
Fork test Hedera Testnet and run your test
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.
Complete
Congratulations, on completing the tutorial on how to fork Hedera Testnet on the latest block.
You have learned how to:
Last updated
Was this helpful?