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 :
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
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
This file has 2 important parts:
abikey which is an interface used by frontend applications to interact with the smart contract
bytecodekey, 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
. 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.
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 :)
Leave a Reply