Forem

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 full post →

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay