hardhat-zksync-verify
Description
The @matterlabs/hardhat-zksync-verify
plugin enables smart contract verification on both ZkSync Era Explorer and Etherscan.
It extends Hardhat's functionality to support ZKsync-specific contract verification workflows and integrates seamlessly with the ZKsync Era ecosystem.
Key features:
- Dual verification support for both ZkSync Era Explorer and Etherscan
- Automatic verification endpoint resolution
- Support for contracts with complex constructor arguments
- Programmatic verification capabilities
Resources:
Installation
Prerequisites
- Node.js version 18 or higher
- Hardhat version 2.16.0 or higher
Setup
Install the plugin and its dependencies:
yarn add -D @matterlabs/hardhat-zksync-verify @nomicfoundation/hardhat-verify
Import the package in your hardhat.config.ts
file:
import "@matterlabs/hardhat-zksync-verify";
Configuration
Configuration Options
Option | Type | Description |
---|---|---|
url | string | RPC URL for the ZKsync network |
ethNetwork | string | Ethereum network RPC URL or network identifier |
zksync | boolean | Flag to indicate ZKsync network configuration |
verifyURL | string | (optional) Custom verification endpoint URL |
browserVerifyURL | string | (optional) Custom browser verification URL |
enableVerifyURL | boolean | (optional) Force verification on ZKsync block explorer |
etherscan.apiKey | string | object | Etherscan API key configuration |
Example Configuration
// hardhat.config.ts
import { HardhatUserConfig } from "hardhat/config";
import "@matterlabs/hardhat-zksync-verify";
const config: HardhatUserConfig = {
defaultNetwork: "zkTestnet",
networks: {
zkTestnet: {
url: "https://sepolia.era.zksync.dev",
ethNetwork: "sepolia",
zksync: true
}
},
// Etherscan verification configuration (optional)
etherscan: {
apiKey: "YOUR_API_KEY",
}
};
export default config;
Verification Methods
Starting from version 1.7.0
, the plugin supports both ZKsync Block Explorer
and Etherscan
verification.
Earlier versions only support ZKsync Block Explorer verification.
ZkSync Block Explorer Verification
The plugin automatically verifies contracts on the ZKsync Block Explorer by default. This is the standard verification method when no Etherscan configuration is provided.
// hardhat.config.ts
const config: HardhatUserConfig = {
networks: {
zkTestnet: {
url: "https://sepolia.era.zksync.dev",
ethNetwork: "sepolia",
zksync: true
}
}
};
You can customize the Block Explorer verification behavior using these optional parameters:
verifyURL
: Custom verification API endpointbrowserVerifyURL
: Custom verification result page URLenableVerifyURL
: Force Block Explorer verification even when Etherscan is configured
Etherscan Verification
To enable Etherscan verification, add the Etherscan configuration to your hardhat.config.ts
:
// hardhat.config.ts
const config: HardhatUserConfig = {
networks: {
zkTestnet: {
url: "https://sepolia.era.zksync.dev",
ethNetwork: "sepolia",
zksync: true
}
},
etherscan: {
apiKey: "YOUR_API_KEY",
}
};
The plugin provides pre-configured chain settings for:
zksyncsepolia
: ZKsync Era Sepolia testnetzksyncmainnet
: ZKsync Era Mainnet
To obtain an API key:
- Visit the ZKsync Etherscan
- Create an account or sign in
- Navigate to the API section
- Generate a new API key
enableVerifyURL: true
in your network configuration while having Etherscan API keys configured.Additions
Added Tasks
verify
: Main verification task for contract verificationverify:verify
: Programmatic verification task
Added HRE Extensions
- Verification status checking functionality
- Constructor arguments handling
Commands
yarn hardhat verify [options] <contract-address> [constructor-arguments...]
Options:
--network <network-name>
: (Required) Network to verify the contract on--contract <fully-qualified-name>
: (Optional) Specify contract to verify (format: "contracts/Contract.sol:ContractName")--constructor-args <file>
: (Optional) File with constructor arguments--libraries <library-addresses>
: (Optional) Specify library addresses
Usage
Basic Contract Verification
yarn hardhat verify --network zkTestnet 0x1234567890123456789012345678901234567890
Verification with Constructor Arguments
yarn hardhat verify --network zkTestnet 0x1234567890123456789012345678901234567890 "Constructor arg 1" "Constructor arg 2"
Using Constructor Arguments File
// arguments.js
module.exports = [
"arg1",
"0x242...",
42
];
yarn hardhat verify --network zkTestnet 0x1234567890123456789012345678901234567890 --constructor-args arguments.js
Programmatic Verification
const verificationId = await hre.run("verify:verify", {
address: contractAddress,
contract: "contracts/MyContract.sol:MyContract",
constructorArguments: ["arg1", "arg2"]
});
Verification Status Check
The verification process consists of two steps:
- A verification request is sent to confirm if the given parameters for your contract are correct
- The verification status of that request is checked
Both steps run automatically when you execute the verify
task, and you'll receive a verification request ID for reference.
Troubleshooting
Unknown zksolc Version
If you encounter an "Unknown zksolc version" error:
- This indicates that the verification system doesn't support your compiler version
- Solution: Use a previous version of the compiler until the backend is updated
Common Issues
- Verification Failure
- Ensure contract source matches deployed bytecode exactly
- Check constructor arguments are correct
- Verify compiler version matches deployment
- Network Configuration
- Confirm RPC URLs are accessible
- Verify network configuration matches deployment network
- Check API keys are valid if using Etherscan verification
- Compiler Version Mismatch
- Make sure the compiler version used for deployment matches the verification
- For zksolc compiler, use a supported version as specified in the error message
- API Key Issues
- Verify API key is valid and has not expired
- Ensure you're using the correct API key for the target network
For more help, please refer to the GitHub issues.