LoginSignup
3
2

More than 5 years have passed since last update.

Vyper × Truffleやってみた

Posted at

truffleのバージョン5.0.0がリリースされました。
https://github.com/trufflesuite/truffle/releases/tag/v5.0.0

Vyperのサポートを追加したとのことでとりあえず触ってみるやつです。
フレームワークとしてはpopulusがVyperをサポートしていく感じになるのかと思ってましたが、どうやら開発あまり進んでいないようで...
https://github.com/ethereum/populus

開発環境

Ubuntu 18.04
Python 3.7
Nodejs 11.4.0
Ganache-Cli v6.2.3
Solidity 0.5.2+commit.1df8f40c.Linux.g++
Vyper 0.1.0b6

truffle 5.0.0インストール

なぜかupdateしてもバージョン変わらなかったので、uninstallして再インストールしました。

$ sudo npm install -g truffle
$ truffle version
Truffle v5.0.0 (core: 5.0.0)
Solidity v0.5.0 (solc-js)
Node v11.4.0

余談ですが、truffle 5.0.0のタイトルがすごく好きです。
今回のアップデートでモチアラモードになりました。
v5.0.0-beta.0 – Chocolate Sushi
v5.0.0-beta.1 – Hazelnut Onigiri
v5.0.0-beta.2 – Bento Box of Candy
v5.0.0 – Mochi à la Mode

調べてて気づきましたが、dockerで使えるようにしてくれてる方がいたのでこちらを使ってもいいかもです。
https://qiita.com/sot528/items/d18d09713d54a8dbfb19

適当なディレクトリを作ってunboxコマンドを実行します。

$ truffle unbox vyper-example

いつものtruffleのディレクトリができました。
contractsディレクトリにはVyperStorage.vyというデータのgetとsetのみを行うシンプルなコントラクトが書かれています。
そのままcompileしてtest実行してみます。

$ truffle compile
Compiling ./contracts/VyperStorage.vy...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

Writing artifacts to ./build/contracts

$ truffle test
Using network 'test'.



  Contract: VyperStorage
    ✓ ...should store the value 89. (135ms)


  1 passing (158ms)

テストOKでした。

せっかくvyperをサポートしたということなので他のvyperのコントラクトも試してみます。
こちらにvyper contractのexampleがあります。
https://github.com/ethereum/vyper/tree/master/examples

今回はシンプルにERC20でいきます。

ちなみにVyperにはオンラインエディタもあります。
https://vyper.online

Vscodeでのシンタックスハイライトもこちらの拡張機能を入れることでできるようになります。
https://github.com/p-/vscode-vyper/releases

truffle init

truffleを初期から作成します。

$ truffle init

contractsディレクトリにERC20.vyをそのまま持ってきます。不要なファイルは消しときます。
https://github.com/ethereum/vyper/blob/master/examples/tokens/ERC20.vy

compileしてみる。成功した模様。

$ truffle compile
Compiling ./contracts/ERC20.vy...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

Writing artifacts to ./build/contracts

migrateする

ganache上にmigrateしてみます。
truffle-config.jsのネットワークの設定を変更してganacheを起動します。

truffle-config.js
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: 8545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
    },
$ truffle migrate
⚠️  Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.
Try: npm install --save truffle-hdwallet-provider@web3-one


Starting migrations...
======================
> Network name:    'development'
> Network id:      1545574627098
> Block gas limit: 6721975


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

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x419afd40a8f33910729971438ef282bb7d55ff89fac5ce8e0a2900076205b238
   > Blocks: 0            Seconds: 0
   > contract address:    0x4cA163379b840810dB88F1E20290756CbE2Dbc6b
   > account:             0x06855F4d58fCd7Bf07b569c294C3a966C4453F44
   > balance:             99.99430184
   > gas used:            284908
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00569816 ETH


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


Summary
=======
> Total deployments:   1
> Final cost:          0.00569816 ETH

ganacheのlog

Listening on 127.0.0.1:8545
net_version
eth_accounts
eth_getBlockByNumber
eth_accounts
eth_getBlockByNumber
net_version
eth_getBlockByNumber
eth_getBlockByNumber
net_version
eth_estimateGas
eth_getBlockByNumber
eth_blockNumber
net_version
eth_sendTransaction

  Transaction: 0x419afd40a8f33910729971438ef282bb7d55ff89fac5ce8e0a2900076205b238
  Contract created: 0x4ca163379b840810db88f1e20290756cbe2dbc6b
  Gas usage: 284908
  Block Number: 1
  Block Time: Sun Dec 23 2018 23:17:15 GMT+0900 (GMT+09:00)

eth_getTransactionReceipt
eth_getCode
eth_getTransactionByHash
eth_getBalance
eth_getBlockByNumber
eth_getBlockByNumber
eth_sendTransaction

  Transaction: 0x187e7d4d31ddef70bf07f64f595415a334a673c7f2e92147228359073202b27d
  Gas usage: 42034
  Block Number: 2
  Block Time: Sun Dec 23 2018 23:17:15 GMT+0900 (GMT+09:00)

これでbuild配下にjsonができてabiの設定などができているのでこれを利用してデプロイすればいけるはず。
と言いつつデプロイどうやってやるのかイマイチよくわかってません。
initのパラメータ渡してあげないといけないと思いますが、truffleでweb3.jsでできるのかな。。。?
Vyperのドキュメントを見る限りはgethとかMist使ってやってください的な感じですね。
長くなってきましたし、次回の課題ということでこの記事は一旦ここまで。

3
2
0

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
3
2