NFT合约解析(2)——Ownable.sol——2021.5.10

NFT合约解析(2)——Ownable.sol

  • 一丶配置需求:
    • 1.环境需求:WeBASE-Front
    • 2.合约语言:Solidity 0.6.10
  • 二丶Ownable.sol合约
  • 三丶解读合约
    • (1)导入合约
    • (2)创建合约
    • (3)定义合约属性
    • (4)定义事件
    • (5)constructor() {}
    • (6)owner() {}
    • (7)onlyOwner(){}
    • (8)renounceOwnership() {}
    • (9)transferOwnership() {}
  • 四丶上一篇:NFT合约解读(1)——Context.sol
  • 五丶参考相关文章

一丶配置需求:

1.环境需求:WeBASE-Front

2.合约语言:Solidity 0.6.10

二丶Ownable.sol合约

pragma solidity 0.6.10;

import "./Context.sol";

abstract contract Ownable is Context {
     
    address private _owner;

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

    constructor() public{
     
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    function owner() public view virtual 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);
    }

    function transferOwnership(address newOwner) public virtual onlyOwner {
     
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

三丶解读合约

(1)导入合约

import "./Context.sol";

导入同级目录下的Context.sol文件

(2)创建合约

abstract contract Ownable is Context {
     }

abstract 定义抽象合约Ownable继承Context

(3)定义合约属性

address private _owner;

定义地址类型 私有属性 _owner

(4)定义事件

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

定义事件 OwnershipTransferred 参数:地址类型 索引

(5)constructor() {}

    constructor() public{
     
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

1.公共构造函数,相当于Ownable进行初始化
2.创建一个address类型的 msgSender属性用于接收_msgSender()函数的返回值——部署合约的人的地址
3.将msgSender的值赋值给_owner
4.创建事件OwnershipTransferred(),将空地址和msg.sender作为参数传给事件

(6)owner() {}

    function owner() public view virtual returns (address) {
     
        return _owner;
    }

1.虚函数owner()
2.返回值类型:address
3.返回值:_owner部署者的地址

(7)onlyOwner(){}

    modifier onlyOwner() {
     
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

1.modifier 函数修饰器
2.require()括号内为判定内容——false or true
3.判定当前调用合约的人是否为部署合约的人
4.函数修饰器结束 加_;

(8)renounceOwnership() {}

    function renounceOwnership() public virtual onlyOwner {
     
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

1.调用renounceOwnership()必须满足onlyOwner条件
2.如果成功调用,则符合条件,创建事件 OwnershipTransferred();传入部署者地址和空地址
3.将空地址赋值给_owner——目的是销毁_owner的权限,让其成为空地址后,无法调用此合约方法;

(9)transferOwnership() {}

    function transferOwnership(address newOwner) public virtual onlyOwner {
     
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }

1.创建函数transferOwnership
2.传入地址参数newOwner
3.符合条件onlyOwner才能调用该函数
4.需要传入的newOwner地址不为空地址
5.创建事件OwnershipTransferred()传入赋权地址,最新权限地址
将newOwner地址传给_owner

四丶上一篇:NFT合约解读(1)——Context.sol

NFT合约解读(1)——Context.sol

五丶参考相关文章

solidity笔记(1)——第一篇
solidity笔记(2)——第二篇
solidity笔记(3)——abstract用法
solidity笔记(4)——冻结和交易属性
solidity笔记(5)——event用法
solidity笔记(6)——modifier用法

你可能感兴趣的:(Solidity智能合约开发,智能合约,区块链,比特币,以太坊,dapp)