How to Set Up Foundry to Test Smart Contracts on Hedera
Foundry provides tools to developers who are developing smart contracts. One of the 3 main components of Foundry is Forge: Foundry’s testing framework. Tests are written in Solidity and are easily run with the forge test command. This tutorial will dive into configuring Foundry with Hedera to use Forge in order to write and run tests for smart contracts.
Note: Hashio, the SwirldsLabs hosted version of the JSON-RPC Relay, is in beta. If issues are encountered while using Foundry and Hashio, please create an issue in the JSON-RPC Relay GitHub repository.
Prerequisites
Basic understanding of TypeScript and Solidity.
Basic understanding of the Hedera JSON-RPC Relay.
Create a Hedera Project and Install the Hedera JS SDK
Open your terminal and create a directory called my-hedera-project
by running the following command:
Initialize a node project and accept all defaults:
Install dependencies and the Hedera JavaScript SDK:
In your project root directory, create your .env
file and fill the contents with your account Id and private key from the developer portal where you created your Hedera Testnet account. They will be used to create your Hedera client.
Create a file named index.ts
and create your Hedera client.
In the root directory, create two new empty folders: cache and test. Inside the cache folder, create the subfolder forge-cache
. Inside the test folder, create a subfolder called foundry
. Your Hedera project directory structure should look similar to this:
Create a Sample Foundry Project
Open a new terminal window and Create a sample Foundry project by running the following command in the new terminal:
Your foundry project will have the following directory structure
Copy the lib/forge-std folder from the sample foundry project into your Hedera Project
Foundry manages dependencies using git submodules by default. Hedera manages dependencies through npm modules. The default sample project comes with one dependency installed: Forge Standard Library. We need to get that over to our Hedera project. Copy the lib/forge folder from the sample Foundry project and put it in your Hedera project. Your Hedera project directory structure will look like this:
Configure Foundry in your Hedera project
Foundry’s default directory for contracts is src/
, but we will need it to map to contracts. Tests will map to our test/foundry
path, and the cache_path
will map to our cache/forge-cache
directory.
Let’s create a Foundry configuration file to update how Foundry behaves. Create a file in the root of your Hedera project and name it foundry.toml
and add the following lines:
Remap dependencies
Foundry manages dependencies using git submodules and has the ability to remap them to make them easier to import. Let’s create a new file at the project's root directory called remappings.txt
and write the following lines:
And what these remappings translate to is:
To import from
ds-test
we write import “ds-test/TodoList.sol”;To import from
forge-std
we write import “forge-std/Contract.sol”;
Create Tests
Create a smart contract
Create a smart contract file named TodoList.sol
under the contracts directory.
Create tests written in Solidity
Create a file named TodoList.t.sol
under test/foundry and write the following test:
Build and run your tests
In your terminal, ensure you are in your forge project directory and run the following command to build:
To run your tests run the following command:
Deploy Your Smart Contract
Step 1: Compile your smart contract using solc
Step 2: Read the compiled bytecode
In your index.ts file import fs in order to read your smart contracts bytecode.
Step 3: Create a function to deploy your smart contract
Step 4: Call your function that deploys your smart contract to the Hedera network
Forge Gas Reports
Forge has functionality built in to give you gas reports of your contracts. First, configure your foundry.toml
to specify which contracts should generate a gas report.
Add the below line to your foundry.toml
file:
In order to generate a gas report run the following command in your VSCode terminal:
Your output will show you an estimated gas average, median, and max for each contract function and total deployment cost and size.
Summary
In this tutorial, we have learned how to configure Foundry to work with a Hedera project to test our smart contracts using the forge framework. We also learned how to generate gas reports for our smart contracts.
Happy Building! Feel free to reach out if you have any questions:
Last updated