Smart Contract Development With Hardhat: Project Setup, Compile, Test With Chai and Deploy to a Local Network
Why Not Only Remix?
Remix is great to learn. Real projects need version control, automated tests, scripts and repeatable deployment. Tools such as Hardhat and Foundry provide these.
| Hardhat | Foundry |
| Language for tests | JavaScript or TypeScript | Solidity |
| Speed | Good | Very fast |
| Best for | Teams with web developers | Contract-focused developers |
This lesson uses Hardhat. Install Node.js first (use a current long-term-support version).
Create the Project
bash
mkdir counter-project && cd counter-project
npm init -y
npm install --save-dev hardhat
npx hardhat init
Choose a JavaScript or TypeScript project when asked and accept the defaults. The generated structure is:
text
contracts/ your .sol files
test/ automated tests
ignition/ deployment modules (or scripts/)
hardhat.config.js
A Simple Contract
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Counter {
uint256 public count;
event Incremented(uint256 newCount);
function increment() external {
count += 1;
emit Incremented(count);
}
function decrement() external {
require(count > 0, "Already zero");
count -= 1;
}
}
Save it as contracts/Counter.sol, then compile:
bash
px hardhat compile
Version note The tests in this course were run with Hardhat 2.x and the Hardhat Toolbox. Hardhat 3 changes the config format and test setup, so if npx hardhat init gives you a different template, follow its official guide and keep the same contract and test logic.
Write Tests
javascript
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Counter", function () {
let counter;
beforeEach(async function () {
const Counter = await ethers.getContractFactory("Counter");
counter = await Counter.deploy();
await counter.waitForDeployment();
});
it("starts at zero", async function () {
expect(await counter.count()).to.equal(0n);
});
it("increments", async function () {
await counter.increment();
expect(await counter.count()).to.equal(1n);
});
it("emits an event", async function () {
await expect(counter.increment()).to.emit(counter, "Incremented").withArgs(1n);
});
it("cannot go below zero", async function () {
await expect(counter.decrement()).to.be.revertedWith("Already zero");
});
});
bash
px hardhat test
Deploy
bash
px hardhat node # terminal 1: local blockchain with test accounts
npx hardhat ignition deploy ./ignition/modules/Counter.js --network localhost
Ignition or scripts Newer Hardhat projects use Ignition modules for deployment. You can also write a normal script with ethers and run npx hardhat run scripts/deploy.js. Check the Hardhat docs for the template your version creates.
Deploy to Sepolia (Outline)
- Get a Sepolia RPC URL from a provider (Alchemy, Infura and others)
- Put the URL and a throwaway private key in an **
.env file**
- Add the network to
hardhat.config.js
- Add
.env to .gitignore
- Run the deploy command with
--network sepolia
Never commit keys Do not push private keys, seed phrases or .env files to GitHub. Bots scan for them within seconds.
Practice
- Add a
reset() function that only the owner can call and test it
- Write a test that checks the event argument for decrement
- Why test contracts before deployment?
Conclusion
Hardhat compiles, tests and deploys contracts with repeatable scripts. Automated tests are essential because deployed contracts cannot be quietly fixed.
Quick Summary: why; tools; project; contract; tests; deploy; Sepolia.