blockchain:truffleを使ったスマートコントラクト開発
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:truffleを使ったスマートコントラクト開発 [2022/04/27 05:47] – dot | blockchain:truffleを使ったスマートコントラクト開発 [2022/04/29 01:44] (現在) – dot | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Truffleを使ったスマートコントラクト開発 ====== | ====== Truffleを使ったスマートコントラクト開発 ====== | ||
| - | Truffleとは | + | Truffle は ethereum スマートコントラクトのコンパイル、マイグレーション、テストを行うためのフレームワークです。 |
| ====== 必要なソフトウエア ====== | ====== 必要なソフトウエア ====== | ||
| 行 59: | 行 59: | ||
| - | TODO: 各ファイルとディレクトリの説明。 | + | 各ファイルとディレクトリの説明。 |
| - | * truffle-config.js | + | * truffle-config.js: truffle の設定ファイル。ethereum ブロックチェーンのアドレス等を設定する。 |
| - | * contracts | + | * contracts: スマートコントラクトのソースコードを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「Migrations.sol」が作られています。 |
| - | * migrations | + | * migrations: マイグレーションの設定ファイルを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「1_initial_migration.js」が作られています。 |
| - | * test | + | * test: テストコードを格納するディレクトリ。 |
| 行 266: | 行 266: | ||
| ===== 実行 ===== | ===== 実行 ===== | ||
| + | Counter コントラクトを呼び出すためのインスタンスを取得する。 | ||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | undefined | ||
| + | truffle(development)> | ||
| + | </ | ||
| + | |||
| + | |||
| + | Counter コントラクトの get を呼び出す。 | ||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null } | ||
| + | truffle(development)> | ||
| + | </ | ||
| + | |||
| + | :!: 「BN」は「BigNumber」という意味です。後々、テスト等で値の比較をするときに int とは扱いが変わってきますので、覚えておいてください。 | ||
| + | |||
| + | |||
| + | Counter コントラクトの inc を呼び出す。 | ||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | { | ||
| + | tx: ' | ||
| + | receipt: { | ||
| + | transactionHash: | ||
| + | transactionIndex: | ||
| + | blockHash: ' | ||
| + | blockNumber: | ||
| + | from: ' | ||
| + | to: ' | ||
| + | gasUsed: 42229, | ||
| + | cumulativeGasUsed: | ||
| + | contractAddress: | ||
| + | logs: [], | ||
| + | status: true, | ||
| + | logsBloom: ' | ||
| + | rawLogs: [] | ||
| + | }, | ||
| + | logs: [] | ||
| + | } | ||
| + | truffle(development)> | ||
| + | </ | ||
| + | |||
| + | もう一度、Counter コントラクトの get を呼び出す。(値が0から1になっている) | ||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | BN { negative: 0, words: [ 1, <1 empty item> ], length: 1, red: null } | ||
| + | truffle(development)> | ||
| + | </ | ||
| + | |||
| + | ====== テスト ====== | ||
| + | |||
| + | ===== 生成 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | truffle(development)> | ||
| + | </ | ||
| + | |||
| + | 上記のコマンドで test ディレクトリの中に simple_counter_test.js ファイルが生成されます。 | ||
| + | |||
| + | ===== 実装 ===== | ||
| + | |||
| + | エディタで simple_counter_test.js を開き、以下のように編集してください。 | ||
| + | |||
| + | <code JavaScript> | ||
| + | const Counter = artifacts.require(" | ||
| + | |||
| + | /* | ||
| + | * uncomment accounts to access the test accounts made available by the | ||
| + | * Ethereum client | ||
| + | * See docs: https:// | ||
| + | */ | ||
| + | contract(" | ||
| + | it(" | ||
| + | await Counter.deployed(); | ||
| + | return assert.isTrue(true); | ||
| + | }); | ||
| + | |||
| + | it(" | ||
| + | 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(), | ||
| + | }); | ||
| + | |||
| + | it(" | ||
| + | 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(), | ||
| + | }); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | ===== 実行 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | truffle(development)> | ||
| + | Using network ' | ||
| + | |||
| + | |||
| + | Compiling your contracts... | ||
| + | =========================== | ||
| + | > Compiling .\contracts\Counter.sol | ||
| + | > Compiling .\contracts\Migrations.sol | ||
| + | > Artifacts written to C: | ||
| + | > 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)> | ||
| + | </ | ||
blockchain/truffleを使ったスマートコントラクト開発.1651038458.txt.gz · 最終更新: by dot
