用PHP在去中心化交易所OceanOne上掛單買賣任意ERC20 token

mixin發表於2019-05-02

上一課中,我們介紹瞭如何在OceanOne交易比特幣。OceanOne支援交易任何Mixin Network上的token,包括所有的ERC20和EOS token,不需要任何手續和費用,直接掛單即可。下面介紹如何將將一個ERC20 token掛上OceanOne交易!在掌握了ERC20 token之後,就可以把任何token在Ocean上買賣。

此處我們用一個叫做Benz的ERC20 token為例。這個token已經被充值進Mixin Network,你可以在區塊鏈瀏覽器看到這個token在Mixin Network內部的總數和交易

預備知識:

先將Benz幣存入你的錢包,然後使用getAssets API讀取它的UUID.

取得該幣的UUID

呼叫 getAssets API 會返回json資料, 如:

  • asset_id 幣的UUID.
  • public_key 該幣的當前錢包的地址.
  • symbol 幣的名稱. 如: Benz.
if ($line == 'aw') {
  $mixinSdk_eachAccountInstance = GenerateWalletSDKFromCSV();
  $asset_info = $mixinSdk_eachAccountInstance->Wallet()->readAssets();
  foreach ($asset_info as $key => $asset) {
    echo  $asset["symbol"] . "   " . $asset["asset_id"] ."   ". $asset["balance"] .
          "   ". $asset["public_key"].PHP_EOL;
  }
}
複製程式碼

呼叫 getAssets API的完整輸出如下:

Make your choose:aw
run...
client id is:26b20aa5-40c0-3e00-9de0-666cfb6f2daa
Benz   2b9c216c-ef60-398d-a42a-eba1b298581d   799   0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
EOS   6cfe566e-4aad-470b-8c9a-2fd35b49c68d   0
CNB   965e5c6e-434c-3fa9-b780-c50f43cd955c   4.72599997   0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
BTC   c6d0c728-2624-429b-8e0d-d9d19b6592fa   0   17z1Rq3VsyvvXvGWiHT8YErjBoFgnhErB8
XIN   c94ac88f-4671-3976-b60a-09064f1811e8   0.01   0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
複製程式碼

限價掛單

  • 掛限價買單 低於或者等於市場價的單.
  • 掛限價賣單 高於或者是等於市場價的單.

OceanOne支援三種基類價格: USDT, XIN, BTC, 即: Benz/USDT, Benz/XIN, Benz/BTC, 這兒示範Benz/USDT.

限價掛賣單.

新幣掛單後,需要等一分鐘左右,等OceanOne來初始化新幣的相關資料.

if ( $ocmd == 's1') {
  $p = readline("Input the Price of XIN/USDT: ");
  $a = readline("Input the Amount of XIN: ");
  $tMemo = GenerateOrderMemo("A",USDT_ASSET_ID,$p);
  echo $tMemo .  PHP_EOL;
  $mixinSdk_WalletInstance = GenerateWalletSDKFromCSV();
  $asset_info = $mixinSdk_WalletInstance->Wallet()->readAsset(XIN_ASSET_ID);
  print_r($asset_info);
  if ( (float) $asset_info["balance"] >= (float) $a ) {
    $transInfos = $mixinSdk_WalletInstance->Wallet()->transfer(XIN_ASSET_ID,OCEANONE_BOT,
                                                $mixinSdk_WalletInstance->getConfig()['default']['pin'],
                                                $a,
                                                $tMemo);
    print_r($transInfos);
    echo "The Order ID (trace_id) is: " . $transInfos["trace_id"] . PHP_EOL;
  } else { echo "Not enough XIN!\n";}
}
複製程式碼

限價掛買單.

新幣掛單後,需要等一分鐘左右,等OceanOne來初始化新幣的相關資料.

if ( $ocmd == 'b1') {
  $p = readline("Input the Price of XIN/USDT: ");
  $a = readline("Input the Amount of USDT: ");
  $tMemo = GenerateOrderMemo("B",XIN_ASSET_ID,$p);
  echo $tMemo .  PHP_EOL;
  $mixinSdk_WalletInstance = GenerateWalletSDKFromCSV();
  $asset_info = $mixinSdk_WalletInstance->Wallet()->readAsset(USDT_ASSET_ID);

  print_r($asset_info);
  if ( ((float) $asset_info["balance"] >= 1) && ( (float) $asset_info["balance"] >= (float) $a ) ) {
    $transInfos = $mixinSdk_WalletInstance->Wallet()->transfer(USDT_ASSET_ID,OCEANONE_BOT,
                                                $mixinSdk_WalletInstance->getConfig()['default']['pin'],
                                                $a,
                                                $tMemo);
    print_r($transInfos);
    echo "The Order ID (trace_id) is: " . $transInfos["trace_id"] . PHP_EOL;
  } else { echo "Not enough USDT!\n";}
}
複製程式碼

讀取幣的價格列表

讀取幣的價格列表,來確認掛單是否成功!

    if ( $ocmd == '2') { getOceanOneMarketInfos(ERC20_BENZ,USDT_ASSET_ID);}
    function getOceanOneMarketInfos($targetCoin, $baseCoin)  {
      $client = new GuzzleHttp\Client();
      $baseUrl = "https://events.ocean.one/markets/".$targetCoin."-".$baseCoin."/book";
      $res = $client->request('GET', $baseUrl, [
          ]);
      if ($res->getStatusCode() == "200") {
        // echo $res->getStatusCode() . PHP_EOL;
        $resInfo = json_decode($res->getBody(), true);
        echo "Side | Price | Amount | Funds" . PHP_EOL;
        foreach ($resInfo["data"]["data"]["asks"] as $key => $exchange) {
          echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL;
        }
        foreach ($resInfo["data"]["data"]["bids"] as $key => $exchange) {
          echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL;
        }
      }
    }
複製程式碼

ERC20相關的操作指令

Commands list of this source code:

  • trb:Transfer ERC20 from Bot to Wallet
  • trm:Transfer ERC20 from Wallet to Master
  • o: Ocean.One Exchange

Make your choose(eg: q for Exit!):

  • x: Orders-Book of ERC20/USDT
  • x1: Buy ERC20 pay USDT
  • x2: Sell ERC20 get USDT
  • c: Cancel the order
  • q: Exit

完整的程式碼

相關文章