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#
- First, you need to apply for a key in OKLink browser.
- After you deploy your contract code, make sure to wait at least one minute before verifying it.
Verify by using plugin (Recommended)#
Example#
- First, install the plugin in your Hardhat project by using the following command:
npm install @okxweb3/hardhat-explorer-verify
- 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;
- 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>
- Once verification is successful, you can view the verification status and the contract code on the OKX Chain blockchain explorer.
- 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#
- Start by creating a Hardhat project and using the Lock contract as an example.
- 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
- Compile your Hardhat contract code and deploy it with this command:
hh run scripts/deploy.js --network xlayer
- 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
Table of contents