Configure Hardhat
Learn how to set up Hardhat for ZippyChain development.
Table of Contents
Introduction
Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
This guide will walk you through setting up Hardhat for ZippyChain development.
Prerequisites
Before you begin, make sure you have:
- Node.js (version 16 or later) and npm installed
- Basic knowledge of JavaScript and Solidity
- A code editor (like Visual Studio Code)
- A wallet with some test ZIPPY tokens (for testnet deployment)
Installation
Create a new directory for your project and navigate to it:
mkdir my-zippy-project
cd my-zippy-project
Initialize a new npm project:
npm init -y
Install Hardhat:
npm install --save-dev hardhat
Install additional dependencies:
npm install --save-dev @nomicfoundation/hardhat-toolbox @openzeppelin/contracts dotenv
Project Setup
Initialize a Hardhat project:
npx hardhat
Select "Create a JavaScript project" and follow the prompts. This will create a basic project structure with the following files:
- hardhat.config.js: Hardhat configuration file
- contracts/: Directory for your Solidity contracts
- scripts/: Directory for deployment and interaction scripts
- test/: Directory for your tests
Configuration
Create a `.env` file in the root of your project to store your private key and other sensitive information:
PRIVATE_KEY=your_private_key_here
ZIPPY_API_KEY=your_zippy_explorer_api_key_here
Update your `hardhat.config.js` file to configure ZippyChain networks:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const PRIVATE_KEY = process.env.PRIVATE_KEY || "0000000000000000000000000000000000000000000000000000000000000000";
const ZIPPY_API_KEY = process.env.ZIPPY_API_KEY || "";
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.20",
networks: {
zippy_testnet: {
url: "https://t.s0.n6.zippychain.ai",
accounts: [PRIVATE_KEY],
chainId: 700,
},
zippy_mainnet: {
url: "https://rpc.zippychain.com",
accounts: [PRIVATE_KEY],
chainId: 700,
},
},
etherscan: {
apiKey: {
zippy_testnet: ZIPPY_API_KEY,
zippy_mainnet: ZIPPY_API_KEY,
},
customChains: [
{
network: "zippy_testnet",
chainId: 700,
urls: {
apiURL: "https://testnet-explorer.zippychain.com/api",
browserURL: "https://testnet-explorer.zippychain.com",
},
},
{
network: "zippy_mainnet",
chainId: 7777,
urls: {
apiURL: "https://explorer.zippychain.com/api",
browserURL: "https://explorer.zippychain.com",
},
},
],
},
};
Note: Never commit your private key to version control. Use environment variables or a secure secret management solution in production.
Testing
Create a simple smart contract for testing. In the `contracts` directory, create a file named `Counter.sol`:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Counter {
uint256 private _count;
event CountIncremented(uint256 newCount);
function increment() public {
_count += 1;
emit CountIncremented(_count);
}
function getCount() public view returns (uint256) {
return _count;
}
}
Create a test file in the `test` directory named `Counter.test.js`:
const { expect } = require("chai");
describe("Counter", function () {
let counter;
beforeEach(async function () {
const Counter = await ethers.getContractFactory("Counter");
counter = await Counter.deploy();
await counter.deployed();
});
it("Should return the initial count of 0", async function () {
expect(await counter.getCount()).to.equal(0);
});
it("Should increment the count", async function () {
await counter.increment();
expect(await counter.getCount()).to.equal(1);
await counter.increment();
expect(await counter.getCount()).to.equal(2);
});
it("Should emit an event when incremented", async function () {
await expect(counter.increment())
.to.emit(counter, "CountIncremented")
.withArgs(1);
});
});
Run the tests:
npx hardhat test
Deployment
Create a deployment script in the `scripts` directory named `deploy.js`:
const hre = require("hardhat");
async function main() {
console.log("Deploying Counter contract...");
const Counter = await hre.ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
await counter.deployed();
console.log(`Counter deployed to: ${counter.address}`);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy to ZippyChain Testnet:
npx hardhat run scripts/deploy.js --network zippy_testnet
When you're ready to deploy to mainnet:
npx hardhat run scripts/deploy.js --network zippy_mainnet
Contract Verification
Verify your contract on ZippyChain Explorer:
npx hardhat verify --network zippy_testnet DEPLOYED_CONTRACT_ADDRESS
Replace `DEPLOYED_CONTRACT_ADDRESS` with the address of your deployed contract.
If your contract has constructor arguments, you can include them like this:
npx hardhat verify --network zippy_testnet DEPLOYED_CONTRACT_ADDRESS "Constructor Arg 1" "Constructor Arg 2"
Troubleshooting
If you encounter issues, try these solutions:
Network Connection Issues
If you can't connect to ZippyChain:
- Check your internet connection
- Verify the RPC URL in your configuration
- Make sure the chainId is correct
Deployment Failures
If your deployment fails:
- Ensure you have enough ZIPPY tokens for gas
- Check that your private key is correct
- Try increasing the gas limit in your config
Verification Errors
If contract verification fails:
- Make sure your API key is correct
- Verify that you're using the same compiler version that was used for deployment
- Check that constructor arguments are provided in the correct format