Developer guide / Development Frameworks
Verifying via Explorer
This page explains how to verify smart contracts on Somnia so their source shows on the Somnia Explorer.
How It Works
Verified source lives in Somnia's Sourcify, https://sourcify.prd.platform.somnia.host. The explorer reads it: a verified contract's page has a Contract tab with its source and ABI, and Verified contracts lists them all.
There are 3 ways to verify:
- Hardhat (
hardhat verify), through the explorer's Etherscan-compatible API - Foundry (
forge verify-contract), through the same API - Sourcify's API directly, with a Standard JSON Input
| Network | Chain ID | Explorer | Verification API (apiURL) |
|---|---|---|---|
| Mainnet | 5031 | https://explorer.somnia.network |
https://explorer.somnia.network/api |
| Testnet | 50312 | https://explorer.testnet.somnia.network |
https://explorer.testnet.somnia.network/api |
The explorer passes each submission to Sourcify, which recompiles it and matches the result against the bytecode on chain. No API key is needed: the tools want one, so any value works. Constructor arguments are read from the creation transaction, so you don't have to pass them.
Note
Recommendation: For production contracts, keep the Standard JSON Input your build produced. It includes full compiler metadata and gives the most reliable results.
1. Hardhat Verification
Use the @nomicfoundation/hardhat-verify plugin.
Installation
npm install --save-dev @nomicfoundation/hardhat-verify
Example hardhat.config.ts
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-verify";
const config: HardhatUserConfig = {
solidity: "0.8.24",
networks: {
"somnia-mainnet": {
url: "<SOMNIA_RPC_URL>",
accounts: ["<PRIVATE_KEY>"]
}
},
etherscan: {
apiKey: {
// Any value: the explorer doesn't check it
"somnia-mainnet": "empty"
},
customChains: [
{
network: "somnia-mainnet",
chainId: 5031,
urls: {
apiURL: "https://explorer.somnia.network/api",
browserURL: "https://explorer.somnia.network"
}
}
]
},
sourcify: {
enabled: false
}
};
export default config;
For testnet, use chain ID 50312 and https://explorer.testnet.somnia.network.
Verification command
npx hardhat verify \
--network somnia-mainnet \
<DEPLOYED_CONTRACT_ADDRESS> \
"<CONSTRUCTOR_ARG_1>" "<CONSTRUCTOR_ARG_2>"
2. Foundry Verification
forge verify-contract \
--rpc-url <SOMNIA_RPC_URL> \
<DEPLOYED_CONTRACT_ADDRESS> \
src/MyContract.sol:MyContract \
--verifier etherscan \
--verifier-url https://explorer.somnia.network/api \
--etherscan-api-key empty \
--watch
To verify at deployment time:
forge create \
--rpc-url <SOMNIA_RPC_URL> \
--private-key $PRIVATE_KEY \
src/MyContract.sol:MyContract \
--verify \
--verifier etherscan \
--verifier-url https://explorer.somnia.network/api \
--etherscan-api-key empty
3. Sourcify API
Send the Standard JSON Input your compiler used (Hardhat keeps it in artifacts/build-info/, Foundry with --build-info in out/build-info/):
curl -X POST \
"https://sourcify.prd.platform.somnia.host/v2/verify/5031/<CONTRACT_ADDRESS>" \
--header "Content-Type: application/json" \
--data '{
"stdJsonInput": <STANDARD_JSON_INPUT>,
"compilerVersion": "0.8.24+commit.e11b9ed9",
"contractIdentifier": "contracts/MyContract.sol:MyContract"
}'
The answer is a 202 with a verificationId. Poll it until isJobCompleted is true:
curl "https://sourcify.prd.platform.somnia.host/v2/verify/<VERIFICATION_ID>"
A 409 means the contract is already verified. For testnet, use chain ID 50312 in the path. To check a contract, GET https://sourcify.prd.platform.somnia.host/v2/contract/5031/<CONTRACT_ADDRESS> answers 404 until it is verified.
Troubleshooting
- Compiler version must match exactly (
0.8.24vs0.8.24+commit...matters). - Optimizer settings and EVM version must exactly match deployment settings.
- A failure carries Sourcify's reason after
Fail - Unable to verify. - For multi-file projects, verify from the Standard JSON Input rather than a flattened file.
Useful Links
- Sourcify API: https://docs.sourcify.dev/docs/api/
- Hardhat verify plugin: https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify
Copied from docs.somnia.network (somnia-main-docs), until it finds a home in a product's docs.Edit this page