ユーザ用ツール

サイト用ツール


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

文書の過去の版を表示しています。


Truffleを使ったスマートコントラクト開発

Truffleとは

必要なソフトウエア

Truffle は npm を使ってインストールしますので、事前に 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)
};

デプロイ

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)>

実行

blockchain/truffleを使ったスマートコントラクト開発.1651037615.txt.gz · 最終更新: 2022/04/27 05:33 by dot