【问题标题】:Error in plugin @nomiclabs/hardhat-etherscan: The contract verification failed. Reason: Fail - Unable to verify - with arguments插件@nomiclabs/hardhat-etherscan 中的错误:合约验证失败。原因:失败 - 无法验证 - 带有参数
【发布时间】:2022-01-20 10:15:36
【问题描述】:

我正在尝试使用参数验证我的合同,但出现此错误:

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+ 编译器版本,但也没有其他版本可以工作。同样的错误。

【问题讨论】:

    标签: solidity ethers.js openzeppelin hardhat etherscan


    【解决方案1】:

    已解决

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

    因为我正在使用库,显然我需要验证每个文件。粘贴每个 Open Zeppelin 文件的退化方式......我不推荐它。我试过了,还是错了。

    怀疑:我没有在我的部署脚本中创建事务,这很奇怪,因为我的参数看起来是正确的。无论如何...

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 2021-11-14
      • 2023-01-16
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多