Learn HARDHAT with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.