June 24, 2021

Hardhat Explained – What is Hardhat?

Table of Contents

Hardhat is a development environment that can help ease the development process of Ethereum-based Web3 applications. It can help with everything from compiling, testing, deploying, and debugging Ethereum software. Also, you can verify a smart contract with Hardhat, which is highly beneficial. 

However, all of this is just the gist of it. If you’re interested in learning more about the ins and outs of this development environment, read on for a full breakdown of what Hardhat is! We’re also going to look at how you can use Hardhat in this article. For example, if you’d like to deploy smart contracts, then make sure to watch the following video:

Overview

The Web3 industry is beginning to take off, and new, decentralized applications (dapps) are popping up frequently. Arguably the most well-known leading development platform for dapps is still Ethereum. What’s more, as more and more developers start looking into creating dapps, there’s a growing need for a more straightforward development process. This is where Hardhat enters the picture. It helps with the Web3 development process and makes the lives of blockchain devs easier.

As mentioned, this development environment can help with everything from compiling, testing, deploying, verifying, and debugging Ethereum software. However, this is just the surface of what Hardhat entails, so let us take a closer look at what Hardhat is and how it works!

What is Hardhat?

Hardhat is an environment developers use to test, compile, deploy, and debug dapps (Web3 applications) based on the Ethereum blockchain. It helps developers to manage many of the tasks inherent to developing dapps and smart contracts. Along with providing developers with the proper tools to manage this process, Hardhat also helps automate some of these steps and provides developers with new, helpful functions. 

Title - What is Hardhat?

Hardhat comes with an already-built local Ethereum network designed with development at its core. This network focuses on Solidity debugging and features stack traces, messages when transactions of the dapps fail, etc. This provides developers with essential tools to understand where an application fails and provides them with the answer to solve them. The environment is characterized by plugins from which a lot of the functionality originates. This means that the developers can choose which plugins they want to include in their development process. However, it comes with built-in defaults, but they can all be overridden. This means that the Hardhat software does not care which tools developers end up using. 

Hardhat Runner

Hardhat Runner is the command-line interface (CLI) command used to interact with Hardhat. This is a task runner, providing users with many different options and alternatives. The design of the task runner revolves around plugins and tasks. This further means that every time a developer runs Hardhat from the CLI, they run a task. An example here would be: 

npx hardhat compile

Running this command would start the built-in compile task. A task can call another task, meaning developing and defining complex workflows is possible. It is also possible for Hardhat users to override tasks that already exist. As such, the workflows are extendable and customizable. 

Exploring the Hardhat Network

The development environment comes with a pre-built network. This is a local Ethereum network that allows for Hardhat Ethereum development. This will enable developers to test, run, debug, and deploy the code and their written contracts. 

Title - Hardhat Network and Ethereum

How Does the Network Function? 

The network mines a block with every transaction and without any delay. Along with this, the network is backed by an EMV implementation, which is the same implementation utilized by other applications such as Remix, Ethereum Studio, and Ganache. Furthermore, the network also supports various hardforks, such as Byzantium, Constantinople, and Petersburg. 

How Can the Network Be Utilized?

The network has some default behaviors. One example is that Hardhat always starts an instance if defaultNetwork is set to hardhat or the network is empty. Furthermore, we can use the network to run tests, tasks, and scripts. 

It is also possible to add plugins to the Hardhat Ethereum network. Some of the usable plugins are ethers.js, Waffle, and Truffle, among other things, and they can connect directly to the provider. It is also possible for external clients to connect to the network. An example of an external client is MetaMask. However, we will discover how Hardhat can connect to MetaMask later in this article. Meanwhile, if you want to learn more about MetaMask, we recommend checking out our full MetaMask breakdown.  

Solidity Stack Traces

The network supports Solidity with a first-class standard, and it knows the contracts that are running and what they intend to do. Also, if they fail, it provides a reason.

Solidity Stack Traces Hardhat

If a call or a transaction were to fail, the network would provide the users with an exception. This exception will give a combined Solidity and JavaScript stack trace. This means that the stack trace starts in JavaScript up to the call of a contract and then follows up with a full Solidity call stack. This means that the stack traces the network provides will give the developer a reason why the contract or transaction failed.

Automatic Error Messages

As the network has the ability to always know what transactions or calls fail and why they are unsuccessful, it utilizes this information to make the debugging process a lot easier. 

When a transaction fails without a known reason, the network will provide users with a clear message explaining the error. Here are some examples of cases where the network will display an error message to help with the debugging process:

  • When someone sends ETH to a contract without a receive function or a payable fallback.
  • Calling a function without the right parameters.
  • Calling an external function on a non-contract account.
  • Trying to send an insufficient amount of ETH.
  • Calling a precompiled contract incorrectly. 
  • Trying to deploy contracts on the network that exceeds the bitcode size limit. 

You can find more information regarding the Hardhat network in their documentation

How to Use Hardhat

In the following sections, we’re going to perform a Hardhat tutorial. As we do so, we’ll look at how to install and use Hardhat. We’ll look at tasks and how to compile, test, deploy, and verify contracts. Last but not least, we’ll look at how to connect Hardhat with MetaMask!

Prerequisites

The prerequisites to using Hardhat involve installing Hardhat and initiating a Hardhat project. That said, Hardhat for Ethereum development is easy; it only takes a few moments. Hardhat is utilized through a local installation in a project. One of the reasons for this is that the developer will reproduce the environment and avoid future version conflicts. Now, let’s start with the first prerequisite!

