使用导入的 OpenZeppelin 文件在 Etherscan 上验证并发布合约

问题描述 投票:0回答:5

我目前正在构建符合 ERC721 标准的合约,并已在此处发布合约:https://ropsten.etherscan.io/address/0xa513bc0a0d3af384fefcd8bbc1cc0c9763307c39 - 我现在正在尝试验证并发布合约源代码

我的文件的开头看起来像这样:

// SPDX-License-Identifier: MIT

// We will be using Solidity version 0.8.4
pragma solidity 0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract ViperToken is ERC721 {

但是,当尝试使用 Solidity 单个文件进行验证和发布时,出现以下错误:

ParserError: Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File import callback not supported
 --> myc:6:1:
  |
6 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol"
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

任何人都可以向我指出 1. 解决此问题或 2. 有关如何正确编写导入了可通过 Etherscan 验证的依赖项的合约的文档。现在这只是一个单文件合同。

ethereum solidity smartcontracts cryptocurrency etherscan
5个回答
12
投票

简而言之,我必须深入研究才能解决这个问题,因为我对 Solidity 还很陌生。

我必须执行以下操作;

  1. 学习并使用 https://www.trufflesuite.com/ 来设置一个项目并将我的合同放在那里(使用 Ganache 对任何刚接触 Solidity 的人进行测试也有很大帮助)
  2. 使用 HD 钱包提供商包并按照此处的教程在 ropsten Etherscan 上获取它 https://medium.com/coinmonks/5-minute-guide-to-deploying-smart-contracts-with-truffle-and-ropsten-b3e30d5ee1e
  3. 最后,使用 truffle-plugin-verify https://github.com/rkalis/truffle-plugin-verify 在 Etherscan 上验证合约

总而言之,我非常确定 Etherscan Web 应用程序中无法验证包含导入文件的合约。

如果有人有兴趣了解我是如何构建这一切的,最终产品就在这里https://github.com/lukecurtis93/viper-nft(这只是我在网上找到的一个 CryptoKitties 克隆作为基础并对其进行了全部更新)


5
投票

如果您正在编译到 REMIX IDE 中

  1. 来自 REMIX IDE

  2. 搜索“Flattener”插件

  3. 右键单击文件 -> 展平 yourcontract.sol

  4. 在 Etherscan 上复制/粘贴


1
投票

我使用

npx hardhat flatten
将所有代码编译到一页中,然后将代码复制粘贴到Etherscan的单文件验证中。我认为如果您只是学习了解如何在 Etherscan 中验证您的智能合约,那很好。但是当涉及到生产级别的代码时,我认为OP的解决方案更好。


0
投票

npx hardhat flatten
尝试在 etherscan 中验证时出现许可证标识符错误。

解决方案是在

hardhat.config.js

中添加以下内容
 task("flat", "Flattens and prints contracts and their dependencies (Resolves licenses)")
  .addOptionalVariadicPositionalParam("files", "The files to flatten", undefined, types.inputFile)
  .setAction(async ({ files }, hre) => {
    let flattened = await hre.run("flatten:get-flattened-sources", { files });
    
    // Remove every line started with "// SPDX-License-Identifier:"
    flattened = flattened.replace(/SPDX-License-Identifier:/gm, "License-Identifier:");
    flattened = `// SPDX-License-Identifier: MIXED\n\n${flattened}`;

    // Remove every line started with "pragma experimental ABIEncoderV2;" except the first one
    flattened = flattened.replace(/pragma experimental ABIEncoderV2;\n/gm, ((i) => (m) => (!i++ ? m : ""))(0));
    console.log(flattened);
  });

然后运行

npx hardhat flat contracts/ContractToFlatten.sol > Flattened.sol


0
投票

对于将来遇到此问题并希望绕过 truffle/hardhat 路由的任何人:请考虑使用 Remix Etherscan 验证插件 (https://remix-ide.readthedocs.io/en/latest/contract_verification.html#etherscan )。或者,您可以手动将所有依赖项上传到 Etherscan,并将导入路径调整为现在与主合约处于同一级别的参考文件。

© www.soinside.com 2019 - 2024. All rights reserved.