Deploy a Smart Contract Using Hardhat

In this tutorial, you'll be guided through setting up a Hardhat project and deploying a Hedera smart contract to the Hedera Testnet using the Hashio JSON-RPC instance.

Hardhat is a development environment for Ethereum smart contracts. It consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps, all working together to create a complete development environment. By the end of this tutorial, you'll have learned how to deploy smart contracts using Hardhat on the Hedera Testnet**.**

Prerequisites

Hardhat Project Setup

To make the setup process simple, you'll use a pre-configured Hardhat project from the hedera-hardhat-example-project repository.

Open a terminal window and navigate to your preferred directory where your Hardhat project will live. Run the following command to clone the repo and install dependencies to your local machine:

git clone https://github.com/hashgraph/hedera-hardhat-example-project.git
cd hedera-hardhat-example-project
npm install

The dotenv package is used to manage environment variables in a separate .env file, which is loaded at runtime. This helps protect sensitive information like your private keys and API secrets, but it's still best practice to add .env to your .gitignore to prevent you from pushing your credentials to GitHub.

Project Configuration

In this step, you will update and configure the Hardhat configuration file that defines tasks, stores Hedera account private key information, and Hashio Testnet RPC URL. First, rename the .env.example file to .env. and update the .env and hardhat.config.js files with the following code.

But first, in order to deploy the contract to the Hedera Testnet, you will need to get a testnet account and key. To get a testnet account, create an ECDSA account using the Hedera Developer Portal. Once you have your ECDSA account and HEX encoded private key, add the private key to the TESTNET_OPERATOR_PRIVATE_KEY variable in the .env file.

Create your testnet account 🛠️

Create Hedera Portal Profile (Faucet)

The Hedera Testnet account allows you to interact with our APIs and pay for the transaction fees. Visit the Hedera portal to create your Hedera Testnet account and follow the instructions.

Once you have completed the instructions, you will receive a Hedera Testnet account ID (0.0.x) and your private/public key pair on your testnet page. You will need to copy over your ECDSA HEX Encoded private key and paste it in the TESTNET_OPERATOR_PRIVATE_KEY .env file variable.

Note: Your Hedera Testnet account will be credited with 10,000 test HBAR upon creation that can only be utilized on the Hedera test network. Your balance will be topped up daily to 10,000 test HBAR when you use your funds.

Environment Variables

The .env file defines environment variables used in the Hardhat configuration file. The TESTNET_OPERATOR_PRIVATE_KEY variable contains the ECDSA Hex Encoded Private Key for the Hedera Testnet account used in the testnet network Hardhat configuration.

The TESTNET_ENDPOINT variable contains the HashIO Testnet endpoint URL. This is the JSON-RPC instance that will submit the transactions to the Hedera test network to test, create and deploy your smart contract.

Hardhat Configuration

The hardhat.config.js file defines tasks for Hardhat, including show-balance, transfer-hbars, deploy-contract, contract-view-call, and contract-call. It exports a configuration object that includes the Solidity version and settings, default network, and network settings for the testnet network.

The url property is set to the TESTNET_ENDPOINT environment variable, and accounts to an array containing the testnet private key imported from the .env file.

Project Contents

In this step, you'll look at the descriptions of the Hardhat project contents. For more information regarding Hardhat projects, check out the Hardhat docs. If you do not need to review the project contents you can skip to "Test and Deploy."

contracts/

The contracts/ folder contains the source file for the Greeter smart contract. Let's review the Greeter.sol smart contract in the hedera-example-hardhat-project/contracts folder. At the top of the file, the SPDX-License-Identifier defines the license, in this case, the MIT license. The pragma solidity ^0.8.9; line specifies the Solidity compiler version to use. These two lines are crucial for proper licensing and compatibility.

NOTE: The pragma solidity line must match the version of Solidity defined in the module exports of your hardhat.config.js file.

scripts/

The scripts/ folder contains the automation scripts for the test file. This project contains 4 scripts.

The scripts/ folder contains test scripts for locally testing a smart contract before deploying it. Please read the comments to help you understand the code and its purpose:

Calls the setGreeting function from the Greeter contract and sets the greeter message to "Greeter."\

test/

The test/ folder contains the test files for the project. The rpc.js file is located in the test folder of the hedera-example-hardhat-project project and references the Hardhat tasks that are defined in the hardhat.config file. When the command npx hardhat test is run, the program executes the rpc.js file.

.env

A file that stores your environment variables like your accounts, private keys, and references to Hedera network (see previous step).

hardhat.config.js

The Hardhat project configuration file. This file includes information about the Hedera network RPC URLs, accounts, and tasks defined (see previous step).

Test and Deploy

Now that you have your project set up and configured, let's deploy the Greeter.sol smart contract to the Hedera Testnet using Hashio. Hashio is an instance of the Hedera JSON-RPC relay hosted by Swirlds Labs and provides convenient access to the Hedera network for transactions and data querying. You can use any JSON-RPC instance supported by the community.

Run the following command in your terminal to run the hedera-hardhat-example-project/test/rpc.js test file on the Hedera testnet.

Tests should pass with "4 passing" returned to the console. Otherwise, an error message will appear indicating the issue.

console check ✅

Lastly, run the following command to deploy the contract to the Hedera Testnet:

console check ✅

You've successfully deployed your contract to the Hedera Testnet! You can view the contract you deployed by searching the smart contract public address in a supported Hedera Network Explorer. For this example, we will use the HashScan Network Explorer. Copy and paste your deployed Greeter.sol public contract address into the HashScan search bar.

The Network Explorer will return the information about the contract created and deployed to the Hedera Testnet. The "EVM Address" field is the public address of the contract that was returned to you in your terminal. The terminal returned the public address with the "0x" hex encoding appended to the public address. You will also notice a contract ID in 0.0.contractNumber (0.0.3478001) format. This is the contract ID used to reference the contract entity in the Hedera Network.

Note: At the top of the explorer page, remember to switch the network to TESTNET before you search for the contract.

Hashscan transaction

Congratulations! 🎉 You have successfully learned how to deploy a smart contract using Hardhat. Feel free to reach out if you have any questions:

Last updated