Install Hardhat

The first step in the installation process is to start an npm project. Then, locate an empty folder, run the npm init command, and follow the instructions. As we finalize the project and it is ready for development, the next step is to run the following command:  

npm install --save-dev hardhat

Once installed, the way to use the local installation is through the npx command. Let’s look at that further!

Create a Project

In this section, we will explore the basics of how to get a project going. When exploring what is possible with Hardhat and how we do things, we will use a sample project as our basis. To get a project going, we need to create it by running the following command in the project folder:

npx hardhat

The sample project will ask to install hardhat-waffle and hardhat-ethers, enabling the project to run Waffle tests. 

Tasks

Running the npx hardhat command will provide us with a list of all available tasks. As we are using a barebones version of Hardhat without any plugins, only a few limited tasks are built-in. However, as we add plugins, more tasks will appear in this list. 

An example of a task in the sample project is the accounts task. To run this task, simply input npx hardhat accounts, and the outcome will be a list of all the accounts connected to the project.

Compile

The next helpful function that Hardhat provides for Ethereum development is the ability to compile code. If you’d like to compile a smart contract inside one of your project folders, run the following command: 

npx hardhat compile

Testing Contracts

It is also possible to run tests. For example, it is possible to run a test by inputting the following command: 

npx hardhat test

Deploying Contracts

It is possible to deploy contracts once a test is finalized, and you can do so through a script. For example, let’s say you have a ”scripts/” folder and a sample script called sample-script.js. We can run the script by simply using the following command:

npx hardhat run scripts/samle-scripts.js

Verify Contracts

Verifying contracts is essential since defective ones can cause major issues. There are multiple steps involved to verify a contract; however, in short, you can use the following Hardhat verification command:

npx hardhat verify “ADDRESS” --network goerli

If you’d like a more in-depth step-by-step explanation of this, make sure to check out our tutorial on how to verify a smart contract with Hardhat!

Connecting Hardhat to MetaMask

The first thing you need to do to connect Hardhat to MetaMask is to follow the steps above on how to install the development environment. 

Once the installation process is complete, the next step is to run a node with the following command: npx hardhat node. By doing this, you will create a local Ethereum node with some additions, such as the ability to utilize console.log()

Once the command has been run, you will be provided with an address right below the inputted command. Copy this address and move on to MetaMask. You can use the drop-down menu and select ”Custom RPC”, and use this address when creating the network. 

As we finish creating the network, the account will have 0 ETH. We can move back to the console and see that several different accounts have been created. We can take the private key from any of them, import an account, and see that the account will have been provided with some ETH. However, this is just ETH on the local network which means that they are not real ETH. But it is now possible to use these ETH to test contracts and so on. 

MetaMask and Moralis

Web3 authentication is the starting point of all Web3 applications, and MetaMask is the most popular alternative. With Moralis, you can easily add blockchain-based authentication to your dapps. If this sounds interesting and you’d like to learn how to integrate backend Web3 authentication, make sure to check out our tutorial on how to connect MetaMask to a website with NextJS! However, before you can start utilizing Moralis, make sure to create a free account. With an active account, you can access all of the enterprise-grade Web3 APIs from Moralis and enjoy a more seamless developer experience!

Hardhat and MetaMask - Scale with Moralis

Hardhat Explained – Summary

As the demand for dapps increases, more and more people will be interested in developing decentralized applications. The development of tools to ease blockchain development has led to several different solutions popping up, one of these tools being Hardhat. Hardhat is a development environment that developers can use to test, debug, compile, deploy, and verify contracts and dapps based on the Ethereum blockchain. 

The installation of the Hardhat environment can be done in just a few minutes, and getting started with a Hardhat project is a simple task. Just the base version of the development environment without plugins provides users with many tasks and the ability to deploy, compile, and test contracts. 

If you enjoyed this article answering the “what is Hardhat?” question, we highly recommend checking out other informative content here on our Web3 blog. For example, you can learn about leading zk-rollup projects, how to build a cryptocurrency portfolio dashboard, explore Web3 marketplace development, and how to get into DeFi blockchain development! In addition, if you’re interested in learning how to find tokens before they pump, make sure to check out our article, teaching you how to find new crypto coins early before they take off! Those articles can be found on the Moralis Academy blog, and Moralis Academy is also the best place to get blockchain certified!

Market Data API
Build amazing trading and portfolio dapps with this Market Data API. Keep users engaged with up-to-date data!
Market Data API
Related Articles
January 22, 2024

Full Guide – The Ethereum Ecosystem in 2024

May 7, 2024

Build Dapps on Moonbeam Network with Moonbeam APIs

February 28, 2023

Arbitrum Testnet – How to Build and Launch Arbitrum Projects

May 29, 2024

Base Nodes – How to Run an RPC Node on Base

September 12, 2023

How to Get All NFT Tokens Owned by a User Address

August 23, 2022

How to Use Filecoin in Unity for Storage

October 21, 2023

Full Example Walkthrough for the eth_call RPC Method

December 16, 2022

How to Call a Smart Contract Function from JavaScript

🔥 LIMITED-TIME OFFER - Get early access to an Enhanced Token Page & boost your token project’s exposure! GET EARLY ACCESS 🔥