foundry: Cannot verify contracts on polygonscan

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (6d94a10 2022-11-04T12:50:41.208303Z)

What command(s) is the bug in?

forge verify-contract --chain-id=137 0x9ddc69d076c67c7e5deeaa1dd39666f2c4a5a7b1 src/Counter.sol:Counter

Operating System

macOS (Apple Silicon)

Describe the bug

Deployed the contract which comes via forge init on polygon mainnet Trying to verify contract on polygonscan, via 2 methods

Fails with “Invalid API Key” when using polygonscan api key

forge verify-contract --chain-id=137 0x9ddc69d076c67c7e5deeaa1dd39666f2c4a5a7b1 src/Counter.sol:Counter
Start verifying contract `0x9ddc69d076c67c7e5deeaa1dd39666f2c4a5a7b1` deployed on polygon

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".
Encountered an error verifying this contract:
Response: `NOTOK`
Details: `Invalid API Key`

Fails with “Etherscan could not detect the deployment” using etherscan api key

forge verify-contract --chain-id=137 0x9ddc69d076c67c7e5deeaa1dd39666f2c4a5a7b1 src/Counter.sol:Counter
Start verifying contract `0x9ddc69d076c67c7e5deeaa1dd39666f2c4a5a7b1` deployed on polygon

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".

Submitting verification for [src/Counter.sol:Counter] "0x9DDc69D076c67C7E5DeEaA1Dd39666f2c4A5A7B1".
Error:
Etherscan could not detect the deployment.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

–etherscan-api-key

Thank you @ryanio now it is working.

@ankitchiplunkar @mattsse This works if you add the polygonscan key at the end of the command. It seems you need the polygonscan key even if it is set in your foundry.toml. This issue may be relevant.

@tkowalczyk try passing the key to the cmd with --etherscan-api-key, it was changed to that recently

I’ve run across this recently as well. Etherscan has different domains and api endpoints.

For Polygon, you have to use the polygonscan.com domain. You need to sign-up there and get an API key. Then, you can pass that API key and a specific verifier url for the right block explorer.

forge verify-contract --verifier-url https://api.polygonscan.com/api/ ADDRESS PATH_TO_CONTRACT $POLYGONSCAN_KEY

Polygon Mainnet: https://api.polygonscan.com/api/ Polygon Mumbai: https://api-testnet.polygonscan.com/api/

You can find this info in the Polygonscan docs. Similar for other chains.

Foundry may route to the right API, but make sure you have a key for that API. I generally pass the URL directly if I’m having issues to make sure it’s going where I expect.