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.
Install the submodule dependencies
Copy Your ECDSA Hex Encoded Private Key
Grab your ECDSA hex encoded private key by logging into Hedera Portal.
If you need to create a Hedera account, follow the create a hedera portal profile faucet tutorial.
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
Your hex encoded private key must be prefixed with `0x`.
You should see output similar to the following:
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:
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.
You should see output similar to the following:
Use cast call
to execute TodoList.sol
's numberOfTodos()
.
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.
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
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
You should see output similar to the following:
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:
Last updated