Hardhat is the most widely used Solidity development environment. It runs on Node.js, integrates with the entire JavaScript ecosystem, and has a rich plugin library. If your team is already JavaScript-heavy, Hardhat is the natural choice.
Prerequisites
- Node.js 20+ installed
- A Sepolia RPC URL (use
https://ethereum-sepolia-rpc.publicnode.comfor free) - Sepolia ETH from a faucet — see the faucets page
- Basic Solidity and JavaScript knowledge
1. Create a new project
mkdir my-hardhat-project && cd my-hardhat-project
npm init -y
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
npx hardhat init
Choose “Create a JavaScript project” when prompted and accept the defaults.
2. Write your contract
Replace contracts/Lock.sol with a simple counter:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Counter {
uint256 public count;
event Incremented(address indexed by, uint256 newCount);
function increment() external {
count += 1;
emit Incremented(msg.sender, count);
}
function reset() external {
count = 0;
}
}
3. Write a test
Replace test/Lock.js with:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Counter", function () {
async function deploy() {
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
return { counter };
}
it("starts at zero", async function () {
const { counter } = await deploy();
expect(await counter.count()).to.equal(0n);
});
it("increments", async function () {
const { counter } = await deploy();
await counter.increment();
expect(await counter.count()).to.equal(1n);
});
it("resets", async function () {
const { counter } = await deploy();
await counter.increment();
await counter.reset();
expect(await counter.count()).to.equal(0n);
});
});
Run the tests locally:
npx hardhat test
4. Configure Sepolia
Open hardhat.config.js and replace its contents:
require("@nomicfoundation/hardhat-toolbox");
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const SEPOLIA_RPC = process.env.SEPOLIA_RPC || "https://ethereum-sepolia-rpc.publicnode.com";
module.exports = {
solidity: "0.8.24",
networks: {
sepolia: {
url: SEPOLIA_RPC,
accounts: PRIVATE_KEY ? [PRIVATE_KEY] : [],
},
},
};
Create a .env file (and add it to .gitignore):
PRIVATE_KEY=0xYOUR_PRIVATE_KEY_HERE
Install dotenv support:
npm install dotenv
Add require("dotenv").config(); at the top of hardhat.config.js.
5. Write a deploy script
Create scripts/deploy.js:
const { ethers } = require("hardhat");
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying from:", deployer.address);
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
await counter.waitForDeployment();
console.log("Counter deployed to:", await counter.getAddress());
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
6. Deploy to Sepolia
npx hardhat run scripts/deploy.js --network sepolia
You will see something like:
Deploying from: 0xYourAddress
Counter deployed to: 0xDeployedContractAddress
Verify on Sepolia Etherscan by searching for the deployed address.
7. Interact with the contract
Use the Hardhat console to call your contract:
npx hardhat console --network sepolia
const Counter = await ethers.getContractFactory("Counter");
const counter = Counter.attach("0xDeployedContractAddress");
await counter.count(); // 0n
await counter.increment();
await counter.count(); // 1n
Useful commands
| Command | What it does |
|---|---|
npx hardhat compile |
Compile all contracts |
npx hardhat test |
Run tests on the local Hardhat network |
npx hardhat node |
Start a local JSON-RPC node |
npx hardhat run scripts/deploy.js |
Deploy locally |
npx hardhat run scripts/deploy.js --network sepolia |
Deploy to Sepolia |
Next steps
- Add Hardhat Ignition for reproducible deployments
- Verify your contract on Etherscan with
@nomicfoundation/hardhat-verify - Explore the same contract on Base Sepolia using the OP Stack