ALISのコントラクトをローカル環境で動作させてみる




ALIS

ALISのICOには個人的にかなりポジティブな影響を受けています。日本のベンチャー企業で、設立からわずか半年くらいでICOを成功させています。

ALISの詳細はこちら

この行動の早さと実行している内容がすごくかっこいいなと思っています。

自分も最近やっと本腰を入れて暗号通貨の勉強を始めました。特にEthereumの分散アプリケーションに対する興味が大きいです。

ALISもEthereumのブロックチェーンを利用したICOを行なっていて、なんとソースコードも公開しているので勉強させていただこうと思います。

ALISのGithub

まずALISのGithubプロジェクトはこちらです。

ALISが公開しているぷリポジトリには

  • 会社のホームページ
  • モック
  • コントラクト
  • ホワイトペーパー

と全て?公開しているんじゃないでしょうか?今回はこちらのコントラクトのリポジトリをローカルに落としてきます。

セットアップ

READMEに手順が乗っているのですが、Node.jsとかEthereum初心者にはわかりづらいかも思ったので追加手順を書いていきます。

まずALISではyarnを使用しているのでyarnをインストールします。npmはNodePackage ManagerといってNode.jsのバージョンとモジュールの管理を行なってくれます。

npm install -g yarn

クローンはREADMEと同じです。

$ git clone https://github.com/AlisProject/contracts.git
$ cd config
$ yarn
$ yarn install

これでcontractsリポジトリのセットアップは完了です。

テストコードの実行

試しにテストを流してみます。

