Skip to content

Hardhat Basics (Blockchain Used in This Site)

This chapter explains Hardhat, the blockchain development environment used in this site.
It is not for production; it is an environment for running and verifying smart contracts locally.

General Explanation

What is Hardhat?

Hardhat is a development environment for Ethereum smart contracts [1]. The course repository uses Hardhat 2.

In this site, it is used as a local development blockchain for safe and reproducible exercises.

What You Can Do

The main operations are as follows [2].

  • start local chain (npx hardhat node)
  • deploy contracts (npx hardhat run ... --network localhost)
  • run tests (npx hardhat test)

Why It Is Good for Learning

A learning environment needs failures that are easy to redo and steps that can be reproduced any number of times.
Hardhat handles this well, so even those new to blockchain can experiment freely.

  • you can experiment locally on one machine
  • test accounts and balances are provided by default
  • deploy/call/observe cycles are short and repeatable

Minimal Flow

sequenceDiagram
  participant Dev as Developer
  participant HH as Hardhat Node
  participant SC as Smart Contract
  Dev->>HH: start local node
  Dev->>SC: deploy script
  Dev->>SC: call contract functions
  SC-->>Dev: tx receipts / events

Position in This System

Role in IW3IP

  • learning phase: use Hardhat to understand the flow of contract execution
  • production review: move on to selecting public/permissioned chains and designing their operation

What You Actually Touch in This Site

Rather than studying Hardhat itself in depth, this site uses it as the experiment platform for IW3IP.

  • npx hardhat node: starts local blockchain
  • deploy scripts: place contracts on the local network
  • MetaMask: connects to the local chain and sends transactions from the UI

The figure below shows how these parts connect.

flowchart LR
  DEP["Deploy script<br>npx hardhat run ... (network: localhost)"]
  subgraph NODE["Local chain: npx hardhat node<br>http://127.0.0.1:8545 (chain ID 31337)"]
    ACC["Test accounts<br>Account #35;0, #35;1, #35;2, ...<br>(10000 ETH each)"]
    SC["Contracts<br>(IoTMarket, Merchandise, etc.)"]
  end
  MM["MetaMask<br>(browser)"]
  DEP -->|"1. compiles and deploys"| SC
  ACC -.->|"2. import the Account #35;2 private key"| MM
  MM -->|"3. sends transactions"| SC
  SC -->|"4. transaction result"| MM

Both the deploy script and MetaMask connect to the same node at 127.0.0.1:8545. When you restart the node, the chain is emptied, so you need to deploy the contracts again.

Typical Issues

  • stale MetaMask network state -> reset/sync accounts and network
  • deploy script failures -> restart local node and redeploy
  • port conflicts -> check 8545 usage

Difference from Production

Keep this difference in mind so you do not confuse a learning-oriented local chain with a production network.

  • Hardhat is mainly for development and education
  • production use requires separate consideration for network operations, gas costs, incident handling, and key management

In the short learning path of the Learning Roadmap, the next page is SSI / DID / VC Basics.

References

[1] Nomic Foundation, "Hardhat 2 Documentation," https://v2.hardhat.org/docs

[2] Nomic Foundation, "Getting started with Hardhat (Hardhat 2)," https://v2.hardhat.org/hardhat-runner/docs/getting-started