Hardhat
Last updated
Last updated
Hardhat is an Ethereum development environment that provides an easy way to deploy smart contracts, run tests and debug Solidity code locally.
In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy a simple smart contract.
Set up Hardhat
Create a simple smart contract
Compile contract
Test contract
Deploy contract
There are a few technical requirements before we start. Please install the following:
(comes with Node)
Once we have those installed, you need to create an npm project by going to an empty folder, running npm init
, and following its instructions to install Hardhat. Once your project is ready, you should run:
To create your Hardhat project, run npx hardhat
in your project folder. Letβs create the sample project and go through these steps to try out a sample task and compile, test and deploy the sample contract.
To create a sample project, run npx hardhat
in your project folder. You should see the following prompt
Choose the JavaScript project and go through these steps to compile, test and deploy the sample contract.
The contracts
folder contains Lock.sol
, which is a sample contract which consistis of a simple digital lock, where users could only withdraw funds after a given period of time.
Go to hardhat.config.js
Update the hardhat-config
with matic-network-credentials
Create .env
file in the root to store your private key
To compile the contract, you first need to install Hardhat Toolbox:
Then, simply run to compile:
To run tests with Hardhat, you just need to type the following:
Run this command in root of the project directory:
Congratulations! You have successfully deployed Greeter Smart Contract. Now you can interact with the Smart Contract.
The sample project used here comes from the , as well as its instructions.:::
Add Polygonscan API key to .env
file to verify the contract on Polygonscan. You can generate an API key by
Find more instructions on how to use DOTENV on .
The contract will be deployed on Gauss Induction Labs Testnet, and you can check the deployment status here:
Run the following commands to quickly verify your contract on the G.I.L. Explorer. This makes it easy for anyone to see the source code of your deployed contract. For contracts that have a constructor with a complex argument list, see .