blockchain:truffleを使ったスマートコントラクト開発
文書の過去の版を表示しています。
目次
Truffleを使ったスマートコントラクト開発
Truffleとは
必要なソフトウエア
Truffle は npm を使ってインストールしますので、事前に npm をインストールしてください。
インストール
> npm install -g truffle
初期化
> mkdir my_project > cd my_project > truffle init Starting init... ================ > Copying project files to C:\Users\shinobu\NoNameSeminer\ethereum\my_project Init successful, sweet! Try our scaffold commands to get started: $ truffle create contract YourContractName # scaffold a contract $ truffle create test YourTestName # scaffold a test http://trufflesuite.com/docs
生成されたファイルを確認してみます。
> tree /f ボリューム シリアル番号は EEAF-D230 です C:. │ truffle-config.js │ ├─contracts │ Migrations.sol │ ├─migrations │ 1_initial_migration.js │ └─test .gitkeep
TODO: 各ファイルとディレクトリの説明。
- truffle-config.js
- contracts
- migrations
- test
設定
truffle-config.js を開いて、コメントを削除し以下のように変更してください。
- *Ganache に接続する場合の設定です。
- *port番号は 8545 から 7545 に変更しています。
networks: { // Useful for testing. The `development` name is special - truffle uses it by default // if it's defined here and no other network is specified at the command line. // You should run a client (like ganache-cli, geth or parity) in a separate terminal // tab if you use this network and you must also set the `host`, `port` and `network_id` // options below to some value. // development: { host: "127.0.0.1", // Localhost (default: none) port: 7545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) },
Ganacheとの連携
Ganache を起動し「NEW WORKSPACE」をクリックします。
「ADD PROJECT」をクリックし、先ほど作成した「truffle-config.js」を選択します。
これで Ganache と Truffle の連携設定は完了です。「SAVE WORKSPACE」をクリックしてください。
Truffle コンソール
> truffle console truffle(development)>
スマートコントラクト
生成
truffle(development)> truffle create contract Counter truffle(development)>
上記のコマンドで constracts ディレクトリの中に Counter.sol ファイルが生成されます。
実装
エディタで Counter.sol を開き、以下のように編集してください。
// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0; contract Counter { uint public count; // Function to get the current count function get() public view returns (uint) { return count; } // Function to increment count by 1 function inc() public { count += 1; } // Function to decrement count by 1 function dec() public { count -= 1; } }
コンパイル
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)>
マイグレーション
Counter コントラクトをデプロイするためのマイグレーションファイルを作成します。
truffle(development)> truffle create migration Counter truffle(development)>
上記のコマンドで migrations ディレクトリの中に nnnnnnnn_counter.js ファイルが生成されます。
ファイルの以下のように書き換えて、Counter コントラクトをデプロイするように設定します。
const Counter = artifacts.require("Counter"); module.exports = function(_deployer) { // Use deployer to state migration tasks. deployer.deploy(Counter) };
実行
blockchain/truffleを使ったスマートコントラクト開発.1651036450.txt.gz · 最終更新: 2022/04/27 05:14 by dot