Comment on page
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.
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.
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>
- 1.Start by installing and activating the "Etherscan - Contract Verification" plugin in your Remix IDE from the plugin manager that can be accessed by clicking theicon

- 2.Once activated, you can access the plugin by clicking on theicon. Enter your Etherscan API key and save the setting

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

- 4.Now navigate to the "Etherscan - Contract Verification" tab, select the contract you want to verify, and enter the constructor parameters (in hexadecimal), and the contract address. Then click thebutton

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.

Click on the
button to copy the hex encoded parameter value, and continue with the verification process.

There are multiple methods of verification available via Mantle Explorer.

Let's go over the steps involved in using the most popular methods.
You can use Remix, or any other tool, to flatten your contract code.
- 1.In Remix, right click on the contract file and click on "Flatten".

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

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.
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.
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

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.
The file can be found under the "Solidity Compiler" tab that can be accessed by clicking on the
button. Click on the
button to bring up a menu from where you can copy the "COMPILERINPUT" code, as shown below.



You can paste and save the copied JSON code in a separate file that can later be imported to the Explorer.
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.
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.
Once you've obtained the standard input JSON values in a file, you can import it to the Explorer, as shown below.

Last modified 2mo ago