我有一个已部署到以太坊主网络的合约,合约代码在部署时可以正常工作和编译,我可以使用 metamask/MEW 与合约交互。
然而,当我去验证 Etherscan 上的合约时,我得到了一个编译错误。
我正在使用 etherscan 的 beta truffle 编译器:https ://etherscan.io/verifyContract2
我使用 npm truffle-flattener 将我的所有代码捆绑在一起
我使用以下方法创建了编码的构造函数参数:https ://abi.hashex.org/
然后我在 truffle.js 中使用了优化器,runs = 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 中没有错误,并且我已经成功地与主网上的合约进行了交互。
任何想法为什么编译器会抛出此错误?