Smart Contract Development Lab: Hardhat (Step by
Step)
A. Create a new project folder
Create a folder for the Hardhat project and move into it.
mkdir hardhat-project
cd hardhat-project
B. Initialize an npm project
Initialises an npm project and creates a [Link] file.
npm init -y
C. Install Hardhat (version 2)
Installs Hardhat locally in the project. We avoid global installs.
npm install --save-dev hardhat@2.22.0
npx hardhat --version
D. Initialize the Hardhat project
Creates the project structure, sample contract, and configuration file.
npx hardhat
E. Compile the smart contracts
Compiles all Solidity files in the contracts folder.
npx hardhat compile
F. Force recompilation (if needed)
Deletes previous build files and recompiles everything.
npx hardhat clean
npx hardhat compile
G. Start a local blockchain
Starts a local Ethereum network with pre-funded test accounts.
npx hardhat node
H. Open the Hardhat console (new terminal)
Opens an interactive console connected to the local blockchain.
npx hardhat console --network localhost
I. Get a test account (Hardhat console)
Retrieves the first test account to deploy and send transactions.
const [owner] = await [Link]()
J. Set an unlock time
Creates a timestamp 60 seconds in the future.
const unlockTime = [Link]([Link]() / 1000) + 60
K. Create contract factory
Loads the compiled Lock contract.
const Lock = await [Link]("Lock")
L. Deploy the contract and send Ether
Deploys the contract and sends 1 ETH to it.
const lock = await [Link](unlockTime, { value: [Link]("1") })
await [Link]()
M. Read contract state
Calls read-only functions to check stored values.
await [Link]()
await [Link]()
N. Withdraw funds
Withdraws funds after the unlock time has passed.
await [Link]()
O. Exit the Hardhat console
Leaves the Hardhat interactive console.
.exit
P. Stop the local blockchain
Stops the local Hardhat network.
Ctrl + C