2017年5月14日日曜日

スマートコントラクトプログラミングを始めてみる

スマートコントラクトプログラミング用の環境を設定する
参考サイト①に沿って進めた。

使用言語

  • python3

環境

  • macOS Sierra ver10.12.4

依存関係のあるパッケージをインストールする

先ずはhomebrewで関連パッケージを取得
    brew install pkg-config libffi autoconf automake libtool openssl
次にpipでpopulusをインストールする
    $ pip install populus
geventの使用も勧められているが、ここではスルーする。
ネットワーク系のライブラリらしいが、中身もよくわからないので。

プロジェクトの初期化

適当に作ったディレクトリに移動して以下コマンドをターミナルに入力する。
結果は参考サイトと全く同じになるので問題ないだろう。
テスト用のコードやプロジェクト用のjsonファイルが自動生成されているのだろう。
    $ populus init
    Wrote default populus configuration to `./populus.json`.
    Created Directory: ./contracts
    Created Example Contract: ./contracts/Greeter.sol
    Created Directory: ./tests
    Created Example Tests: ./tests/test_greeter.py

コンパイルしてみる

早速コンパイルしてみる
    $ populus compile
するとsolcがない、というError発生。おそらくコンパイラがないのだろうと推測する。
参考サイト②の通りにsolodityをインストールする。
    $ brew update
    $ brew upgrade
    $ brew tap ethereum/ethereum
    $ brew install solidity #20min以上かかるのとCPUフル使用でPCが熱くなるので注意
    $ brew linkapps solidity  #以下の不安な警告が出る、しかしコンパイルは問題なくできる
    Warning:`brew linkapps` has been deprecated and will eventually be removed!
    Unfortunately `brew linkapps` cannot behave nicely with e.g. Spotlight using
    either aliases or symlinks and Homebrew formulae do not build "proper" `.app`
    bundles that can be relocated. Instead, please consider using `brew cask` and
    migrate formulae using `.app`s to casks.
改めてコンパイルしてみる
参考サイトと若干異なるがコンパイルが成功したようだ。
    $ populus compile
    > Found 1 contract source files
      - contracts/Greeter.sol
    > Compiled 1 contracts
      - Greeter
    > Wrote compiled assets to: build/contracts.json

コントラクトのテスト

greeterというコントラクトのテストをしてみる。いわゆるHello,Worldのようなものだろう。
    $ py.test tests/
    =========================================== test session starts ============================================
    platform darwin -- Python 3.5.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
    rootdir: /Users/xxx/Documents/testSC1, inifile:
    plugins: populus-1.6.9
    collected 2 items 
    
    tests/test_greeter.py ..
    
    ========================================= 2 passed in 0.54 seconds =========================================
おそらく成功したようだ。しかし結果として何かの挨拶を出力するわけではないのか? ひとまずはこれでよしとしよう。

参考サイト

①:https://populus.readthedocs.io/en/latest/quickstart.html
②:http://solidity.readthedocs.io/en/develop/installing-solidity.html

0 件のコメント:

コメントを投稿