Tutorials, for Developer

Verifying contracts using Hardhat on Klaytnscope

Overview

Klaytnscope allows you to find data by monitoring network health and statistics of Klaytn blockchain as well as profiling blocks and transactions. Verifying a contract on Klaytnscope is typically recommended when you want to make your smart contract’s source code publicly available and verifiable which are deployed on Klaytn network. This is particularly important for contracts that involve handling user funds or executing critical functions to ensure transparency and trustworthiness. Here are some common scenarios where verifying a contract on Klaytnscope is beneficial:

1. Token Contracts: If you’ve deployed an KIP7/ERC20, KIP17/ERC721, or any other token standard contract, verifying it on Klaytnscope allows users to review the contract’s code and confirm its functionality and security.

2. DeFi Contracts: Contracts used in decentralized finance (DeFi) applications, such as lending protocols, decentralized exchanges (DEXs), yield farming protocols, or liquidity pools, should be verified to assure users of their integrity and security.

3. Crowdsale/ICO Contracts: If you’re conducting a token sale or initial coin offering (ICO), verifying the contract on Klaytnscope provides transparency to potential investors by allowing them to inspect the code and understand the token distribution mechanism.

4. Multi-signature Wallets: Contracts managing multi-signature wallets, where multiple parties must approve transactions, should be verified to ensure that the transaction logic is correct and the funds are secure.

5. Governance Contracts: Contracts responsible for on-chain governance, such as voting on proposals or protocol upgrades, should be verified to demonstrate the transparency and fairness of the governance process.

6. Escrow Contracts: Contracts handling escrow services for transactions or agreements between parties should be verified to show that the terms and conditions are enforced as intended.

7. Game Contracts: Smart contracts powering blockchain-based games should be verified to confirm the fairness of the game mechanics and to prevent potential exploits.

8. Any Contract Handling User Funds or Sensitive Operations: In general, any contract that handles user funds or performs critical operations should be verified on Klaytnscope to build trust with users and auditors.

Verifying your contract on Klaytnscope is a good practice to enhance transparency and credibility, especially in decentralized applications where users rely on the correctness and security of the underlying smart contracts.

Prerequisites

  • Node.js and its package manager, NPM, version 18. Verify Node.js is installed by running the following terminal command: node -v and npm -v

Verifying Klaytnscope Contracts with Hardhat

Here’s a step-by-step guide to verify your smart contracts on Klaytnscope using Hardhat for Baobab network. Similar steps can be followed to verify contracts for Klaytn cypress mainnet.

1.Setup the project folder
a. mkdir contractverification 

2. Install Hardhat: if you haven’t already installed Hardhat globally using npm:
a. npm install -g hardhat

3. Initialize hardhat project. Navigate to the project directory and run.
a. cd contractverification
b. npx hardhat
c. Follow the prompts to setup Hardhat project. Choose appropriate options based on your project’s needs.

4. Setup network details and private key
a. Run `export PRIVATE_KEY=<your_private_key_here>` . Make sure configured account has some test klay (Faucet: https://baobab.wallet.klaytn.foundation/faucet )
b. Replace the code in your `hardhat.config.js` file with the code below for baobab network. (Refer to configuration-for-hardhat-verify-plugin for Cypress Mainnet)

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


const PRIVATE_KEY = process.env.PRIVATE_KEY || "";


// 1. Mainnet Cypress configuration details https://docs.klaytnscope.com/contract/configuration-for-hardhat-verify-plugin
// 2. RPC details can be found https://docs.klaytn.foundation/docs/references/service-providers/public-en/


/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
 solidity: "0.8.24",
 networks: {
   klaytn: {
     chainId: 1001,
     url: "https://public-en-baobab.klaytn.net",
     accounts:[PRIVATE_KEY]
   },
 },
 etherscan: {
   apiKey: {
     klaytn: "unnecessary",
   },
   customChains: [
     {
       network: "klaytn",
       chainId: 1001,
       urls: {
         apiURL: "https://api-baobab.klaytnscope.com/api",
         browserURL: "https://baobab.klaytnscope.com",
       },
     },
   ]
 },
 sourcify: {
   enabled: false
 }
}

5. Compile contracts: Once your project is initialized, compile your contracts using Hardhat
a. npx hardhat compile
– This command will compile your solidity contracts from the contracts folder and generate necessary artifacts in `artifacts/` directory.

6. Deploy contracts: Deploy your contracts to the Klaytn network using Hardhat. You’ll need to write deployment scripts in the `scripts/` directory or use existing ones.
a. npx hardhat run scripts/deploy.js –network <network>
b. Ex: npx hardhat run scripts/deploy.js –network klaytn

*Output:

Lock with 0.001ETH and unlock timestamp 1709729354 deployed to 0x131b54E65c99d34BCA738F29051fDAceEa91C969

7. Run verify command
a. npx hardhat verify –network <network> <deployed_address> <parameters>
b. Ex: npx hardhat verify –network klaytn 0x131b54E65c99d34BCA738F29051fDAceEa91C969 1000000000000000

*Output:

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


Successfully verified contract Lock on the block explorer.
https://baobab.klaytnscope.com/address/0x131b54E65c99d34BCA738F29051fDAceEa91C969#code

Conclusion

The demo project can be found here: https://github.com/klaytn/examples/tree/main/tools/klaytnscope/hardhat-contract-verification