Hardhat
About
Hardhat is an Ethereum development environment for professionals. It facilitates performing frequent tasks, such as running tests, automatically checking code for mistakes or interacting with a smart contract. Additionally, Hardhat is a comprehensive set of tools for creating Ethereum-based software, which includes various components that aid in editing, compiling, debugging, and deploying smart contracts and decentralized applications.
Initialization
To be able to use the Hardhat functionalities, you will first have to initialize a Hardhat project. You can do so using the following commands:
$ npm init
$ npm install --save-dev hardhat @nomicfoundation/hardhat-ethers
$ npx hardhat init
For more details you can check out the official hardhat documentation page https://hardhat.org/hardhat-runner/docs/getting-started#installation
Deployment
To deploy your contracts, you can use Hardhat Ignition. You can deploy the contract by using its accompanying Ignition module. An Ignition module is a TypeScript or JavaScript file that allows you to specify what needs to be deployed.
The Ignition module, is located under the ./ignition/modules
directory and looks like this:
- Typescript
- Javascript
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
const testModule = buildModule("Module", (m) => {
const contract = m.contract("ContractName");
return { contract };
});
export default testModule;
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");
module.exports = buildModule("Module", (m) => {
const contract = m.contract("ContractName");
return { contract };
});
After double-checking that your private key is accessible to Hardhat and HamsterChain is correctly configured in hardhat.config.js, simply run the deployment script:
$ npx hardhat compile
Compiled 1 Solidity file successfully (evm target: cancun).
$ npx hardhat run --network hamsterChain scripts/deploy.js
Successfully deployed Contract to 0x...
Contract verification
HamsterChain block explorer uses Blockscout's open-source technology and supports contract verification through the hardhat-verify plugin with an Etherscan-compatible API. For detailed instructions, refer to the "Smart Contract Verification" section.
To verify your smart contract, you should do the following steps.
Note that the hardhat-etherscan plugin is now hardhat-verify, which you need to install:
Firstly install the hardhat-verify library:
$ npm install --save-dev @nomicfoundation/hardhat-verify
Secondly update your hardhat.config.js file as follows:
require("@nomicfoundation/hardhat-ethers");
require("@nomicfoundation/hardhat-verify");
Configure the plugin, using "test" as the API key placeholder for Blockscout:
module.exports = {
solidity: "0.8.25",
networks: {
hardhat: {},
"hamsterChain": {
url: "https://rpc.hamsterchain.org/",
chainId: 11211,
accounts: [process.env.PRIVATE_KEY]
}
},
etherscan: {
apiKey: {
"hamsterChain": process.env.API_KEY
},
customChains: [
{
network: "hamsterChain",
chainId: 11211,
urls: {
browserURL: "https://explorer.hamsterchain.org/"
}
}
]
}
};
To verify your contract, save your constructor arguments (like the deployer's address) to an environment variable and run the verify command on the deployed contract address:
$ export DEPLOYER_ADDRESS=0x...
$ export CONTRACT_ADDRESS=0x...
$ npx hardhat verify --network hamsterChain \
$CONTRACT_ADDRESS $DEPLOYER_ADDRESS
You should see a message confirming successful verification and a link to the contract on the block explorer.