LoginSignup
4
2

More than 5 years have passed since last update.

0x.jsを使って0xプロトコルのスマートコントラクトを使用する(概要編)

Last updated at Posted at 2018-12-18

モチベーション

DEX(分散型取引所)のためのプロトコルである0x[1]を触り、0x.jsにて、オーダーの作成、記入、検証を行う。0x.jsの詳細に関してはまた次回まとめる。間違い、ご意見あればTwitter経由でご連絡ください。

セットアップ

0x.jsはEthereumと接続しアプリケーションとブロックチェーン間のインターフェースとなる役割がある。Ethereum nodeとしてはganache-cliを用いる。

0xが用意してくれている0x starter project[2]を使用する。
git clone https://github.com/0xProject/0x-starter-project.git

ディペンデンシーをインストールするためにyarnを用いる。
1. yarnをインストールしていない場合
brew install yarn --without-node
2. yarnをインストールしている場合
yarn
3. 最新のGanache 0x snapshotをダウンロード
yarn download_snapshot
4. もう1つターミナルを開きganacheを起動する
yarn ganache-cli

ここまでをまとめると以下のような感じ。
Screen Shot 2018-12-18 at 13.34.51.png

Stareter Projectで用意されたfill_order_erc20を実行する。
yarn scenario:fill_order_erc20

fill_order_erc20はWETHとZRXを交換するオーダー

yarn run v1.12.3
$ cross-env yarn build && node ./lib/scenarios/fill_order_erc20.js
$ cross-env tsc
┌────────────┐
│ Fill Order │
└────────────┘
 Accounts
 Maker     0x5409ed021d9299bf6814279a6a1411a7e866a631
 Taker     0x6ecbe1db9ef729cbe972c83fb886247691fb6beb
 Setup
 Maker ZRX Approval   0xf8f93512e3ebaaf57fa0eb9dd1c485b6f4633c37d199e9a913e76d05084f2604
 Taker WETH Approval  0x292759c139ed6fdc40646083c0346a9630b2b33cfed2947e1a21f289c8eb703a
 Taker WETH Deposit   0x566b6a876c4482849cab9563b6a1ebd03eb31201c2b028c15770b185a4b4aab1
 Order
 exchangeAddress        0x35dd2932454449b14cee11a94d3674a936d5d7b2
 makerAddress           0x5409ed021d9299bf6814279a6a1411a7e866a631
 takerAddress           0x0000000000000000000000000000000000000000
 senderAddress          0x0000000000000000000000000000000000000000
 feeRecipientAddress    0x0000000000000000000000000000000000000000
 expirationTimeSeconds  1545108409
 salt                   7119368013313422661221815072055438267987023853367738570035607824850986278…
 makerAssetAmount       5000000000000000000
 takerAssetAmount       100000000000000000
 makerAssetData         0xf47261b00000000000000000000000002002d3812f58e35f0ea1ffbf80a75a38c32175fa
 takerAssetData         0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c
 makerFee               0
 takerFee               0

Allowances
 Token  Maker     Taker
 WETH   MAX_UINT  MAX_UINT
 ZRX    MAX_UINT  MAX_UINT

Balances
 Token  Maker                 Taker
 WETH   2.43070141021160417   1.536283635869423603
 ZRX    9998.9999945          929.25100549999999803
 ETH    2.169765236821913251  0.102365525385971059

Transaction
┌───────────┬────────────────────────────────────────────────────────────────────┐
│ fillOrder │ 0x16f4754cc08f9f76f53185685c591e384dffdcc21165254f60840082fd1585b6 │
├───────────┼────────────────────────────────────────────────────────────────────┤
│ orderHash │ 0x2f25d912d6e5384eb2ab73275c0ba66ff39f7f132ce6b7e5b79be018baa4bcf5 │
├───────────┼────────────────────────────────────────────────────────────────────┤
│ gasUsed   │ 117586                                                             │
├───────────┼────────────────────────────────────────────────────────────────────┤
│ status    │ Success                                                            │
└───────────┴────────────────────────────────────────────────────────────────────┘

Logs
 Fill
 contract                0x35dd2932454449b14cee11a94d3674a936d5d7b2
 makerAddress            0x5409ed021d9299bf6814279a6a1411a7e866a631
 feeRecipientAddress     0x0000000000000000000000000000000000000000
 takerAddress            0x6ecbe1db9ef729cbe972c83fb886247691fb6beb
 senderAddress           0x6ecbe1db9ef729cbe972c83fb886247691fb6beb
 makerAssetFilledAmount  5000000000000000000
 takerAssetFilledAmount  100000000000000000
 makerFeePaid            0
 takerFeePaid            0
 orderHash               0x2f25d912d6e5384eb2ab73275c0ba66ff39f7f132ce6b7e5b79be018baa4bcf5
 makerAssetData          0xf47261b00000000000000000000000002002d3812f58e35f0ea1ffbf80a75a38c32175fa
 takerAssetData          0xf47261b0000000000000000000000000d0a1e359811322d97991e03f863a0c30c2cf029c
 Transfer
 contract  0x2002d3812f58e35f0ea1ffbf80a75a38c32175fa
 _from     0x5409ed021d9299bf6814279a6a1411a7e866a631
 _to       0x6ecbe1db9ef729cbe972c83fb886247691fb6beb
 _value    5000000000000000000
 Transfer
 contract  0xd0a1e359811322d97991e03f863a0c30c2cf029c
 _from     0x6ecbe1db9ef729cbe972c83fb886247691fb6beb
 _to       0x5409ed021d9299bf6814279a6a1411a7e866a631
 _value    100000000000000000

Balances
 Token  Maker                 Taker
 WETH   2.53070141021160417   1.436283635869423603
 ZRX    9993.9999945          934.25100549999999803
 ETH    2.169765236821913251  0.102247939385971059
✨  Done in 70.05s.

ここでは、ganache上のアカウント1(0x5409ed021d9299bf6814279a6a1411a7e866a631)がオーダーを作成し、アカウント2(0x6ecbe1db9ef729cbe972c83fb886247691fb6beb)がオーダーを記入している。つまりアカウント1がMakerであり、アカウント2がTakerである。Balanceをみると、MakerのWETHが増えており、TakerのZRXが少なくなっているのが分かる。

参考記事

  1. 0x org (https://0x.org/)
  2. 0x starter project (https://github.com/0xProject/0x-starter-project)
  3. 0x doc (https://0xproject.com/wiki#Build-A-Relayer)
4
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
4
2