How to Setup Solidity Environment with Hardhat
Tags: blockchain, typescript, web3, softwareengineering
Published: 2023-05-17 11:52:18

### Table Of Contents
        * [Chapter 1](Introduction)
        * [Chapter 2](Hardhat setup with Typescript)
        * [Chapter 3](Code sample with Hardhat)
        * [Chapter 4](Conclusion)

### Chapter 1: Introduction

Tooling around EVM based blockchain has improved drastically over the years, thanks to the unrelenting efforts of creators of tools like ganache, brownie, Hardhat etc.

In this tutorial, our main focused will be on hardhat, due to the large pool of Javascript developers and improving on the cons of previous tools, it has become the go-to tools for Ethereum Smart Contract Development.

Hardhat offers various features, including compiling, debugging, testing, and more. It can also be used for other EVM based development such as Binance Smart Chain,Polygon, Avalanche etc. This article will provide step-by-step guide on how to set up Hardhat with TypeScript, which can help you get started with building decentralized applications on the Ethereum Network.

### Chapter 2: Hardhat Setup

#### Prerequisite
>*Node*
>*Npm/Yarn*
>*Command Line Interface (CLI)*
>*Code editor (e.g., VS Code)*

There are different ways to set up Hardhat on your PC, and you can choose to install it globally. However, for this tutorial, we will install it locally, allowing us to have a version of Hardhat specific to this project that can be reused in different repositories or projects. In this tutorial, I will be using Yarn and the VS Code editor, but feel free to use any tools that suit you.

##### Steps to set up Hardhat locally:

1. Create a new folder on your PC.
2. Open your command line interface (CLI).
3. Navigate to the folder you just created.
4. Initialize the project using Yarn. In this tutorial, I will be using Yarn version 2. You start by typing the code below in your terminal.

###### Code

```Js
  yarn init -2
```

5.Install the required node modules.
   If you're using Yarn version 2, you need to perform an additional configuration of setting up the node modules. If you were using npm, this step is not necessary because npm uses node modules by default. Use the following code to configure Yarn:

```Js
   yarn config set nodeLinker node-modules
```

6.Install Hardhat as a development dependency.
   Adding Hardhat as a dependency will populate the package.json file with all the necessary information for Hardhat. It will also install some packages in the project environment.

```Js
   yarn add hardhat --dev
```

After successfully installing Hardhat, you will find it listed in your package.json file.

###### screenshot of actions 4, 5, and 6 executed in the command prompt (CMD)
![Image1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8t3hgavxx8en3ktm9x0.PNG)

7.Initialize Hardhat.
   After installing Hardhat, the next step is to initialize it using the following code. The use of Yarn in the command is necessary because we installed it locally in this project, not globally on the machine.

```Js
   yarn hardhat init
```

During the initialization process, I will select TypeScript as an option. However, you may choose JavaScript as there isn't much difference for this guide.

![Image2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf64w1xdsenq5lr6llx5.PNG)
Additionally, while installing, Hardhat will suggest some dependencies for you to install. It's important to install them as they are required for the project. You can copy the entire line of code shown in your command line interface and paste it into your terminal. This will add all the necessary dependencies to your local project environment.

![Image3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92gpyecxj7xe15cdqj81.PNG)
8.Compiling the Code
   To compile your Hardhat code, simply run the following command:

```Js
  yarn hardhat compile
```

After successful compilation, you will see the following output in your terminal:

![Image4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8jgu5vzl3poskfjykszz.PNG)
Once compilation is complete, you should see the following directory structure in your code editor:

contracts: contains _lock.sol_,
scripts contain _deploy.ts_,
tests contains _lock.ts_, and
artifacts contains the ABI (Application Binary Interface) for your contracts.

![Image5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gru1hj0a6kqhc98s6gop.PNG)
### Chapter 3: Code Sample with Hardhat

Now that we have successfully installed Hardhat, let's proceed to create a new contract named Hello.sol, which will execute a basic "Hello, World!" program.

#### Solidity Code

```Js
 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld {
    string public message;

    constructor() {
        message = "Hello, World!";
    }

    function getMessage() public view returns (string memory) {
        return message;
    }
}

```

#### Typescript code

Here, I have created a _result.ts_ script file for deploying our contract, as demonstrated above.

_note: We're deploying locally only available on hardhat_

```Js
  import { ethers } from "hardhat";

async function main() {

 const HelloWorld = await ethers.getContractFactory("HelloWorld");
 const helloWorld = await HelloWorld.deploy();

const resp = await helloWorld.getMessage();
console.log('Get message response', resp)
}

main()
 .then(() => process.exit(0))
 .catch((error) => {
   console.error(error);
   process.exit(1);
 });

```

To get the output of the code, run the following commands. The first command is used to compile the contract, and the second command is used to run the script with the title

```Js
yarn hardhat compile
yarn hardhat run scripts/result.ts
```

##### screenshot of the code directory showing Hello.sol & result.ts

![Image6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1duv23xb0fvt35dsqx8.PNG)

##### Output
After deploying our contract, we can see the "Hello, World!" message displayed in the terminal.

![Image7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gp3b1icd3979428s6y6.PNG)
For future Hardhat projects, you can use the project we created here as a starter file to avoid starting from scratch. Simply copy and paste the files into a new folder, excluding the node_modules folder to save copy time. Be sure to delete the existing files in the contracts, scripts, and tests folders before starting your new project.

### Conclusion

In conclusion, By following this step-by-step instructions and harnessing the powerful features of Hardhat and TypeScript, developers can ensure a fantastic experience in Ethereum smart contract development. Enjoy coding and have a great time building!