Learn Hardhat - 10 Code Examples & CST Typing Practice Test
Hardhat is a JavaScript/TypeScript-based Ethereum development environment and framework. It enables developers to compile, deploy, test, and debug smart contracts efficiently on Ethereum and EVM-compatible networks.
View all 10 Hardhat code examples →
Learn HARDHAT with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Hardhat Minimal Smart Contract Deployment
// contracts/Counter.sol
pragma solidity ^0.8.0;
contract Counter {
uint256 public count = 0
function increment() public { count += 1 }
}
// scripts/deploy.js
async function main() {
const [deployer] = await ethers.getSigners()
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.deploy()
await counter.deployed()
console.log('Counter deployed to:', counter.address)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
A minimal example showing how to deploy a simple smart contract using Hardhat scripts.
Hardhat Call Contract Function
async function main() {
const [deployer] = await ethers.getSigners()
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.attach('0xYourContractAddress')
await counter.increment()
const value = await counter.count()
console.log('Counter value:', value)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Call a deployed smart contract function using Hardhat runtime environment.
Hardhat Get Accounts
async function main() {
const accounts = await ethers.getSigners()
accounts.forEach((account, i) => console.log(`Account ${i}: ${account.address}`))
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Retrieve and display the list of accounts provided by Hardhat network.
Hardhat Estimate Gas
async function main() {
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.attach('0xYourContractAddress')
const gasEstimate = await counter.estimateGas.increment()
console.log('Estimated gas:', gasEstimate)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Estimate gas for a contract method call using Hardhat.
Hardhat Deploy with Parameters
// contracts/Greeting.sol
pragma solidity ^0.8.0;
contract Greeting {
string public message
constructor(string memory _msg) { message = _msg }
}
// scripts/deploy.js
async function main() {
const Greeting = await ethers.getContractFactory('Greeting')
const greeting = await Greeting.deploy('Hello Hardhat')
await greeting.deployed()
console.log('Greeting deployed at:', greeting.address)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Deploy a contract with constructor parameters using Hardhat.
Hardhat Listen to Events
async function main() {
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.attach('0xYourContractAddress')
counter.on('Incremented', (newValue) => {
console.log('Counter incremented to:', newValue)
})
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Listen to events emitted by a contract using Hardhat provider.
Hardhat Read Contract Variable
async function main() {
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.attach('0xYourContractAddress')
const value = await counter.count()
console.log('Counter value:', value)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Read a public variable from a deployed contract using Hardhat.
Hardhat Transfer Ether
async function main() {
const [sender, receiver] = await ethers.getSigners()
const tx = await sender.sendTransaction({ to: receiver.address, value: ethers.parseEther('1') })
await tx.wait()
console.log('Ether transferred')
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Send Ether from one account to another on Hardhat local network.
Hardhat Deploy Multiple Contracts
async function main() {
const Counter = await ethers.getContractFactory('Counter')
const counter = await Counter.deploy()
await counter.deployed()
const Greeting = await ethers.getContractFactory('Greeting')
const greeting = await Greeting.deploy('Hi there')
await greeting.deployed()
console.log('Contracts deployed')
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Deploy multiple contracts sequentially in Hardhat script.
Hardhat Upgradeable Contract Deployment
const { upgrades } = require('hardhat')
async function main() {
const Counter = await ethers.getContractFactory('Counter')
const counter = await upgrades.deployProxy(Counter, [0])
await counter.deployed()
console.log('Upgradeable Counter deployed at:', counter.address)
}
main().catch((error) => { console.error(error); process.exitCode = 1 })
Deploy an upgradeable contract using Hardhat and OpenZeppelin upgrades plugin.
Frequently Asked Questions about Hardhat
What is Hardhat?
Hardhat is a JavaScript/TypeScript-based Ethereum development environment and framework. It enables developers to compile, deploy, test, and debug smart contracts efficiently on Ethereum and EVM-compatible networks.
What are the primary use cases for Hardhat?
Compiling and deploying Solidity smart contracts. Running local Ethereum test networks. Automated testing of smart contracts. Debugging and logging transactions. Scripted contract interactions and deployments
What are the strengths of Hardhat?
Fast local blockchain simulation. Easy Solidity compilation and deployment. Highly extensible with plugins. Debugging and stack traces for contracts. Integrates seamlessly with JS/TS frontends
What are the limitations of Hardhat?
Requires JavaScript/TypeScript knowledge. Primarily Ethereum/EVM-focused. Local network state resets on restart (ephemeral). Some plugins may have version compatibility issues. Less Python integration compared to Web3.py
How can I practice Hardhat typing speed?
CodeSpeedTest offers 10+ real Hardhat code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.