DEV Community

Kristaps Grinbergs
Kristaps Grinbergs

Posted on

Programmatically Verifying Solidity Smart Contract Code with Hardhat

In a previous blog post, we discussed the importance of verifying Solidity smart contracts before deploying them onto the blockchain. However, there is a way to automate that process and make it more efficient. With the help of Hardhat, a popular development environment for Ethereum, developers can now verify their smart contract code programmatically as part of the deployment process. This means the verification process can be integrated into the development pipeline, ensuring the code is verified automatically each time the contract is deployed. This blog post will explore how to use Hardhat to verify smart contract code and streamline the verification process programmatically.

Hardhat Runtime Environment

HRE stands for Hardhat Runtime Environment. It provides a robust set of tools and APIs that can be used to verify the correctness of a smart contract code programmatically. Verification is an essential step in the development process of smart contracts, as it ensures that the code behaves as intended and is secure.

Veryfing smart contract programmatically

To programmatically verify a smart contract with HRE, we should use a plugin called hardhat-etherscan in combination with the HRE subtask verify:verify.

To verify the smart contract automatically, we need to pass:

  • the network name that we can get from the Hardhat Network addon;
  • smart contract address;
  • constructor agruments.

We should wait for the deploy transaction to be finished to avoid problems when verifying a smart contract that hasn't been deployed yet.

Let's now put it all together in a Hardhat script.

import hre, { network, ethers } from "hardhat";

async function main() {
  const SaveNumber = await ethers.getContractFactory("SaveNumbero");

  const number = 6;

  const saveNumber = await SaveNumber.deploy(number);

  await saveNumber.deployed();

  console.log(`SecretNumber deployed to ${saveNumber.address}`);

  try {
    console.log("\nEtherscan verification in progress...");
    await saveNumber.deployTransaction.wait(6);
    await hre.run("verify:verify", {
      network: network.name,
      address: saveNumber.address,
      constructorArguments: [number],
    });
    console.log("Etherscan verification done. ✅");
  } catch (error) {
    console.error(error);
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
Enter fullscreen mode Exit fullscreen mode

TL;DR

Automated smart contract verification with HRE provides a much more streamlined and efficient way to verify smart contract code than manually verifying it from the terminal. It saves time and reduces the chances of errors compared to manual verification from the terminal.

Links

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay