wighawag / hardhat-deploy

hardhat deployment plugin
MIT License
1.17k stars 283 forks source link

Incorrect evaluation of the etherscanApiKey when verifying contracts. #502

Closed ivanzhelyazkov closed 6 months ago

ivanzhelyazkov commented 6 months ago

Describe the bug Incorrect evaluation of the etherscanApiKey when verifying contracts.

To Reproduce Steps to reproduce the behavior: Attempt to verify a contract on a different network than mainnet (base, arbitrum, polygon, etc.) with correctly set hardhat configuration (hh.network.verify.etherscan.apiKey) and valid api key leads to error: failed to submit : "NOTOK" : "Invalid API Key"

Expected behavior Should verify contract as expected.

More context The code down below (line 857 in index.ts) evaluates the etherscanApiKey based on 4 fields in the order:

  1. command line argument apiKey
  2. If ETHERSCAN_API_KEY is present
  3. hre.network.verify.etherscan.apiKey
  4. hre.config.verify.etherscan.apiKey

Which means if you have set ETHERSCAN_API_KEY for mainnet, and an api key for another network in the hardhat config, the api key deduced will be the mainnet one and the verification will fail with invalid key.

 .setAction(async (args, hre) => {
    const etherscanApiKey =
      args.apiKey ||
      process.env.ETHERSCAN_API_KEY ||
      hre.network.verify?.etherscan?.apiKey ||
      hre.config.verify?.etherscan?.apiKey;
    if (!etherscanApiKey) {
      throw new Error(
        `No Etherscan API KEY provided. Set it through command line option, in hardhat.config.ts, or by setting the "ETHERSCAN_API_KEY" env variable`
      );
    }

    const solcInputsPath = await deploymentsManager.getSolcInputPath();
    await submitSources(hre, solcInputsPath, {
      contractName: args.contractName,
      etherscanApiKey,
      license: args.license,
      fallbackOnSolcInput: args.solcInput,
      forceLicense: args.forceLicense,
      sleepBetween: args.sleep,
      apiUrl: args.apiUrl || hre.network.verify?.etherscan?.apiUrl,
      writePostData: args.writePostData,
    });
  });

versions

ivanzhelyazkov commented 6 months ago

Fixed docs for the evaluation in #504