LoginSignup
1
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-10-18

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:

1
2
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
1
2