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. One of the initiatives trying to solve this is Hardhat. Hardhat is a development environment that can help ease the development process of Ethereum based dApps. So, if you are interested in learning more about Hardhat, then read on for the full breakdown of what Hardhat is and how you can easily use Hardhat with Moralis.
Web3 and blockchain development can be a tricky process, and it is helpful to utilize tools to make the process easier and more comfortable. This is precisely what both Moralis and Hardhat provide for their users. It helps with the development process and makes the life of the developers easier.
This development environment can help with everything from compiling, testing, deploying, and debugging Ethereum software. However, this is just the surface of what Hardhat is, so let us take a closer look at what Hardhat is and how it works. There are already existing Moralis YouTube videos if you want to learn more about Hardhat, such as connecting Hardhat to MetaMask, connecting Hardhat to Moralis, and Solidity writing to console (console.log). If you need a refresher regarding working with Solidity, check out our comprehensive Moralis guide explaining “What is Solidity?”.
What is Hardhat?
What is Hardhat? Hardhat is an environment developers use to test, compile, deploy and debug dApps based on the Ethereum blockchain. As such, it helps coders and developers to manage many of the tasks that are 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.
Hardhat comes with a pre-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 the 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 which a lot of the functionality originates from. This means that the developers can choose exactly which plugins they would like 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 that the developers end up using.
Hardhat Runner is the command-line interface (CLI) command used to interact with Hardhat. This is a task runner, and that provides the 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:
Running this command would start the built-in compile task. Furthermore, a task can call another task which means that it is possible to develop and define complex workflows. It is also possible for Hardhat users to override tasks that already exist, meaning that the workflows are extendable and customizable.
As was mentioned in the previous section, 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.
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 hard forks 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, 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 on in the article. Meanwhile, if you want to learn more about MetaMask, we recommend reading the following article for our full MetaMask breakdown.
Solidity Stack Traces
The network supports Solidity with a first-class standard. Therefore, the network knows the contracts that are running, what they intend to do, and if they fail, also provides a reason.
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 the 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.
Furthermore, you can find more information regarding the Hardhat Network in their documentation.
How to Install Hardhat
Installing Hardhat for Ethereum development is an easy task to manage, and it only takes a few moments. Hardhat is utilized through a local installation in a developer’s projects. One of the reasons for this is that the developer will reproduce the environment and avoid future version conflicts.
The first step in the installation process is to create an npm project. We can accomplish this by locating an empty folder, run the ”npm init” command, and follow the instructions. As we finalize a project and it is ready for development, the next step is to run the following command:
As it is installed, the way to use the local installation is through the ”npx” command. An example here would be ”npx hardhat”.
How to Start Using Hardhat
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.
As soon as we finalize the installation of the barebones version of the environment, it is possible to run tasks, compile code, run tests, use the Hardhat Network, and deploy contracts without any plugins installed.
To get a project going, we need to create a project by running ”npx hardhat” in the project folder. After we complete this task, we can create a sample project that we will use to explore how we get started with Hardhat. The sample project will ask to install ”hardhat-waffle” and ”hardhat-ethers”, enabling the project to run Waffle tests.
The first thing that we are going to look at is what tasks are available. 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.
The next helpful function that Hardhat provides for Ethereum development is the ability to compile code. For this, we can look at the ”contracts/” folder to explore the smart contracts of the project. In the sample project, we will find ”Greeter.sol”. To compile this, it is possible just to run the following command:
Previously we installed Waffle and Ethers.js to the sample project, which provides a few tests. However, it is also possible to use other tests from other libraries if desired. For the sample project, if we look at the ”test/” folder, we will find a sample test. It is possible to run this test by inputting the following command:
To deploy contracts once we finalize the tests, it is possible to do so through a script. Inside the ”scripts/” folder, if we are using the sample project, we will find a sample script: ”sample-script.js”. We can run the script by simply using the following command:
Connecting Hardhat to MetaMask
One of the great things about Moralis is all the different solutions integrated with the development platform. One example of this is how easy it is to connect Hardhat to Moralis. However, before connecting Hardhat to Moralis, we must first connect it to a wallet or a dApp. In this case, we will take a closer look at how to connect 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.
Moreover, on the Moralis YouTube channel, you will find a more detailed video explaining the process of connecting Hardhat to MetaMask.
Connecting Hardhat to Moralis
Once we have established a connection between Hardhat and MetaMask, we can start the process of connecting Hardhat to Moralis.
The first thing that needs to be done is to add a new server to Moralis and ensure that this server is set to a ”Local Dev Chain”. After this, we connect the new server to the local chain that we set up in the previous section when connecting Hardhat to MetaMask. To connect the server so that Moralis can know what happens on the local chain, we need to look at the server details.
Once we can view the details, there should be a ”Devchain Proxy Server”-tab available at the top right. Initially, the server status will be ”disconnected”, but we can connect the server to the local chain by following the steps presented in the tab.
If you complete these steps, you will establish a way for Moralis to communicate with the local node running on your computer.This means that if something happens on the local chain, Moralis will be able to know what happened.
However, we recommend that you watch the Moralis YouTube channel for the full breakdown of this process and an example of how the connection between Moralis and Hardhat works.
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 coders and developers can use to test, debug, compile and deploy contracts and dApps based on the Ethereum blockchain. This means that the development process can become simpler as these are practical tasks in developing smart contracts and dApps. Some examples of helpful tools here are automatic error messages and Solidity stack traces.
The installation of the Hardhat Ethereum development 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 the users with many tasks, the ability to deploy, compile, and test contracts.
Furthermore, connecting Hardhat to Moralis can be done in a few steps. The first thing is to connect Hardhat to MetaMask to enable the setup of a local chain. After this, following the instructions in the previous section will provide you with all the necessary information to establish the connection.
Suppose you want to learn more about how Hardhat is integrated into Moralis. In that case, we recommend that you check out the complete documentation and the videos explaining how to establish the connection. Moreover, sign up with Moralis for free to access great features and become a dApp developer today!