Documentation Index
Fetch the complete documentation index at: https://hedera-0c6e0218-mintlify-enhance-hollow-account-docs-97196.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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.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
castcommand - Verify your smart contract on Hashscan
Prerequisites
Before you begin, you should have completed the following tutorial:forge, cast, anvil, and chisel.
Table of Contents
- Step 1: Project Setup
- Step 2: Creating the ERC20 Contract
- Step 3: Create a Deployment Script
- Step 4: Deploy your ERC20 Smart Contract
- Step 5: Interacting with the Contract
- Step 6: Verifying the Contract on Hashscan
Step 1: Project Setup
Initialize Project
Set up your Foundry project: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.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.
OPERATOR_KEY in a .env file. For the JSON RPC_URL, we’ll use the Hashio RPC endpoint for testnet.
.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.
Configure Foundry
Foundry uses thefoundry.toml file for configuration. Open it and add profiles for the Hedera Testnet RPC endpoint.
foundry.toml
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 token is named “HederaToken” with the symbol “HEDT”
- The
constructormints an initial supply of 1,000 tokens to the deployer of the contract - An
onlyOwnermintfunction is included to allow the contract owner to mint more tokens in the future.
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 namedHederaToken.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.
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 usingcast, 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:
$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.
balanceOf function to check the token balance of your account.
1000000000000000000000.
Transfer Tokens
Next, let’s transfer 100 tokens to a new account. For this example, we’ll generate a new random private key.
100e18 is a convenient way to write 100 * 10^18.
Step 6: Verifying the Contract on Hashscan
Verifying your smart contract makes its source code publicly available on Hashscan. 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.
Next Steps
- Check out OpenZeppelin ERC-20 Documentation
- See the full code in the Hedera-Code-Snippets Repository
- Follow more Foundry guides with Hedera.