Ownerable Pattern in Solidity

In this pattern we can define the owner of a contract, which restrict or control the protected methods, also can transfer his ownership to another.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract OwnerAblePattern{
    
    event OwnershipTransfer(
            address indexed previousOwner,
            address indexed newOwner
        );
    
    address owner;
    
    constructor(){
        owner = msg.sender;
    }
    
    modifier onlyOnwner(){
        require(msg.sender == owner,"Only owner Allowed");
        _;
    }
    
    function transferOwnerShip(address newOwner)public onlyOnwner{
        _transferOwnerShip(newOwner);
    }
    
    function _transferOwnerShip(address newOwner) internal {
        require(newOwner != address(0));
        emit OwnershipTransfer(owner, newOwner);
        owner = newOwner;
    }
    
}

By Shabair

Frontend | Blockchain | Smart Contract Developer