blockchain:truffleを使ったスマートコントラクト開発
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:truffleを使ったスマートコントラクト開発 [2022/04/28 00:24] – 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: テストコードを格納するディレクトリ。 |
| 行 280: | 行 280: | ||
| truffle(development)> | truffle(development)> | ||
| </ | </ | ||
| + | |||
| + | :!: 「BN」は「BigNumber」という意味です。後々、テスト等で値の比較をするときに int とは扱いが変わってきますので、覚えておいてください。 | ||
| + | |||
| Counter コントラクトの inc を呼び出す。 | Counter コントラクトの inc を呼び出す。 | ||
| 行 329: | 行 332: | ||
| <code JavaScript> | <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を使ったスマートコントラクト開発.1651105475.txt.gz · 最終更新: by dot
