ユーザ用ツール

サイト用ツール


blockchain:truffleを使ったスマートコントラクト開発

差分

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

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
blockchain:truffleを使ったスマートコントラクト開発 [2022/04/27 05:01] dotblockchain:truffleを使ったスマートコントラクト開発 [2022/04/29 01:44] (現在) dot
行 1: 行 1:
 ====== Truffleを使ったスマートコントラクト開発 ====== ====== Truffleを使ったスマートコントラクト開発 ======
  
-Truffle+Truffle は ethereum スマートコントラクトのコンパイル、マイグレーション、テストを行うためのフレームワークです。
  
 ====== 必要なソフトウエア ====== ====== 必要なソフトウエア ======
行 9: 行 9:
  
 [[https://nodejs.org/en/download/|npm パッケージ配布サイト]] [[https://nodejs.org/en/download/|npm パッケージ配布サイト]]
 +
 +また、ethereum ブロックチェーンも使用しますので [[blockchain:ethereum構築ハンズオン_ganache編|ethereumブロックチェーンの構築(Ganache編)]] を参考に Ganache をインストールしてください。
  
 ====== インストール ====== ====== インストール ======
行 57: 行 59:
  
  
-TODO: 各ファイルとディレクトリの説明。+各ファイルとディレクトリの説明。
  
-  * truffle-config.js +  * truffle-config.js: truffle の設定ファイル。ethereum ブロックチェーンのアドレス等を設定する。 
-  * contracts +  * contracts: スマートコントラクトのソースコードを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「Migrations.sol」が作られています。 
-  * migrations +  * migrations: マイグレーションの設定ファイルを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「1_initial_migration.js」が作られています。 
-  * test+  * test: テストコードを格納するディレクトリ。
  
  
行 125: 行 127:
 エディタで Counter.sol を開き、以下のように編集してください。 エディタで Counter.sol を開き、以下のように編集してください。
  
-<code Solidity>+<code JavaScript>
 // SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
 pragma solidity >=0.4.22 <0.9.0; pragma solidity >=0.4.22 <0.9.0;
行 131: 行 133:
 contract Counter { contract Counter {
   uint public count;   uint public count;
- 
-  constructor() public { 
-  } 
  
   // Function to get the current count   // Function to get the current count
行 154: 行 153:
 ===== コンパイル ===== ===== コンパイル =====
  
-===== マイグレーション =====+<code PowerShell> 
 +truffle(development)> truffle compile 
 + 
 +Compiling your contracts... 
 +=========================== 
 +> Compiling .\contracts\Counter.sol 
 +> Compiling .\contracts\Counter.sol 
 +> Compiling .\contracts\Migrations.sol 
 +> Artifacts written to C:\Users\shinobu\NoNameSeminer\ethereum\my_project\build\contracts 
 +> Compiled successfully using: 
 +   - solc: 0.8.13+commit.abaa5c0e.Emscripten.clang 
 +truffle(development)> 
 +</code> 
 + 
 +===== マイグレーションファイルの作成 ===== 
 + 
 +Counter コントラクトをデプロイするためのマイグレーションファイルを作成します。 
 + 
 +<code PowerShell> 
 +truffle(development)> truffle create migration Counter 
 +truffle(development)> 
 +</code> 
 + 
 +上記のコマンドで migrations ディレクトリの中に nnnnnnnn_counter.js ファイルが生成されます。 
 + 
 +ファイルの以下のように書き換えて、Counter コントラクトをデプロイするように設定します。 
 + 
 +<code JavaScript> 
 +const Counter = artifacts.require("Counter"); 
 +module.exports = function(_deployer) { 
 +  // Use deployer to state migration tasks. 
 +  _deployer.deploy(Counter) 
 +}; 
 +</code> 
 + 
 +===== デプロイ ===== 
 + 
 +<code PowerShell> 
 +truffle(development)> truffle migrate 
 + 
 +Compiling your contracts... 
 +=========================== 
 +> Compiling .\contracts\Counter.sol 
 +> Compiling .\contracts\Migrations.sol 
 +> Artifacts written to C:\Users\shinobu\NoNameSeminer\ethereum\my_project\build\contracts 
 +> Compiled successfully using: 
 +   - solc: 0.8.13+commit.abaa5c0e.Emscripten.clang 
 + 
 + 
 +Starting migrations... 
 +====================== 
 +> Network name:    'development' 
 +> Network id:      5777 
 +> Block gas limit: 6721975 (0x6691b7) 
 + 
 + 
 +1_initial_migration.js 
 +====================== 
 + 
 +   Replacing 'Migrations' 
 +   ---------------------- 
 +   > transaction hash:    0x09353f6ea3c2ce57acf8de3a9c2c1110fb2abfeea6d51d896477509d6e6edc6a 
 +   > Blocks: 0            Seconds: 0 
 +   > contract address:    0x9864601fA497E29d85720c336b15267Ff0a89386 
 +   > block number:        1 
 +   > block timestamp:     1651037552 
 +   > account:             0x12C1B9B5152b14CE5af7A83947971108Dc89e54D 
 +   > balance:             99.99502292 
 +   > gas used:            248854 (0x3cc16) 
 +   > gas price:           20 gwei 
 +   > value sent:          0 ETH 
 +   > total cost:          0.00497708 ETH 
 + 
 +   > Saving migration to chain. 
 +   > Saving artifacts 
 +   ------------------------------------- 
 +   > Total cost:          0.00497708 ETH 
 + 
 + 
 +1651036112_counter.js 
 +===================== 
 + 
 +   Replacing 'Counter' 
 +   ------------------- 
 +   > transaction hash:    0x45b148c06609f587e5636a17a088f6685fc2e05603aed75a54bfcf01ba881129 
 +   > Blocks: 0            Seconds: 0 
 +   > contract address:    0x852560406282C788aeF29AbD5A9313588d86c3dB 
 +   > block number:        3 
 +   > block timestamp:     1651037554 
 +   > account:             0x12C1B9B5152b14CE5af7A83947971108Dc89e54D 
 +   > balance:             99.99086736 
 +   > gas used:            165265 (0x28591) 
 +   > gas price:           20 gwei 
 +   > value sent:          0 ETH 
 +   > total cost:          0.0033053 ETH 
 + 
 +   > Saving migration to chain. 
 +   > Saving artifacts 
 +   ------------------------------------- 
 +   > Total cost:           0.0033053 ETH 
 + 
 +Summary 
 +======= 
 +> Total deployments:   2 
 +> Final cost:          0.00828238 ETH 
 + 
 + 
 +truffle(development)> 
 +</code> 
  
 ===== 実行 ===== ===== 実行 =====
  
 +Counter コントラクトを呼び出すためのインスタンスを取得する。
 +<code PowerShell>
 +truffle(development)> let counter = await Counter.deployed()
 +undefined
 +truffle(development)>
 +</code>
 +
 +
 +Counter コントラクトの get を呼び出す。
 +<code PowerShell>
 +truffle(development)> counter.get()
 +BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null }
 +truffle(development)>
 +</code>
 +
 +:!: 「BN」は「BigNumber」という意味です。後々、テスト等で値の比較をするときに int とは扱いが変わってきますので、覚えておいてください。
 +
 +
 +Counter コントラクトの inc を呼び出す。
 +<code PowerShell>
 +truffle(development)> counter.inc()
 +{
 +  tx: '0x10e87f126ce0934123624a8bd0d9c70a7d2d492fd1ade783646e828ccad6ad42',
 +  receipt: {
 +    transactionHash: '0x10e87f126ce0934123624a8bd0d9c70a7d2d492fd1ade783646e828ccad6ad42',
 +    transactionIndex: 0,
 +    blockHash: '0xc95665ead9fae2045cff12ba600d778a99454fbcdfcafe257f6c5dee61f76917',
 +    blockNumber: 5,
 +    from: '0x12c1b9b5152b14ce5af7a83947971108dc89e54d',
 +    to: '0x852560406282c788aef29abd5a9313588d86c3db',
 +    gasUsed: 42229,
 +    cumulativeGasUsed: 42229,
 +    contractAddress: null,
 +    logs: [],
 +    status: true,
 +    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 +    rawLogs: []
 +  },
 +  logs: []
 +}
 +truffle(development)>
 +</code>
 +
 +もう一度、Counter コントラクトの get を呼び出す。(値が0から1になっている)
 +<code PowerShell>
 +truffle(development)> counter.get()
 +BN { negative: 0, words: [ 1, <1 empty item> ], length: 1, red: null }
 +truffle(development)>
 +</code>
 +
 +====== テスト ======
 +
 +===== 生成 =====
 +
 +<code PowerShell>
 +truffle(development)> truffle create test SimpleCounterTest
 +truffle(development)>
 +</code>
 +
 +上記のコマンドで test ディレクトリの中に simple_counter_test.js ファイルが生成されます。
 +
 +===== 実装 =====
 +
 +エディタで simple_counter_test.js を開き、以下のように編集してください。
 +
 +<code JavaScript>
 +const Counter = artifacts.require("Counter");
 +
 +/*
 + * uncomment accounts to access the test accounts made available by the
 + * Ethereum client
 + * See docs: https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-javascript
 + */
 +contract("SimpleCounterTest", function (/* accounts */) {
 +  it("should assert true", async function () {
 +    await Counter.deployed();
 +    return assert.isTrue(true);
 +  });
 +
 +  it("Counter inc", async function () {
 +    let counter = await Counter.deployed();
 +
 +    let before = await counter.get();
 +    await counter.inc();
 +    let after = await counter.get();
 +
 +    // before and after is BN(BigNumber)
 +    return assert.equal(after.toNumber(), before.toNumber() + 1);
 +  });
 +
 +  it("Counter dec", async function () {
 +    let counter = await Counter.deployed();
 +
 +    let before = await counter.get();
 +    await counter.dec();
 +    let after = await counter.get();
 +
 +    // before and after is BN(BigNumber)
 +    return assert.equal(after.toNumber(), before.toNumber() - 1);
 +  });
 +});
 +</code>
 +
 +===== 実行 =====
 +
 +<code PowerShell>
 +truffle(development)> truffle test
 +Using network 'development'.
 +
 +
 +Compiling your contracts...
 +===========================
 +> Compiling .\contracts\Counter.sol
 +> Compiling .\contracts\Migrations.sol
 +> Artifacts written to C:\Users\shinobu\AppData\Local\Temp\test--15628-Lt5184Kc9NTX
 +> Compiled successfully using:
 +   - solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
 +
 +
 +  Contract: SimpleCounterTest
 +    √ should assert true (55ms)
 +    √ Counter inc (1361ms)
 +    √ Counter dec (1419ms)
 +
 +
 +  3 passing (3s)
 +
 +truffle(development)>
 +</code>
blockchain/truffleを使ったスマートコントラクト開発.1651035692.txt.gz · 最終更新: 2022/04/27 05:01 by dot