DEV Community

yosi
yosi

Posted on

Getting Started with Hardhat for Smart Contracts

In the world of Ethereum development, one tool stands out for its robustness and versatility: Hardhat. This tool is designed to help developers manage, test, and deploy smart contracts on Ethereum with ease. In this post, we'll dive into what Hardhat is, how it works, and how it uses RPC. Moreover, we'll walk you through the steps of compiling and interacting with smart contracts using Hardhat (on a test environment).

What is Hardhat?

Hardhat is a development environment and task runner for Ethereum that helps developers build and interact with smart contracts. It is designed to provide developers with a seamless experience in writing, testing, and deploying smart contracts. It comes with built-in TypeScript support, debugging, and networking capabilities, making it a one-stop-shop for Ethereum development.

How it Works?

Hardhat works by setting up a local Ethereum network on your machine for development purposes. This local network, called Hardhat Network, allows you to execute and test your smart contracts in a controlled environment. It provides detailed error messages and console.log functionality, which makes debugging a breeze.

Hardhat and RPC

The Hardhat Network communicates with your Ethereum node via an API called JSON-RPC (Remote Procedure Call). RPC allows a client to interact with a remote server. In the case of Hardhat, it connects to the Ethereum node (either a local one or a remote one) and sends requests to interact with the blockchain, like deploying a smart contract or querying a transaction. Hardhat uses the Ethereum JSON-RPC API, which is a set of methods used to interact with the Ethereum network.

Compiling and Interacting with Smart Contracts

Lets walk through an example of compiling and interacting with a smart contract using Hardhat.

Step 1: Install Hardhat

First, you need to install Hardhat. Make sure you have Node.js installed, then run the following command in your terminal:

npm install --save-dev hardhat
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a New Hardhat Project

Next, create a new Hardhat project by running the following command:

npx hardhat
Enter fullscreen mode Exit fullscreen mode

Choose "Create an empty hardhat.config.js" when prompted. This will create a new directory with the necessary configuration file (hardhat.config.js) in the root folder.

Step 3: Write The Smart Contract

Create a new file in the contracts directory (/contracts), say HelloWorld.sol, and add the following Solidity code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.3;

contract HelloWorld {
    function sayHello() public pure returns (string memory) {
        return "Hello, Hardhat!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Compile The Smart Contract

Compile the smart contract by running the following command:

npx hardhat compile
Enter fullscreen mode Exit fullscreen mode

Step 5: Write a Test

Create a new file in the test directory (/test), say HelloWorld.test.js, and add the following JS code:

const { expect } = require("chai");

describe("HelloWorld contract", function () {
  it("Should return the right greeting", async function () {
    const HelloWorld = await ethers.getContractFactory("HelloWorld");
    const helloWorld = await HelloWorld.deploy();
    await helloWorld.deployed();

    expect(await helloWorld.sayHello()).to.equal("Hello, Hardhat!");
  });
});
Enter fullscreen mode Exit fullscreen mode

Let's break it down:

const { expect } = require("chai");
Enter fullscreen mode Exit fullscreen mode

This line imports the expect function from the chai library, which is a popular JavaScript testing library. expect is used to make assertions about your code.

const HelloWorld = await ethers.getContractFactory("HelloWorld");
Enter fullscreen mode Exit fullscreen mode

This line creates a contract factory for the HelloWorld contract. A contract factory is an object that can be used to deploy new instances of a contract.

const helloWorld = await HelloWorld.deploy();
Enter fullscreen mode Exit fullscreen mode

This line deploys an instance of the HelloWorld contract.

await helloWorld.deployed();
Enter fullscreen mode Exit fullscreen mode

This line waits for the contract to be deployed. Contract deployment is an asynchronous operation that involves a transaction being mined on the Ethereum network.

expect(await helloWorld.sayHello()).to.equal("Hello, Hardhat!");
Enter fullscreen mode Exit fullscreen mode

This line calls the sayHello function of the HelloWorld contract and checks that it returns the string "Hello, Hardhat!".

Step 6: Run The Test

Run the test by executing the following command:

npx hardhat test
Enter fullscreen mode Exit fullscreen mode

If everything is set up correctly, you should see a message saying your test passed.

That's it! You have just written, compiled, deployed (to a test network) and tested a smart contract with Hardhat.

Summary

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate the recurring tasks involved in building smart contracts and performing complex deployments. 🚀

Top comments (0)