Docs

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:

  1. Hardhat (hardhat verify), through the explorer's Etherscan-compatible API
  2. Foundry (forge verify-contract), through the same API
  3. Sourcify's API directly, with a Standard JSON Input
Chain Chain ID Explorer Verification API (apiURL)
Global L1, mainnet 5031 https://explorer.somnia.network https://explorer.somnia.network/api
Global L1, testnet 50312 https://explorer.testnet.somnia.network https://explorer.testnet.somnia.network/api
Tokyo Subchain, mainnet 50321 https://explorer.somnia.network/tokyo https://explorer.somnia.network/tokyo/api
Tokyo Subchain, testnet 50383 https://explorer.testnet.somnia.network/tokyo https://explorer.testnet.somnia.network/tokyo/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 the Sourcify API doesn't need them. hardhat verify checks them before it submits, so pass them there. Tokyo testnet (50383) is the exception: Sourcify can't find creation transactions there, so pass constructor arguments explicitly on every route.

Note

Recommendation: For production contracts, keep the Standard JSON Input your build produced. It includes full compiler metadata and gives the most reliable results.

Note

Sourcify supports Tokyo testnet (chain 50383), but it can't find creation transactions there, so it can't read constructor arguments automatically. Pass them explicitly when you verify a contract that has constructor arguments.

1. Hardhat Verification

Use the @nomicfoundation/hardhat-verify plugin.

Installation

This config is for Hardhat 2. Hardhat 3 changes the config format, so pin both packages:

npm install --save-dev hardhat@2 @nomicfoundation/hardhat-verify@2

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.

Tokyo mainnet config

Use chain ID 50321, https://explorer.somnia.network/tokyo as the browser URL and https://explorer.somnia.network/tokyo/api as the API URL:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-verify";

const config: HardhatUserConfig = {
  solidity: "0.8.24",
  networks: {
    "somnia-tokyo": {
      url: "https://api.tokyo.infra.mainnet.somnia.network",
      accounts: ["<PRIVATE_KEY>"]
    }
  },
  etherscan: {
    apiKey: {
      // Any value: the explorer doesn't check it
      "somnia-tokyo": "empty"
    },
    customChains: [
      {
        network: "somnia-tokyo",
        chainId: 50321,
        urls: {
          apiURL: "https://explorer.somnia.network/tokyo/api",
          browserURL: "https://explorer.somnia.network/tokyo"
        }
      }
    ]
  },
  sourcify: {
    enabled: false
  }
};

export default config;

Verification command

npx hardhat verify \
  --network somnia-mainnet \
  <DEPLOYED_CONTRACT_ADDRESS> \
  "<CONSTRUCTOR_ARG_1>" "<CONSTRUCTOR_ARG_2>"

On Tokyo mainnet, use --network somnia-tokyo in the same command.

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 \
  --broadcast \
  src/MyContract.sol:MyContract \
  --verify \
  --verifier etherscan \
  --verifier-url https://explorer.somnia.network/api \
  --etherscan-api-key empty

To verify on Tokyo mainnet, point the RPC and the verifier at the /tokyo paths:

forge verify-contract \
  --rpc-url https://api.tokyo.infra.mainnet.somnia.network \
  <DEPLOYED_CONTRACT_ADDRESS> \
  src/MyContract.sol:MyContract \
  --verifier etherscan \
  --verifier-url https://explorer.somnia.network/tokyo/api \
  --etherscan-api-key empty \
  --watch

forge create --verify takes the same two Tokyo values.

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, 50321 for Tokyo mainnet and 50383 for Tokyo testnet (pass the constructor arguments explicitly there). 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.24 vs 0.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.

Copied from docs.somnia.network (somnia-main-docs), until it finds a home in a product's docs.Edit this page

Send feedback

Agents: POST the same to /api/feedback.