Build on X Layer

Verifying with Hardhat#

There are currently two methods to verify your code on OKLink. You can either use the recommended @okxweb3/hardhat-explorer-verify plugin, or modify the hardhat.config.js file according to Hardhat's official documentation.

Important notes#

  1. First, you need to apply for a key in OKLink browser.
  2. After you deploy your contract code, make sure to wait at least one minute before verifying it.

Verify by using plugin (Recommended)#

Example#

  1. First, install the plugin in your Hardhat project by using the following command:

npm install @okxweb3/hardhat-explorer-verify

  1. In your Hardhat configuration file (usually hardhat.config.js or hardhat.config.ts), import and configure the plugin. ensure that your network configuration and API keys are correctly set. Here is a sample configuration:
import "@nomicfoundation/hardhat-toolbox";
import '@okxweb3/hardhat-explorer-verify';  // Import the plugin

const config: HardhatUserConfig = {
  solidity: "0.8.20",
  sourcify: {
    enabled: true,
  },
  networks: {
    xlayer: {
      url: "https://xlayerrpc.example.com",
      accounts: ["<Your Wallet Private Key>"],
    },
  },
  etherscan: {
     apiKey: '...'
  },
  okxweb3explorer: {
    apiKey: "<Your API Key>",
  }
};

export default config;
  1. After deploying the contracts, use Hardhat to run the verification script. This typically involves running a specific Hardhat task that automatically fetches the contract data and submitting it to the OKX Chain explorer for verification. Here is an example command:

npx hardhat okverify --network xlayer <Your Contract Address>

  1. Once verification is successful, you can view the verification status and the contract code on the OKX Chain blockchain explorer.

  1. Verify TransparentUpgradeableProxy contract

An example command:

npx hardhat okxverify --network xlayer --contract <Contract>:<Name> --proxy <address>

  • --proxy refers to the proxy contract address.
Note
If you are using 897 Contract, you do not need to add --proxy. Instead, replace it with --contract.

Verify by modifying hardhat.config.js (Alternative)#

Example#

  1. Start by creating a Hardhat project and using the Lock contract as an example.
  2. Next, modify the hardhat.config.js file in your project directory with the following changes. For X Layer testnet or mainnet:
module.exports = {
    solidity: "0.8.9",
    networks: {
        xlayer: {
            url: "https://testrpc.xlayer.tech", //or https://rpc.xlayer.tech for mainnet
            accounts: [process.env.PRIVKEY]
        }
    },
    etherscan: {
        apiKey: process.env.ETHERSCAN_KEY,
        customChains: [
            {
                network: "xlayer",
                chainId: 195, //196 for mainnet
                urls: {
                    apiURL: "https://www.oklink.com/api/v5/explorer/contract/verify-source-code-plugin/XLAYER_TESTNET", //or https://www.oklink.com/api/v5/explorer/contract/verify-source-code-plugin/XLAYER for mainnet
                    browserURL: "https://www.oklink.com/xlayer-test" //or https://www.oklink.com/xlayer for mainnet
                }
            }
        ]
    }
};

Replace process.env.PRIVKEY with your own deployment address’s private key, and process.env.ETHERSCAN_KEY can be filled with your OKLink API Key, which can be applied from [My account - API management] on https://www.oklink.com/ for free

  1. Compile your Hardhat contract code and deploy it with this command:
hh run scripts/deploy.js --network xlayer

  1. Wait for one to two minutes, and then verify the contract by running the following command and specifying the contract file you want to verify.
hh verify --contract contracts/Lock.sol:Lock <address> <unlock time> --network xlayer

  1. Check if the contract has been successfully verified by visiting here for testnet, or here for mainnet.