In the fast-paced world of blockchain development, ensuring the integrity and security of smart contracts is paramount. Smart contracts, the cornerstone of decentralized applications (DApps), execute predefined actions when specific conditions are met, making them indispensable in various blockchain ecosystems. However, verifying the reliability and authenticity of these contracts has been a manual and time-consuming process, often prone to errors. In this article, we explore the groundbreaking solution that streamlines the verification process for Hardhat smart contracts on BlocksScan with XDC Network, revolutionizing the way developers ensure the reliability of their code.
BlocksScan's Role in Ensuring Contract Reliability
BlocksScan serves as a critical tool for blockchain enthusiasts, offering a wide array of functionalities from exploring transaction histories to analyzing contract interactions. One important feature it provides is the verification of smart contract code, contributing to the overall trustworthiness of blockchain applications. By scrutinizing the code of smart contracts, BlocksScan helps users verify the reliability of these contracts before engaging with them, fostering trust within the community.
To simplify the verification process, a groundbreaking tool called hardhat-plugin-verify has been created. This plugin for Hardhat enables automatic verification of Hardhat contracts. With hardhat-plugin-verify, verifying your contracts is as easy as running just one command:
npx hardhat verify --network <network_name> <contract_address>
Prerequisites
For this guide we assume you already have a Hardhat project with a predefined deployment process. If you're unfamiliar with setting up deployment for Hardhat projects, refer How to Deploy and Verify Smart Contracts on XDC Network using HardHat for guidance.
Creating a ".env" File
Before proceeding, you need to create a .env file in the root directory of your project. This file will contain sensitive information such as your private key and network URLs.
In the .env file, define the following environment variables:
PRIVATE_KEY=your_private_key
API_URL_KEY=https://apothem.xdcrpc.com
Replace your_private_key with your actual private key. Ensure that you never share your private key publicly to maintain the security of your accounts and assets.
Smart Contract
For demonstration purposes, let's use a simple Pepe contract as an example. It's like creating your own digital money on XDC Network, similar to making your own coins or tokens. When someone starts this contract, they decide how many of these digital coins will exist right from the beginning, and those coins are given to them.
This contract follows specific rules called the ERC20 standard, which ensures it can smoothly interact with other digital currencies and platforms on Ethereum, XDC Network - EVM Compatible network. People can send these digital coins to each other, just like sending money, and they can also check how many coins they have in their digital wallet. Additionally, they can allow other addresses to spend their coins on their behalf if they want.
So essentially, the PepeToken contract helps you create, distribute, and keep track of your own custom digital coins on XDC Network. It's like having your own digital currency system!
Adding XDC Network in your hardhat.config.js file
Once you've set up your project and are ready to configure your Hardhat environment, you'll need to add the XDC Network configuration to your hardhat.config.js file. This step is crucial for interacting with the XDC Network and verifying contracts on BlocksScan.
Open your hardhat.config.js file and add the following code snippet:
/** @type import('hardhat/config').HardhatUserConfig */
require('dotenv').config();
require('@nomiclabs/hardhat-ethers');
require("@nomicfoundation/hardhat-verify");
const {API_URL_KEY, PRIVATE_KEY} = process.env;
module.exports = {
solidity: "0.5.8",
defaultNetwork: 'apothem',
networks: {
hardhat: {},
apothem: {
url: API_URL_KEY, // Use API URL from environment variables
accounts: [`0x${PRIVATE_KEY}`] // Use private key from environment variables
},
// Add XDC Network configuration below
xdc: {
url: "https://rpc.apothem.network",
chainId: 50,
accounts: [`0x${PRIVATE_KEY}`]
}
},
etherscan: {
apiKey: {
apothem: "abc",
devnet: "abc",
xdc: "abc",
},
customChains: [
{
network: "apothem",
chainId: 51,
urls: {
apiURL: "https://api-apothem.xdcscan.io/api",
browserURL: "https://apothem.xdcscan.io/",
},
},
{
network: "devnet",
chainId: 551,
urls: {
apiURL: "https://api-devnet.xdcscan.io/api",
browserURL: "https://devnet.xdcscan.io/",
},
},
{
network: "xdc",
chainId: 50,
urls: {
apiURL: "https://api.xdcscan.io/api",
browserURL: "https://xdcscan.io/",
},
},
],
},
};
Verifying the Smart Contract
Now that we have our contract ready, let's demonstrate how simple it is to verify this contract with hardhat-plugin-verify.
Install Dependencies & enable hardhat-plugin-verify
You can install the hardhat plugin using npm or yarn:
For npm
- For installing dotenv
npm install dotenv
- For installing @nomiclabs/hardhat-ethers as a development dependency with forced installation
npm install --save-dev @nomiclabs/hardhat-ethers --force
- For installing @nomicfoundation/hardhat-verify as a development dependency with forced installation
npm install --save-dev @nomicfoundation/hardhat-verify --force
- For forcibly installing @openzeppelin/contracts
npm install --force @openzeppelin/contracts
- For installing ethers as a development dependency with version 5.7.2 or higher
npm install - save-dev ethers@5.7.2
For yarn
- For installing dotenv
yarn add dotenv
- For installing @nomiclabs/hardhat-ethers as a development dependency with forced installation
yarn add --dev @nomiclabs/hardhat-ethers --force
- For installing @nomicfoundation/hardhat-verify as a development dependency
yarn add --dev @nomicfoundation/hardhat-verify --force
- For installing @openzeppelin/contracts forcibly
yarn add --force @openzeppelin/contracts
- For installing ethers as a development dependency with version 5.7.2 or higher
yarn add --dev ethers@5.7.2
Once installed, enable the plugin in your Hardhat configuration file hardhat.config.js as follows:
require('dotenv').config();
require('@nomiclabs/hardhat-ethers');
require("@nomicfoundation/hardhat-verify");
module.exports = {
/* … rest of hardhat-config */
Deploying Smart Contracts
To deploy your smart contracts, you'll use a deployment script. Create a new file named deploy.js in the test directory of your Hardhat project. Copy and paste the following code into this file:
const { ethers } = require("hardhat");
async function main() {
const PepeToken = await ethers.getContractFactory("PepeToken");
const pepeToken = await PepeToken.deploy(1000000); // Initial supply: 1,000,000 tokens
await pepeToken.deployed();
console.log("PepeToken deployed to:", pepeToken.address);
console.log("PepeToken deployed to HASH:", pepeToken.deployTransaction.hash);
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
This script creates and deploys a PepeToken contract with an initial supply of 1,000,000 tokens and displays its address and deployment transaction hash.
Deploy & Verify the Smart Contract
Now, with everything set up to use hardhat-plugin-verify, deploy and verify the smart contract.
npx hardhat run scripts/deploy.js --network apothem
After successful deployment, verify the contract using the following command:
# Note: The constructor for contracts/Pepetoken.sol:PepeToken has 1 parameter.
npx hardhat verify --network <network_name> <contract_address><parameter>
Once you've added the parameter, your command should look like the below one
npx hardhat verify --network apothem 0x84D5096e7b12a84BF9ba046e289532b79ADD1489 1000000
Conclusion
The manual process of verifying smart contracts on BlocksScan can be cumbersome and error-prone. However, with hardhat-plugin-verify, developers can automate this process, saving time and reducing the likelihood of mistakes. The introduction of hardhat-plugin-verify revolutionizes the verification of Hardhat contracts on BlocksScan, making it faster, more efficient, and less error-prone. This innovation contributes to upholding the standards of reliability and security in blockchain development, fostering trust and confidence within the blockchain community. If you found this guide helpful and wish to integrate hardhat-plugin-verify into your projects, explore it and share your Feedback. Your feedback is valuable, so feel free to share your experiences and suggestions in the comments section below. Don't forget to spread the word on social media platforms like Facebook, Twitter, and LinkedIn.
Discussion (0)