Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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.

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

markusveeyola
こんにちは、マルクスと言って、日本語の学生で、同時に、ITエンジニアとして、アルバイトしています。主にKotlinとC#言語を得意としています。自分自身を理解しているように、新しい学ぶことが好きです。特にIT技術に関して、やりたい事があれば、絶対に集中して、絶え間なくします。学び続けて頑張ります!
https://www.twitter.com/markusveeyola
bit-okutama
外国人ITエンジニア育成を目的とした日本語学校です
http://bit-okutama.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした