What is Hardhat?
Hardhat is a dev environment for Ethereum smart contracts that enables compiling, deploying, testing, and debugging. It has local testing, Solidity compilation, and easy contract deployment.
Previous Section Recap
Compiling smart contracts produces two important artifacts:
- the ABI representing the public interface of the contract
- the bytecode representing the program executable that actually lives on the blockchain
If you want to interact with a smart contract, two main pieces are needed: the ABI and the deployed contract's address.
For example, using the ethers.js Contract abstraction:
const myContract = new ethers.Contract(ADDRESS_OF_CONTRACT, ABI, SIGNER);
Intro
You might be wondering, what the heck is Hardhat!? We'll define that shortly, but the important concept to know is that we've made it to the most powerful Ethereum smart contract development tool. 🔨
We started at the low-level and have slowly moved our way up to the high-level. This is the flow the AU Ethereum Developer Bootcamp has taken:
- Blockchain and Cryptography Fundamentals
- Blockchain Account Tracking and Blockchain Storage
- Ethereum Introduction and Using JSON-RPC to Read, Signed JSON-RPC to Write, Ethers.js & Alchemy SDK for Higher Developer Abstraction
- Solidity Syntax, Smart Contracts, and Hardhat (we are here!!)
Hardhat is an extremely powerful tool for any developer in web3. Let's dive in... 🤿
What is Hardhat?
Hardhat is a development environment to compile, deploy, test, and debug Ethereum smart contracts.
Why Hardhat?
It helps developers manage and automate the recurring tasks that are inherent to the process of building smart contracts and dApps, as well as easily introducing more functionality around this workflow
Hardhat Features
Hardhat facilitates common development flows for smart contracts with the following features:
- Local testing, including local Hardhat Network (super useful!!)
- Solidity compilation and error-checking
- Flexible combination with other tooling/plugins (ie, Ethers.js)
- Easy deployment of and interaction with smart contracts
Hardhat Project Structure
When you initialize a Hardhat project in your local environment, the structure includes the following important files/folders:
/contracts
: All of your .sol files/scripts
: .js files for running scripts/artifacts
: artifacts produced out of the contract compilation/test
: .js files for using testing librarieshardhat.config.js
: file with project configurations (very important!!!!)
Important Hardhat Concepts
The hardhat.config.js
file is the most important file in your project!
Important
If you have a bug/issue, this is first file to look at! 👀
Also, when you compile a contract, its artifacts (the ABI and bytecode) will be stored in a newly created /artifacts
folder
AU Suggested Hardhat Flow
Want to get up and running with a Hardhat project? 🏇 Just follow this flow:
- Open a terminal
- Run
cd Desktop
, then create a new folder viamkdir au-hardhat-practice
, then move into that newly-created folder by runningcd au-hardhat-practice
- Once you are in the
au-hardhat-practice
folder, in your terminal runnpm init -y
to initialize apackage.json
- Run
npm i hardhat
- Run
npm i dotenv
- Run
touch .env
in order to create a.env
file at the root level of your project, then populate it with important data and save - Run
npx hardhat
which will initialize a brand new Hardhat project - We recommend the following flow: Choose the current root >
YES
to the.gitignore
>YES
to install the sample project's dependencies - Add
require(‘dotenv’).config()
at the top of yourhardhat.config.js
file - Add
networks
flag tohardhat.config.js
, add your Alchemy RPC URL underurl
and your testnet private key underaccounts
- Set up your scripts and contracts, then deploy in a flash! ⚡️
Conclusion
Hardhat is one of the ultimate web3 developer tools. It is specifically built to cover the entire smart contract developer flow end-to-end. Master it, and thou shalt become a Web3 Master. 🧠
Learn More About Hardhat
Alchemy University offers free web3 development bootcamps that explain Hardhat in-depth and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!
Updated over 1 year ago