$ yarn test:all
yarn run v1.1.0
$ scripts/test.sh ./test/*.js
Connection to localhost port 8545 [tcp/*] succeeded!
Using existing testrpc instance
Using network 'testrpc'.

/Users/ユーザー名/private_work/contracts/migrations/2_deploy_contracts.js:25
    ));
    ^
SyntaxError: Unexpected token )
    at Object.createScript (vm.js:56:10)
    at /Users/ユーザー名/private_work/contracts/node_modules/truffle/build/cli.bundled.js:96390:23
    at tryToString (fs.js:456:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

おっと、通らなかった。。。けど、エラーになってるファイルをみるとカンマが一個多かっただけみたいなので消したら通りました!

ちなみにこのときのブランチはmasterブランチで、ハッシュ値はc72c4b18449714321bf065d0b1a814ecbbd4f63dでした。他にもブランチがあったのでそっちが本番運用されてる方かもしれません。

テスト結果の一部を記載します。Pre saleからweek1,2,3とちゃんとテストされているのがわかりますね!!

コントラクトのテストの仕組みはちゃんと理解してないですが、テストを実行するとテスト用のtestrpcを立ち上げてそことのやりとりをテストしてるみたいです。

$ yarn test:all
yarn run v1.1.0
$ scripts/test.sh ./test/*.js
Starting our own testrpc instance
Using network 'testrpc'.
  Contract: AlisCrowdsale
    creating a valid rate customizable crowdsale
      ✓ should initial rate be 20,000 ALIS for pre sale (2415ms)
    Pre sale
      ✓ should reject payments if not white listed member (1197ms)
      ✓ should not lose ETH if payments were rejected in pre sale (1394ms)
      ✓ should accept payments if white listed member (1254ms)
      ✓ should accept payments 249,999 ALIS tokens (1202ms)
      ✓ should accept payments 250,000 ALIS tokens (1224ms)
      ✓ should reject payments 250,001 ALIS tokens (1157ms)
      ✓ should not lose ETH if payments 250,001 ALIS tokens (1427ms)
    Week1
      ✓ should rate of week1 be 2,900 ALIS when just started (2358ms)
      ✓ should rate of week1 be 2,900 ALIS when 1 minuit after started (2245ms)
      ✓ should accept payments over 250,001 ALIS tokens (1209ms)
      ✓ should accept payments until 250,000,000 ALIS tokens (1230ms)
      ✓ should reject payments over 250,000,000 ALIS tokens (1377ms)
      ✓ should rate of week1 be 2,900 ALIS when 1 minute before ended (2291ms)
    Week2
      ✓ should rate of week2 be 2,600 ALIS when just started (2272ms)
      ✓ should rate of week2 be 2,600 ALIS when 1 minuit after started (2318ms)
      ✓ should accept payments over 250,001 ALIS tokens (1259ms)
      ✓ should accept payments until 250,000,000 ALIS tokens (1250ms)
      ✓ should reject payments over 250,000,000 ALIS tokens (1211ms)
      ✓ should rate of week2 be 2,600 ALIS when few minuit before ended (2317ms)
    Week3
      ✓ should rate of week3 be 2,300 ALIS when just started (2269ms)
      ✓ should rate of week3 be 2,300 ALIS when 1 minuit after started (2272ms)
      ✓ should accept payments over 250,001 ALIS tokens (1260ms)
      ✓ should accept payments until 250,000,000 ALIS tokens (1288ms)
      ✓ should reject payments over 250,000,000 ALIS tokens (1203ms)
      ✓ should rate of week3 be 2,300 ALIS when few minute before ended (2268ms)
    From week4 to until the end of token sale
      ✓ should rate of week4 be 2,000 ALIS when just started (2267ms)
      ✓ should rate of week4 be 2,000 ALIS when few minute after started (2335ms)
      ✓ should accept payments over 250,001 ALIS tokens (1283ms)
      ✓ should accept payments until 250,000,000 ALIS tokens (1285ms)
      ✓ should reject payments over 250,000,000 ALIS tokens (1205ms)
      ✓ should rate of week4 be 2,000 ALIS when few minute before ended (2322ms)

コントラクトのデプロイ

次はプライベート・ネットにコントラクトをデプロイしてみます。EthereumのCLIクライアントであるgethの基本的な使い方は下記を参照してみてください。

Macのローカル環境でEthereumの送金を行う

ジェネシスブロックの生成

まずはALISの手順どおりconfig配下にdatadirフォルダを作成し、ジェネシスブロックを作成します。

$ cd config
$ mkdir datadir
$ geth init private.genesis.json --datadir ./datadir/

アカウント作成

次にアカウントを作ります。まだrpcで起動しなくていいので下記コマンドでCLIを立ち上げてアカウントを作成します。

geth --networkid "10" --nodiscover --datadir ./datadir/ console

立ち上げたら下記コマンドでアカウントを2つ作成しておきます。作成するとアドレスが発行されます。

> personal.newAccount("user1")
"0xe47840ec0669e3734235224567239ebeb640e6ef"
> personal.newAccount("user2")
"0xb625578df6a782419b157cad5a425a99f89bc6c8"

Solidityのコンパイル

コンパイルは下記コマンドで、完了するとDoneと表示されます。問題なければ/build配下にcontractsフォルダが生成されてコンパイル結果ができ上がります。

$ yarn run compile
yarn run v1.1.0
$ rm -rf ./build/* && truffle compile
Compiling ./contracts/AlisCrowdsale.sol...
Compiling ./contracts/AlisFund.sol...
Compiling ./contracts/AlisToken.sol...
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/WhitelistedCrowdsale.sol...
Compiling ./contracts/lib/BurnableToken.sol...
Compiling ./contracts/lib/MultiSigWallet.sol...
Compiling zeppelin/contracts/crowdsale/CappedCrowdsale.sol...
Compiling zeppelin/contracts/crowdsale/Crowdsale.sol...
Compiling zeppelin/contracts/crowdsale/FinalizableCrowdsale.sol...
Compiling zeppelin/contracts/crowdsale/RefundVault.sol...
Compiling zeppelin/contracts/crowdsale/RefundableCrowdsale.sol...
Compiling zeppelin/contracts/lifecycle/Pausable.sol...
Compiling zeppelin/contracts/math/SafeMath.sol...
Compiling zeppelin/contracts/ownership/Ownable.sol...
Compiling zeppelin/contracts/token/BasicToken.sol...
Compiling zeppelin/contracts/token/ERC20.sol...
Compiling zeppelin/contracts/token/ERC20Basic.sol...
Compiling zeppelin/contracts/token/MintableToken.sol...
Compiling zeppelin/contracts/token/StandardToken.sol...

Compilation warnings encountered:

/Users/ユーザー名/private_work/contracts/contracts/lib/MultiSigWallet.sol:39:13: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
            throw;
            ^---^
,/Users/ユーザー名/private_work/contracts/contracts/lib/MultiSigWallet.sol:45:13: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
            throw;
            ^---^
,/Users/ユーザー名/private_work/contracts/contracts/lib/MultiSigWallet.sol:51:13: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
            throw;
            ^---^
途中省略

Writing artifacts to ./build/contracts

✨  Done in 19.42s.

プライベート・ネットの起動とデプロイ

次にrpcを立ち上げます。

geth --datadir ./datadir --networkid 10 --ipcpath /Users/ユーザー名/Library/Ethereum/geth.ipc  --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --mine --minerthreads 4 --unlock 0,1  console 2>> /tmp/geth.log

立ち上がったら別コンソールからコントラクトをデプロイします。

yarn deployを行うとcleanの処理で/build配下が削除されてうまくいかなかったのでdeployコマンドを下記に修正して実行しました。

"deploy": "truffle deploy --network development"

Doneが表示されればデプロイが完了です。

$ yarn deploy
yarn run v1.1.0
$ truffle deploy --network development
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xc6d0d0558efbbb6226e76c80417214d22be2ae35a23fc01d72a57b2c6bc92f95
  Migrations: 0x6466ba3f656c8d017ce3823ee53f22b3742260b4
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying AlisFund...
  ... 0x16a0ec44dd41bc168d2b56e09a2aff8da6f48f93ee4c6208893891d4e3f792af
  AlisFund: 0xd479c6f832fd34def7b41cac7b1f859b954de4bd
  Deploying AlisCrowdsale...
  ... 0x91bed6362ec16831c999a0639b8bb6fb97e945dafbbd8498afe1234c01d80eef
  AlisCrowdsale: 0xa8e90a1e628f4bd34427383da5977f4cdd266c18
Saving artifacts...
✨  Done in 40.55s.

注意としてデプロイの前にtruffle.jsのdevelopのgasとgasPriceを変更しないとデプロイが成功しないので調整してみでください。自分は下記のようにしました。適当に調べてたら4712388がデフォルトの値だと記載されていたのでそうしてみました。

development: {
    host: "localhost",
    port: 8545,
    network_id: "*", // Match any network id
    gas: 4712388,
    gasPrice: 2100000000000
}

コントラクトの実行

デプロイまではスムーズに進んだんだけどこのあとどうすればいいか分からなくなったのでここは調査してまた追記します!

おすすめ書籍

Ethereumを使ったDApps開発を学びたいなら今だとこの1冊が1番良いです!開発環境の構築から使うべきツール、フレームワーク、実装方法・注意点まで網羅的に解説されている書籍なのでおすすめです。出版も2018年1月ということでかなり新しい本で、DMM Bitcoinを作っているネクストカレンシー所属の方が書いているので信頼できます。

ビットコインとブロックチェーンの詳細をしっかりと学びたい方にはこちらの書籍が非常におすすめです。ウォレットの仕組み、楕円曲線暗号、P2Pプロトコル、公開鍵暗号などビットコインを支える技術について詳細に解説されています。また、サンプルコードを通して実際に手を動かして学べるので非常に濃い内容となっています。

The following two tabs change content below.

髙妻智一

2013年CyberAgent新卒入社 スマホゲームを作る子会社に所属し、サーバーサイドのエンジニアを担当。2年目の終わりから新規子会社の立ち上げに参加し、サーバーサイドのエンジニアリーダーとしてサービースのリリースから運用までを担当。 2018年仮想通貨のスマホウォレットを提供するGinco Incにブロックチェーンエンジニアとして入社。






よく読まれている関連記事はこちら




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です