Verifying a Smart Contract on ApeChain with Hardhat
Introduction
Verifying a smart contract on the blockchain is crucial for ensuring its transparency, security, and facilitating interaction for both users and developers. Hardhat is a versatile tool designed for compiling, testing, deploying, and verifying smart contracts.
This guide will walk you through verifying your smart contract on ApeScan (opens in a new tab) for ApeChain Mainnet and the Blue and Curtis testnets using Hardhat. No API key is needed.
hardhat-verify setup keeps working. Only the explorer URLs change. Hardhat still calls this verifier etherscan in its config and commands; that's the name of the API standard, and with the config below every request goes to ApeScan.Prerequisites
Before you begin the verification process, make sure you have the following:
- Hardhat 3 Installed: This is essential for compiling, deploying, and verifying your smart contract. Using Hardhat 2? See Using Hardhat 2.
- Node.js and NPM: Hardhat operates on Node.js, and NPM (Node Package Manager) is used to install necessary packages.
- Deployed Smart Contract: Ensure your smart contract is already deployed on ApeChain, Blue or Curtis. If it isn't, refer to the guide on Deploying a Smart Contract.
Step 1: Installing Hardhat Verify Package
The @nomicfoundation/hardhat-verify plugin is included in both Hardhat toolboxes (hardhat-toolbox-viem and hardhat-toolbox-mocha-ethers). If you are not using a toolbox, install it in your project's root directory:
npm install --save-dev @nomicfoundation/hardhat-verifyStep 2: Configuring Hardhat
Next, add the ApeChain networks and the ApeScan URLs to your hardhat.config.ts file. ApeScan doesn't require an API key, but Hardhat refuses an empty one, so the config sets a placeholder.
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { configVariable, defineConfig } from "hardhat/config";
export default defineConfig({
plugins: [hardhatToolboxViemPlugin],
solidity: "0.8.28",
networks: {
apechain: {
type: "http",
url: "https://rpc.apechain.com/http",
accounts: [configVariable("PRIVATE_KEY")],
},
blue: {
type: "http",
url: "https://rpcblue.apechain.com/",
accounts: [configVariable("PRIVATE_KEY")],
},
curtis: {
type: "http",
url: "https://curtis.rpc.caldera.xyz/http",
accounts: [configVariable("PRIVATE_KEY")],
},
},
chainDescriptors: {
33139: {
name: "ApeChain",
blockExplorers: {
etherscan: {
name: "ApeScan",
url: "https://apescan.io",
apiUrl: "https://api.apescan.io/api",
},
},
},
3313939: {
name: "Blue",
blockExplorers: {
etherscan: {
name: "ApeScan",
url: "https://blue.apescan.io",
apiUrl: "https://blue.apechain.com/api",
},
},
},
33111: {
name: "Curtis",
blockExplorers: {
etherscan: {
name: "ApeScan",
url: "https://curtis.apescan.io",
apiUrl: "https://curtis.apechain.com/api",
},
},
},
},
verify: {
etherscan: {
// ApeScan doesn't need an API key; Hardhat just requires a non-empty value.
apiKey: "apescan",
},
},
});configVariable keeps your private key out of the config file. Store it with Hardhat's encrypted keystore:
npx hardhat keystore set PRIVATE_KEYStep 3: Verifying Your Contract
Once your project is configured for verification, you can proceed to verify your smart contract on ApeChain Mainnet or Testnet. Replace <contract-address> with the actual address of your deployed contract, and append any constructor arguments after it.
For Mainnet
npx hardhat verify etherscan --network apechain <contract-address>For Blue Testnet
npx hardhat verify etherscan --network blue <contract-address>For Curtis Testnet
npx hardhat verify etherscan --network curtis <contract-address>This command submits your contract's source code and metadata to ApeScan. Once successfully verified, your contract's source code will be publicly accessible on ApeScan, linked to its on-chain address.
verify etherscan verifies on ApeScan only. Plain npx hardhat verify also submits to Blockscout and Sourcify, and reports a Blockscout error unless you add a blockscout entry to chainDescriptors.Using Hardhat 2
On Hardhat 2, use @nomicfoundation/hardhat-verify@hh2 and add ApeScan as custom chains:
import "@nomicfoundation/hardhat-toolbox";
import type { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
solidity: "0.8.28",
networks: {
apechain: {
url: "https://rpc.apechain.com/http",
accounts: [process.env.PRIVATE_KEY!],
},
blue: {
url: "https://rpcblue.apechain.com/",
accounts: [process.env.PRIVATE_KEY!],
},
curtis: {
url: "https://curtis.rpc.caldera.xyz/http",
accounts: [process.env.PRIVATE_KEY!],
},
},
etherscan: {
// ApeScan doesn't need an API key; Hardhat just requires a non-empty value.
apiKey: {
apechain: "apescan",
blue: "apescan",
curtis: "apescan",
},
customChains: [
{
network: "apechain",
chainId: 33139,
urls: {
apiURL: "https://api.apescan.io/api",
browserURL: "https://apescan.io",
},
},
{
network: "blue",
chainId: 3313939,
urls: {
apiURL: "https://blue.apechain.com/api",
browserURL: "https://blue.apescan.io",
},
},
{
network: "curtis",
chainId: 33111,
urls: {
apiURL: "https://curtis.apechain.com/api",
browserURL: "https://curtis.apescan.io",
},
},
],
},
};
export default config;On Hardhat 2, verify with npx hardhat verify --network apechain <contract-address>.
Conclusion
Verifying your smart contract not only strengthens your project's transparency and credibility but also greatly enhances the user experience by making interactions and verifications more straightforward. By following the steps in this guide, developers can ensure their contracts are verified and available for public scrutiny, contributing to a more secure and trustworthy ecosystem.