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

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"

##ソースコード

```javascript

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.

```javascript

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.

```javascript

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.

```javascript

  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  

```javascript

  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. 

```javascript

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.

```javascript

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.

```javascript

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


##Event
###OwnershipRenounced
Logs and records that the ownership was renounced in the blockchain.

```javascript

event OwnershipRenounced(address indexed previousOwner);
```
###TransferedOwnership
Logs and records that the ownership was transfered.

```javascript

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. 

###ありがとうございました！