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

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

More than 1 year has passed since last update.

This article is meant to explain how Minters or people who are allowed to mint are determined in the ERC-721 Token Standard. This article aims to help beginners like me, who are still starting out in learning about Blockchain technology.

自己紹介

こんにちは、サム (@sbenemerito) です。奥多摩の留学生です。日本語とブロクチェーン技術を勉強しています。2018年10月に日本にフィリピンから来ました。

概要

We will be learning about the ERC-721 Non-Fungible Token Standard through OpenZeppelin's ERC-721 Token Implementation. 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

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 is being imported. Next, is where the smart contract for MinterRole lies.

Constructor関数

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

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.

基本的な関数

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:

sbenemerito
日本語を勉強中の留学生。自らの技術力の向上に関心を持つ、ウェブ開発とシステムの自動化、変化を恐れないエンジニア。主に Python、Javascript 使い。
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
ユーザーは見つかりませんでした