At the previous tutorial: Create an ERC20 using Remix, without writing Solidity, we have learned how to deploy contracts with the Remix, and now in this tutorial, we will learn how to verify the deployed contracts by a single file.
Cause many people deploy contracts by the Remix, so in this part, we will use the plugin FLATTENER
in the Remix to help us to package all contracts file into a single file to verify.
In short, all steps are:
- Compile contract
- Flatten contract
- Copy the whole code to verify on the Etherscan
Okay, next let us go through it step by step.
Compile contract
I think you have followed the previous tutorial to deploy the contracts by the Remix, but you should notice the network
, compiler version
and compiler configuration
when you deployed the contract. In this part, what you need to do is just checking the option Auto compile
to compile your contract automatically, as for the another option Enable optimization
, it depends on you, at here, we will use it, so all just like the following:
NOTICE: your contracts should be compiled without any errors.
Flattern contract
We will use the plugin FLATTENER
in the Remix to flatten all contracts into a single file. Firstly, click the Plugin manager
on the left side of the page, then find the plugin FLATTENER
in the Inactive Modules
and click the button Activate
to use it, just like:
Then the plugin FLATTENER
will show at the left side of the page, click it and you will find the blue button Flatten YOUR_CONTRACT.sol
, click it, and then click the bottom button Save YOUR_CONTRACT_flat.sol
to save the flattened file, just like following:
it will ask your permission to save file, just click the Accept
, it will show File saved
, so click on the option in the upper left corner File explorers
and then you will find the flattened file at the bottom, just like
Common Errors:
Cause we have checked the option Auto compile
at the step 1, so when we click the flattened contract file, it will compile automatically. What we need to do is solving the errors after compiling if has. Generally, we will encounter the following two errors:
One is:
ParserError: Multiple SPDX license identifiers found in source file. Use "AND" or "OR" to combine multiple licenses. Please see https://spdx.org for more information. --> contracts/OpenZeppelin.sol
For the current version, you need to remove the duplicate SPDX license manually, only keep one valid SPDX license. Here is some discussion of the issue: Deduplicate or remove SPDX license identifier comments
The other one is:
TypeError: Definition of base has to precede definition of derived contract --> OpenZeppelin_flat.sol:506:30: | 506 | abstract contract Ownable is Context { | ^^^^^^^
As for this, you should adjust the order of the contracts, that is putting the parent contract in front, and put child contract behind.
For example: the wrong code is:
abstract contract Ownable is Context{}
contract Context{}
The right order should be:
contract Context{}
abstract contract Ownable is Context{}
Here is an issue on the github about this error: Wrong contract order
Verify on the Etherscan
Nice! we are only one step away from success. Find the contract you have deployed on the Etherscan, click the option Contract
and then click Verify and Publish
, just like following:
Choose the right Compiler Type
: Solidity(Single file)
Choose the right Compiler Version
: YOUR_DEPLOYED_VERSION
Choose a right License Type
.
just like following:
Click the button Continue
to enter the next page, click the Optimization
if you use it when you deploy, and then paste the flattened source code. Generally, Etherscan will upload constructor arguments automatically, but some times it will upload the wrong constructor arguments due to your constructor arguments are a little complex, so in this case, you can encode arguments by this tool: https://abi.hashex.org/ (if you do not know how to use it, you can have a look at this Verify Timelock contract on BSC | OpenZeppelin Community @Bsc_Talk, thanks for your detailed explanation) Finally, click the button Verify and Publish
, just like following:
After waiting for a while, you can find out your contract has been verified successfully, congratulations! Like following:
This really has so many steps to do by this way, so I recommend you to read this tutorial to verify your contract:
- Verify smart contract inheriting from OpenZeppelin Contracts, this is more easier.