【问题标题】:Verify smart contract code deployed with Truffle验证使用 Truffle 部署的智能合约代码
【发布时间】:2018-10-12 18:12:53
【问题描述】:

我正在使用 Truffle 在 Rinkeby 网络上部署智能合约。智能合约包含一个库的导入(Ownable)。

我正在尝试在 Etherscan 上验证合同,但我无法 :(

Truffle 似乎“扁平化”了合约代码,但我找不到用于编译的实际输出。

我检查了构建文件夹,我可以找到字节码和部署字节码,但找不到“扁平化”合约源。

我在哪里可以找到这些信息?

在 Rinkeby 上部署:

michael$ truffle deploy --reset --network rinkeby
Using network 'rinkeby'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0xe179c58d10d66def5d26a06c89848b88c812458f1c2e92bcff40372e6c476f08
  Migrations: 0xa06c5370a513ad9aa25213db9610d77a9533c4c1
Saving successful migration to network...
  ... 0xaa08dbc87a185613854689ffe408e3dc441344191c52194d835124e37a2a4fd1
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing BlockBetGameRegistry...
  ... 0x9bc7e990dc4ef9dd87f5c69c8a65b0e22cbcda10102abc7067fcfb451ca429bc
  BlockBetGameRegistry: 0x7be5198a14ff47815a85adc47bb5f1da31d352e6
Saving successful migration to network...
  ... 0xb942099bc2201d955bf60ce7ecba9edbe2f664b744f8543d43aa5588ff4d2f2f
Saving artifacts...

合同代码:

pragma solidity 0.4.18;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';

contract BlockBetGameRegistry is Ownable {
  address[] public games;

  event eventGameAdded(address game);

  function addGame (address _contractAddress) onlyOwner public {
    require(_contractAddress != address(0));
    games.push(_contractAddress);
    eventGameAdded(_contractAddress);
  }

  function numberOfGames () view public returns (uint256) {
    return games.length;
  }
}

【问题讨论】:

    标签: ethereum solidity truffle


    【解决方案1】:

    正如另一个答案所述,没有本地 Truffle 功能可以帮助解决这个问题。然而,Truffle 团队在今年年初确实发布了插件功能。所以我创建了truffle-plugin-verify 来自动在 Etherscan 上验证 Truffle 合约。


    1. 使用 npm 安装插件
    npm install truffle-plugin-verify
    
    1. 将插件添加到您的truffle.jstruffle-config.js 文件中
    module.exports = {
      /* ... rest of truffle-config */
    
      plugins: [
        'truffle-plugin-verify'
      ]
    }
    
    1. 在您的 Etherscan 帐户上生成 API 密钥(请参阅 Etherscan website
    2. 将您的 Etherscan API 密钥添加到您的 truffle 配置中
    module.exports = {
      /* ... rest of truffle-config */
    
      api_keys: {
        etherscan: 'MY_API_KEY'
      }
    }
    

    将合约迁移到公共网络后,您可以通过运行以下命令在 Etherscan 上对其进行验证:

    truffle run verify ContractName [--network networkName]
    

    更多信息可以在the repository或我的文章Automatically verify Truffle smart contracts on Etherscan中找到。

    【讨论】:

    • 刚用过这个。太棒了。希望我在 17/18 年积极编写/部署/验证合同时能拥有这个!干得好!
    【解决方案2】:

    不幸的是,Truffle 还不支持这一点。它目前是一个开放的功能请求(请参阅feature request)。这似乎是 Truffle 背后的工程师相信支持的一个流行问题,因此实施它可能只是时间问题。

    在此之前,您必须使用一个实用程序来为您扁平化您的代码。 cmets中提到了2个:sol-mergertruffle-flattener

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2022-08-05
      相关资源
      最近更新 更多