Setup and Overview
An overview of hardhat, a smart contract development framework
Setup and Overview
In this article, you’ll learn about Hardhat: a development framework to create, test, and deploy smart contracts to Ethereum and other supported EVM chains.
Objectives
By the end of this lesson, you should be able to:
- Install and create a new Hardhat project with Typescript support
- Describe the organization and folder structure of a Hardhat project
- List the use and properties of hardhat.config.ts
Overview
Hardhat is a development environment that allows you to develop and test Solidity on your local machine. It includes debugging and unit testing tools, and has an ecosystem of third-party-developed plugins that ease development and deployment.
Among other things, these plugins can help you deploy contracts, see the size of your compiled byte-code, and even see unit test coverage.
Installing Hardhat and creating a new project
As a pre-requisite to start developing smart contracts with Hardhat, Node.js must be installed.
You can then simply type npx hardhat init
, which provides a set of options to bootstrap a Hardhat project:
You are encouraged to select Create a TypeScript project, since it provides you with some benefits such as static typing that can reduce the number of errors during development.
You can then enter ‘yes’ for the remaining options, which include installing the @nomicfoundation/hardhat-toolbox
package that contains some of the most used Hardhat plugins.
Anatomy of a Hardhat project
After you complete the previous step, the folder structure looks like the following:
- contracts # contracts will go here
- hardhat.config.ts # configuration file for hardhat
- node_modules # node.js package folder
- package-lock.json # node.js package lock file
- package.json # node.js package file
- scripts # place the scripts here
- test # place the tests here
- tsconfig.json # typescript configuration file
It is also common to save hardhat tasks in a task
folder.
It is important to mention that all these paths are fully configurable in the hardhat.config.ts
file. You can specify a different folder for the contracts, such as src
.
Configuration
You can configure the Hardhat environment in the hardhat.config.ts
file.
Since the project uses Typescript, you have the benefit of using static typing.
The following is the default configuration:
You can configure aspects such as:
- default network
- networks
- solidity
- paths
- mocha
For example:
Compiling smart contracts
At this point, you should have a Hardhat project up and running to start developing smart contracts. You may notice Hardhat includes a sample contract called Lock.sol
.
To run your first command, enter npx hardhat compile
, which compiles the smart contracts and generates the correct artifacts that includes the bytecode and ABI.
After running the npx hardhat compile
command, you should see a new folder named artifacts. This folder contains each contract name as a folder and a
{ContractName}.json
file.