Deploy a Smart Contract Using Hardhat and Hiero JSON-RPC Relay
A step-by-step guide on deploying a smart contract to Hedera testnet and Hiero Local Node using Hardhat.
Last updated
Was this helpful?
A step-by-step guide on deploying a smart contract to Hedera testnet and Hiero Local Node using Hardhat.
Last updated
Was this helpful?
In this tutorial, you will walk through the step-by-step guide on deploying using Hardhat and Hiero JSON-RPC Relay. is a development environment for the EVM. It consists of different components for editing, compiling, debugging, and deploying smart contracts and dApps, all working together to create a complete development environment.
The is an implementation of Ethereum JSON-RPC APIs for Hedera and utilizes both Hiero Consensus Nodes and Mirror Nodes to support RPC queries defined in the . The project enables developers to establish their own local network for development and testing. The local network comprises the consensus node, mirror node, JSON-RPC relay, and other Hedera products, and can be set up using the CLI tool and Docker. This setup allows you to seamlessly build and deploy smart contracts from your local environment.
By the end of this tutorial, you'll be equipped to deploy smart contracts on the Hedera Testnet or your local Hedera node, leveraging Hardhat's tools for testing, compiling, and deploying.
Basic understanding of smart contracts.
Basic understanding of and JavaScript.
Basic understanding of .
Run the following command to clone the repo, change into the directory, and install dependencies:
Open the project in Visual Studio Code or your IDE of choice. The project structure of the repo you just cloned should look like this:
In this step, you will update and configure your environment variables and Hardhat configuration files. These files play a crucial role in defining tasks, securely storing account private keys, and specifying RPC endpoint URLs required for network interactions.
The .env
file securely stores environment variables, such as your Hedera network endpoints and private keys, which are then imported into the hardhat.config.js
file. This helps protect sensitive information like your private keys and API secrets, but it's still best practice to add .env
to .gitignore
file to prevent you from committing and pushing your credentials to GitHub. Go to the tab corresponding to your deployment path and follow the steps to set up your environment variables.
Rename the .env.example
File
Start by renaming the provided .env.example
file to .env
.
Review the .env
File
Open the .env
file to modify with your private keys and understand its contents.
Hiero Local Node environment variables
The variables are predefined for the purposes of this tutorial.
Variables explained
LOCAL_NODE_OPERATOR_PRIVATE_KEY
: This is your Alias ECDSA hex-encoded private key for your Hedera Local Node. Replace the example value with your actual private key. Once you set up your local node and run the command to start, the accounts list for alias ECDSA private keys will be generated and returned to your console (see screenshot below). Replace the example value with your actual private key.
LOCAL_NODE_ENDPOINT
: This is the URL endpoint for your Hedera Local Node's JSON-RPC Relay. Typically, this would be your localhost
followed by the port number (http://localhost:7546/
).
Configuring these environment variables enables your Hardhat project to interact with the Hedera network or your local node. Let's review the Hardhat configuration file, where these environment variables are loaded into.
The Hardhat config (hardhat.config.js
) file serves as the central configuration file for your Hardhat project. This file is crucial for specifying various settings, including Hardhat tasks, network configurations, compiler options, and testing settings. Let’s review the configuration settings.
These first lines import the required Hardhat plugins and the dotenv
module that loads environment variables from the .env
file. This will allow you to keep your private keys secure while using them in your dApp and will keep you from committing these to GitHub.
Here, the Solidity compiler version is set to "0.8.9". The optimizer is enabled with 500 runs to improve the contract's efficiency.
The networks
object is essential for defining the Hedera networks your Hardhat project will connect to. Additionally, the defaultNetwork
key specifies the network Hardhat will default to if none is specified at deployment.
Key/value breakdown:
defaultNetwork
: This property specifies which network configuration will be used by default when you run Hardhat commands.
networks
: This property contains configurations for different networks you might connect to.
url
: This specifies the URL endpoint for the network. The value is pulled from the .env
file where the environment variables are defined.
accounts
: This lists the private keys for the accounts you'll use when connecting to the network. The value is pulled from the .env
file where the environment variables are defined.
Now that your project is configured compile your contract. Run the following command in the hedera-hardhat-example-project
terminal:
After the initial compilation, if you don't modify any files, nothing will be compiled when you run the compile
command. To force a compilation you can use the --force
flag or run npx hardhat clean
to clear the cache and delete the artifacts to recompile.
Once your contract is compiled successfully, deploy the Greeter.sol
smart contract. There are additional steps required if you're deploying to your local node:
Test your contract before deploying it. In the hedera-hardhat-example-project
terminal, run the following command to compile and test your contract:
Tests should pass with "4 passing" returned to the console. Otherwise, an error message will appear indicating the issue.
In the same terminal, run the following command to deploy your contract to the default network specified in your config file:
Alternatively, you can target any network configured in your Hardhat config file. For testnet:
To simplify the setup process, you can clone a boilerplate Hardhat example project from the hedera-hardhat-example-project
. Open a terminal window and navigate to your preferred directory where your Hardhat project will be stored.
Let's review the Hardhat project folders/content. For more information regarding Hardhat projects, check out the . If you do not need to review the project contents, you can skip this optional step.
NOTE: The pragma solidity line must match the version of Solidity defined in the of your hardhat.config.js
file.
The test/
folder contains the test files for the project.
The rpc.js
file is located in this folder in the hedera-example-hardhat-project
project and references the Hardhat that are defined in the hardhat.config file
. When the command npx hardhat test
is run, the program executes the rpc.js
file.
A file that stores your environment variables like your accounts, private keys, and references to Hedera network. Details of this file are available in of this tutorial.
The Hardhat configuration file. This file includes information about the Hedera network RPC URLs, accounts, and tasks defined. Details of this file are available in of this tutorial.
Prerequisite: A Hiero Local Node set up and running ().
Prerequisite: A Hedera testnet account from the .
For this tutorial, we'll use Hashio, an instance of the hosted by . You can use any JSON-RPC instance the community supports.
These lines define tasks that are accessed and executed from the or folders.
The compiled artifacts will be saved in the artifacts/
directory by default, or whatever your is. The metadata file generated in this directory will be used for the in a later step.
Note: If you have not set up your Hedera Local Node, you can do so by following tutorial and returning to this step once you complete the setup.
Stop your local node and remove Docker containers by running hedera stop
or docker compose down
in your hedera-local-node
terminal. Reference the section of the local node setup tutorial.
You can view the contract you deployed by searching the smart contract public address in a supported . For this example, we will use the Network Explorer. Copy and paste your deployed Greeter.sol
public contract address into the HashScan search bar.
Additionally, you can verify your contract using the HashScan verification feature (beta). Follow these to learn how.
Congratulations! 🎉 You have successfully learned how to deploy a smart contract using Hardhat and Hedera JSON-RPC Relay. Feel free to reach out in !
➡
➡
➡
➡
➡
Writer: Krystal, Technical Writer
Editor: Simi, Sr. Software Manager
Editor: Nana, Sr Software Manager
Editor: Georgi, Sr Software Dev (LimeChain)
|
|
|
|