Skip to main content

Deploying a Contract Using Foundry

This tutorial will walk you through writing and compiling an ERC-20 Solidity smart contract. You’ll then deploy and interact with it on the Hedera network using the Hedera Smart Contract Service (HSCS) and Foundry, connecting via the JSON-RPC relay.
Developing or running CI? Solo runs a full Hedera network locally, no testnet rate limits, faucet, or resets, and works with the same EVM tooling. See the Solo quickstart and Using Solo with EVM tools.

What you will accomplish

By the end of this tutorial, you will be able to:
  • Compile and deploy a smart contract using Foundry
  • Interact with a smart contract using Foundry’s cast command
  • Verify your smart contract programmatically with forge verify-contract

Prerequisites

Before you begin, you should have completed the following tutorial:
This will install forge, cast, anvil, and chisel.

Table of Contents

  1. Step 1: Project Setup
  2. Step 2: Creating the ERC20 Contract
  3. Step 3: Create a Deployment Script
  4. Step 4: Deploy your ERC20 Smart Contract
  5. Step 5: Interacting with the Contract
  6. Step 6: Verify Your Smart Contract with Foundry

Step 1: Project Setup

Initialize Project

Set up your Foundry project:
This creates a new directory with a standard Foundry project structure, including src, test, and script folders.

Install Dependencies

Foundry uses git submodules to manage dependencies. We’ll install the OpenZeppelin Contracts library, which provides a standard and secure implementation of the ERC20 token.
This command will download the contracts and add them to your lib folder.

Create .env File

Create a .env file in your project’s root directory to securely store your private key and the RPC URL for the Hedera Testnet.
Securely store your sensitive data like the OPERATOR_KEY in a .env file. For the JSON RPC_URL, we’ll use the Hashio RPC endpoint for testnet.
.env
Replace the 0x-your-private-key environment variable with the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
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.

Configure Foundry

Foundry uses the foundry.toml file for configuration. Open it and add profiles for the Hedera Testnet RPC endpoint.
foundry.toml
Note the values in remappings field. We need this to import prefix to a filesystem path so both Foundry(forge) and our editor can resolve short, package-like imports instead of long relative paths.

Step 2: Creating the ERC20 Contract

Create a new Solidity file (HederaToken.sol ) inside the src directory:
src/HederaToken.sol
The contract uses the OpenZeppelin ERC20 and Ownable implementations.
  • The token is named “HederaToken” with the symbol “HEDT”
  • The constructor mints an initial supply of 1,000 tokens to the deployer of the contract
  • An onlyOwner mint function is included to allow the contract owner to mint more tokens in the future.
Now, compile the contract:
This command compiles your contracts and places the artifacts(including the ABI and bytecode) into the out directory. We are now ready to deploy the smart contract.

Step 3: Create a Deployment Script

Using a script is the standard and most reliable way to handle deployments in Foundry. Create a new file named HederaToken.s.sol inside the script directory.
script/HederaToken.s.sol

Step 4: Deploy Your ERC20 Smart Contract

Now, execute the script to deploy your contract. Foundry will automatically load the variables from your .env file.
After a few moments, you will see the address of your newly deployed contract:
Copy the deployed contract address. You’ll need this in subsequent steps.

Step 5: Interacting with the Contract

Now that the contract is deployed, you can interact with it using cast, Foundry’s command-line tool for making RPC calls. To use cast and other command-line tools, you need to load the variables from your .env file into your current terminal session. Load Environment Variables Run the following command to load the HEDERA_PRIVATE_KEY and HEDERA_RPC_URL into your shell:
Now your shell knows the value of $HEDERA_PRIVATE_KEY and $HEDERA_RPC_URL. Set Up Shell Variables Next, set up variables for your contract address and public address to make the next commands easier to read. Please export these variables in your shell.
Check Your Balance Let’s call the balanceOf function to check the token balance of your account.
The output will be the balance in its raw form (with 18 decimals):
You can use the following to convert the hexadecimal to a decimal number so it’s human readable.
You should see the value 1000000000000000000000. Transfer Tokens Next, let’s transfer 100 tokens to a new account. For this example, we’ll generate a new random private key.
Now, send 100 tokens to this new address. Note that 100e18 is a convenient way to write 100 * 10^18.
After the transaction confirms, check the recipient’s balance:
The output will show the 100 tokens you sent:

Step 6: Verify Your Smart Contract with Foundry

Foundry can verify your contract programmatically straight from the command line via forge verify-contract. Programmatic verification is the most reliable and efficient method, especially for complex or upgradeable contracts, because Foundry already knows your exact compilation settings, dependency graph, and deployment artifacts. forge verify-contract submits the verification request to Sourcify, which natively supports Hedera Mainnet (chain ID 295) and Testnet (chain ID 296). Once Sourcify accepts the match, the verified status surfaces automatically on HashScan and any other explorer that reads from Sourcify. The constructor for this contract takes one argument (initialOwner), which we must provide for successful verification. Run the following command, using the variables you set earlier.
After running the command, you should see a success message.
Congratulations! 🎉 You have successfully deployed, interacted with, and verified an ERC20 smart contract on the Hedera Testnet using Foundry. Feel free to reach out in Discord!

Next Steps

Writer: Kiran, Developer Advocate

Editor: Luke, DevRel Engineer