Overview
Contract verification publishes your source code on block explorers like Etherscan, allowing users to:- Read contract source code
- Interact with contracts through the explorer UI
- Verify contract behavior matches documentation
- Build trust through transparency
Prerequisites
Before verifying:- API Keys: Get API keys from block explorers
- Deployed Contract: Have the contract address
- Constructor Arguments: Know the exact constructor parameters used
- Compiler Settings: Match the settings used during deployment
Getting API Keys
- Etherscan
- BSCscan
- Polygonscan
- Create account at https://etherscan.io/register
- Go to https://etherscan.io/myapikey
- Create new API key
- Add to
.env:ETHERSCAN_API_KEY=YOUR_KEY
Automatic Verification
Using Hardhat’s verification plugin:Example: Verify DeBridgeGate
Verify DeBridgeGate
Verify Proxy Contracts
For upgradeable contracts (using OpenZeppelin proxies):Verify Proxy and Implementation
Hardhat automatically verifies both proxy and implementation for upgradeable contracts.
Manual Verification
If automatic verification fails, verify manually through the block explorer:Flatten Contract
Create a single file with all imports:Remove duplicate SPDX license identifiers (keep only one at the top).
Go to Block Explorer
Navigate to your deployed contract:
- Etherscan:
https://etherscan.io/address/YOUR_ADDRESS - BSCscan:
https://bscscan.com/address/YOUR_ADDRESS - Polygonscan:
https://polygonscan.com/address/YOUR_ADDRESS
Fill Verification Form
- Compiler Type: Solidity (Single File) or (Standard Json)
- Compiler Version: Match your hardhat.config.ts (e.g., v0.8.17)
- License: BUSL-1.1 or your license
- Paste Source Code: From flattened file
Add Constructor Arguments
If contract has constructor arguments, encode them:Paste the encoded hex in the “Constructor Arguments” field.
Encode Constructor Args
Verification Script
Create a script to verify all contracts:scripts/verify-all.js
Verifying Libraries
If your contracts use libraries, verify them first:Verify Library
Verify with Libraries
libraries.js contains:
libraries.js
Hardhat Configuration
Configure verification inhardhat.config.ts:
hardhat.config.ts
Troubleshooting
Already Verified
Already Verified
Contract is already verified.Solution: This is actually success! The contract is already public.
Bytecode does not match
Bytecode does not match
Compiled bytecode doesn’t match deployed bytecode.Common causes:
- Different compiler version
- Different optimizer settings
- Different Solidity code
- Check compiler version in
hardhat.config.ts - Verify optimizer settings (enabled, runs)
- Ensure source code is identical
- Check if contract is a proxy (verify implementation separately)
Invalid constructor arguments
Invalid constructor arguments
Constructor arguments are incorrectly encoded.Solution: Use ethers to encode:
Too many requests
Too many requests
Hit rate limit on block explorer API.Solution: Wait 1-2 minutes between verification attempts.
Contract source code not published
Contract source code not published
Verification succeeded but source not showing.Solution:
- Wait a few minutes for indexing
- Clear browser cache
- Try a different browser
- Check if you’re on the correct network
Verification Checklist
Before Verification
- Contract deployed successfully
- Have deployment transaction hash
- Know exact constructor arguments
- Have API key configured
During Verification
- Compiler version matches deployment
- Optimizer settings match
- License type is correct
- Constructor arguments encoded properly
Benefits of Verification
- Transparency: Users can review contract logic
- Trust: Proves contract matches documentation
- Usability: Direct interaction through explorer UI
- Debugging: Easier to trace transactions and events
- Integration: Other services can read contract ABI
Related Documentation
Deployment Guide
Deploy contracts to networks
Development Setup
Configure your environment