LoginSignup
27
26

More than 5 years have passed since last update.

Truffle チュートリアル

Last updated at Posted at 2016-07-04

今回はEthereumのフレームワークであるTruffleの使い方についてご説明します。

開発環境は以下です。

  • NodeJS 5.0以上
  • Windows, Linux or Mac OS X

インストール

以下のコマンドでTruffleをインストールします。

$ npm install -g truffle

続いてプロジェクトを作りましょう。

$ mkdir myproject
$ cd myproject
$ truffle init

初期化が完了したら、以下のフォルダ/ファイルが出来ているはずです。

  • app/
  • contracts/
  • environments/
  • test/
  • truffle.js

コントラクトのコンパイル

Solidityファイルは./contractsに置いてください。

solidityファイルをコンパイルするには、以下のコマンドを打ちます。

$ truffle compile

1点contract fileを書くときの注意として、ファイル名と同一のコントラクトを準備する必要があります。

contract MyContract {
  ...
}
// or
library MyContract {
  ...
}

デプロイ

デプロイする前にhttp://localhost:8545のポートを空けてください。
Ethereumクライアントにコントラクトをデプロイするためには、以下のコマンドを実行してください。

$ truffle deploy

テスト

TruffleはMochaとChaiをテストツールとして用いてます。テストファイルは./testsに置きます。

テストの実行は以下のコマンドを実効するだけで大丈夫です。

$ truffle test

特定のfileをtestしたい場合は、以下のように行う。

$ truffle test ./path/to/test/file.js

コントラクトとの接続

Ethereumのネットワークと接続するためにはweb3.jsを使うのが一般的ですが、TruffleではEther Paddingを使います。これはWeb3の上に作られたライブラリであり、コントラクトとより簡単に接続できます。

データの書き込み/読み込み

Ethereumでは、データの書き込みはTransaction、読み込みはCallと言います。

書き込み(Transaction)

  • Cost gas (Ether)
  • Change the state of the network
  • Aren't processed immediately
  • Won't expose a return value (only a transaction id).

読み込み(Call)
- Are free (do not cost gas)
- Do not change the state of the network
- Are processed immediately
- Will expose a return value (hooray!)

概要
理解しやすくするために参考となるファイルを作ります。このコントラクトは初期化メソッドの他に、3つのファンクションを含みます。(sendCoin, getBalanceEth, getBalanceです)

import "ConvertLib.sol";

contract MetaCoin {
  mapping (address => uint) balances;

  function MetaCoin() {
    balances[tx.origin] = 10000;
  }

  function sendCoin(address receiver, uint amount) returns(bool sufficient) {
    if (balances[msg.sender] < amount) return false;
    balances[msg.sender] -= amount;
    balances[receiver] += amount;
    return true;
  }

  function getBalanceInEth(address addr) returns(uint) {
    return ConvertLib.convert(getBalance(addr), 2);
  }

  function getBalance(address addr) returns(uint) {
    return balances[addr];
  }
}

Transaction作成
Transactionは以下のようにして作成します。

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta = MetaCoin.deployed();
meta.sendCoin(account_two, 10, {from: account_one}).then(function(tx_id) {
  // If this callback is called, the transaction was successfully processed.
  // Note that Ether Pudding takes care of watching the network and triggering
  // this callback.
  alert("Transaction successful!")
}).catch(function(e) {
  // There was an error! Handle it.
})

Call作成
Callは以下のようにして作成します。

var account_one = "0x1234..."; // an address

var meta = MetaCoin.deployed();
meta.getBalance.call(account_one, {from: account_one}).then(function(balance) {
  // If this callback is called, the call was successfully executed.
  // Note that this returns immediately without any waiting.
  // Let's print the return value.
  console.log(balance.toNumber());
}).catch(function(e) {
  // There was an error! Handle it.
})

コンソール

コンソールを使うには、以下のコマンドを打ちます。

$ truffle console

まとめ

以上がTruffleの基本的な使い方です。次回はTruffleを使ったアプリケーションについて詳しく説明して参ります。

参考資料

27
26
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
26