LoginSignup
4
3

More than 5 years have passed since last update.

ERC20 準拠のトークン(OpenZeppelin v2.0.0)

Last updated at Posted at 2018-12-08

OpenZeppelinのv2.0.0 への対応版 ( https://qiita.com/infinith4/items/199a54dd416eaa1dee51 はv1.12.0版)

$ mkdir erc20_zeppelin2_token
$ cd erc20_zeppelin2_token/
$ truffle init
$ npm init -y
$ npm i --save-exact openzeppelin-solidity

contract

contracts/E20Z2Token.sol

pragma solidity ^0.4.23;

import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
//import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Burnable.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";

contract E20Z2Token is ERC20, ERC20Mintable, ERC20Burnable, ERC20Detailed {

    string private _name = "E20Z2Token";
    string private _symbol = "Z2T";
    uint8 private _decimals = 18;

    address account = msg.sender;

    constructor(uint value)
        ERC20Detailed(_name, _symbol, _decimals)
        ERC20Burnable()
        ERC20Mintable()
        public
    {
        _mint(account, value);
    }
}

migration

migrations/2_deploy_e20z2_token.js

var E20Z2Token = artifacts.require("./E20Z2Token.sol")

module.exports = function(deployer) {
    var initialSupply = 100000;
    deployer.deploy(E20Z2Token, initialSupply);
}

test

test/E20Z2Token.js

var E20Z2Token = artifacts.require("E20Z2Token.sol");

contract('E20Z2Token', function(accounts) {
    it("account[0] own 100000 E20Z2Token.", function() {
        return E20Z2Token.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
Using network 'develop'.

Compiling ./contracts/E20Z2Token.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/access/Roles.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/access/roles/MinterRole.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Burnable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol...


  Contract: E20Z2Token
    ✓ account[0] own 100000 E20Z2Token.


  1 passing (126ms)

truffle(develop)> migrate
Compiling ./contracts/E20Z2Token.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/access/Roles.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/access/roles/MinterRole.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Burnable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol...
Writing artifacts to ./build/contracts

Using network 'develop'.

Running migration: 2_deploy_e20z2_token.js
  Deploying E20Z2Token...
  ... 0x93a83d9720d735e16d964f4c1a364c639279eb0f4ce9efa629800def1a1a79df
  E20Z2Token: 0xd95b1dbec167c6cf547d018ddecf41a4cb2e2f73
Saving successful migration to network...
  ... 0xfc97643e2ba3f4a79580184ceb5ab1f882f0827f4c36feda08d92cfd4c833f1e
Saving artifacts..

送金

$ truffle develop
truffle(develop)> migrate
truffle(develop)> e20z2Token = E20Z2Token.at(E20Z2Token.address)
truffle(develop)> e20z2Token.name()
'E20Z2Token'
truffle(develop)> e20z2Token.symbol()
'Z2T'
truffle(develop)> e20z2Token.totalSupply()
BigNumber { s: 1, e: 5, c: [ 100000 ] }

truffle(develop)> e20z2Token.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 5, c: [ 100000 ] }
truffle(develop)> e20z2Token.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }
truffle(develop)> e20z2Token.transfer(web3.eth.accounts[1], 10)
{ tx: '0x016a269aab91313529d7f13b540ca9cf55dd767a7a223b86e938c72aada5f363',
  receipt: 
   { transactionHash: '0x016a269aab91313529d7f13b540ca9cf55dd767a7a223b86e938c72aada5f363',
     transactionIndex: 0,
     blockHash: '0x26d27224a937f09edcba068678f725b297f23ebcb9094e4930bb820e15070137',
     blockNumber: 61,
     gasUsed: 51739,
     cumulativeGasUsed: 51739,
     contractAddress: null,
     logs: [ [Object] ],
     status: '0x01',
     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000100000000000000000000000000000000010000008000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000030000000000000000000010000000000000000000000000000000000000000010000000002000000000000000000000000002000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
  logs: 
   [ { logIndex: 0,
       transactionIndex: 0,
       transactionHash: '0x016a269aab91313529d7f13b540ca9cf55dd767a7a223b86e938c72aada5f363',
       blockHash: '0x26d27224a937f09edcba068678f725b297f23ebcb9094e4930bb820e15070137',
       blockNumber: 61,
       address: '0xd95b1dbec167c6cf547d018ddecf41a4cb2e2f73',
       type: 'mined',
       event: 'Transfer',
       args: [Object] } ] }
truffle(develop)> e20z2Token.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 4, c: [ 99990 ] }
truffle(develop)> e20z2Token.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 1, c: [ 10 ] }

Source Code:
https://github.com/tys-hiroshi/dapps/tree/master/erc20_zeppelin2_token

参考
https://qiita.com/toshiaki_takase/items/75fe140f78e03aa5f732

4
3
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
4
3