Use node.js to list and buy and sell Benz on oceanone, an open exchange

Time:2019-11-2

Use node.js to list and buy and sell Benz on oceanone, an open exchange

In the last lesson, we introduced how to trade bitcoin in oceanone. Oceanone supports the transaction of any token on the mixin network, including all erc20 and EOS tokens, without any formalities and fees, and can directly register the order. Here’s how to put an erc20 token on the oceanone transaction. Mastering the transaction method of erc20 token, you can trade any other mixin network token.

Here we use an erc20 token called Benz as an example. This token has been recharged into mixin network. You can see the total number and transaction of this token in mixin network in the blockchain browser

Preparatory knowledge:

First deposit the Ben into your wallet, and then usegetAssetsAPI reads its UUID

Get UUID of the currency

callgetAssetsThe API will return JSON data, such as:

  • asset_idUUID. of coins
  • public_keyThe address of the current wallet for the currency
  • symbolThe name of the coin
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);
}

callgetUserAssetsThe complete output of the API is as follows:

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

limit entry

  • Pay with limit priceA single below or equal to the market price
  • Sell with limited priceA single above or equal to the market price

Oceanone supports three basic types of prices: usdt, Xin, BTC, i.e. Benz / usdt, Benz / Xin, Benz / BTC, which is demonstrated here

Limit the price and put up a sales order

After registering the new currency, you need to wait about a minute, wait for oceanone to initialize the data related to the new currency

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!");
  }
}

Limit the price and pay for it

After registering the new currency, you need to wait about a minute, wait for oceanone to initialize the data related to the new currency

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!");
 }
}

Read price list of currency

Read the price list of currency to confirm whether the registration is successful!

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 related operation instructions

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

Complete code

Recommended Today

Details of multi-path and large capacity hard disk mount under CentOS

I. application environment and requirementsBlade servers connect HP storage through fiber switches, forming a 2×2 link The storage capacity of the operating system for CentOS 6.4 64 bit mount is 2.5t Based on this application environment, two problems need to be solved: In order to ensure the stability and transmission performance of the link, multi-path […]