Verifying Contracts

Different ways to verify your contracts on Mantle Network

Verifying a contract means making its source code public, along with the compiler settings you used, which allows anyone to compile it and compare the generated bytecode with the one that is deployed on-chain.

Using Hardhat

To verify your contracts using Hardhat, you'll need an Etherscan API key and custom network configuration in your hardhat.config.ts file. Feel free to refer to the configuration from Mantle Hardhat Starter Kit.

You'll need to modify the hardhat.config.ts configuration to include customChains before moving forward.

const config: HardhatUserConfig = {
    solidity: "0.8.19", // solidity version
    defaultNetwork: "mantleTest",
    networks: {
        mantle: {
        url: "https://rpc.mantle.xyz", //mainnet
        accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? ''],
        },
        mantleTest: {
        url: "https://rpc.testnet.mantle.xyz", // testnet
        accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? '']
        }
    },
    etherscan: {
        apiKey: process.env.API_KEY,
        customChains: [
            {
                network: "mantleTest",
                chainId: 5001,
                urls: {
                apiURL: "https://explorer.testnet.mantle.xyz/api",
                browserURL: "https://explorer.testnet.mantle.xyz"
                }
            }
        ]
    },
};

Run the following command to verify the contract located in the "contracts" directory:

npx hardhat verify --network <network> DEPLOYED_CONTRACT_ADDRESS "Constructor arguments"

Here's a sample request and the command line output it produces:

npx hardhat verify --network mantleTest 0x4ECd62E55...
Successfully submitted source code for contract
contracts/Storage.sol:Storage at 0x4ECd62E55...
for verification on the block explorer. Waiting for verification result...

Successfully verified contract Storage on the block explorer.
https://explorer.testnet.mantle.xyz/address/0x4ECd62E554dF15d3E9A69b97A127A89155E84E18#code

If you see an error that says "Hardhat found multiple contracts in the project...", see the fix on the troubleshooting page.

Using Foundry

Use the following forge command to verify the contract you've deployed on Mantle Testnet:

   forge verify-contract --verifier blockscout --watch \
   --verifier-url "https://explorer.testnet.mantle.xyz/api?module=contract&action=verify" \
   --compiler-version "v0.8.19+commit.7dd6d404" \
   --num-of-optimizations 200 \
   --constructor-args $(cast abi-encode <constructor> <param>)
   --chain 5001 \
   <your contract address> <path>:<contract>

Using Remix IDE

  1. Select the "Injected Provider - MetaMask" environment to connect to Mantle Testnet under the "Deploy and Run Transactions" tab

Obtaining Constructor Parameters

Consider the following sample contract code:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Name {
    constructor(string memory name) {}
}

In order to obtain the hex encoded constructor parameters, first you'll need to compile your code. Then, navigate to the "Deploy and Run Transactions" tab, enter the string parameter in the field next to the "Deploy" button, and then bring up the drop-down menu.

Using Explorer

There are multiple methods of verification available via Mantle Explorer.

Let's go over the steps involved in using the most popular methods.

Via Flattened Source Code

You can use Remix, or any other tool, to flatten your contract code.

Flatten Using Remix

  1. In Remix, right click on the contract file and click on "Flatten".

  1. Flattening a contract with imports will generate a new Solidity file with the flattened code that you can copy, as shown below.

Flatten Using Hardhat

If you're using Hardhat to flatten your contract, you can use the following command:

npx hardhat flatten contracts/Airdrop.sol > asAirdropFlatten.sol

You can copy the flattened contract code from the newly generated file.

Flatten Using Foundry

If you're using Foundry to flatten your contract, you can use the following command:

forge flatten --output src/Storage.flattened.sol src/Storage.sol

You can copy the flattened contract code from the newly generated file.

Plugging Flattened Code Into Explorer

Once you have obtained the flattened code, fill in the contract address, contract name, the flattened code, and other fields on Mantle Explorer and proceed with verifying your contract.

Make sure to select the correct EVM version

Via Standard Input JSON

You can obtain the standard input JSON file by compiling your contract using any of the tools we've used so far. Let's see where the input value code can be found in each case.

Compiled Using Remix

You can paste and save the copied JSON code in a separate file that can later be imported to the Explorer.

Compiled Using Hardhat

Once you've successfully compiled your contract code, you'll be able to find a JSON file (with a long name in hexadecimal) in the "../artifacts/build-info" directory, as shown below.

Within this JSON file, you'll need to copy the input field, as shown below, and then paste the copied code into a new empty JSON file. You can import this file to the Explorer later.

Compiled Using Foundry

With Foundry, you'll need to run the following command to obtain the input and output information for your contract code.

forge build --build-info

You'll then be able to find a JSON file (with a long name in hexadecimal) in the "../out/build-info" directory, as shown below. Copy the input field and paste it in a new empty JSON file, which you can then import to the Explorer.

Importing JSON File to Explorer

Once you've obtained the standard input JSON values in a file, you can import it to the Explorer, as shown below.

Last updated