๐ท Hardhat
Hardhat is a development environment for Ethereum and EVM chains: compile Solidity, run tests, and run scripts (deploy, interact) from the command line. Itโs the standard choice for serious EVM development.
๐ฏ Why Hardhat
- Local network โ Built-in Hardhat Network for fast tests and debugging
- TypeScript/JavaScript โ Write tests and scripts in JS/TS with Ethersย or Viemย
- Plugins โ Hardhat Toolboxย , Etherscan verificationย , deployment, coverage
- Documentation โ hardhat.orgย and Hardhat docsย
๐ Quick Start
mkdir my-project && cd my-project
npm init -y
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
npx hardhat initChoose Create a JavaScript project (or TypeScript). Then:
npx hardhat compile
npx hardhat test๐ Project Layout
contracts/ # Solidity sources
scripts/ # Deploy and utility scripts
test/ # Tests (e.g. *.js / *.ts)
hardhat.config.jsโ๏ธ Config Example (Multiple Networks)
// hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.24",
networks: {
hardhat: {
chainId: 1337,
},
sepolia: {
url: process.env.SEPOLIA_RPC_URL,
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
},
mainnet: {
url: process.env.MAINNET_RPC_URL,
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
},
},
etherscan: {
apiKey: {
sepolia: process.env.ETHERSCAN_API_KEY,
mainnet: process.env.ETHERSCAN_API_KEY,
},
},
};Secrets go in .env; see Environment Variables.
๐ Deploy Script Example
// scripts/deploy.js
const hre = require("hardhat");
async function main() {
const [deployer] = await hre.ethers.getSigners();
console.log("Deploying with", deployer.address);
const MyContract = await hre.ethers.getContractFactory("MyContract");
const contract = await MyContract.deploy(/* constructor args */);
await contract.waitForDeployment();
const address = await contract.getAddress();
console.log("Deployed to", address);
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});Run:
npx hardhat run scripts/deploy.js --network sepolia๐งช Test Example
// test/MyContract.js
const { expect } = require("chai");
const hre = require("hardhat");
describe("MyContract", function () {
it("should do something", async function () {
const [owner] = await hre.ethers.getSigners();
const MyContract = await hre.ethers.getContractFactory("MyContract");
const contract = await MyContract.deploy();
await contract.waitForDeployment();
await expect(contract.someFunction()).to.not.be.reverted;
});
});๐ Resources
- Hardhat official siteย
- Hardhat documentationย
- Hardhat Toolboxย
- TUM Hardhat & Web3 Dev Setup โ Full workshop flow
Last updated on