Different Ways to Verify Your Smart Contract Code | QuickNode (2024)

8 min read

Overview

Smart contracts are self-executing agreements that are stored on a blockchain. They are written in programming languages, such as Solidity, and are executed on the blockchain platform. Smart contracts are used to automate various processes and transactions, and are used in many blockchain applications, such as decentralized finance (DeFi), non-fungible tokens (NFTs), and more.

However, like any software, smart contracts are susceptible to bugs and security vulnerabilities. A bug or vulnerability in a smart contract can lead to unintended consequences, such as loss of funds or a security breach. Therefore, it is crucial to verify the source code of a smart contract before it is deployed on the blockchain. This guide will discuss the five different methods to apply smart contract source code verification to your project.

What You Will Need


  • Access to an Ethereum endpoint (other EVM-based chains are also supported! Sign up for a free account here)
  • Etherscan API Keys
  • ETH on your testnet of choice (you can use the Multi-Chain QuickNode Faucet)
  • A desire to troubleshoot and learn!

What You Will Do


  • Learn about the Smart Contract verification process and different tools available
  • Deploy a Smart Contract with Remix.IDE
  • Learn the methods to apply Smart Contract source code verification to your project
  • Recap Troubleshooting Techniques

Why Verify a Smart Contract?

You may want to open-source (verify) your smart contract on a public block explorer for several reasons.

  • Transparency: One of the most important aspects of Web3 is decentralization and transparency. When you open-source your project's smart contracts, it increases trust in the community and allows anyone to inspect the code and suggest improvements.

  • Security: Once your smart contract is verified, it will effectively have more eyes on it, and smart contract experts can discover any potential vulnerabilities. The downside is that someone (a black hat) may find the vulnerability first, which can lead to your contract being exploited.

  • Innovation & Adoption: Open-sourced code leads to more innovation and adoption since now other developers can fork your code, add features and continue to build on top of your code. Effectively leading to a more active developer ecosystem with a fast learning rate.


  • Hardhat (JavaScript): A development environment for Ethereum software. It consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps.
  • Remix.IDE (Browser-based): An open-source IDE for developing and testing smart contracts. It provides a browser-based interface for creating and testing contracts and deploying them to the Ethereum network.
  • Etherscan (Browser-based): A block explorer and analytics platform for the Ethereum blockchain. It provides a user-friendly web interface that allows users to explore and track the activity on the Ethereum network.
  • Brownie (Python): A Python-based development and testing framework for Ethereum smart contracts. It provides a simple and intuitive interface for creating, compiling, and deploying contracts and running automated tests.
  • Foundry (Solidity): A Solidity based smart contract development toolkit enabling you to manage dependencies, compile your project, runs tests, deploy, and lets you interact with the chain from the command-line and via Solidity scripts.

Accessing Ethereum with QuickNode

You'll need an API endpoint to communicate with the Ethereum blockchain. You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like 8x faster response times, you can leave the heavy lifting to us. Sign up for a free account here.

Once signed in, you can create an API Endpoint in just a few clicks. Click Create Endpoint, and select the Ethereum chain and the network you need (e.g., Mainnet, Goerli, etc.). For this guide, we will be deploying a contract on the Goerli testnet.

Different Ways to Verify Your Smart Contract Code | QuickNode (1)

Note that you must update your non-custodial wallet RPC Provider to include your QuickNode HTTP Provider URL. An example can be found in this guide.

Before proceeding, also be sure to have enough ETH in your wallet. You can use the Multi-Chain QuickNode Faucet to get free testnet ETH! (make sure to select Goerli!)

Deploying A Smart Contract

Before starting the smart contract verification process, we'll need to deploy a contract onto the blockchain. To do so, we'll use the Remix.IDE, an online IDE and toolset for developing, deploying, debugging, and testing Ethereum and EVM-compatible smart contracts.

Navigate to Remix.IDE, create a file called "MyToken.sol" and update the file to include the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {}
}

For this guide, we'll be demonstrating the verification process of an ERC-20 token

Then, compile the contract on the Solidity Compiler tab and then go to Deploy & Run Transactions tab to deploy the contract. Select Injected Provider or WalletConnect, connect your wallet, and deploy the contract.

Method 1: Verify via Etherscan

The simplest way to verify your source code is via the Etherscan UI. This process does not require any programming skills.

Navigate to the Contract tab of the smart contract you are viewing on Etherscan and click the Verify and Publish link.

Different Ways to Verify Your Smart Contract Code | QuickNode (2)

Then, you'll need to select the file type, compiler version, and license. This step is crucial because the verification process will fail if there are mismatches between the smart contract type or license.

If your smart contract references different solidity files in its imports (as the one above does), you'll have to either select Single File and upload a flattened version or select Multi-File and upload each solidity file your smart contract references.

To flatten your contract, right-click the filename on your Remix.IDE file explorer, and choose flatten. Remix.IDE will generate a flattened version of your smart contract (e.g., MyToken_flat.sol) that you can paste into Etherscan. Otherwise, you'll need to select Multi-File and import both the MyToken.sol and @openzeppelin/contracts/token/ERC20/ERC20.sol files.

