How to setup local Ethereum blockchain using hardhat for build and deploy smart contract

Hardhat is a development environment that compiles, deploys, and tests smart contracts on the Ethereum blockchain.

Hardhat provides tools to start a local Ethereum network and test ETH and accounts. It serves as a local server using Node.js and operates as a local blockchain server node.

Hardhat Local Ethereum Network Setup

The following steps are required:

Prerequisites

  • Install the Node.js environment.
  • Ensure that Node.js and npm are of valid versions.

First, create a directory using the terminal.

A:\work\blockchain>mkdir helloworld

Change to the directory

A:\work\blockchain>cd helloworld

Create a node application using default settings with the npm init -y command.

A:\work\blockchain\helloworld>npm init -y
Wrote to A:\work\blockchain\helloworld\package.json:

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Next, install the Hardhat npm dependency.

A:\work\blockchain\helloworld>npm install --save-dev hardhat
npm WARN deprecated [email protected]: Deprecated in favor of '@metamask/eth-sig-util'
npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment

added 335 packages, and audited 336 packages in 1m

61 packages are looking for funding
  run `npm fund` for details

9 moderate severity vulnerabilities

To address all issues, run:
  npm audit fix

Run `npm audit for details.

Run the npx hardhat command to create a Hardhat application. Select the Create a basic sample project option and choose all default options.

A:\work\blockchain\helloworld>npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v2.8.3

√ What do you want to do? · Create a basic sample project
√ Hardhat project root: · A:\work\blockchain\helloworld
√ Do you want to add a .gitignore? (Y/n) · y
√ Help us improve Hardhat with anonymous crash reports & basic usage data? (Y/n) · true

You need to install these dependencies to run the sample project:
  npm install --save-dev "hardhat@^2.8.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2

Project created
See the README.md file for some example tasks you can run.

Next, install the following dependencies:

  • hardhat-waffle: used to build and test smart contracts in a Hardhat environment.
  • ethereum-waffle: provides an API to interact with the Ethereum blockchain.
  • chai: a test library for JavaScript applications.
A:\work\blockchain\helloworld>npm install --save-dev "hardhat@^2.8.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.
npm WARN deprecated [email protected]: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated [email protected]: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/[email protected]: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/[email protected]: Please use @ensdomains/ens-contracts

added 1099 packages, and audited 1438 packages in 4m

119 packages are looking for funding
  run `npm fund` for details

56 vulnerabilities (8 low, 21 moderate, 27 high)

To address issues that do not require attention, run:
  npm audit fix

Some issues need review and may require choosing
a different dependency.

Run `npm audit` for details.

Generate Ethereum user addresses and test data in a fake Ethereum environment using npx hardhat accounts

A:\work\blockchain\helloworld>npx hardhat accounts
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
0x70997970C51812dc3A010C7d01b50e0d17dc79C8
0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
0x90F79bf6EB2c4f870365E785982E1f101E93b906
0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65
0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc
0x976EA74026E726554dB657fA54763abd0C3a0aa9
0x14dC79964da2C08b23698B3D3cc7Ca32193d9955
0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f
0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
0xBcd4042DE499D14e55001CcbB24a551F3b954096
0x71bE63f3384f5fb98995898A86B02Fb2426c5788
0xFABB0ac9d68B0B445fB7357272Ff202C5651694a
0x1CBd3b2770909D4e10f157cABC84C7264073C9Ec
0xdF3e18d64BC6A983f673Ab319CCaE4f1a57C7097
0xcd3B766CCDd6AE721141F452C550Ca635964ce71
0x2546BcD3c84621e976D8185a91A922aE77ECEc30
0xbDA5747bFD65F08deb54cb465eB87D40e51B197E
0xdD2FD4581271e230360230F9337D5c0430Bf44C0
0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199

Compile Solidity code by download & running npx hardhat compile command.

A:\work\blockchain\helloworld> npx hardhat compile
Downloading compiler 0.8.4
Compiling 2 files with 0.8.4
Solidity compilation finished successfully

Finally, deploy the default contract by running npx hardhat test:

A:\work\blockchain\helloworld>npx hardhat test


  Greeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
 Should return the new greeting once it's changed (1407ms)


  1 passing (1s)

This should successfully deploy the default contract.