Not sure whether to use the Hiero Local Node, Hashio, or a custom relay setup?
Read this blog
post
comparing the different options.
You can take a look at the complete code in the Hedera-Code-Snippets
repository.
- Hedera Localnet (via the Hiero Local Node + JSON-RPC Relay)
- Hedera Testnet (via Hashio JSON-RPC Relay)
foundry.toml, write a simple contract and a Foundry deployment script, and learn how to switch between Localnet and Testnet with a single flag.
If you’re looking for an end-to-end “Hello, ERC-20” tutorial, see: “Getting started with Foundry” (deploy, interact, verify on Hashscan). This guide focuses on environment configuration and workflow for Localnet/Testnet.
What you will accomplish
- Configure Foundry to talk to the Hedera JSON-RPC Relay (Localnet and Testnet)
- Deploy and interact with a sample contract using
forge scriptandcast - Verify contracts on Hashscan (Testnet)
Prerequisites
- Foundry installed (forge, cast, anvil, chisel):
curl -L https://foundry.paradigm.xyz | bashfoundryup
- ECDSA account and private key
- For Testnet: create/fund an account via the Hedera Portal
- Basic Solidity / CLI familiarity
Table of Contents
- Option A: Run Hedera Localnet
- Option B: Use Hedera Testnet
- Step 1: Initialize a Foundry project
- Step 2: Add environment variables
- Step 3: Configure foundry
- Step 4: Add a simple contract
- Step 5: Update the deploy script
- Step 6: Deploy the contract
- Step 7: Run tests
- Step 8: Verifying the contract on Hashscan
- Interact using cast
Option A: Run Hedera Localnet (Hiero Local Node)
Hedera provides a local node configuration that includes a mirror node, a consensus node, and the JSON-RPC relay. You can run it vianpm.
Clone, install, and run the node:
- On startup, the Local Node prints funded ECDSA accounts to the console logs.
You can use one of these for local development. Make sure to only use accounts
listed under “Accounts list (Alias ECDSA keys)” * The JSON-RPC Relay is
typically on port 7546. So, the URL would be
http://localhost:7546
Option B: Use Hedera Testnet (Hashio)
Hashio is a community relay node suitable for development/testing:- RPC URL:
https://testnet.hashio.io/api
Step 1: Initialize a Foundry project
src, script, test, and lib.
Project Structure
The Foundry project initialization creates the following file structure:foundry.toml: You can configure Foundry’s behavior using this file such as defining RPC URLssrc: Serves as the default for storing your smart contract source codescript: This is where you store Solidity scripts for deploying contracts and performing other on-chain operationstest: Serves as the dedicated location for Solidity-based unit and integration tests for your smart contracts
Step 2: Add environment variables
Create an.env for your RPC URL and private key.
.env
Please note: that Hashio is intended for development and testing
purposes only. For production use cases, it’s recommended to use
commercial-grade JSON-RPC Relay or host your own instance of the Hiero
JSON-RPC Relay.
Step 3: Configure “foundry.toml”
Foundry uses thefoundry.toml file for configuration. Open it and add profiles for the Hedera RPC endpoint.
foundry.toml
Step 4: Add a simple contract
Use a tinyCounter contract to focus on configuration rather than external dependencies for this exercise.
Compile with:
Step 5: Update the deploy script
We are going to update our deploy script a little bit so we can use our private key instead of passing it as a flag every time:script/Counter.s.sol
Step 6: Deploy the contract
Now, execute the script to deploy your contract:Note that Foundry hardcodes “ETH” in its summary. However, even if it says
ETH, because we’re connected to Hedera, the currency used is HBAR..env values to point to another Hedera Network(Localnet or Testnet) and try again.
Step 7: Run tests
You can also run the test suite that’s included as part oftest/ directory:
Step 8: Verifying the Contract on Hashscan
Verifying your smart contract makes its source code publicly available on Hashscan. *Note: Please note that verification of the contract can not be done on Localnet and this process is relevant only for Hedera Testnet/Mainnet. Also, make sure to replace<your-contract-address> with your own contract address you got after the deployment above.*
Run the following command, using the variables you set earlier.
Interact using cast
Set helpers:43 since the counter is at 43 now after having added 42 + 1.
Further Learning & Next Steps
Want to take your local development setup even further? Here are some excellent tutorials to help you dive deeper into smart contract development on Hedera using Foundry:- How to Mint and Burn an ERC-721 Token (Part 1)
Learn how to create a basic ERC-721 NFT, mint it, and burn it on Hedera. - How to Write Tests in Solidity (Part 2)
Learn how to start writing tests in Foundry using Solidity - How to Fork the Hedera Network for Local Testing
Learn how to fork hedera network(testnet/mainnet) locally so you can start testing against the forked network