[BLACK FRIDAY SALE - 70% OFF ON LIFETIME PASS - ENDS TODAY](https://eattheblocks-pro.teachable.com/p/lifetime-all-access-pass)
[BLACK FRIDAY SALE - 70% OFF ON LIFETIME PASS - ENDS TODAY](https://eattheblocks-pro.teachable.com/p/lifetime-all-access-pass)

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

New Ebook To Learn How to Build Ethereum Dapps

I have just turned the tutorial series on how to build a todo list Dapp into an 62 pages ebook.…

Read Story

Metamask explained in 2 mins

Metamask is the most popular wallet in crypto

But when you are a beginner, Metamask is NOT easy.

 In this article…

Read Story

Walkthrough the new UI of Remix

TIP: If you have never used Remix, check out my intro to Remix on Youtube. Remix, the online IDE for…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.

    Only great content, we don’t share your email with third parties.

    Arrow-up