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 :)


0 Comments

Leave a Reply

More great articles

Solidity explained in 2 mins

Solidity is a language for creating Blockchain smart contracts.

You can use it to create smart contracts, on more than 200…

Read Story

The 3 most important smart contracts in DeFi

It’s so exhausting to keep up with all these new DeFi projects. 

But very few DeFi projects are really original.…

Read Story

How to manipulate strings in Solidity?

Manipulating strings in Solidity is notoriously difficult. This is surprising for most developers who are used to manipulating easily in…

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