In this tutorial, you'll learn how to use Hardhat to compile, test, and
deploy smart contracts. Before getting started, ensure that you have
Node.js installed on your local machine, along with a
code editor like VSCode. To have a better performance, we also use
pnpm for package management. However, a faster alternative
like bun
is also welcome.
Note: If you are on
Windows
, you may want to useWSL
orGit Bash
to run the commands.
Create a new folder
bash mkdir hardhat-tutorial
Change dir to hardhat-tutorial
cd hardhat-tutorial
Initialize a new Node.js project with a package.json
file in it
pnpm init
package.json
file is the entry point for the project. It contains the
project's metadata and dependencies. And any folder contains a
package.json
file is considered a Node.js project.
package.json
file, you will see the following content:name
: The name of the project.scripts
: A set of scripts that can be run using pnpm run {script - name}
In your termainal, run the following command to install Hardhat
pnpm add @nomicfoundation/hardhat-toolbox hardhat @nomicfoundation/hardhat-chai-matchers
@nomicfoundation/hardhat-ethers @nomicfoundation/hardhat-ignition @openzeppelin/contracts dayjs dotenv ethers
solidity-coverage chai@4.4.1 @types/chai
After the installation, you will see a node_modules
folder in your project.
Please add this folder to your .gitignore
file. Do not commit this folder to your repository.
And now you will see the following content added to your package.json
file's dependencies
field
Add test, coverage and compile scripts to your package.json
file.
test
: Run testscoverage
: Run tests with coveragecompile
: Compile contracts
Create a new file named hardhat.config.ts
in the root of your project.
This file is the configuration file for Hardhat.
Add the following code to the hardhat.config.ts
file
dotenv
is use for loading the private key into the system.
You can check this
link
to know how to export your private key from Metamask. DO NOT put your private key in your code and commit it
to the repository!!!!
Create a .env
file in the root of your project and add the following content
Put your private key in the .env
file
Create a file called tsconfig.json
in the root of your project
The tsconfig.json
file is the configuration file for TypeScript. Typescript is a superset of JavaScript that
adds static typing to the language. It is used to compile the TypeScript code to JavaScript.
Add the following content to the tsconfig.json
file
Create a folder named contracts
in the root of your project and create a file named HelloWorld.sol
in it.
Add the following content to the HelloWorld.sol
file
Run the following command to compile the contract
pnpm run compile
Now you have successfully set up a Hardhat project. Next, we will test the contract.
Testing is an essential part of the development process. It helps to ensure that the code works as expected.
Create a folder named test
in the root of your project and create a file named HelloWorld.spec.ts
in it.
Add a describe block to the HelloWorld.spec.ts
file
A describe block organizes related tests under a common description, making it easier to understand the specific functionality or behavior being tested. This structure is particularly useful for grouping tests that share similar purposes or behaviors.
Add a deploy function to the HelloWorld.spec.ts
file
The deploy function is used to deploy the contract to the network. The contract will be deployed to the hardhat testing network, so you don't need to worry about the gas fee.
Note, this HelloWorld
is the same as the contract name in the HelloWorld.sol
file.
Add a test function to the HelloWorld.spec.ts
file
The test function is used to test the contract. It checks if the contract works as expected.
Note, this helloWorld
is the same as the contract's function name in the HelloWorld.sol
file.
You can simply expect some results from the contract and check if the results are correct.
You can run the test by running the following command
pnpm run test
Congratulations!
You have successfully set up a Hardhat project. In the next section, you will learn how to connect your sma rt contract with the frontend.