Deploy with Hardhat
Deploying a Counter Contract with Hardhat
This guide will walk you through deploying a counter contract using Hardhat, a popular JavaScript-based development environment for Ethereum.
1. Prerequisites
Before you begin, ensure you have:
Node.js installed (v12 or later)
npm (comes with Node.js)
A code editor (e.g., VS Code)
MetaMask wallet and testnet tokens for deployment
2. Install Hardhat
Open your terminal and create a new project directory:
mkdir counter-project
cd counter-project
Initialize a new npm project:
npm init -y
Install Hardhat and required dependencies:
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox dotenv
npm install --save-dev @nomicfoundation/hardhat-ignition
3. Create a New Hardhat Project
Run the Hardhat setup wizard:
npx hardhat
Choose “Create a JavaScript project” when prompted.
This will create a project structure like:
contracts/
- for Solidity contractsigntion/
- for deployment scriptstest/
- for testshardhat.config.js
- configuration file
4. Write Your Smart Contract
Create a new file in the contracts directory, Counter.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Counter {
uint256 private count;
function increment() public {
count += 1;
}
function decrement() public {
count -= 1;
}
function getCount() public view returns (uint256) {
return count;
}
}
5. Compile the Smart Contract
Compile your contracts with:
npx hardhat compile
You should see a success message if there are no errors.
6. Write a Deployment Script
Create a new file in the ignition directory, Counter.js
:
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); module.exports = buildModule("CounterModule", (m) => {
const counter = m.contract("Counter");
return { counter };
});
7. Configure Network Settings
Create a .env
file in your project root:
PRIVATE_KEY=your_private_key_here
Edit hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.28",
networks: {
hardhat: {
chainId: 31337,
},
lazai: {
url: "https://lazai-testnet.metisdevops.link",
chainId: 133718,
accounts: [process.env.PRIVATE_KEY],
},
},
};
8. Deploy Your Contract
Local Deployment (Optional)
Start the Hardhat local node in a separate terminal:
npx hardhat node
Deploy to local network:
npx hardhat ignition deploy ignition/modules/Counter.js --network localhost
Deploy to LazAI Testnet
Make sure to:
Get testnet tokens from the faucet
Add your private key to the
.env
fileNever share your private key
Deploy to LazAI:
npx hardhat ignition deploy ignition/modules/Counter.js --network lazai
Test Setup
Create test/Counter.js
:
const { expect } = require("chai");
describe("Counter", function () {
it("Should increment the counter", async function () {
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
await counter.deployed();
await counter.increment();
expect(await counter.getCount()).to.equal(1);
});
});
Running Tests
npx hardhat test