# Develop with Hardhat

{% hint style="info" %}
Hardhat is a Node.js-based toolkit that makes Ethereum application development easier. It is known for being flexible and highly extensible.
{% endhint %}

## Requirements

### Node v22+

This tutorial requires Node version 22 or higher to be installed.

* Download [Node v22+](https://nodejs.org/en/download)

If you use `nvm` to manage Node versions, run `nvm install 22`.

***

## Set up development environment

Install [Hardhat](https://hardhat.org/) and create a project.

```bash
mkdir giwa-project
cd giwa-project

npx hardhat --init
npm install --save-dev @nomicfoundation/hardhat-verify
```

The generated project structure looks like the following.

```
giwa_project/
├── hardhat.config.ts # project configurations
├── contracts         # project source code directory
├── test              # contract test directory
├── ignition          # Hardhat Ignition deployment script
└── scripts           # scripts for deployment / simulation
```

***

## Connecting Hardhat to the GIWA chain

To deploy smart contracts to the GIWA chain, add GIWA chain to `hardhat.config.ts`.

```typescript
import type { HardhatUserConfig } from "hardhat/config";

import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import hardhatVerify from "@nomicfoundation/hardhat-verify";
import 'dotenv/config'

const config: HardhatUserConfig = {
  plugins: [hardhatToolboxViemPlugin, hardhatVerify],
  solidity: {
    profiles: {
      default: {
        version: "0.8.28",
      },
      production: {
        version: "0.8.28",
        settings: {
          optimizer: {
            enabled: true,
            runs: 200,
          },
        },
      },
    },
  },
  networks: {
    giwaSepolia: {
      type: "http",
      chainType: "op",
      url: "https://sepolia-rpc.giwa.io",
      accounts: [process.env.PRIVATE_KEY as string]
    },
  },
  chainDescriptors: {
    91342: {
      name: "Giwa Sepolia",
      blockExplorers: {
        blockscout: {
          name: "Giwa Sepolia Explorer",
          url: "https://sepolia-explorer.giwa.io",
          apiUrl: "https://sepolia-explorer.giwa.io/api",
        },
      },
    },
  },
};

export default config;
```

### Loading environment variables <a href="#loading-environment-variables" id="loading-environment-variables"></a>

The above configuration reads `PRIVATE_KEY` environment variable defined in `.env` file using [dotenv](https://www.npmjs.com/package/dotenv). NEVER hard-code private keys in source code.

To install  `dotenv`, run:

```
npm install --save dotenv
```

Once  `dotenv` is installed, create  `.env` file and fill it with like below.

```
PRIVATE_KEY=<YOUR_PRIVATE_KEY>
```

Replace `<YOUR_PRIVATE_KEY>` with your own wallet's private key to be used for deployment.

{% hint style="warning" %}
`PRIVATE_KEY` is the private key used to deploy contracts.
{% endhint %}

***

## Writing a contract

Let’s start with a simple contract. FIrst, delete `Counter.sol`, and `Counter.t.sol` in `contracts/` folder. Also delete `Counter.ts` and `ignition/modules/Counter.ts` in `test/` folder. Then write a new contract as follows:

{% code title="contracts/Giwa.sol" lineNumbers="true" fullWidth="false" %}

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

contract Giwa {
    event HelloGiwa();

    function helloGiwa() public {
        emit HelloGiwa();
    }
}
```

{% endcode %}

The smart contract code above is a contract named `Giwa`, which has a function called `helloGiwa` and an event named `HelloGiwa`.

To compile with Hardhat, run:

```bash
npx hardhat compile
```

### Writing tests

To make sure your contract works as intended, you’ll need to write test code. With Hardhat, you can write tests in Typescript/Javascript using [viem](https://viem.sh/), or write unit tests in Solidity. In this example, we’ll use viem.

{% code title="test/Giwa.ts" lineNumbers="true" %}

```typescript
import { describe, it } from "node:test";

import { network } from "hardhat";

describe("Giwa", async function () {
  const { viem } = await network.connect();

  it("Should emit the HelloGiwa event when calling the helloGiwa() function", async function () {
    const giwa = await viem.deployContract("Giwa");

    await viem.assertions.emit(
      giwa.write.helloGiwa(),
      giwa,
      "HelloGiwa"
    );
  });
});

```

{% endcode %}

To run the tests, execute:

```bash
npx hardhat test
```

***

## Deploying the contract

Once the smart contract compiles successfully, you can deploy it to the GIWA Sepolia testnet.

One way to deploy contracts with Hardhat is to use a tool called Hardhat Ignition. You can start by writing code as below:

{% code title="ignition/modules/Giwa.ts" lineNumbers="true" %}

```typescript
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("GiwaModule", (m) => {
  const giwa = m.contract("Giwa");

  m.call(giwa, "helloGiwa", []);

  return { giwa };
});

```

{% endcode %}

When ready, deploy to the GIWA Sepolia testnet with:

```
npx hardhat ignition deploy ignition/modules/Giwa.ts --network giwaSepolia
```

{% hint style="info" %}
Low on gas? [Claim Test ETH](/giwa-chain/en/get-started/faucets.md) here.
{% endhint %}

If you see output like the following, your smart contract is deployed on-chain and anyone can interact with it.

```
Hardhat Ignition 🚀

Resuming existing deployment from ./ignition/deployments/chain-91342

Deploying [ GiwaModule ]

Batch #1
  Executed GiwaModule#Giwa

Batch #2
  Executed GiwaModule#Giwa.helloGiwa

[ GiwaModule ] successfully deployed 🚀

Deployed Addresses

GiwaModule#Giwa - 0x<deployed-contract-address>

```

***

## Verify and Interact with the contract in the explorer

To interact with your contract through the block explorer, it must first be verified — either by you or someone else.

Take the deployed contract address from above and run:

```
npx hardhat verify --network giwaSepolia <deployed-contract-address>
```

If you see a response like the one below, you can visit the block explorer to view and interact with your verified code.

```
=== Blockscout ===

📤 Submitted source code for verification on Giwa Sepolia Explorer:

  contracts/Giwa.sol:Giwa
  Address: <deployed-contract-address>

⏳ Waiting for verification result...


✅ Contract verified successfully on Giwa Sepolia Explorer!

  contracts/Giwa.sol:Giwa
```

<div data-full-width="false"><figure><img src="/files/KLXXnCc9hc8ueunmLglu" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.giwa.io/giwa-chain/en/get-started/smart-contract/develop/hardhat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
