Getting Started with Hardhat on INK
This guide will walk you through setting up a new project using Hardhat, a popular development environment for Ethereum software.
Prerequisites
First, make sure you have Node.js installed (version 20 or later). You can check your Node version with:
node --version
Setting Up a New Hardhat Project
- Create a new directory for your project and initialize it:
mkdir my-hardhat-project
cd my-hardhat-project
npm init -y
- Install Hardhat and necessary dependencies:
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
- Create a new Hardhat project:
npx hardhat init
Choose "Create a JavaScript project" when prompted. This will create a project with this structure:
my-hardhat-project/
├── contracts/
├── scripts/
├── test/
├── hardhat.config.js
└── package.json
Writing Your First Contract
Create a new file in the contracts
directory:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract InkContract {
string public greeting = "Hello, Ink!";
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
Configuring Hardhat
Create a .env
file in your project root:
PRIVATE_KEY=your_private_key_here
INK_SEPOLIA_URL=https://rpc-gel-sepolia.inkonchain.com/
BLOCKSCOUT_API_KEY=your_blockscout_api_key_here
Update your hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.19",
networks: {
inksepolia: {
url: process.env.INK_SEPOLIA_URL || "",
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
},
},
etherscan: {
apiKey: {
inksepolia: process.env.BLOCKSCOUT_API_KEY,
},
customChains: [
{
network: "inksepolia",
chainId: 763373,
urls: {
apiURL: "https://explorer-sepolia.inkonchain.com/api/v2",
browserURL: "https://explorer-sepolia.inkonchain.com/",
},
},
],
},
};
Building and Testing
- Compile your contracts:
npx hardhat compile
- Create a test file in
test/InkContract.js
:
const { expect } = require("chai");
describe("InkContract", function () {
it("Should return the correct greeting", async function () {
const InkContract = await ethers.getContractFactory("InkContract");
const contract = await InkContract.deploy();
await contract.deployed();
expect(await contract.greeting()).to.equal("Hello, Ink!");
});
});
- Run the tests:
npx hardhat test
Deployment
Create a deployment script in scripts/deploy.js
:
async function main() {
const InkContract = await ethers.getContractFactory("InkContract");
const contract = await InkContract.deploy();
await contract.deployed();
console.log("InkContract deployed to:", contract.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy to INK Sepolia testnet:
npx hardhat run scripts/deploy.js --network inksepolia
Verifying Your Contract
After deployment, verify your contract:
npx hardhat verify --network inksepolia <DEPLOYED_CONTRACT_ADDRESS>
Interacting with Your Contract
You can use Hardhat Console to interact with your deployed contract:
npx hardhat console --network inksepolia
const Contract = await ethers.getContractFactory("InkContract");
const contract = await Contract.attach("YOUR_CONTRACT_ADDRESS");
await contract.greeting();
await contract.setGreeting("New greeting!");
Additional Tools
Hardhat comes with several built-in tools:
- Hardhat Network: Local Ethereum network for development
- Console: Interactive JavaScript environment
- Gas Reporter: Gas usage reporting
- Coverage: Code coverage for Solidity tests
To use the network for local development:
npx hardhat node
Next Steps
- Explore Hardhat Documentation for more features