【问题标题】:ParserError when verifying Truffle contract on Etherscan在 Etherscan 上验证 Truffle 合约时出现 ParserError
【发布时间】:2018-09-12 22:51:43
【问题描述】:

我有一个已部署到以太坊主网络的合约,合约代码在部署时可以正常工作和编译,我可以使用 metamask/MEW 与合约交互。

但是,当我在 Etherscan 上验证合约时,出现编译错误。

我正在使用 etherscan 的 beta truffle 编译器:https://etherscan.io/verifyContract2

我使用 npm truffle-flattener 将我的所有代码组合在一起

我创建了编码的构造函数参数,使用:https://abi.hashex.org/

然后我在 truffle.js 中使用了优化器,run = 200:

  solc: {
    optimizer: {
      enabled: true,
      runs: 200
    }
  }

然后我使用了下面 JSON 文件中设置的编译器版本:

...
...
  },
  "compiler": {
    "name": "solc",
    "version": "0.4.21+commit.dfe3193c.Emscripten.clang"
  },
  "networks": {
    "1": {
      "events": {},
      "links": {
        "SaleStagesLib": "0x0ea918c7c1bed2358530baad064d361438543067",
        "BytesDeserializer": "0x278cfd9ed99cf90ebe2e1a750b50e5811a81af77"
      },
      "address": "0x3a2c34ba7be06c7104bb642f4ce74dc4c317f373",
      "transactionHash": "0x02e0a895cd3dcf98ee71b9d823d69b5701d6e76bd326757babac1c2bf805ff8d"
    }
  },
  "schemaVersion": "2.0.0",
  "updatedAt": "2018-03-31T14:09:25.579Z"
}

以下在指向 Ownable 合约的 etherscan 中引发 ParseError:

contract Ownable {
  address public owner;

  event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  function Ownable() public {
    owner = msg.sender;
  }

  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }


  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
  function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0));
    emit OwnershipTransferred(owner, newOwner);
    owner = newOwner;
  }

}

错误显示:

myc:150:30: ParserError: Expected token Semicolon got 'LParen'
    emit OwnershipTransferred(owner, newOwner)
                             ^

但是,我想指出,这段代码编译正确,在 truffle 中没有错误,并且我已经成功地与主网上的合约进行了交互。

任何想法为什么编译器会抛出此错误?

【问题讨论】:

  • 您确定您的合约是使用 0.4.21 部署的吗?它似乎迫使编译器降到 0.4.19(在emit 可用之前)
  • 编译器和版本在 build 文件夹中指定,如上图所示 0.4.21 但是,我不确定如何检查?

标签: ethereum solidity truffle ether consensys-truffle


【解决方案1】:

我删除了导致 ParseError 被抛出并且验证工作的“发射”。我相信这可能是 truffle 和 etherscans 编译器版本之间的问题。

【讨论】:

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