ユーザ用ツール

サイト用ツール


blockchain:openzeppelin_で_erc20_トークンの作成

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
blockchain:openzeppelin_で_erc20_トークンの作成 [2022/05/08 02:10] – 作成 dotblockchain:openzeppelin_で_erc20_トークンの作成 [2022/05/12 00:26] (現在) – [まとめ!] dot
行 1: 行 1:
- 
 ====== OpenZeppelin で ERC20 トークンの作成 ====== ====== OpenZeppelin で ERC20 トークンの作成 ======
  
 ほぼ OpenZeppelin オフィシャルの[[https://docs.openzeppelin.com/contracts/4.x/erc20|ドキュメント]]通りに ERC20 トークンを実装します。つまり、オリジナルのコインを発行します。 ほぼ OpenZeppelin オフィシャルの[[https://docs.openzeppelin.com/contracts/4.x/erc20|ドキュメント]]通りに ERC20 トークンを実装します。つまり、オリジナルのコインを発行します。
 +
 +作成したコインは address 間で送金することもできます! それも最後で確認します。
  
 ===== 前提 ===== ===== 前提 =====
行 15: 行 16:
 > mkdir coin_project > mkdir coin_project
 > cd coin_project > cd coin_project
-nft_project> truffle init+coin_project> truffle init
 </code> </code>
  
行 25: 行 26:
  
 <code PowerShell> <code PowerShell>
-nft_project> npm init+coin_project> npm init
 This utility will walk you through creating a package.json file. This utility will walk you through creating a package.json file.
 It only covers the most common items, and tries to guess sensible defaults. It only covers the most common items, and tries to guess sensible defaults.
行 36: 行 37:
  
 Press ^C at any time to quit. Press ^C at any time to quit.
-package name: (nft_project)+package name: (coin_project)
 version: (1.0.0) version: (1.0.0)
 description: description:
行 45: 行 46:
 author: author:
 license: (ISC) license: (ISC)
-About to write to C:\Users\miyazato\work\ethereum\nft_project\package.json:+About to write to C:\Users\miyazato\work\ethereum\coin_project\package.json:
  
 { {
-  "name": "nft_project",+  "name": "coin_project",
   "version": "1.0.0",   "version": "1.0.0",
   "description": "",   "description": "",
行 65: 行 66:
 Is this OK? (yes) Is this OK? (yes)
  
-nft_project> npm install --save-dev @openzeppelin/contracts+coin_project> npm install --save-dev @openzeppelin/contracts
 </code> </code>
  
-===== NFTの実装 =====+===== ERC20スマートコントラクト(コイン)の実装 =====
  
 <code PowerShell> <code PowerShell>
-nft_project> truffle create contract GameItem +coin_project> truffle create contract OCCToken  
-nft_project> code contracts/GameItem.sol+coin_project> code contracts/OCCToken.sol
 </code> </code>
  
 <code JavaScript> <code JavaScript>
-// contracts/GameItem.sol+// contracts/OCCToken.sol
 // SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
 pragma solidity ^0.8.0; pragma solidity ^0.8.0;
  
-import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; +import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
-import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";+
  
-contract GameItem is ERC721URIStorage { +contract OCCToken is ERC20 
-    using Counters for Counters.Counter; +    constructor(uint256 initialSupplyERC20("OkinawaComputerCenter", "OCC") { 
-    Counters.Counter private _tokenIds; +        _mint(msg.senderinitialSupply);
- +
-    constructor() ERC722("GameItem", "ITM") {+
- +
-    function awardItem(address player, string memory tokenURI) +
-        public +
-        returns (uint256) +
-    { +
-        uint256 newItemId = _tokenIds.current(); +
-        _mint(playernewItemId)+
-        _setTokenURI(newItemId, tokenURI); +
- +
-        _tokenIds.increment(); +
-        return newItemId;+
     }     }
 } }
行 106: 行 93:
  
 <code PowerShell> <code PowerShell>
-nft_project> truffle create migration GameItem +coin_project> truffle create migration OCCToken 
-nft_project> code .\migrations\1651959757_game_item.js+coin_project> code .\migrations\1651976599_o_c_c_token.js
 </code> </code>
 +
 +OCCToken スマートコントラクトはデプロイ時にコインの総数(initialSupply)を決定する必要があります。
 +マイグレーションファイルで initialSupply を10000000枚に指定してみます。
 +
 +:!: _deployer.deploy の引数は「OCCToken(10000000)」ではなく「OCCToken, 10000000」と記述する必要があります。
  
 <code JavaScript> <code JavaScript>
-const GameItem = artifacts.require("GameItem");+const OCCToken = artifacts.require("OCCToken");
 module.exports = function(_deployer) { module.exports = function(_deployer) {
   // Use deployer to state migration tasks.   // Use deployer to state migration tasks.
-  _deployer.deploy(GameItem)+  _deployer.deploy(OCCToken, 10000000)
 }; };
 </code> </code>
  
-===== デプロイ =====+===== デプロイ(Mint) ===== 
 + 
 +ERC20 はデプロイ時に新しいToken(コイン)が Mint されます。 
 +実際、OCCToken スマートコントラクトのコンストラクタ(デプロイ時に1度だけ実行される)を見ると「_mint」メソッドを呼び出しています。
  
 <code PowerShell> <code PowerShell>
-nft_project> truffle migrate+coin_project> truffle migrate
 </code> </code>
  
-===== 実行(Mint) =====+===== 実行 =====
  
-今回作成したスマートコントラクトの awardItem を実行すると、新しい NFT が生成されます。 +OCCToken スマートコントラクトを呼び出して、コイン総数を確認します。
-新たな NFT を生成することを Mint と言います。実際 awardItem のソースコードでは「_mint(player, newItemId);」と _mint メソッドを呼び出しています。 +
-Mint は英語「Minting 鋳造(ちゅうぞう)」から来ます。(例: minting authority 造幣局)+
  
 <code PowerShell> <code PowerShell>
-nft_project> truffle console +coin_project> truffle console 
-truffle(development)> let gameItem = await GameItem.deployed();+truffle(development)> let OCCToken = await OCCToken.deployed(); 
 +truffle(development)> OCCToken.totalSupply() 
 +BN { 
 +  negative: 0, 
 +  words: [ 10000000, <1 empty item> ], 
 +  length: 1, 
 +  red: null 
 +}
 truffle(development)> let address = await web3.eth.getAccounts(); truffle(development)> let address = await web3.eth.getAccounts();
-truffle(development)> let playerAddress = address[0]; +truffle(development)> let deployerAddress = address[0]; 
-truffle(development)> let transaction = await gameItem.awardItem(playerAddress"https://game.example/item-id-8u5h2m.json");+truffle(development)> OCCToken.balanceOf(deployerAddress); 
 +BN { 
 +  negative: 0, 
 +  words[ 10000000, <1 empty item> ], 
 +  length: 1, 
 +  red: null 
 +
 +</code> 
 + 
 +===== 送金 ===== 
 + 
 +OCCToken を他のアドレスに送金してみます。 
 + 
 +<code PowerShell> 
 +truffle(development)> let receiverAddress = address[1]; 
 +undefined 
 +truffle(development)> OCCToken.balanceOf(receiverAddress); 
 +BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null } 
 +truffle(development)> OCCToken.transfer(receiverAddress, 10000);
 { {
-  tx: '0xf32a1d6dd67167b16537ddc0308cdd5918773aca77f91438be056adf38a212bf',+  tx: '0xe0d508b45e2bb412f5c540d96e133a185f5ae4162eb24288d36ee09b7970e825',
   receipt: {   receipt: {
-    transactionHash: '0xf32a1d6dd67167b16537ddc0308cdd5918773aca77f91438be056adf38a212bf',+    transactionHash: '0xe0d508b45e2bb412f5c540d96e133a185f5ae4162eb24288d36ee09b7970e825',
     transactionIndex: 0,     transactionIndex: 0,
-    blockHash: '0x3c932e396c46e28469fa2db720481e5d1814a624061429bd5eab1a135c0382ff',+    blockHash: '0xf7c10358304e2a6de5ac1e1f4e13f83df5a785f49a151df39477727b11ddfa86',
     blockNumber: 5,     blockNumber: 5,
-    from: '0x9384fc1b3f3cc59e6e30a0ba0267451570bb7aa6', +    from: '0x0172ffd2b8ff73056a187fc242af249ac585cc3b', 
-    to: '0x36c73d181564e2bc7ef390f7067ffabf4c23d535', +    to: '0xaee728a629b239fcf3b56d21067ba36a0a4dbd6c', 
-    gasUsed: 153238+    gasUsed: 51839
-    cumulativeGasUsed: 153238,+    cumulativeGasUsed: 51839,
     contractAddress: null,     contractAddress: null,
     logs: [ [Object] ],     logs: [ [Object] ],
     status: true,     status: true,
-    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000010000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000008000000000000000000000000000000000020000000000002000000000000000000000000000000000000000000000000040020000000100000000000000000000000000000000000000000000000000000000000',+    logsBloom: '0x00000008001010000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000002000000000000000010000000000001000000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000',
     rawLogs: [ [Object] ]     rawLogs: [ [Object] ]
   },   },
行 157: 行 175:
       logIndex: 0,       logIndex: 0,
       transactionIndex: 0,       transactionIndex: 0,
-      transactionHash: '0xf32a1d6dd67167b16537ddc0308cdd5918773aca77f91438be056adf38a212bf', +      transactionHash: '0xe0d508b45e2bb412f5c540d96e133a185f5ae4162eb24288d36ee09b7970e825', 
-      blockHash: '0x3c932e396c46e28469fa2db720481e5d1814a624061429bd5eab1a135c0382ff',+      blockHash: '0xf7c10358304e2a6de5ac1e1f4e13f83df5a785f49a151df39477727b11ddfa86',
       blockNumber: 5,       blockNumber: 5,
-      address: '0x36c73D181564e2BC7Ef390F7067FfABf4C23d535',+      address: '0xaeE728a629b239fCf3B56d21067ba36A0A4dbd6C',
       type: 'mined',       type: 'mined',
-      id: 'log_ca6a0b3b',+      id: 'log_e6c66317',
       event: 'Transfer',       event: 'Transfer',
       args: [Result]       args: [Result]
行 168: 行 186:
   ]   ]
 } }
 +truffle(development)> OCCToken.balanceOf(deployerAddress);
 +BN {
 +  negative: 0,
 +  words: [ 9990000, <1 empty item> ],
 +  length: 1,
 +  red: null
 +}
 +truffle(development)> OCCToken.balanceOf(receiverAddress);
 +BN {
 +  negative: 0,
 +  words: [ 10000, <1 empty item> ],
 +  length: 1,
 +  red: null
 +}
 +truffle(development)>
 </code> </code>
  
-トランザクションログから Mint した NFT の TOKENID を取得しす。 +===== とめ! =====
-(GameItem スマートコントラクトは内部的に自動インクリメントする TOKENID を生成しています。これが主キーの役割を果たしています。)+
  
-<code PowerShell> +:!: ここまで来る、実は ETH の実体もスマートコントラクトであことが分かります! 8-O LOL
-truffle(development)> let TOKENID = transaction.logs[0].args.tokenId; +
-</code> +
- +
-Mint した NFT の所有者と NFT に保存されている tokenURI を確認します。 +
- +
-<code PowerShell> +
-truffle(development)> gameItem.ownerOf(TOKENID) +
-'0x9384FC1B3F3CC59e6e30a0BA0267451570bb7AA6' +
-truffle(development)> gameItem.tokenURI(TOKENID) +
-'https://game.example/item-id-8u5h2m.json' +
-</code>+
blockchain/openzeppelin_で_erc20_トークンの作成.1651975801.txt.gz · 最終更新: 2022/05/08 02:10 by dot