How to Verify and Publish Solidity Smart Contracts Source Code: Etherscan, PolygonScan, BSCScan
Introduction
Are you the contract creator? Verify and Publish your contract source code today!
Have you ever tried “Verify and Publish” on Etherscan? Have you failed? This tutorial is for you. There is a much easier way than using the UI.
Step 1: Get an API KEY
Register on etherscan.io/register (same for polygonscan.com, bscscan.com, etc).
Once logged in, go to etherscan.io/myapikey clic on Add button and give it a name (optional).
Save the API KEY/token for later use.
Step 2: Install hardhat-etherscan
For the scope of this tutorial, I’ll be verifiying a standard OpenZeppelin ERC20 contract deployed on Ethereum Kovan testnet.
Within your proyect, install hardhat and hardhat-etherscan
npm install --save-dev "hardhat@^2.6.6"
npm install --save-dev @nomiclabs/hardhat-etherscan
Note: Even if you want to verify on a different explorer (PolygonScan, BSCScan, etc) the package will still be hardhat-etherscan, the naming can be confusing. Also, you can install hardhat with -g if you want to use it globally.
If you don’t have it, create a file named hardhat-config.js (or .ts)
If you are using JavaScript, include the following line:
require("@nomiclabs/hardhat-etherscan");
If you are using TypeScript, use this instead:
import "@nomiclabs/hardhat-etherscan";
Then, add etherscan
key to your hardhat-config.js replacing the API KEY you got earlier.
module.exports = {
networks: {
kovan: {
url: "url: "https://kovan.infura.io/v3/{API_KEY}",
accounts": "{PRIVATE_KEY}",
}
},
etherscan: {
url: "https://kovan.etherscan.io",
apiKey: "YOUR_API_KEY"
}
};
Note: Even if you are using any other explorer, the key should still be etherscan (confusing yes, hopefully they update it in the future). If you are using etherscan the url key is not required. If you are using another explorer, update it to the apropiate url.
Step 3: Constructor Arguments
If your contract doesn’t take any arguments, go straight to step 4.
Create a file called arguments.js
(name doesn’t really matters) exporting your constructor arguments in order. In my case, my ERC20 takes name
and symbol
so:
module.exports = [
"Test Token",
"TEST",
];
Note: It must match the values you have deployed your contract with, otherwise verification will fail.
Step 4: Verification
For the last step you’ll need:
- Blockchain Network
- Arguments file (only if your contract constructor takes any)
- Contract name (usually the same as the file).
- Contract file name (relative to the directory of your command line)
- Deployed contract address
Run the following command
npx hardhat verify \
--network {NETWORK} \
--constructor-args arguments.js \
--contract {CONTRACT_FILE_PATH}:{CONTRACT_NAME} \
{CONTRACT_ADDRESS}
Network: the name will vary depending on how you setup your hardhat-config.js. In this case it will be kovan
Constructor args: If your contract doesn’t take any arguments, remove this
Contract: This will be your relative path to the deployed solidity contract file, then :
and the contract name (right after contract
key word in solidity code)
And last but not least the address your contract was deployed on
The full command I’m going to run for my example contract is:
npx hardhat verify \
--network kovan \
--constructor-args arguments.js \
--contract contracts/token/ERC20/ERC20.sol:ERC20 \
0x70390af655c3Ce25f22a08b8480A070d9915281c
And… that’s it! Enjoy your verified badge!
Example contract: https://kovan.etherscan.io/address/0x70390af655c3Ce25f22a08b8480A070d9915281c#code
If you find it useful, please consider leaving feedback on the comments section.