SaaSベンチャーで働くエンタープライズ部長のブログ

SaaSベンチャーでエンジニア→プロダクトマネージャー→エンタープライズ部長として働いています。

ganache2.0のコントラクト管理機能を試す

GanacheはGUIでローカルにEthereumのブロックチェーンネットワークを生成できるツールです。

実行方法はとても簡単で、公式サイトからダウンロードして、クリックで起動するだけです。

f:id:naomasabit:20190912224620p:plain
ganache起動画面

Ganache2系が正式リリースされたのは2019年3月で結構前ですが、2系ではコントラクトのデプロイステータスを管理できるようになっていました。

今回は、open-zeppelinでERC721トークンのデプロイを例に、この機能を紹介していきます。

truffleのプロジェクトを作成する

まずはtruffleのプロジェクトを作成します。空ディレクトリを作ります。

~/> mkdir mytoken

作成したディレクトリでtruffle initコマンドを打ちます。

~/> cd mytoken
~/mytoken> truffle init

✔ Preparing to download
✔ Downloading
✔ Cleaning up temporary files
✔ Setting up box

Unbox successful. Sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

作成できている事を確認します。

~/mytoken> ls
contracts       migrations      test            truffle-config.js

open-zeppelinを使ったERC721コントラクトを作る

今回、open-zeppelinを使ったERC721コントラクトを例として作成します。npm installします。

~/mytoken> npm install @openzeppelin/contracts

openzeppelinのERC721コントラクトを継承したMyNFT.solというコントラクトを作成します。

~/mytoken> vi contracts/MyNFT.sol
pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721Mintable.sol";

contract MyNFT is ERC721Full, ERC721Mintable {
  constructor() ERC721Full("MyNFT", "MNFT") public {
  }
}

migrationファイル

~/mytoken> vi migrations/2_deploy_my_nft.js
const Migrations = artifacts.require("MyNFT");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

truffle-config.jsを作る

truffle-config.jsを

~/mytoken> vi truffle-config.js

以下のように編集して、developmentのnetworkでlocalhost:7545で起動するようにします。

module.exports = {
  /**
   * Networks define how you connect to your ethereum client and let you set the
   * defaults web3 uses to send transactions. If you don't specify one truffle
   * will spin up a development blockchain for you on port 9545 when you
   * run `develop` or `test`. You can ask a truffle command to use a specific
   * network from the command line, e.g
   *
   * $ truffle test --network <network-name>
   */

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

  },

  // Configure your compilers
  compilers: {
    solc: {
       version: "0.5.0",    // Fetch exact version from solc-bin (default: truffle's version)
       docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
       settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: false,
          runs: 200
        },
        evmVersion: "byzantium"
       }
    }
  }
}

ganacheにtruffle-config.jsを読み込ませる

GanacheをQuick Startで起動した後のコントラクト画面はこのようになっています。

f:id:naomasabit:20190912014758p:plain
コントラクト画面

LINK TRUFFLE PROJECTボタンを押す事でプロジェクトの紐付け画面に移動します。

f:id:naomasabit:20190912014852p:plain
Link project画面

ADD PROJECTから、今のプロジェクトのtruffle-config.jsを追加します。

f:id:naomasabit:20190912015637p:plain
truffle-config追加後画面

この状態でSAVEを押すとコントラクトの一覧が表示されます。

f:id:naomasabit:20190912014947p:plain
コントラクト状態画面

コントラクト一覧には継承したコントラクトなども全て表示されます。

ganacheネットワークにコントラクトをデプロイする

ローカルのganacheネットワークにコントラクトをデプロイしてみます。

truffle migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      5777
> Block gas limit: 0x65c72b7


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xb6e289bafa878a99762d3a82e64c4311767eafbfd7027bba301314c2311ece48
   > Blocks: 0            Seconds: 0
   > contract address:    0x960D0596f88d0A1E18b6442a8fFBb55A5EaFaDC9
   > block number:        1
   > block timestamp:     1568220119
   > account:             0xBc25ffEc79d0965933306EB775EF7Ab37c95C100
   > balance:             99.99477214
   > gas used:            261393
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00522786 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00522786 ETH


2_deploy_my_nft.js
==================

   Deploying 'MyNFT'
   -----------------
   > transaction hash:    0x69190597141952beb24a5baf0f561793a19b501624c20de99c4557fe50746ffa
   > Blocks: 0            Seconds: 0
   > contract address:    0xc59847B072c854aF8AA5693dD967E159B8829B3a
   > block number:        3
   > block timestamp:     1568220120
   > account:             0xBc25ffEc79d0965933306EB775EF7Ab37c95C100
   > balance:             99.93418106
   > gas used:            2987531
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.05975062 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.05975062 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.06497848 ETH

デプロイが完了した事を確認して、コントラクト画面を表示します。

f:id:naomasabit:20190912015030p:plain
デプロイ後の画面

デプロイされたコントラクトには、Deployedマークがついています。

f:id:naomasabit:20190912015848p:plain
コントラクト詳細

さらに、詳細画面には作成したトランザクションやストレージの状態も表示されます。

まとめ

ganacheのコントラクト管理画面で、コントラクトのデプロイステータスを管理できます。開発中などは繰り返しデプロイのテストをする事があるので、便利な機能として活用できます。