使用 Node.js 在開放交易所OceanOne上掛單買賣奔馳幣

上一課中,咱們介紹瞭如何在OceanOne交易比特幣。OceanOne支持交易任何Mixin Network上的token,包括全部的ERC20和EOS token,不須要任何手續和費用,直接掛單便可。下面介紹如何將將一個ERC20 token掛上OceanOne交易。掌握了ERC20代幣的交易方法,就能夠交易任何其餘Mixin Network代幣了。node

此處咱們用一個叫作Benz的ERC20 token爲例。這個token已經被充值進Mixin Network,你能夠在區塊鏈瀏覽器看到這個token在Mixin Network內部的總數和交易ios

預備知識:

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

取得該幣的UUID

調用 getAssets API 會返回json數據, 如:github

  • asset_id 幣的UUID.
  • public_key 該幣的當前錢包的地址.
  • symbol 幣的名稱. 如: Benz.
if ( args.type === TYPE_WALLET_ASSETS_INFO ) {
  const assetsInfo = await newUserClient.getUserAssets();
  console.log("-AssetID--Asset--Balance--public_key--");
  assetsInfo.forEach(function(element) {
     console.log(element.asset_id + " " +
                 element.symbol + " " +
                 element.balance + " " +
                 element.public_key + " " +
                 element.account_name + " " +
                 element.account_tag
               );
   });
  // console.log(assetsInfo);
}
複製代碼

調用 getUserAssets API的完整輸出以下:json

Make your choose aw: Read Wallet All Asssets Information
You choice to : { type: 'aw: Read Wallet All Asssets Information' }
You wallet is : 0b10471b-1aed-3944-9eda-5ab947562761
-AssetID--Asset--Balance--public_key--
2b9c216c-ef60-398d-a42a-eba1b298581d  Benz  99.9  0xA35722B0a5Ab20f2d2276999F5b18D42C71Ba688
6cfe566e-4aad-470b-8c9a-2fd35b49c68d  EOS  0   eoswithmixin  30f0c36057b9b22151173b309bd0d79c
965e5c6e-434c-3fa9-b780-c50f43cd955c  CNB  999.99999993  0xA35722B0a5Ab20f2d2276999F5b18D42C71Ba688
c6d0c728-2624-429b-8e0d-d9d19b6592fa  BTC  0  15MySY7UnA827TRMQWuCKGiogCYXUmt21M
複製代碼

限價掛單

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

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

限價掛賣單.

新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.瀏覽器

else if ( args.type === TYPE_OO_SELL_ERC_USDT ) {
  var prompts = [
    {
      name: 'price',
      type: 'input',
      message: "Input the price of ERC(Benz)/USDT: ",
    },
  ];
  price = await inquirer.prompt(prompts);
  var prompts = [
    {
      name: 'amount',
      type: 'input',
      message: "Input the amount of ERC20(Benz): ",
    },
  ];
  amount = await inquirer.prompt(prompts);
  console.log(price);
  console.log(amount);
  const memo = GenerateOceanMemo(USDT_ASSET_ID,"A",price.price);
  const assetInfo = await newUserClient.getUserAsset(ERC20_BENZ);
  console.log("The Wallet 's USDT balance is ", assetInfo.balance);
  if ( assetInfo.balance >= amount.amount ) {
    const Obj = {
      assetId: ERC20_BENZ,
      recipientId: OCEANONE_BOT,
        traceId: newUserClient.getUUID(),
        amount: amount.amount,
        memo: memo,
      }
      const transInfo = await newUserClient.transferFromBot(Obj);
      console.log(transInfo);
      console.log("The Order id is " + transInfo.trace_id + " It is needed to cancel the order!");
  } else {
    console.log("Not enough ERC20_BENZ!");
  }
}
複製代碼

限價掛買單.

新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.bash

else if ( args.type === TYPE_OO_BUY_ERC_USDT ) {
 var prompts = [
   {
     name: 'price',
     type: 'input',
     message: "Input the price of ERC20(Benz)/USDT: ",
   },
 ];
 price = await inquirer.prompt(prompts);
 var prompts = [
   {
     name: 'amount',
     type: 'input',
     message: "Input the amount of USDT: ",
   },
 ];
 amount = await inquirer.prompt(prompts);
 console.log(price);
 console.log(amount);
 const memo = GenerateOceanMemo(ERC20_BENZ,"B",price.price);
 const assetInfo = await newUserClient.getUserAsset(USDT_ASSET_ID);
 console.log("The Wallet 's USDT balance is ", assetInfo.balance);
 if ( assetInfo.balance >= amount.amount && assetInfo.balance >= 1 ) {
   const Obj = {
     assetId: USDT_ASSET_ID,
     recipientId: OCEANONE_BOT,
       traceId: newUserClient.getUUID(),
       amount: amount.amount,
       memo: memo,
     }
     const transInfo = await newUserClient.transferFromBot(Obj);
     console.log(transInfo);
     console.log("The Order id is " + transInfo.trace_id + " It is needed to cancel the order!");
 } else {
   console.log("Not enough USDT!");
 }
}
複製代碼

讀取幣的價格列表

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

else if ( args.type === TYPE_OO_FETCH_ERC_USDT ) {
  FetchOceanOneMarketInfos(ERC20_BENZ, USDT_ASSET_ID);
}
function FetchOceanOneMarketInfos(asset_id, base_asset) {
  var instance = axios.create({
  baseURL: "https://events.ocean.one/markets/" + asset_id + "-" + base_asset + "/book",
  timeout: 3000,
  headers: {'X-Custom-Header': 'foobar'}
  });
  instance.get()
  .then(function(response) {
    console.log("--Price--Amount--Funds--Side")
    response.data.data.data.asks.forEach(function(element) {
       console.log(element.price + " " +
                   element.amount + " " +
                   element.funds + " " +
                   element.side);
     });
     response.data.data.data.bids.forEach(function(element) {
        console.log(element.price + " " +
                    element.amount + " " +
                    element.funds + " " +
                    element.side);
      });
    // console.log(response.data.data.data.asks);
  });
}
複製代碼

ERC20相關的操做指令

Commands list of this source code:區塊鏈

  • 21: Fetch ERC20/USDT order book
  • 24: Sell ERC20/USDT
  • 27: Buy ERC20/USDT
  • 28: Cancel the order
  • q: Exit

完整的代碼

相關文章
相關標籤/搜索