// Run package allows us to run any hardhat task in code// Network package gives us network informationconst { ethers,run,network } =require("hardhat");asyncfunctionmain() {// ...// Only verify if we are on goerli test network/* 4 == 4 -> true 4 == "4" -> true 4 === "4" -> false (no type conversion is done) */if (network.config.chainId ===5&&process.env.ETHERSCAN_API_KEY) {console.log("Waiting for block txes...");awaitsimpleStorage.deployTransaction.wait(6);awaitverify(simpleStorage.address, []); }}asyncfunctionverify(contractAddress, args) {console.log("Verifying contract...");try {awaitrun("verify:verify", { address: contractAddress, constructorArguments: args, }); } catch (e) {if (e.message.toLowerCase().includes("already verified")) {console.log("Already Verified!"); } else {console.log(e); } }}
Interacting with Contracts in Hardhat
deploy.js
constcurrentValue=awaitsimpleStorage.retrieve();console.log(`Current Value is: ${currentValue}`);consttransactionResponse=awaitsimpleStorage.store(7);awaittransactionResponse.wait(1);constupdatedValue=awaitsimpleStorage.retrieve();console.log(`Updated Value is: ${updatedValue}`);
Artifacts Troubleshooting
Delete the cache and artifacts folder if any issue arises (yarn hardhat clean)
Hardhat will re compile and generate the artifacts
Custom Hardhat Tasks
Can be defined in hardhat.config.js
Generally created in tasks folder
Can be accessed via yarn hardhat block-number
tasks/block-number.js
const { task } =require("hardhat/config");task("block-number","Prints the current block number").setAction(// hre is like importing hardhat, gives access to all functionalitiesasync (taskArgs, hre) => {constblockNumber=awaithre.ethers.provider.getBlockNumber();console.log(`Current block number: ${blockNumber}`); });module.exports= {};
hardhat.config.js
// ...require("./tasks/block-number");// ...
Hardhat Localhost Node
yarn hardhat node spins up a local blockchain node as ganache, gives us accounts and rpc
The rpc url must be added to hardhat config like before in order to use that localnode
Hardhat console
let us run interactive commands to interact with blockchain
yarn hardhat console --network <network>
Running Tests
Hardhat works with mocha framework which is a javascript framework for running tests
chai framework is required for assert and expect statements
We can run individual tests using grep pattern matching in it description like yarn hardhat test --grep store
We can also use it.only keyword which will ignore all other test cases and run only this test case
test/test-deploy.js
const { ethers } =require("hardhat");const { expect,assert } =require("chai");describe("SimpleStorage",function () {let simpleStorageFactory, simpleStorage;beforeEach(asyncfunction () { simpleStorageFactory =awaitethers.getContractFactory("SimpleStorage"); simpleStorage =awaitsimpleStorageFactory.deploy(); });it("Should start with a favorite number of 0",asyncfunction () {constcurrentValue=awaitsimpleStorage.retrieve();constexpectedValue="0";// expect(currentValue.toString()).to.equal(expectedValue)assert.equal(currentValue.toString(), expectedValue); });it("Should update when we call store",asyncfunction () {constexpectedValue="7"consttransactionResponse=awaitsimpleStorage.store(expectedValue)awaittransactionResponse.wait(1)constcurrentValue=awaitsimpleStorage.retrieve()assert.equal(currentValue.toString(), expectedValue) })});
Hardhat Gas Reporter
Popular hardhat extension to see how much gas our funciton costs