How to Verify a Smart Contract with Hardhat

Smart contracts are a vital part of Web3 development, and they hold an essential role in the blockchain ecosystem of dApps. Since they have such a crucial function, it’s critical that they work as intended and remain entirely secure. This is because incomplete and malfunctioning smart contracts can lead to vast economic losses for businesses. For this reason, it’s highly beneficial to verify smart contracts to check their validity, correctness, and security. As such, we’re going to look closer at the process of how to verify a smart contract using Hardhat.

Furthermore, to make the process even quicker and easier, we’re going to utilize Moralis. Moralis is the number one platform for developing dApps, and users gain access to a fully functioning backend infrastructure. Furthermore, the platform provides a selection of great development tools such as the Moralis Price API, Web3 API, cross-chain compatibility, and much more. 

So, if you’d like to get started in dApp development, the first step is to sign up with Moralis. As a user, you’ll be able to significantly shorten the time to market for all your blockchain projects in the future!

What is Hardhat? 

Hardhat is an environment developers use to compile, test, deploy, and debug Ethereum based dApps. This means that it helps developers and coders to manage many of the native tasks of developing smart contracts. 

Hardhat comes with an already pre-built local Ethereum network with a focus on development. The network is specified for Solidity debugging and features error messages, stack traces, among other things. As such, this environment is especially helpful in enabling developers to understand where their dApps fail and how they can solve the issues. 

 

Furthermore, plugins characterize Hardhat, and this is where most of the functionalities originate. As such, it’s a modular design allowing users to choose which plugins to include in their development process. 

However, this only covers the basics of Hardhat, and if you’re interested in further information, then check out the article from Moralis explaining Hardhat in more detail.

How to Verify a Smart Contract in 5 Steps Using Hardhat

The process of how to verify a smart contract doesn’t have to be complicated. With the proper tools, we can accomplish this in just a matter of minutes. For this example, we’ll use Hardhat, Visual Studio Code, and, of course, Moralis. 

With Moralis, we can accomplish this process in five quick steps: 

  1. Setup Hardhat.
  2. Create an NFT smart contract.
  3. Create a deployment script.
  4. Modify “hardhat.config.js”.
  5. Run scripts and verify the contract.

In the proceeding sections, we’ll provide you with a complete walkthrough of the process where we’ll create a contract, deploy it to the Ropsten Testnet, and finally verify the contract using Hardhat. Furthermore, since we’ll use Moralis, the process becomes both quick and easy, which is highly beneficial to you as a developer. 

So, if you’re interested, follow along as we start with installing Hardhat and setting up our own project!

Step 1: How to Verify a Smart Contract — Setup Hardhat

First and foremost, we must install Hardhat before we can begin creating our project. Installing Hardhat is relatively easy, and all we need to do is input the following command into the terminal of our source code editor: 

This will immediately install Hardhat, and it will only take a couple of seconds. Once the development environment is installed, we kick off the process by initiating a new Hardhat project. To create a new project, we need to input “npx hardhat” into the terminal. This command will provide us as developers with a few different options. The first thing we need to determine is what kind of project we would like to create. In our case, we’re going to select a basic sample project. Once we’re done selecting the project, we set the root for the project, and finally, we can also add a “.gitignore”. 

Once we’re done with our selections, we’re provided with a basic structure for our project. For example, the sample project we just created contains some files by default, such as a smart contract called ”Greeter.sol”.

Now with our sample project at hand, we can continue with installing the OpenZeppelin package. We can do this by inputting the following command into the terminal: 

This will supply us with all the different OpenZeppelin contracts so that we can import them into our code. That’s the final step for setting up our environment. We can now move on to the next step and how to create a smart contract

Step 2: How to Verify a Smart Contract — Create an NFT Smart Contract

In the previous step, we mentioned the default smart contract ”Greeter.sol”. We can begin by deleting that and create a new contract. We can call this new contract ”NFT.sol”, which will be a fundamental NFT smart contract. Since this is a tutorial on how to verify a smart contract, we’re not going to put a lot of effort into creating our contract. So, if you’re interested, the Moralis blog offers you a complete guide on how to create your own NFT. Nonetheless, this is the complete file: 

As you can see from the image above, we’re implementing a pragma line into our code. This line determines which version of Solidity is to be used when we compile our contract. Once we have established the correct Solidity version, we must also import the proper OpenZeppelin contract, which in this case, is the contract for an ERC-1155 token. 

The contract itself follows the import line, and we are naming the contract ”NFT” and then determining that it will inherit from the ERC-1155 token contract that we previously imported. After this, we add the contract’s constructor, which is followed by a second ERC-1155 constructor. The latter needs to pass an argument, and in this example, we’re simply going to input the string ”URL”. However, when creating a real NFT, we would rather pass a URI as the argument instead.

Finally, within the curly braces of our constructor, we simply initiate the mint function that, in turn, takes a few arguments. For example, the first argument, ”msg.sender”, ensures that the NFT(s) are provided to the wallet used to deploy the contract. 

Once we’re done creating our contract, we can go ahead and compile the smart contract using Hardhat. To do so, we input the following command into the terminal: 

Step 3: How to Verify a Smart Contract — Create a Deployment Script

