ERC20 準拠のトークンの作り方
そもそも Ethereum とは
Ethereum は分散型アプリケーションのプラットフォームのこと。
https://www.ethereum.org/
ERC20 とは
ERC20とは、Ethereum上のトークンを標準化する仕様のこと。
ERC(Ethereum Request for Comments)はトークンの規格を示したもの。
20とは20番目の仕様なので20と書いてある。
最終的に決められたERC20の仕様は以下の通り。
(最低限いくつかのMethodとEventを定義する必要がある)
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
※EIPs(Ethereum Improvement Proposals)とは、Ethereumに対する改善提案書。
( https://eips.ethereum.org/ )
流れとして、EIPsで提案されたものが、実際のERCとして規格が決められることになる。
上記でも触れられているが、OpenZeppelin はERC20の実装を含んでいるので、OpenZeppelin を使うとERC20準拠のトークンを作成することが出来る。
なので、OpenZeppelinを使ってトークンを作成しましょう。
その前に、
Truffle
Truffle とは、Ethereumのスマートコントラクト開発フレームワークのこと。
まず、Truffleでプロジェクトを作成する。
$ mkdir erc20_token
$ cd erc20_token
$ truffle init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
truffle init するときは、ディレクトリ内は空でなければならない。
OpenZeppelin
まず、OpenZeppelinをインストールする。
$ npm init -y
$ npm i --save-exact openzeppelin-solidity@1.12.0
OpenZeppelin 1.12.0を利用する。
現在は2.0.0が最新
コントラクトの作成
contracts/E20Token.sol の作成
pragma solidity ^0.4.23;
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
contract E20Token is StandardToken {
string public name = "E20Token"; //Token name
string public symbol = "E20T"; //Token unit
uint public decimals = 18; //18 is the most common number of decimal places
constructor(uint initialSupply) public {
totalSupply_ = initialSupply;
balances[msg.sender] = initialSupply;
}
}
Migrationファイルの作成
migrations/2_deploy_erc20_token.js の作成
var E20Token = artifacts.require("./E20Token.sol")
module.exports = function(deployer) {
var initialSupply = 100000;
deployer.deploy(E20Token, initialSupply);
}
テストの作成
test/E20Token.js
var ERC20Token = artifacts.require("E20Token.sol");
contract('E20Token', function(accounts) {
it("account[0] own 100000 E20Token.", function() {
return ERC20Token.deployed().then(function(instance) {
return instance.balanceOf.call(accounts[0]);
}).then(function(balance) {
assert.equal(balance.valueOf(), 100000, "account[0] don't own 100000");
});
});
});
$ truffle develop
truffle(develop)> test
Compiling ./contracts/E20Token.sol...
Compiling ./contracts/Migrations.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/BasicToken.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol...
Contract: E20Token
✓ account[0] own 100000 E20Token.
1 passing (40ms)
※Openzeppelinは2.0.0が最新なので最新に対応したい。