Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: verification in forge create with custom explorer when passing --verifier etherscan with --etherscan-api-key doesn't work #9220

Closed
2 tasks done
antoinedc opened this issue Oct 29, 2024 · 7 comments · Fixed by #9311
Assignees
Labels
A-verification Area: verification Cmd-forge-create Command: forge create Cmd-forge-verify Command: forge verify-contract/check T-bug Type: bug
Milestone

Comments

@antoinedc
Copy link

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 (a428ba6 2024-10-29T00:21:52.576754000Z)

What command(s) is the bug in?

forge create

Operating System

macOS (Apple Silicon)

Describe the bug

When running:

forge create \
  --rpc-url http://localhost:8545 \
  --private-key 0x1234 \
  contracts/UniswapV2Factory.sol:UniswapV2Factory \
  --constructor-args 0xF4E864F40476ac400a5AD55f8eA06c6CCd08597d --legacy \
  --verify \
  --verifier etherscan \
  --verifier-url http://localhost:8888/api \
  --etherscan-api-key anvil-23

I get this ouput:

[⠊] Compiling...
[⠆] Compiling 11 files with Solc 0.5.16
[⠰] Solc 0.5.16 finished in 250.35ms
Compiler run successful!
Error: ETHERSCAN_API_KEY must be set

Setting ETHERSCAN_API_KEY env variable does the same
http://localhost:8888/api is an Etherscan compatible API (https://tryethernal.com)

forge verify-contract works as expected though

@antoinedc antoinedc added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Oct 29, 2024
@github-project-automation github-project-automation bot moved this to Todo in Foundry Oct 29, 2024
@grandizzy
Copy link
Collaborator

grandizzy commented Oct 29, 2024

@antoinedc pls check book on how to add unknown chains in config https://book.getfoundry.sh/reference/config/etherscan#etherscan so smth like this should work

[rpc_endpoints]
anvil = "http://127.0.0.1:8545"

[etherscan]
anvil = { key = "anvil-23", url = "http://localhost:8888/api", chain = "anvil"}
forge create \
  --rpc-url anvil \
  --private-key 0x2a871d... \
  src/Counter.sol:Counter \
  --verify

Deployer: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
Deployed to: 0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35
Transaction hash: 0xa0c56fe7347058f526206c4dbb7ba92ce70306fdf80bc23bcaf7a86d06a3f149
Starting contract verification...
Waiting for etherscan to detect contract deployment...
Start verifying contract `0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35` deployed on anvil-hardhat

going to close for now, please reopen if still issues. thank you!

@grandizzy grandizzy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Oct 29, 2024
@antoinedc
Copy link
Author

Thanks @grandizzy for the reply. I had actually seen that page before, but I'm looking at being able to do the verification without having to create or edit an extra file, directly in the forge create command.

Are there any technical reasons why this can't be supported?

This really does make a difference on my end. Contract verification is obviously very important for any explorer, and so is compatibility with user tooling/workflow. Not being able to be a drop-in replacement to Blockscout or Etherscan hurts adoption. And "gatekeeping" this feature to those two explorers seems a bit too opinionated.

As an example, Hardhat verification system works perfectly with any endpoint (based on Etherscan API-), and users can switch explorers by just changing a value, without having to change anything in their workflow.

@grandizzy
Copy link
Collaborator

grandizzy commented Oct 29, 2024

I see, IMO is easier and cleaner to put an alias in a config file (that could be easily reused in other projects) opposed to change command but I can understand your point.

As an example, Hardhat verification system works perfectly with any endpoint (based on Etherscan API-), and users can switch explorers by just changing a value, without having to change anything in their workflow.

This is valid in the config I put above too, just specify desired URL

anvil = { key = "anvil-23", url = "http://localhost:8888/api", chain = "anvil"}

@zerosnacks @yash-atreya could you please share your thoughts? thanks!

@zerosnacks
Copy link
Member

zerosnacks commented Oct 29, 2024

Hi @antoinedc

