What is Hardhat?
A whimsical introduction to a handy tool to help with your blockchain development adventure
So, It's been a tough week and one with mini-miracles sprinkled in. This week I wrote some more smart contracts and cried a little. In all my travails, Hardhat appears to be the friend that's kept me sane and rocky. If you've dipped your foot recently into the roaring sea of web3, chances are you've seen or heard about Hardhat.
If you have not, don't worry, I'd be gentle.
In this article, I will introduce you to hardhat and we will look at tools providee by hardhat to author and deploy smartcontracts.
Hardhat, Hardhat, Hardhat
The first question that seems proper to attend to is:
what the goobies is hardhat?
Hardhat is one of the few blockchain dev tooling out there championing the fight for your sanity while writing your beautiful soup of code for the blockchain.
Let's explore a slightly more boring version.
Hardhat is an environment developers use to test, compile, deploy and debug dApps based on the Ethereum blockchain.
Think of hardhat as a minion that was created to make your life easy while writing code for the blockchain. So this minion comes packed with even tinier minions - referring here to the sweet plugins, that can be used to extend the super capabilities of hardhat.
Also, it includes a command-line interface (CLI, if you don't know what this means, I'm working on something to help), a bunch of commands, which are task-driven, to create a smooth experience while in the development phase of your "disruptive" blockchain project.
The CLI
If you were tracking along, I mentioned something about the CLI above, yeah?
It is called, the Hardhat Runner.
With the hardhat runner, you suddenly get powers to talk to the Hardhat minion. You take control ( I know you want this). You take charge. You can now give hardhat instructions and tell it to perform tasks.
A Big part of hardhat runs on Tasks. Think of tasks like a bunch of code written to perform a certain intent.
For example, after writing our solidity contract, we need to compile it. Compilation is a fancy word meaning we need to do something to the code we've written to make sure the computer ( in this case, the EVM) understands. Remember your PC doesn't understand English(not yet)
So anyhoo, we need to compile your code, Hardhat, by its infinite mercies has a task set out to do nothing else but compile code. Here it is.
npx hardhat compile
so this instruction runs the compile task. This Task can run other tasks and create a complex workflow of tasks.
Hardhat Comes bearing a Network
Now, this is interesting. Hardhat provides a local Ethereum network that allows for Hardhat Ethereum development. This will enable you, the developer, to test, run, debug, and deploy the code and their written contracts.
The keyword here is Local, which means it is all on your machine. No need to connect to the main/test network to deploy your code. shweet right?
And that's just the surface. Since you now have a local network that is highly customizable, hardhat has also gone out of their way to provide you with meaningful error messages and remind you of all the various reasons why your code suck.
The network has the ability to always know what transactions or tasks fail and the reason behind their failure, it then uses this information to make the debugging process a lot easier.
When a transaction fails without a known reason, the network will offer the users a clear message explaining the error. Here is an example of a case where the network will knock you with an error message to help with the debugging process:
Going forward, Go to Hardhat and read more about their Network in their documentation.
At this point, you might wanna send out a tweet, claiming you are a hardhat expert.
Go ahead. I won't stop you.
Installation
To install hardhat, you need to:
- Create an npm project. You do that by running
npm init
in an empty directory - Now you can run
npm install --save-dev hardhat
Usage
To create a hardhat project, run
npx hardhat
If you had a smooth installation, you should see this
Yours might have a dash of blue, don't panic, you are safe.
Go ahead and choose what kind of project structure you prefer. Hardhat is waiting.
After you selected your project type, you should see a project scaffold looking like this one in the directory in which you ran the code.
Pat yourself at the back now. You have just initialized a hardhat project.
Let's talk about tasks
Hardhat is all about tasks. There is a bunch of pre-bundled tasks that you can use right away.
Run npx hardhat
again to see a list of tasks available to you.
So there you have it. A whimsical introduction to hardhat.
If you've got any questions, I'd happily answer them in the comment section below.
Anyhoo, Go out there and conquer, friend
-- Koha