Method 2: Verify via Hardhat

Hardhat is a popular smart contract development framework, and it's fairly simple to verify your source code. Note that you'll need an Etherscan API Key to verify your source code.

First, create a Hardhat boilerplate project, then within the project directory, install the plug-in:

npm install --save-dev @nomiclabs/hardhat-etherscan

Then, add the following import at the top of your hardhat.config.js file:

require("@nomiclabs/hardhat-etherscan");

Note, TypeScript uses the following import: import "@nomiclabs/hardhat-etherscan";

Update your hardhat.config.js file to include the etherscan object and your Etherscan API key.

module.exports = {
networks: {
goerli: { ... }
},
etherscan: {
apiKey: "YOUR_ETHERSCAN_API_KEY"
}
};

Then, run the following command to verify your source code:

npx hardhat verify --network goerli DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

If you are using complex constructor arguments, reference the following Hardhat Documentation.

Method 3: Verify via Remix.IDE / Etherscan Plug-in

With Remix.IDE, you can install the Etherscan plug-in to verify your smart contract via the browser.

Once the contracts are compiled, go to the Etherscan plug-in, and you can select the smart contract you wish to verify. Then, include any constructor arguments if required and click verify.

Different Ways to Verify Your Smart Contract Code | QuickNode (3)

Method 4: Verify via Brownie

If you use Brownie to deploy and test your smart contracts, you can easily apply the following techniques to verify your smart contract source code. If you're new to Brownie, check out this introductory guide on How to deploy a smart contract with Brownie.

To verify a contract upon deployment, add the publish_source=True argument:

acct = accounts.load('deployment_account')
Token.deploy("MyToken", "MTK", 18, 1e28, {'from': acct}, publish_source=True)

Alternatively, if the contract is already deployed, you can use the following command:

token = Token.at("0x114A107C1931de1d5023594B14fc19d077FC4dfD")
Token.publish_source(token)

More reference material can be found on the Brownie docs.

Method 5: Verify via Foundry

If you are using Foundry to manage your smart contract project, verifying your smart contracts on Etherscan is simple.

You can add the flag --etherscan-api-key YOUR_ETHERSCAN_API_KEY --verify to your deployment command to verify your source code on Etherscan right after contract deployment. For example:

forge script script/DeployToken.s.sol --rpc-url QUICKNODE_HTTP_URL --private-key PRIVATE_KEY --broadcast --etherscan-api-key YOUR_ETHERSCAN_API_KEY --verify

In the command above, we are referencing an example terminal command that will execute a deployment script with the given flags. The QUICKNODE_HTTP_URL should be replaced with your actual QuickNode HTTP Provider URL. Additionally, the PRIVATE_KEY should reference a private key, and lastly, replace the YOUR_ETHERSCAN_API_KEY placeholder with your real Etherscan API key. Reference the documentation for more information.

Alternatively, if you want to verify a smart contract that has already been deployed, you can use the forge verify-contract command:

forge verify-contract --etherscan-api-key YOUR_ETHERSCAN_API_KEY CONTRACT_ADDRESS PATH_TO_CONTRACT:CONTRACT_NAME --chain CHAIN_ID

In the command above, we are reference again our Etherscan API with the YOUR_ETHERSCAN_API_KEY placeholder, as well as the CONTRACT_ADDRESS, path to the directory and contract name (e.g., src/MyToken.sol:MyToken) and the chain ID (e.g., 11155111 or sepolia).

An example response would look like this:

Submitting verification for [src/MyToken.sol:MyToken] "0x39919853588384E093B12BaEAaBB5e03d4DC765a".
Submitted contract for verification:
Response: `OK`
GUID: `vdgcv4ti9wsjc5pdphbzj1pjuybxwajrmtv8eudzaik9ap9b3p`
URL:
https://sepolia.etherscan.io/address/0x39919853588384e093b12baeaabb5e03d4dc765a

Note that you can also use forge-verify-check to check the verification status of a smart contract on a given provider (e.g., Etherscan).

For more information, reference the Foundry documentation here. Feel free to also check out our different Foundry guides across different experience levels!

Troubleshooting techniques

When encountering issues with verifying smart contract source code, there are a few troubleshooting techniques that you can use:

  1. Double-check the contract address: Ensure you verify the correct contract address. If you have deployed multiple contracts, ensure you are verifying the correct contract.

  2. Check the compiler version: Ensure that the compiler version used to compile the source code matches the version specified in the verification tool. If the compiler version differs, you may need to recompile the source code using the correct version.

  3. Check the source code: Ensure that the source code you verify matches the deployed contract. You will need to recompile and redeploy the contract if there are any differences.

  4. Check the settings and parameters: Some verification tools require specific settings and parameters to be set correctly. Double-check the settings and parameters to ensure that they are correct. If your smart contract used optimization during compilation, you must ensure you configure this setting in Etherscan.

  5. Check that the constructor arguments are correct. If your contract used a constructor during deployment, you'll need to add this during the verification process.

  6. Check the error message: If you receive an error message, read it carefully and try to understand the problem. The error message may provide clues to the root cause of the issue.

  7. Check for help and documentation: Many smart contract development tools have documentation, forums, and other resources available to help troubleshoot issues. Check these resources for guidance or seek help from the community.

