LogoLogo
WebsiteLinksMedia KitGitHub
  • Introduction
  • Sonic
    • Overview
    • S Token
    • Wallets
    • Sonic Gateway
    • Build on Sonic
      • Getting Started
      • Deploy Contracts
      • Verify Contracts
      • Tooling and Infra
      • Contract Addresses
      • Network Parameters
      • Programmatic Gateway
      • Learn Solidity
    • Node Deployment
      • Archive Node
      • Validator Node
      • Node Recovery
    • FAQ
  • Funding
    • Sonic Airdrop
      • Sonic Points
      • Sonic Gems
      • Game Gems
      • Sonic Boom
        • Winners
    • Fee Monetization
      • Apply
      • Dispute
      • FeeM Vault
    • Innovator Fund
    • Sonic & Sodas
  • MIGRATION
    • Overview
    • App Token Migration
    • Migration FAQ
  • Technology
    • Overview
    • Consensus
    • Database Storage
    • Proof of Stake
    • Audits
Powered by GitBook

© 2025 Sonic Labs

On this page
  • Method 1. Hardhat Verification (Recommended)
  • Method 2: Programmatic Verification
  • Method 3: Manual Verification
  • Method 4: Flattened Source
  • Troubleshooting
Export as PDF
  1. Sonic
  2. Build on Sonic

Verify Contracts

PreviousDeploy ContractsNextTooling and Infra

Last updated 4 months ago

Verifying your smart contract makes its source code publicly visible and auditable on the block explorer, creating transparency and trust. Here are the recommended methods to verify contracts on the and the .

— — — — —

Method 1. Hardhat Verification (Recommended)

The most streamlined way to verify contracts is using Hardhat with hardhat-toolbox:

  1. Install Hardhat toolbox:

npm install --save-dev @nomicfoundation/hardhat-toolbox
  1. Configure hardhat.config.js:

require("@nomicfoundation/hardhat-toolbox");

module.exports = {
  solidity: "0.8.26",
  networks: {
    sonic: {
      url: "https://rpc.soniclabs.com",
      chainId: 146,
      accounts: [SONIC_PRIVATE_KEY]
    },
    sonicTestnet: {
      url: "https://rpc.blaze.soniclabs.com",
      chainId: 57054,
      accounts: [SONIC_PRIVATE_KEY]
    }
  },
  etherscan: {
    apiKey: {
      sonic: "YOUR_SONICSCAN_API_KEY",
      sonicTestnet: "YOUR_SONICSCAN_TESTNET_API_KEY" 
    },
    customChains: [
      {
        network: "sonic",
        chainId: 146,
        urls: {
          apiURL: "https://api.sonicscan.org/api",
          browserURL: "https://sonicscan.org"
        }
      },
      {
        network: "sonicTestnet",
        chainId: 57054,
        urls: {
          apiURL: "https://api-testnet.sonicscan.org/api",
          browserURL: "https://testnet.sonicscan.org"
        }
      }
    ]
  }
};
  1. Store your SonicScan API key in a .env file:

API_KEY=your_sonicscan_api_key
  1. Verify your contract:

# For mainnet
npx hardhat verify --network sonic DEPLOYED_CONTRACT_ADDRESS [CONSTRUCTOR_ARGUMENTS]

# For testnet
npx hardhat verify --network sonicTestnet DEPLOYED_CONTRACT_ADDRESS [CONSTRUCTOR_ARGUMENTS]

Method 2: Programmatic Verification

For automated deployments, you can verify contracts programmatically in your deployment scripts:

async function main() {
  // Deploy contract
  const Contract = await ethers.getContractFactory("YourContract");
  const contract = await Contract.deploy(constructorArg1, constructorArg2);
  await contract.waitForDeployment();
  
  console.log("Contract deployed to:", await contract.getAddress());
  
  // Wait for some block confirmations
  await new Promise(resolve => setTimeout(resolve, 30000));
  
  // Verify the contract
  await hre.run("verify:verify", {
    address: await contract.getAddress(),
    constructorArguments: [constructorArg1, constructorArg2]
  });
}

Method 3: Manual Verification

If automated methods fail, you can verify manually through the explorer interface:

  1. Navigate to your contract address

  2. Click the Contract tab and Verify & Publish

  3. Fill in the verification details:

    • Contract address

    • Compiler type (single file recommended)

    • Compiler version (must match deployment)

    • Open-source license

    • Optimization settings (if used during deployment)

  4. If your contract has constructor arguments:

    • Paste them in the Constructor Arguments field

  5. Complete the captcha and submit

Method 4: Flattened Source

For contracts with complex dependencies that fail standard verification:

  1. Install Hardhat flattener:

npm install --save-dev hardhat-flattener
  1. Flatten your contract:

npx hardhat flatten contracts/YourContract.sol > flattened.sol
  1. Clean up the flattened file:

    • Keep only one SPDX license identifier

    • Keep only one pragma statement

    • Use this file for manual verification

Troubleshooting

Common verification issues to check:

  • Compiler version must match deployment exactly

  • Optimization settings must match deployment

  • Constructor arguments must be correctly ABI-encoded

  • Library addresses must be provided if used

  • Source code must match deployed bytecode exactly

  • Flattened files should not have duplicate SPDX/pragma statements

Go to the (or the )

Generate ABI-encoded arguments at e.g.

Sonic explorer
testnet explorer
HashEx
Sonic mainnet explorer
Sonic Blaze testnet explorer
Method 1: Hardhat Verification
Method 2: Programmatic Verification
Method 3: Manual Verification
Method 4: Flattened Source
Troubleshooting