blockchain:truffleを使ったスマートコントラクト開発
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
blockchain:truffleを使ったスマートコントラクト開発 [2022/04/27 05:06] – dot | blockchain:truffleを使ったスマートコントラクト開発 [2022/04/29 01:44] (現在) – dot | ||
---|---|---|---|
行 1: | 行 1: | ||
====== Truffleを使ったスマートコントラクト開発 ====== | ====== Truffleを使ったスマートコントラクト開発 ====== | ||
- | Truffleとは | + | Truffle は ethereum スマートコントラクトのコンパイル、マイグレーション、テストを行うためのフレームワークです。 |
====== 必要なソフトウエア ====== | ====== 必要なソフトウエア ====== | ||
行 9: | 行 9: | ||
[[https:// | [[https:// | ||
+ | |||
+ | また、ethereum ブロックチェーンも使用しますので [[blockchain: | ||
====== インストール ====== | ====== インストール ====== | ||
行 57: | 行 59: | ||
- | TODO: 各ファイルとディレクトリの説明。 | + | 各ファイルとディレクトリの説明。 |
- | * truffle-config.js | + | * truffle-config.js: truffle の設定ファイル。ethereum ブロックチェーンのアドレス等を設定する。 |
- | * contracts | + | * contracts: スマートコントラクトのソースコードを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「Migrations.sol」が作られています。 |
- | * migrations | + | * migrations: マイグレーションの設定ファイルを格納するディレクトリ。最初からマイグレーションの現在のバージョンを管理するための「1_initial_migration.js」が作られています。 |
- | * test | + | * test: テストコードを格納するディレクトリ。 |
行 152: | 行 154: | ||
<code PowerShell> | <code PowerShell> | ||
- | truffle(development)> | + | truffle(development)> |
Compiling your contracts... | Compiling your contracts... | ||
行 165: | 行 167: | ||
</ | </ | ||
- | ===== マイグレーション ===== | + | ===== マイグレーションファイルの作成 |
+ | |||
+ | Counter コントラクトをデプロイするためのマイグレーションファイルを作成します。 | ||
+ | |||
+ | <code PowerShell> | ||
+ | truffle(development)> | ||
+ | truffle(development)> | ||
+ | </ | ||
+ | |||
+ | 上記のコマンドで migrations ディレクトリの中に nnnnnnnn_counter.js ファイルが生成されます。 | ||
+ | |||
+ | ファイルの以下のように書き換えて、Counter コントラクトをデプロイするように設定します。 | ||
+ | |||
+ | <code JavaScript> | ||
+ | const Counter = artifacts.require(" | ||
+ | module.exports = function(_deployer) { | ||
+ | // Use deployer to state migration tasks. | ||
+ | _deployer.deploy(Counter) | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | ===== デプロイ ===== | ||
+ | |||
+ | <code PowerShell> | ||
+ | truffle(development)> | ||
+ | |||
+ | 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 | ||
+ | |||
+ | |||
+ | Starting migrations... | ||
+ | ====================== | ||
+ | > Network name: ' | ||
+ | > Network id: 5777 | ||
+ | > Block gas limit: 6721975 (0x6691b7) | ||
+ | |||
+ | |||
+ | 1_initial_migration.js | ||
+ | ====================== | ||
+ | |||
+ | | ||
+ | | ||
+ | > transaction hash: 0x09353f6ea3c2ce57acf8de3a9c2c1110fb2abfeea6d51d896477509d6e6edc6a | ||
+ | > Blocks: 0 Seconds: 0 | ||
+ | > contract address: | ||
+ | > block number: | ||
+ | > block timestamp: | ||
+ | > account: | ||
+ | > balance: | ||
+ | > gas used: 248854 (0x3cc16) | ||
+ | > gas price: | ||
+ | > value sent: 0 ETH | ||
+ | > total cost: 0.00497708 ETH | ||
+ | |||
+ | > Saving migration to chain. | ||
+ | > Saving artifacts | ||
+ | | ||
+ | > Total cost: 0.00497708 ETH | ||
+ | |||
+ | |||
+ | 1651036112_counter.js | ||
+ | ===================== | ||
+ | |||
+ | | ||
+ | | ||
+ | > transaction hash: 0x45b148c06609f587e5636a17a088f6685fc2e05603aed75a54bfcf01ba881129 | ||
+ | > Blocks: 0 Seconds: 0 | ||
+ | > contract address: | ||
+ | > block number: | ||
+ | > block timestamp: | ||
+ | > account: | ||
+ | > balance: | ||
+ | > gas used: 165265 (0x28591) | ||
+ | > gas price: | ||
+ | > value sent: 0 ETH | ||
+ | > total cost: 0.0033053 ETH | ||
+ | |||
+ | > Saving migration to chain. | ||
+ | > Saving artifacts | ||
+ | | ||
+ | > Total cost: | ||
+ | |||
+ | Summary | ||
+ | ======= | ||
+ | > Total deployments: | ||
+ | > Final cost: 0.00828238 ETH | ||
+ | |||
+ | |||
+ | truffle(development)> | ||
+ | </ | ||
===== 実行 ===== | ===== 実行 ===== | ||
+ | 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を使ったスマートコントラクト開発.1651035969.txt.gz · 最終更新: 2022/04/27 05:06 by dot