Within the default structure of our Hardhat sample project, we’ll find a JavaScript file called ”sample.script.js”, which we can use to create our own deployment script. So, we can begin by renaming the script to ”deploy.js” and use the file’s basic structure to create the script. 

All we really need to do here is rename and change some of the parameters within the file. We’re going to change everything that says ”Greeter” and replace it with ”NFT” or ”nft”. So, we are, for example, going to set the “getContractFactory” to ”NFT” and then remove the constructor arguments from the next line in the code. As such, this is what the changes will look like: 

However, this is not the complete ”deploy.js” file; this is only to showcase what we’ve changed. The other part of the file remains completely unaltered.

Step 4: How to Verify a Smart Contract — Modify “hardhat.config.js”

The fourth step in the process will involve making some minor modifications to the file ”hardhat.config.js”. However, before doing so, we’re going to install the verify plugin. To install this plugin, all we need to do is input the following command into the terminal: 

Once the plugin is installed, we also need to load the module by implementing the ”require()” function. There should already be an existing ”require()” function call at the beginning of the file, and all we need to do is write ”require(”@nomiclabs/hardhat-etherscan”);” below this function. 

Next up, we must also modify ”module.exports”, which can be found further down in the code. We’ll create a ”networks” object where we add the Ropsten network with a few secret details.

We’re going to need a URL and a private key which we’re going to fetch from the ”secrets.json” file, which we’ll create in just a moment. Moreover, the last thing we need to do is to add an Etherscan API key to the config file. To fetch the API key, we must create an Etherscan account. Once we have an account, we can click our profile and navigate to the ”My API Keys” page. Here we can create a new API key that we can copy and paste into our code. 

This is what the module looks like once we’re done with the modifications: 

Create a ”secrets.json” File

To enable our ”networks” object to access the secret URL and key, we must create a ”secrets.json” (JSON) file. The reason for doing this is because of the sensitive information stored, which is not something we’d like to share with others.

As such, we can go ahead and create a file called ”secrets.json”. In this file, we’ll add ”url”, which we’ll get from the Moralis Speedy Nodes, and ”key,” which we get from our MetaMask account. This is what the file should look like: 

As you’re logged in at moralis.io, we can fetch the needed URL by navigating ourselves to the ”Speedy Nodes” tab. Once we’re there, click the “Endpoints” button on the Ethereum network, which will present us with a selection of URLs. Since we’re going to deploy the contract to the Ropsten Testnet, we want to copy the testnet URL and implement it into our code. 

Furthermore, we can find the private key of our MetaMask account by clicking the three dots in our MetaMask interface and then select ”Account details”. Then we copy the key and implement it into our code from above. 

Once we’re done with the file, we must also load the module with an additional ”require()” function. As such, these will be the three ”require()” function calls of the ”hardhat.config.js” file:

Step 5: How to Verify a Smart Contract — Run Scripts and Verify the Contract

Now that we’ve completed all the previous steps of creating a smart contract, a deployment script, and also modified the config file, we have everything we need to verify our smart contract. So, the first thing that we can do is go ahead and deploy our contract to the blockchain using the following command: 

However, the account we added holding the private key to the ”secrets.json” file must contain tokens for the Ropsten Testnet to be able to run. If it doesn’t, all you need to do is find a faucet and input your wallet address to receive some. 

Once we execute the previous command, we’ll be provided with an address to where the contract was deployed. We can then use this address (without the quotation marks in the example below) to run the verification command, which looks like this: 

That’s it; congratulations! You have verified your own smart contract with Hardhat! If you still have questions regarding the process, we recommend checking out the following video from the Moralis YouTube channel. Here you’ll find an even clearer and more straightforward explanation of the process: 

How to Verify a Smart Contract — Summary

With the proper tools/platforms such as Moralis and Hardhat, it’s possible to verify a smart contract easily. Utilizing tools such as Moralis Speedy Nodes, we can verify an NFT smart contract, or any other contracts for that matter, in just five simple steps: 

  1. Setup Hardhat.
  2. Create an NFT smart contract.
  3. Create a deployment script.
  4. Modify “hardhat.config.js”.
  5. Run scripts and verify the contract.

Following these five steps allows us to verify any smart contract in a matter of minutes. Furthermore, in this article, we decided to upload the contract to the Ropsten Testnet, but the fundamentals of the process remain the same for any other blockchain. However, if you decide to opt for another chain, there are a few alterations that need to be made regarding the process. 

Moreover, this is only one of the many scenarios in which the Moralis platform comes in handy. For example, you can also use Moralis to build ETH dApps or create your own ERC-20 token. Furthermore, if you’re interested in learning more about the platform, we recommend checking out the Moralis blog. The blog features great articles to better understand the potential of working with Moralis. 

So, if you are looking to verify contracts or develop dApps and tokens, you should sign up with Moralis today. As a Moralis user, you’ll gain access to all the platform’s tools, allowing you to save valuable time and resources throughout the complete development process! 

 

Share on facebook
Share on twitter
Share on linkedin
Moralis Blog
Moralis Blog

Related Articles

The latest in Blockchain
Development

Subscribe to the Moralis Magazine, and be the first to find out about the latest Web3 tech!