我有一个简单的 ERC20 合约,它是 ERC20Detailed、ERC20Mintable 和 Ownable。在部署时我想要:
- 将 Minter 添加到新帐户,传入参数
- 从 Minter 角色中删除部署者
- 将所有权转移到新帐户,在参数中传递
我在另一个名为transferRights()的函数中声明的相同操作
问题是我收到“气体估计失败错误”,这不是因为我没有足够的气体,而是代码中可能存在错误。如果我删除前两个(addMinter,renounceMinter)动作,那么一切都很好(没有警告)。
我已经在 Ropsten 上部署了这个合约,一开始我遇到了同样的错误,但是通过评论前两个操作并再次添加它们,交易在没有任何警告的情况下通过,并且合约按预期工作。
pragma solidity ^0.5.0;
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol";
contract MyToken is ERC20Detailed, ERC20Mintable, Ownable {
uint256 private msgCount;
address constant ETHER = address(0);
mapping(uint256 => string) private message;
constructor(string memory name, string memory symbol, uint8 decimals, address _newOwner) ERC20Detailed(name, symbol, decimals) public {
addMinter(_newOwner);
renounceMinter();
transferOwnership(_newOwner);
}
function doMint(uint256 _amount, address _beneficiary1, address _beneficiary2, address _beneficiary3) public onlyOwner {
require (_amount >= 0);
require(_beneficiary1 != ETHER && _beneficiary2 != ETHER && _beneficiary3 != ETHER);
require(mint(_beneficiary1, _amount.mul(20).div(100)));
require(mint(_beneficiary2, _amount.mul(30).div(100)));
require(mint(_beneficiary3, _amount.mul(50).div(100)));
}
function setMessage(string memory _message) public onlyOwner {
message[msgCount] = _message;
msgCount = msgCount.add(1);
}
function readMessage(uint256 _msgId) public view returns(string memory) {
return message[_msgId];
}
function transferRights(address _newOwner) public onlyOwner {
addMinter(_newOwner);
renounceMinter();
transferOwnership(_newOwner);
}
}
我猜我仍然可以发送交易并进行部署(如上所述,我是在测试网上完成的),即使它说“交易执行可能会失败”,但我想确保代码没有错误。任何反馈将不胜感激。谢谢!
更新 发现问题!在构造函数中,我传递了与用于部署相同的帐户地址。因此,它为我自己添加了 Minter 角色,而我已经拥有了。