How to setup local Ethereum blockchain using hardhat for build and deploy smart contract
- Admin
- Oct 3, 2023
- Blockchain
Hardhat is a development environment that provides compiles, deploys, and tests smart contracts in the Ethereum blockchain.
Hardhat provides to start local Ethereum network, test ETH and accounts
It is a local server with Nodejs and it is a local blockchain server node.
Hardhat local Ethereum network setup
The following are steps required
Prerequisite
Install nodejs environment Node and npm should give a valid version
First, create a directory using terminal
A:\work\blockchain>mkdir helloworld
Change to 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 hardhat npm dependency
A:\work\blockchain\helloworld>npm install --save-dev hardhat
npm WARN deprecated eth-sig-util@2.5.4: Deprecated in favor of '@metamask/eth-sig-util'
npm WARN deprecated debug@3.2.6: 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 npx hardhat command to create a hardhat application and select the Create a basic sample project
option and select 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 below dependencies
- hardhat-waffle uses to build test smart contracts in a hardhat environment
- Ethereum-waffle: API to interact with Ethereum blockchain
- chai: It is 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 ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/resolver@0.2.4: 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.
Next run, npx hardhat accounts
to generate Ethereum user address and it is test data in fake Ethereum environment.
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
Run npx hardhat compile
for download and compile solidity code
A:\work\blockchain\helloworld> npx hardhat compile
Downloading compiler 0.8.4
Compiling 2 files with 0.8.4
Solidity compilation finished successfully
Then, run npx hardhat test
command to deploy the default contract.
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)