LoginSignup
3
1

More than 5 years have passed since last update.

OpenZeppelin: Ownableコントラクトの紹介

Last updated at Posted at 2018-10-18

The Ownable contract has always been a part of ERC tokens, but it's also a good idea to understand how it works with the ERC tokens. That's why, I'll be explaining about it in detail, so you would know its importance in deploying your token smart contracts.

こんにちは、わたしはマルクス (http://www.twitter.com/markusveeyola) です。

Let's start discussing!

OpenZeppelinとは

The Ownable contract is part of the OpenZeppelin library, along with the ERC tokens.

You can find the contract here:
https://openzeppelin.org/api/docs/ownership_Ownable.html

OpenZeppelin is a library for writing secure Smart Contracts to Ethereum. OpenZeppelin allows you to build applications using common contract security patterns.

Learn more about OpenZeppelin's libraries here:
https://openzeppelin.org/

Ownableのコントラクト

The very purpose of of the contract, Ownable, is to provide basic authorization control functions which makes it "monitor user permissions"

ソースコード


contract Ownable {
  address private _owner;


  event OwnershipRenounced(address indexed previousOwner);

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


  constructor() public {
    _owner = msg.sender;
  }


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


  modifier onlyOwner() {
    require(isOwner());
    _;
  }


  function isOwner() public view returns(bool) {
    return msg.sender == _owner;
  }


  function renounceOwnership() public onlyOwner {
    emit OwnershipRenounced(_owner);
    _owner = address(0);
  }


  function transferOwnership(address newOwner) public onlyOwner {
    _transferOwnership(newOwner);
  }


  function _transferOwnership(address newOwner) internal {
    require(newOwner != address(0));
    emit OwnershipTransferred(_owner, newOwner);
    _owner = newOwner;
  }
}

You can find the code here:
https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.0.0-rc.2/contracts/ownership/Ownable.sol

You might be wondering how all all these functionalities harmonize with each other to implement a fool-proof ownership for tokens. I'll be explaining all these by dividing the code in 3 parts: Functions, Events, and the Modifier. Starting with:

Modifier

This modifier reverts if called by any account other than the owner. This is what makes the functions secured since it will only be accessible for the owner of the contract. Use this on your functions to implement SuperAdmin functions.


modifier onlyOwner() {
    require(isOwner());
    _;
  }

Functions

transferOwnership

This transfers the ownership of the contract to a new owner. All you have to do is to throw in the new address that will inherit the contract.


function _transferOwnership(address newOwner) internal {
    require(newOwner != address(0));
    emit OwnershipTransferred(_owner, newOwner);
    _owner = newOwner;
  }

constructor

the ownable constructor sets the original owner of the contract to the sender account.


  address private _owner;

  constructor() public {
    _owner = msg.sender;
  }

isOwner

returns a boolean value, it checks the sender (msg.sender) is the owner of the contract. This is a void function


  function isOwner() public view returns(bool) {
    return msg.sender == _owner;
  }

owner

This function simply returns the address of the owner, this is a void function so this can be called directly.


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

renounceOwnership (Owner Function)

Allows the current owner to renounce his control for the contract, this will leave the contract without an owner. With that, it will not be possible to call functions with onlyOwner modifier.


function renounceOwnership() public onlyOwner {
    emit OwnershipRenounced(_owner);
    _owner = address(0);
  }

transferOwnership (Owner Function)

Allows the current owner to transfer his control for the contract to a new owner. Throwing the new address in the parameter will make the address as new owner. The previous owner will no longer be able to do functions with "onlyOwner" modifiers.


function transferOwnership(address newOwner) public onlyOwner {
    _transferOwnership(newOwner);
  }

Event

OwnershipRenounced

Logs and records that the ownership was renounced in the blockchain.


event OwnershipRenounced(address indexed previousOwner);

TransferedOwnership

Logs and records that the ownership was transfered.


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

Conclusion

And we're done, I hope you learned a lot about how the smart contract, Ownable works in your ERC tokens. Since you have realized how the ownership works with this contract, you might get to utilize your tokens a little better now.

ありがとうございました!

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1