@nomiclabs/hardhat-etherscan插件出错:合约验证失败。原因:失败-无法验证-使用参数。

11

我正在尝试验证我的带有参数的合同,但出现了以下错误:

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

我也正在导入Open Zeppelin合约中的ERC721EnumerableOwnable

这是我的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);
    }
}


这是我的 `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();


这是我的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
  }
};

我意识到Hardhat不支持0.8.10+编译器版本,但其他版本也无法正常工作。出现了相同的错误。


建议将此问题转移到Ethereum StackExchange - Paul Razvan Berg
6个回答

6

我认为这并没有回答原问题,但是它回答了我的问题。谢谢! - trliner

4

您之所以出现此错误,是因为部署时使用的参数与验证时不同,请确保在部署和验证时使用相同的参数。


2

对我有用的是改变:

etherscan: {
    apiKey: process.env.ETHERSCAN_KEY
}

to:

etherscan: {
    apiKey: {
        rinkeby:"ETHERSCAN API KEY HERE",
    }
}

1

解决方案

这并不是一个真正的答案,但我的解决方法是同时使用Remix和Hardhat,后者通过VSCode。基本上,通过Remix部署并通过Hardhat进行验证。

因为我使用了库,所以显然需要验证每个文件。打开Zeppelin文件的degen方法...我不建议这样做。我尝试过,还是错了。

猜测:我在我的部署脚本中没有创建一个交易,这很奇怪,因为我的参数似乎是正确的。无论如何...


关于这个问题有任何更新吗?我遇到完全相同的问题,而且很奇怪,因为我尝试了两个以太坊扫描器和富途扫描器。Etherscan 可以工作,而 ftmscan 出现错误。 - Happy Block
请查看下面我回答的 已解决 解决方案。我通过 Remix 发布到区块链,然后通过 Hardhat 在 VSCode 中验证合约。https://hardhat.org/plugins/nomiclabs-hardhat-etherscan.html - solidityguy
具体来说,我使用这个命令并且它可以正常工作:npx hardhat verify --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS --network mainnet - solidityguy

0
我的问题已经通过在“hardhat.config.js”文件中添加自己的代理配置来解决:
// set proxy
const { ProxyAgent, setGlobalDispatcher } = require("undici");
const proxyAgent = new ProxyAgent('http://127.0.0.1:7890'); // change to yours
setGlobalDispatcher(proxyAgent);

我终于成功了: 最终成功验证


0
我刚刚使用了这里的文档,并且使用npx hardhat --network xxxx verify xxxx对我有用。还有一件事是要耐心等待,也许你需要在智能合约部署后几个区块之后执行这个命令。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接