Verify Smart Contracts with Foundry Forge

Foundry is a smart contract development toolchain. Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command line and via Solidity scripts.

Forge is a command-line tool that ships with Foundry. Forge tests, builds, and deploys your smart contracts. Forge supports contract verification out of the box (https://www.getfoundry.sh/reference/forge/verify-contract).

Verify contract

In Forge, contracts can be verified at the time of the deployment (e.g. using forge script), or later, if the contracts are already deployed (e.g. using forge verify-contract).

Tips:

  1. Specify the --verifier blockscout flag to use the verification flow for Blockscout-compatible explorers such as OpenScan.AI. An API key is optional for OpenScan.AI verification.
  2. Specify the --verifier-url=https://xdcscan.io/api/ flag to connect to the OpenScan.AI instance for XDC Network.
  3. You can specify most configuration options (e.g. EVM version, disabling optimizations) via the usual Forge configuration.

Example (Deploy and verify)

Verify a contract with OpenScan.AI right after deployment (make sure you add /api/ to the end of the OpenScan.AI explorer URL):

Terminal window
forge create \
--rpc-url <rpc_https_endpoint> \
--private-key $PRIVATE_KEY \
<contract_file>:<contract_name> \
--verify \
--verifier blockscout \
--verifier-url https://xdcscan.io/api/

Or if using Foundry scripts:

Terminal window
forge script <script_file> \
--rpc-url <rpc_https_endpoint> \
--private-key $PRIVATE_KEY \
--broadcast \
--verify \
--verifier blockscout \
--verifier-url https://xdcscan.io/api/

Example (Verify already deployed contract)

Terminal window
forge verify-contract \
--rpc-url <rpc_https_endpoint> \
<address> \
<contract_file>:<contract_name> \
--verifier blockscout \
--verifier-url https://xdcscan.io/api/

Or if using Foundry scripts for the last executed script run:

Terminal window
forge script <script_file> \
--rpc-url <rpc_https_endpoint> \
--private-key $PRIVATE_KEY \
--resume \
--verify \
--verifier blockscout \
--verifier-url https://xdcscan.io/api/