Hardhat framework overview

Loading

Hardhat is a modern development environment designed for building and testing smart contracts on Ethereum and other EVM-compatible blockchains. It’s known for its fast iteration cycles, advanced debugging features, and seamless plugin ecosystem. Hardhat has become one of the most popular tools in the Web3 developer community, often preferred over traditional tools like Truffle due to its flexibility and developer-focused experience.

This guide provides a step-by-step overview of the Hardhat framework, explaining its core components, usage, and why it stands out in the blockchain development ecosystem.


1. What is Hardhat?

Hardhat is a JavaScript/TypeScript-based framework that allows developers to:

  • Compile Solidity contracts
  • Deploy them to local or test networks
  • Test using Mocha/Chai
  • Debug with stack traces and error messages
  • Run scripts to interact with contracts

Hardhat supports customizable plugins, network forking, and console logging inside smart contracts, making it ideal for modern dApp development.


2. Installing Hardhat

Make sure you have Node.js and npm installed. Then initialize a Node project:

mkdir my-hardhat-project
cd my-hardhat-project
npm init -y
npm install --save-dev hardhat

Create a Hardhat project:

npx hardhat

Choose “Create a basic sample project” to scaffold your directory structure.


3. Project Structure

Once initialized, the basic Hardhat structure includes:

  • contracts/ – Solidity smart contracts
  • scripts/ – Deployment and interaction scripts
  • test/ – Mocha tests for your contracts
  • hardhat.config.js – Project configuration file

4. Writing a Smart Contract

Inside contracts/, create Greeter.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Greeter {
string greeting;

constructor(string memory _greeting) {
greeting = _greeting;
}

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}

5. Compiling the Contract

Run:

npx hardhat compile

This compiles your smart contracts using the Solidity version defined in the config file.


6. Deploying the Contract

Create a new script in scripts/deploy.js:

async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();

console.log("Greeter deployed to:", greeter.address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

Run the script:

npx hardhat run scripts/deploy.js --network localhost

7. Running a Local Blockchain (Hardhat Node)

Start a local network:

npx hardhat node

Then deploy your contracts to it:

npx hardhat run scripts/deploy.js --network localhost

Hardhat node mimics Ethereum’s behavior and also supports mainnet forking.


8. Testing Smart Contracts

Create a test in test/greeter-test.js:

const { expect } = require("chai");

describe("Greeter", function () {
it("Should return the new greeting once it's changed", async function () {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello");
await greeter.deployed();

expect(await greeter.greet()).to.equal("Hello");

await greeter.setGreeting("Hola");
expect(await greeter.greet()).to.equal("Hola");
});
});

Run the tests:

npx hardhat test

9. Console Debugging

You can run an interactive console with:

bashCopyEditnpx hardhat console

From there:

const [deployer] = await ethers.getSigners();
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Debug time");
await greeter.deployed();
await greeter.greet();

10. Using Plugins

Hardhat’s ecosystem is plugin-based. Some popular plugins:

  • @nomicfoundation/hardhat-toolbox – Includes testing and deployment tools
  • hardhat-deploy – Simplifies deployment scripts
  • hardhat-etherscan – For contract verification on Etherscan
  • hardhat-gas-reporter – Shows gas usage during tests

Install toolbox:

npm install --save-dev @nomicfoundation/hardhat-toolbox

And include it in hardhat.config.js:

require("@nomicfoundation/hardhat-toolbox");

11. Configuring Networks

To deploy on a public network like Sepolia, use an Infura or Alchemy key and wallet mnemonic or private key.

In hardhat.config.js:

require("@nomicfoundation/hardhat-toolbox");

module.exports = {
solidity: "0.8.20",
networks: {
sepolia: {
url: "https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
accounts: ["YOUR_PRIVATE_KEY"]
}
}
};

Deploy:

npx hardhat run scripts/deploy.js --network sepolia

12. Forking Mainnet (Advanced Feature)

Hardhat allows you to fork Ethereum Mainnet or other chains:

networks: {
hardhat: {
forking: {
url: "https://mainnet.infura.io/v3/YOUR_KEY"
}
}
}

This allows real-world contract interactions and simulations with local testing.


13. Advantages of Hardhat

  • Custom plugin support
  • Fast, efficient local blockchain
  • Mainnet forking capability
  • Better stack trace and error handling
  • TypeScript support
  • Deep integration with Ethers.js
  • Popular in DeFi and NFT projects

14. Hardhat vs Truffle

FeatureHardhatTruffle
Plugin systemHighly modularLess modular
DebuggingAdvanced stack tracesBasic error output
Network forkingYesLimited
Built-in consoleYesYes
Front-end integrationUses Ethers.jsUses Web3.js/Drizzle
Developer preferenceGrowing in popularityTraditional choice

Leave a Reply

Your email address will not be published. Required fields are marked *