Error in plugin @nomiclabs/hardhat-etherscan: The contract verification failed. Reason: Fail - Unable to verify - with arguments
Asked Answered
S

6

11

I am trying to verify my contract with arguments and I am getting this error:

Error in plugin @nomiclabs/hardhat-etherscan: The contract verification failed.
Reason: Fail - Unable to verify

I am also importing Open Zeppelin contracts ERC721Enumerable and Ownable.

Here's my NFTCollectible.sol


pragma solidity 0.8.10;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";

contract NFTCollectible is ERC721Enumerable, Ownable {
    using Strings for uint256;

    string public baseURI;
    string public baseExtension = ".json";
    uint256 public cost = 0.08 ether;
    uint256 public maxSupply = 5000;
    uint256 public maxMintAmount = 25;
    mapping(address => uint256) public addressMintedBalance;

    constructor(
        string memory _name,
        string memory _symbol,
        string memory _initBaseURI,
        string memory _initNotRevealedUri
    ) ERC721(_name, _symbol) {
        setBaseURI(_initBaseURI);
        setNotRevealedURI(_initNotRevealedUri);
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return baseURI;
    }

    function mint(uint256 _mintAmount) public payable {
        require(!paused, "the contract is paused");
        uint256 supply = totalSupply();
        require(_mintAmount > 0, "need to mint at least 1 NFT");
    }

    function walletOfOwner(address _owner)
        public
        view
        returns (uint256[] memory)
    {
        uint256 ownerTokenCount = balanceOf(_owner);
        uint256[] memory tokenIds = new uint256[](ownerTokenCount);
        for (uint256 i; i < ownerTokenCount; i++) {
            tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokenIds;
    }

    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );

        string memory currentBaseURI = _baseURI();
        return
            bytes(currentBaseURI).length > 0
                ? string(
                    abi.encodePacked(
                        currentBaseURI,
                        tokenId.toString(),
                        baseExtension
                    )
                )
                : "";
    }

    function setCost(uint256 _newCost) public onlyOwner {
        cost = _newCost;
    }

    function setBaseURI(string memory _newBaseURI) public onlyOwner {
        baseURI = _newBaseURI;
    }

    function setBaseExtension(string memory _newBaseExtension)
        public
        onlyOwner
    {
        baseExtension = _newBaseExtension;
    }

    function withdraw() public payable onlyOwner {
        (bool me, ) = payable(owner())
            .call{value: address(this).balance}("");
        require(me);
    }
}

Here's my deploy.js

const main = async () => {
  const nftContractFactory = await hre.ethers.getContractFactory('NFTCollectible');
  const nftContract = await nftContractFactory.deploy(
      "NFTCollectible",
      "NFTC",
      "ipfs://CID",
      "https://www.example.com"
  );
  await nftContract.deployed();
  console.log("Contract deployed to:", nftContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.log(error);
    process.exit(1);
  }
};

runMain();

Here's my hardhat.config

require('@nomiclabs/hardhat-waffle');
require('@nomiclabs/hardhat-etherscan');
require('dotenv').config();

module.exports = {
  solidity: '0.8.10',
  networks: {
    rinkeby: {
      url: process.env.STAGING_ALCHEMY_KEY,
      accounts: [process.env.PRIVATE_KEY],
    },
    mainnet: {
      chainId: 1,
      url: process.env.PROD_ALCHEMY_KEY,
      accounts: [process.env.PRIVATE_KEY],
      gasPrice: 3000000
    },
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_KEY
  }
};

I realize Hardhat does not support 0.8.10+ compiler version, but no other versions work either. Same error.

Sing answered 17/12, 2021 at 13:32 Comment(1)
Suggestion to transfer this question to the Ethereum StackExchange.Flyer
B
6

You need to pass in the constructor into the command line.

hh verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

Here's the documentation. https://hardhat.org/plugins/nomiclabs-hardhat-etherscan.html

Beulabeulah answered 23/3, 2022 at 2:19 Comment(1)
I don't think this answered the original question, but it answered mine. Thanks!Divagate
W
4

You have this error because you are not using the same arguments that you used during the deployment, be sure to use the same arguments at deployment as at verification

Wortham answered 14/4, 2022 at 10:18 Comment(0)
D
2

What worked for me is changing:

etherscan: {
    apiKey: process.env.ETHERSCAN_KEY
}

to:

etherscan: {
    apiKey: {
        rinkeby:"ETHERSCAN API KEY HERE",
    }
}
Dominions answered 18/8, 2022 at 9:12 Comment(0)
S
1

SOLVED

This isn't really an answer, but my solve was to use both Remix and Hardhat, the latter via VSCode. Basically, deploy via Remix and verify through Hardhat.

Because I'm using libraries, I need to verify every file, obviously. The degen way of pasting every Open Zeppelin file...I don't recommend it. I tried and still got it wrong.

Suspicion: I was not creating a transaction in my deploy script, which is weird because my params seem correct. In any event...

Sing answered 18/12, 2021 at 12:51 Comment(3)
Any updates regarding this issue? I'm having exactly the same problem and it's weird because I try 2 setups with etherscan and ftmscan. Etherscan works, and ftmscan errors out.Hood
See SOLVED solution below that I answered. I publish to the blockchain on Remix and then verify the contract in VSCode via Hardhat. hardhat.org/plugins/nomiclabs-hardhat-etherscan.htmlSing
Specifically, I use this command and it works npx hardhat verify --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS --network mainnetSing
T
0

My issue is resolved by simply adding my own proxy config in the "hardhat.config.js" file:

// set proxy
const { ProxyAgent, setGlobalDispatcher } = require("undici");
const proxyAgent = new ProxyAgent('http://127.0.0.1:7890'); // change to yours
setGlobalDispatcher(proxyAgent);

I got success finally: finally successfully verified

Tamworth answered 3/7, 2022 at 8:49 Comment(0)
A
0

I just used the docs here and using the npx hardhat --network xxxx verify xxxx worked for me. One more thing is be patient maybe you need to execute this after a few block since the Smart contract deployment.

Assignable answered 2/2, 2023 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.