准备工作
- 准备好【权限控制】相关代码
- 有足够的金额来支付gas费用
对智能合约有疑问请参考我的另一篇文章《从零教你学习以太坊ERC20智能合约》
相关操作
请在需要拥有【丢弃权限】功能的合约中添加以下代码,deploy完成后执行renounceOwnership()
函数即可完成丢弃权限操作,此时权限地址为0x0000000000000000000000000000000000000000
注:
renounceOwnership()
操作不可挽回!
contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
//构造函数
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
//权限所有人
function owner() public view returns (address) {
return _owner;
}
/**
* 只有管理员可以操作
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* 丢弃权限
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev 将合同的所有权转移到一个新帐户(“newOwner”)。
* 只能由当前所有者调用。
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}