NFT contract analysis (2) - Ownable.sol - 2021.5.10

1. Configuration requirements:

1. Environmental requirements: WeBASE-Front

2. Contract language: Solidity 0.6.10

2. Ownable.sol contract

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;
    }
}

3. Analyze the contract

(1) Import the contract

import "./Context.sol";

Import the Context.sol file in the same directory

(2) Create a contract

abstract contract Ownable is Context {
    
    }

abstract defines the abstract contract Ownable inherits Context

(3) Define contract attributes

address private _owner;

Define the address type private property _owner

(4) Define events

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

Define event OwnershipTransferred parameter: address type index

(5)constructor() {}

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

1. Public constructor, equivalent to Initializing Ownable
2. Create a msgSender property of type address to receive the return value of the _msgSender() function - the address of the person who deployed the contract
3. Assign the value of msgSender to _owner
4 .Create the event OwnershipTransferred(), pass the empty address and msg.sender as parameters to the event

(6)owner() {}

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

1. Virtual function owner()
2. Return value type: address
3. Return value: _owner address of the deployer

(7)onlyOwner(){}

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

1.modifier function modifier
2.require() brackets are the content of the judgment - false or true
3. Determine whether the person who is currently calling the contract is the person who deployed the contract
4. The end of the function modifier adds _;

(8)renounceOwnership() {}

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

1. Calling renounceOwnership() must meet the onlyOwner condition
2. If the call is successful, the condition is met, create the event OwnershipTransferred(); pass in the deployer address and empty address
3. Assign the empty address to _owner - the purpose is to destroy _owner , after making it an empty address, this contract method cannot be called;

(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. Create the function transferOwnership
2. Pass in the address parameter newOwner
3. Only the Owner can call this function if the conditions are met
4. The incoming newOwner address needs to be not an empty address
5. Create the event OwnershipTransferred() and pass in the empowered address, the latest permission address
will The newOwner address is passed to _owner

Fourth, the last article: NFT contract analysis (1) - Context.sol

Interpretation of NFT Contracts (1) - Context.sol

5. The next article: NFT contract analysis (3) - SafeMath.sol

NFT contract analysis (3) - SafeMath.sol

6. Reference related articles

solidity notes (1) - first
solidity notes (2) - second
solidity notes (3) - abstract usage
solidity notes (4) - freezing and transaction properties
solidity notes (5) - event usage
solidity Notes (6) - modifier usage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686807&siteId=291194637