OpenZeppelinのERC-721を見る：Minterの目的 (MinterRole)

This article is meant to explain how Minters or people who are allowed to mint are determined in the [ERC-721 Token Standard](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md). This article aims to help beginners like me, who are still starting out in learning about Blockchain technology.

## 自己紹介
こんにちは、サム （[@sbenemerito](https://twitter.com/sbenemerito)） です。奥多摩の留学生です。日本語とブロクチェーン技術を勉強しています。２０１８年１０月に日本にフィリピンから来ました。

## 概要
We will be learning about the ERC-721 Non-Fungible Token Standard through [OpenZeppelin's ERC-721 Token Implementation](https://github.com/OpenZeppelin/openzeppelin-solidity/). This article will focus on the discussion of the role of Minters in the ERC-721 ecosystem.

## ERC-721とは
ERC-721はトークン基準。様々資産を管理できます。例えば、CryptoKittiesはERC-721トークン基準で資産を管理しています。

__記事__
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md

## OpenZeppelinとは
OpenZeppelin is an open-source library for smart contracts, especially used for token implementation.

__オフィシャルサイト__
https://openzeppelin.org/

__コード__
https://github.com/OpenZeppelin/openzeppelin-solidity


## Minting
ミントすることはERC-721トークン基準にトークンの作り方です。「Token minters」はミントをしています。

However, not everyone in the network can mint tokens. So, how do we determine who can mint? We assign the role of 'minter' to certain users.

## OpenZeppelin's MinterRole Implementation
We will be taking a look at how OpenZeppelin implements the restriction of MinterRole.

__文献集__
https://openzeppelin.org/api/docs/access_roles_MinterRole.html

__コード__
https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/access/roles/MinterRole.sol

### SolidityのバーションとRole import
```javascript:MinterRole.sol
pragma solidity ^0.4.24;

import "../Roles.sol";

contract MinterRole {
  ...
}
```
On the first line, the version of Solidity used in the smart contract is being defined. Then, the [Role library](https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/access/Roles.sol) is being imported. Next, is where the smart contract for MinterRole lies.

### Constructor関数
```javascript:MinterRole.sol
contract MinterRole {
  using Roles for Roles.Role;

  event MinterAdded(address indexed account);
  event MinterRemoved(address indexed account);

  Roles.Role private minters;

  constructor() public {
    _addMinter(msg.sender);
  }
}
```
From here on, we will be using the previously imported Role library for all Roles.Role types. We then have two events being defined, "MinterAdded" and "MinterRemoved", which basically keeps track of all user addresses being added or removed as Minter. The "minters" variable is then defined to keep track of all minters. Finally, the contract creator is set as a minter by default on the constructor function.

### Inspecting MinterRole.sol
```javascript:MinterRole.sol
contract MinterRole {
  ...
  modifier onlyMinter() {
    require(isMinter(msg.sender));
    _;
  }

  function isMinter(address account) public view returns (bool) {
    return minters.has(account);
  }
  ...
}
```
Here, we create a modifier onlyMinter(), which extends the isMinter() function. Basically, these parts of the code will be called to check if a user (by his/her address) is a Minter.

### 基本的な関数
```javascript:MinterRole.sol
contract MinterRole {
  ...
  function addMinter(address account) public onlyMinter {
    _addMinter(account);
  }

  function renounceMinter() public {
    _removeMinter(msg.sender);
  }

  function _addMinter(address account) internal {
    minters.add(account);
    emit MinterAdded(account);
  }

  function _removeMinter(address account) internal {
    minters.remove(account);
    emit MinterRemoved(account);
  }
  ...
}
```
These are the core functions of the MinterRole smart contract. The function addMinter() enables us to assign the Minter role to a certain user. Then, renounceMinter() is a function that can be called by a user to surrender his Minter role. Finally, removeMinter() is a function used to remove a certain user from the list of Minters.

## 結論
In the ERC-721 Non-Fungible Token Standard, there is a limited number of users who are allowed to mint or create new tokens. By creating a Minter role, like what was discussed (OpenZeppelin's implementation), we are able to easily determine who the minters are in the network. 

I hope you learned something, if not a lot, in this article :smile: I recommend that you check out these links:

* [ERC-721 Non-Fungible Token Standard](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md)
* [OpenZeppelin Documentation](https://openzeppelin.org/api/docs/get-started.html)
* [OpenZeppelin GitHub repository](https://github.com/OpenZeppelin/openzeppelin-solidity)