Via hardhat plugin

Hardhat is a full-featured development environment for contract compilation, deployment and verification. The Hardhat Etherscan plugin supports contract verification on U2U Chain blockchain explorer.

Sample Hardhat Project

1) Install Hardhat

If you are starting from scratch, create an npm project by going to an empty folder (npm version 7 or higher is recommended):

npm init -y

Once your project is ready:

npm instructions

npm install --save-dev hardhat

2) Create a project

Run npx hardhat init in your project folder and follow the instructions to create (more info here).

If you select Create a Typescript project, a simple project creation wizard will ask you some questions, please proceed with all "y" (yes).

After that, the wizard will create a sample project with some directories and files and install the necessary dependencies. The initialized project has the following structure:

contracts/
scripts/
test/
hardhat.config.ts

Config File

Your basic Hardhat config file (hardhat.config.js or hardhat.config.ts) will be setup to support the network you are working on.

Here we add an RPC url without an API key, however some value is still required. You can use any arbitrary string. More info.

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

const config: HardhatUserConfig = {
  solidity: "0.8.19",
  networks: {
    solaris: {
      url: 'https://rpc-mainnet.uniultra.xyz/',
      accounts: ["YOUR_PRIVATE_KEY"], // it should start with 0x...
    },
    nebulas: {
      url: 'https://rpc-nebulas-testnet.uniultra.xyz/',
      accounts: ["YOUR_PRIVATE_KEY"], // it should start with 0x...
    }
  },
  etherscan: {
    apiKey: {
      solaris: "abc", // arbitrary string
      nebulas: "abc", // arbitrary string
    },
    customChains: [
      {
        network: "solaris",
        chainId: 39,
        urls: {
          apiURL: "https://u2uscan.xyz/api",
          browserURL: "https://u2uscan.xyz"
        }
      },
      {
        network: "nebulas",
        chainId: 2484,
        urls: {
          apiURL: "https://testnet.u2uscan.xyz/api",
          browserURL: "https://testnet.u2uscan.xyz"
        }
      },
    ]
  }
};

export default config;

Deploy and Verify

Deploy

> npx hardhat run scripts/deploy.ts --network solaris

# Contract deployed to: 0x8D51395C76C34d0e11178c1F7EE3219B4b958e30

Verify

You can include constructor arguments with the verify task.

npx hardhat verify --network solaris DEPLOYED_CONTRACT_ADDRESS [ARG_1 ARG_2 ...]

In that:

  • DEPLOYED_CONTRACT_ADDRESS: your deployed contract address

  • [ARG_1 ARG_2 ...]: arguments that have been passed to the constructor or initialize function. More detail please see:

Back to our sample project, you can take a look at contract verification:

> npx hardhat verify --network solaris 0x8D51395C76C34d0e11178c1F7EE3219B4b958e30 1000000000000000

Successfully submitted source code for contract
contracts/Lock.sol:Lock at 0x8D51395C76C34d0e11178c1F7EE3219B4b958e30
for verification on the block explorer. Waiting for verification result...

Successfully verified contract Lock on the block explorer.
https://u2uscan.xyz/address/0x8D51395C76C34d0e11178c1F7EE3219B4b958e30?tab=contract

After the task is successfully executed, you'll see a link to the publicly verified code of your contract.

Resources

Read more: https://hardhat.org/hardhat-runner/docs/guides/verifying

Last updated