fbpx

Hardhat in 2 mins

Julien Klepatch


In this article I will introduce Hardhat, a framework for smart contracts.

 With Hardhat you can compile, deploy, test, and debug smart contracts.



Hardhat started as an alternative to Truffle, an older smart contract framework, 
before becoming the most popular framework for smart contracts.



Create a Hardhat project

First you need to install Hardhat. 

It will work on MacOS, Linux and Windows.

 Before installing Hardhat, you need to install NodeJS and Npm.



After, in your terminal, you create a folder for your project:

mkdir myproject && cd myproject



You create a new npm project:

npm init -y



Then you install Hardhat locally:

npm install -D hardhat

Thanks to a local installation, you can be sure that the correct version hardhat is going to be used when you use Hardhat CLI.



After, create an hardhat project, using your local installation

:

npx hardhat

It will ask you a couple of questions:

  • Create a sample project: yes
  • Use this path: yes
  • Create a gitignore file: it doesn’t matter for this tutorial
  • Install ethers and waffle dependencies: yes. These are optional dependencies that you will most likely need

Now your folder has been populated with the file structure of Hardhat

README.md
contracts         //For smart contracts - There is already a example contract `Greeter.sol`
hardhat.config.js //Config files
node_modules
package-lock.json
package.json
scripts           //For deployment scripts
test              //For smart contract tests

Running Hardhat commands



You can run npx hardhat to get a list of all commands available.

For example, with npx hardhat accounts you can the list of all accounts generated by Hardhat.



You can also compile your smart contracts with npx hardhat compile. It will compile the example Greeter.sol contract that was created when you previously initialized your Hardhat project.



After the compilation, you will have 2 new folders:

artifacts    //Compilation artifacts are there
cache        //You can ignore this one

In the artifacts folder, you will create one json file per smart contract. Each of this file containes the result of the compilation. In our case, our smart contract is Greeter.sol, and the corresponding json file is artifacts/contracts/Greeter.sol/Greeter.json.

This file has 2 important parts:

  • the abi key which is an interface used by frontend applications to interact with the smart contract
  • the bytecode key, which represents your compiled smart contract as a series of EVM opcodes. That’s what will be used to deploy your smart contract to the Blockchain



When you created the Hardhat project, it also created some sample tests in the test folder

. In Hardhat, testing is done with Mocha and Chai, plus some extra plugins.

 To communicate with the Blockchain, we use the Ethers library. To run the test, you just have to run npx hardhat test.



When you created the Hardhat project, it also created a sample deployment script.

 To run this script, run the npx hardhat run scripts/sample-script.js command.




Conclusion



That’s it for this quick intro to Hardhat.

 Hardhat is an amazing tool, and if you are learning Web3 and Blockchain development, you should spend some time to explore all its features. 

For example, it has the ability to do console.log() in smart contracts.



And if you want a longer tutorial on Hardhat which covers most features, you can checkout this video on my channel.



I will see you there :)


1 Comment

  1. Majed Alshaikhi
    July 20, 2023

    Thanks a lot, Julian.
    I have a question, though.

    Do I have to install hardhat and run all the below scripts again, after I deploy my first smart contract ? I mean if I want to write another SC? Could You drop me a hint or so about what I need to do.

    - npm install --save-dev hardhat
    - npm install --save-dev @nomicfoundation/hardhat-chai-matchers

    thanks again

Leave a Reply

More great articles

Web3.js explained in 2 mins

If you want to interact with a smart contract on Ethereum,



 the API of Ethereum is complicated.

 To interact with…

Read Story

Introducing “Smart Contract 30”, a 30-day course on Solidity

Are you a beginner learning Solidity? Are you overwhelmed by the complexity of developing an Ethereum smart contract and don't…

Read Story

New videos on EatTheBlocks: Vue Dapp, Solidity Assembly, Oracle

Tip: If you don't want to miss any updates, checkout the updates page of EatTheBlocks Pro. Vue Dapps The Drizzle…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up