I think this issue arises because you are passing the verifier as etherscan forcing a check for ETHERSCAN_API_KEY

In lieu of a more generalized solution I would be open to accept a PR that adds your custom explorer Ethereal as an EtherscanVerificationProvider to here:

#[derive(Clone, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum VerificationProviderType {
#[default]
Etherscan,
Sourcify,
Blockscout,
Oklink,
}
impl VerificationProviderType {
/// Returns the corresponding `VerificationProvider` for the key
pub fn client(&self, key: &Option<String>) -> Result<Box<dyn VerificationProvider>> {
match self {
Self::Etherscan => {
if key.as_ref().map_or(true, |key| key.is_empty()) {
eyre::bail!("ETHERSCAN_API_KEY must be set")
}
Ok(Box::<EtherscanVerificationProvider>::default())
}
Self::Sourcify => Ok(Box::<SourcifyVerificationProvider>::default()),
Self::Blockscout => Ok(Box::<EtherscanVerificationProvider>::default()),
Self::Oklink => Ok(Box::<EtherscanVerificationProvider>::default()),
}
}
}

@antoinedc
Copy link
Author

Hi @zerosnacks,

Shouldn't this check pass if I provide the key? My assumption was that the command would work as long as the verifier-url is an Etherscan compatible endpoint & that an api key is passed.

Thanks for the code snippet, I will take a look at that (have never done any Rust so not sure how it'll go ahah)

@zerosnacks
Copy link
Member

zerosnacks commented Oct 29, 2024

Hi @antoinedc, unfortunately this is due to the incorrect use of the --verifier etherscan. When this is passed a number of assumptions are made in regards to the resolving of the Etherscan configuration using alloy-chains. Due to anvil-hardhat not having a valid Etherscan API URL we fail to resolve this api_url.

cc @grandizzy, this incorrectly casts to None on failure of etherscan_urls()?; resulting in the ETHERSCAN_API_KEY must be set error

return Ok(ResolvedEtherscanConfig::create(key, chain));

let (api_url, browser_url) = chain.etherscan_urls()?;

I think the simplest solution here is to ask alternative explorers to add themselves to the VerificationProviderType and declare that they are compatible with an API rather than us inferring it. It is explicit and standardized.

We should also disallow the --etherscan-api-key flag to be passed if a verifier other than --verifier etherscan is set.

Re-opening as there are some action items

@zerosnacks zerosnacks reopened this Oct 29, 2024
@zerosnacks zerosnacks added A-verification Area: verification Cmd-forge-create Command: forge create Cmd-forge-verify Command: forge verify-contract/check and removed T-needs-triage Type: this issue needs to be labelled labels Oct 29, 2024
@zerosnacks zerosnacks changed the title Verification in forge create with custom explorer doesn't work Verification in forge create with custom explorer when passing --verifier etherscan with --etherscan-api-key doesn't work Oct 29, 2024
@zerosnacks zerosnacks changed the title Verification in forge create with custom explorer when passing --verifier etherscan with --etherscan-api-key doesn't work bug: verification in forge create with custom explorer when passing --verifier etherscan with --etherscan-api-key doesn't work Oct 29, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Oct 29, 2024
@antoinedc
Copy link
Author

When this is passed a number of assumptions are made in regards to the resolving of the Etherscan configuration using alloy-chains. Due to anvil-hardhat not having a valid Etherscan API URL we fail to resolve this api_url.

Aah got it, I see. Thanks for the explanation! From what I've seen in other verifiers, the "method" (ie etherscan, sourcify, etc...), is more about knowing what api format to expect (and then allowing any endpoint) rather than something that will "force" the usage of a specific platform.

@grandizzy grandizzy self-assigned this Nov 18, 2024
@grandizzy grandizzy moved this from Done to Completed in Foundry Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-verification Area: verification Cmd-forge-create Command: forge create Cmd-forge-verify Command: forge verify-contract/check T-bug Type: bug
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

3 participants