Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
Following document contains the minimal implementation steps to get started with hardhat:
- Initialise an empty node project.
npm init
/yarn init
- Install hardhat.
npm i hardhat
/yarn add hardhat
- Install following dev dependencies:
npm install -D @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
/yarn add -D @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
- Run this command:
npx hardhat
. After running this command you will get the following prompt: (choose to create an empty hardhat.config.js)
- Replace the contents of hardhat.config.js with the following: hardhat.config.js
- Create the following folders in the root directory,
contracts/
scripts/
test/
contracts/
- Here you will store all your contracts
scripts/
- Here you will store scripts for deploying contracts to the network
test/
- Here you will store all your test scripts
The upcoming text will show you how to, compile, test and deploy a basic ERC20 contract using hardhat.
NOTE: run this command “npm i -g hardhat-shorthand”
, to use “hh”
with every hardhat command you run instead of ”npx hardhat”
.
Contract Compilation Using Hardhat:
- You can use this contract.
- Make a file “Token.sol” in the contracts/ folder and copy paste the code from the above link.
- Save the file and then go to the terminal and run
hh compile
. - After compiling two folders will be created namely
artifacts/
andcache/
.
-
artifacts/
folder will contain the contract’s ABI and bytecode.
Contract Testing Using Hardhat:
- You can use this basic testing script to get started.
- Make a file “TokenTest.js” in the
test/
folder and copy paste the code from above link. - Save the file and then go to the terminal and run
“hh test test/TokenTest.js”
. Output should look something like below:
Contract Deployment Using Hardhat:
- You can use this deployment script to get started.
Note: To deploy the script, replace values with your own privateKey in the hardhat’s config file.
- Make a file “TokenDeploy.js” in the
scripts/
folder and copy paste the code from above link. - Save the file and then go to the terminal and run
“hh run scripts/TokenDeploy.js --network bscTestnet”
. - You can search for the address here and verify that the contract has been deployed. Save the contract’s address somewhere.
Contract Verification Using Hardhat:
To verify contracts using hardhat we will need to install a hardhat plugin “hardhat-etherscan”
. Following steps will walk you through with that.
- If you use yarn, run the following command:
“yarn add -D @nomiclabs/hardhat-etherscan”
and if you are using npm:“npm i -D @nomiclabs/hardhat-etherscan”
. - Then require the plugin in the hardhat’s config file.
- Now make an account here and then go into the API keys section to create and get your API key.
- Copy the API key and paste it in the hardhat’s config as shown below.
- Now run this command in your terminal: hh verify --network bscTestnet "CONTRACT_ADDRESS_HERE".
This concludes the basics of getting started with the hardhat. I hope this has helped you.
Top comments (0)