By using these troubleshooting techniques, you can identify and resolve issues when verifying smart contract source code and ensure that your code is secure and reliable.

Final Thoughts

Congrats, you now know the different ways to verify smart contract source code using various programming languages and software libraries. Share your verified smart contract with us on Discord or Twitter!

We ❤️ Feedback!

Let us know if you have any feedback or requests for new topics. We'd love to hear from you.

Different Ways to Verify Your Smart Contract Code | QuickNode (2024)

FAQs

How to verify smart contract code? ›

Verifying a smart contract basically involves the following steps:
  1. Input the source files and compilation settings to a compiler.
  2. Compiler outputs the bytecode of the contract.
  3. Get the bytecode of the deployed contract at a given address.
  4. Compare the deployed bytecode with the recompiled bytecode.
Nov 23, 2023

How to check if a smart contract is legit? ›

Look up the address on the relevant block explorer.

MetaMask will also show you the smart contract's address before you sign any transaction. Input the address into a block explorer's search bar. Many of these, including Etherscan, will tell you if the code is verified or not, as highlighted below.

How do I verify my smart contract Etherscan API? ›

  1. Using the Verify Contract Endpoint. In Postman, set your request method to HTTP POST and your URL to https://api.etherscan.io/api . ...
  2. Specify Your chainId. Select the chain you've deployed your contract, which is supported by an Etherscan-like explorer. ...
  3. Add Contract Source Code. ...
  4. Add Contract Metadata. ...
  5. Submitting Verification.
May 6, 2024

How do you test smart contracts? ›

There are three main types of testing methods for smart contracts: unit testing, integration testing, and end-to-end testing. Unit testing means testing each function or component of your smart contract individually, to ensure that they work as expected and handle errors properly.

Can you view smart contract code? ›

Blockchain has a culture of transparency, and generally speaking the coding for smart contract functions will be published for anyone to review and read. So, if you are into coding, you can check the contract code by clicking on “Contract” in the same tab where you checked the contract transactions.

How do I know if my contract is valid? ›

It is vital that a contract meet all the requirements for a contract to be valid. Usually, this involves certain key elements, including clearly defined terms (terms of the contract), mutual agreement among parties of sound mind, and legality, meaning that the agreement cannot relate to illegal activities.

Are smart contracts risky? ›

Security Flaws and Loopholes

Security flaws, such as reentrancy attacks or overflow/underflow bugs, pose serious threats to smart contracts. These vulnerabilities can be exploited by attackers, leading to unauthorized access or manipulation of contract functions.

How is a smart contract identified? ›

Smart contracts do not contain the legal language or even the terms of a contract between two parties. They are scripts that contain functions, module imports, and other programming that automate the actions between two parties. Many of Szabo's predictions in the paper came true in ways preceding blockchain technology.

How do you check if an address is a smart contract? ›

Method 2: Detecting if an address is a smart contract with code. length. The recommended way for a smart contract to test if an address is a smart contract is to measure the size of its bytecode. If an address has bytecode, then it is a smart contract.

Can you read a smart contract? ›

There are tools available that can assist you in reading smart contract data. To use these tools, you first need to locate the smart contract address for the project you're examining. This can typically be found on a project's official website, white paper, or marketplace listing page.

What is the smart contract code? ›

What is a Smart Contract? Smart Contracts 📝 are simple programs stored on a blockchain network. You can say it's like an agreement between two people in the form of computer code. The transactions in a smart contract are processed by the blockchain and stored as a 42 character hex address with the prefix "0x" ).

How are smart contracts verified? ›

Smart contract verification involves submitting the source code of your smart contracts to ensure that the deployed bytecode on the blockchain matches the source code provided. Verification is essential for Tenderly's development tooling to work properly.

What frameworks are used in smart contract testing? ›

Truffle, Waffle, Chai, and Mocha are popular testing tools.
  • Reentrancy and front-running are common smart contract problems.
  • Unit tests and integration tests are the two types of smart contract testing.
  • Truffle, Waffle, Chai, and Mocha are popular testing tools.

How do I check my smart contract in Eth? ›

In Ethereum, the easiest way to determine whether an address is a contract is by going to Etherscan and pasting the address. Search for the Address: In the search bar, enter the Ethereum contract address you want to check. And then click on search.

How do I run a smart contract code? ›

To deploy your smart contract, go to the “Deploy & Run Transactions” tab, and select “IncrementDecrement” from the dropdown menu. In the “Environment” dropdown, select the network you want to deploy your contract to (e.g., “Remix VM” for a local testing network or “Injected Web3” for the main Ethereum network).

Is smart contract code public? ›

The code that defines the smart contract is public, as all data on the blockchain is public.

How to verify an NFT contract? ›

You can use a contract code verification tool like Sourcify to associate source code with any deployed contract. This will verify that the payload matches what's on the blockchain and then associate the payload with the contract. Once contracts are verified, the code can be viewed on the Palm network Network Details.

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6353

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.