DEV Community

Mayank Vats
Mayank Vats

Posted on • Edited on

Hardhat Implementation

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:

  1. Initialise an empty node project. npm init / yarn init
  2. Install hardhat. npm i hardhat / yarn add hardhat
  3. 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
  4. Run this command: npx hardhat. After running this command you will get the following prompt: (choose to create an empty hardhat.config.js)

Image description

  1. Replace the contents of hardhat.config.js with the following: hardhat.config.js
  2. 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.
  1. Make a file “Token.sol” in the contracts/ folder and copy paste the code from the above link.
  2. Save the file and then go to the terminal and run hh compile.
  3. After compiling two folders will be created namely artifacts/ and cache/.

Image description

  • artifacts/ folder will contain the contract’s ABI and bytecode.

Contract Testing Using Hardhat:

  • You can use this basic testing script to get started.
  1. Make a file “TokenTest.js” in the test/ folder and copy paste the code from above link.
  2. Save the file and then go to the terminal and run “hh test test/TokenTest.js”. Output should look something like below:

Image description

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.

  1. Make a file “TokenDeploy.js” in the scripts/ folder and copy paste the code from above link.
  2. Save the file and then go to the terminal and run “hh run scripts/TokenDeploy.js --network bscTestnet”.
  3. 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.

  1. 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”.
  2. Then require the plugin in the hardhat’s config file.
  3. Now make an account here and then go into the API keys section to create and get your API key.
  4. Copy the API key and paste it in the hardhat’s config as shown below.

Image description

  1. 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)