Develop with Hardhat

In this tutorial, you’ll go through the full process of developing, testing, deploying, and verifying smart contracts on the GIWA chain.

circle-info

Hardhat is a Node.js-based toolkit that makes Ethereum application development easier. It is known for being flexible and highly extensible.

Requirements

Node v22+

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

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


Set up development environment

Install Hardhatarrow-up-right and create a project.

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.

Loading environment variables

The above configuration reads PRIVATE_KEY environment variable defined in .env file using dotenvarrow-up-right. NEVER hard-code private keys in source code.

To install dotenv, run:

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

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

circle-exclamation

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:

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:

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 viemarrow-up-right, or write unit tests in Solidity. In this example, we’ll use viem.

To run the tests, execute:


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:

When ready, deploy to the GIWA Sepolia testnet with:

circle-info

Low on gas? Claim Test ETH here.

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


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:

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

Last updated