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

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.



Solution 1:[1]

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

Solution 2:[2]

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...

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ferdinand
Solution 2 solidityguy
Solution 3 Chan Wai Hsuen