Using java to list and buy any erc20 token on oceanone, a decentralized exchange

Time:2019-11-20

Using java to list and buy any erc20 token on oceanone, a decentralized 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!

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 ( input.equals("aw") ) {
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  JsonArray assets = mixinApiUser.getAssets();
  System.out.println("------------------------All Assets Information---------------------------");
  System.out.println(assets);
  assets.forEach((element) ->  {
     JsonObject jsonObj = element.getAsJsonObject();
     System.out.println(jsonObj.get("asset_id").getAsString() + " " +
                        jsonObj.get("symbol").getAsString() + " " +
                        jsonObj.get("public_key").getAsString() + " " +
                        jsonObj.get("balance").getAsString() );
  });
  System.out.println("-----------------------------------------------------------------------");
}

callgetAssetsThe complete output of the API is as follows:

Make your choose(eg: q for Exit!): aw
aw
------------------------All Assets Information---------------------------
[{"type":"asset","asset_id":"2b9c216c-ef60-398d-a42a-eba1b298581d","chain_id":"43d61dcd-e413-450d-80b8-101d5e903357","symbol":"Benz","name":"Benz coin","icon_url":"https://images.mixin.one/yH_I5b0GiV2zDmvrXRyr3bK5xusjfy5q7FX3lw3mM2Ryx4Dfuj6Xcw8SHNRnDKm7ZVE3_LvpKlLdcLrlFQUBhds=s128",
"balance":"1000","public_key":"0x62F20013472a72b8Fe3f9a99f36e5802E6e93c15","account_name":"",
"account_tag":"","price_btc":"0","price_usd":"0","change_btc":"0","change_usd":"0",
"asset_key":"0xc409b5696c5f9612e194a582e14c8cd41ecdbc67","confirmations":100,
"capitalization":0},{"type":"asset","asset_id":"6cfe566e-4aad-470b-8c9a-2fd35b49c68d",
"chain_id":"6cfe566e-4aad-470b-8c9a-2fd35b49c68d","symbol":"EOS","name":"EOS",
"icon_url":"https://images.mixin.one/a5dtG-IAg2IO0Zm4HxqJoQjfz-5nf1HWZ0teCyOnReMd3pmB8oEdSAXWvFHt2AJkJj5YgfyceTACjGmXnI-VyRo=s128","balance":"0","public_key":"","account_name":"eoswithmixin","account_tag":"889ed66a1059bc3dab60e2ee44d0f993",
"price_btc":"0.0008953","price_usd":"4.73591781","change_btc":"-0.004161988128557199","change_usd":"0.00012182287348895748",
"asset_key":"eosio.token:EOS","confirmations":64,"capitalization":0},
{"type":"asset","asset_id":"965e5c6e-434c-3fa9-b780-c50f43cd955c","chain_id":"43d61dcd-e413-450d-80b8-101d5e903357","symbol":"CNB","name":"Chui Niu Bi",
"icon_url":"https://images.mixin.one/0sQY63dDMkWTURkJVjowWY6Le4ICjAFuu3ANVyZA4uI3UdkbuOT5fjJUT82ArNYmZvVcxDXyNjxoOv0TAYbQTNKS=s128",
"balance":"0.99984","public_key":"0x62F20013472a72b8Fe3f9a99f36e5802E6e93c15","account_name":"",
"account_tag":"","price_btc":"0","price_usd":"0","change_btc":"0","change_usd":"0",
"asset_key":"0xec2a0550a2e4da2a027b3fc06f70ba15a94a6dac","confirmations":100,
"capitalization":0},{"type":"asset","asset_id":"c6d0c728-2624-429b-8e0d-d9d19b6592fa",
"chain_id":"c6d0c728-2624-429b-8e0d-d9d19b6592fa","symbol":"BTC","name":"Bitcoin",
"icon_url":"https://images.mixin.one/HvYGJsV5TGeZ-X9Ek3FEQohQZ3fE9LBEBGcOcn4c4BNHovP4fW4YB97Dg5LcXoQ1hUjMEgjbl1DPlKg1TW7kK6XP=s128","balance":"0",
"public_key":"15RNBb5GzoXWRoEMT5MJL3pqzSjdPoXf6J","account_name":"","account_tag":"",
"price_btc":"1","price_usd":"5289.7409549","change_btc":"0","change_usd":"0.004160319338628907",
"asset_key":"c6d0c728-2624-429b-8e0d-d9d19b6592fa","confirmations":6,"capitalization":0}]
2b9c216c-ef60-398d-a42a-eba1b298581d Benz 0x62F20013472a72b8Fe3f9a99f36e5802E6e93c15 1000
6cfe566e-4aad-470b-8c9a-2fd35b49c68d EOS  0
965e5c6e-434c-3fa9-b780-c50f43cd955c CNB 0x62F20013472a72b8Fe3f9a99f36e5802E6e93c15 0.99984
c6d0c728-2624-429b-8e0d-d9d19b6592fa BTC 15RNBb5GzoXWRoEMT5MJL3pqzSjdPoXf6J 0

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

if ( subinput.equals("x2") ) {
  MakeTheSellOrder(ERC20_BENZ,USDT_ASSET_ID);
}
public static void MakeTheSellOrder(String AssetID, String BaseAssetID) {
  System.out.print(String.format("Please input the %s price of %s: ",AssetID,BaseAssetID));
  String pinput = System.console().readLine();
  System.out.println(pinput);

  System.out.print(String.format("Please input the %s amount: ",AssetID));
  String aminput = System.console().readLine();
  System.out.println(aminput);
  float amountf = Float.valueOf(aminput.trim()).floatValue();

  String OrderMemo = GenerateOrderMemo("A",BaseAssetID,pinput);
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  // UUID usdtUUID         =  UUID.fromString(USDT_ASSET_ID);
  // String memoTarget     = encodeUUID(usdtUUID);
  System.out.println("------------------Ocean.one-EXCHANGE----------------------------");
  System.out.println(OrderMemo);
  JsonObject asset = mixinApiUser.getAsset(AssetID);
  System.out.println(asset);
  System.out.println(asset.get("balance").getAsFloat());
  if ( asset.get("balance").getAsFloat()  > 0 && asset.get("balance").getAsFloat() >= amountf ) {
      JsonObject transInfo = mixinApiUser.transfer(AssetID, OCEANONE_BOT,
                                                   aminput,
                                                   OrderMemo);
      System.out.println(String.format("--------------%s Transfer To EXCHANGE Information---------",AssetID));
      System.out.println(transInfo);
      System.out.println(String.format("---Order is %s: ------",transInfo.get("trace_id").getAsString()));
   } else System.out.println(String.format("----------------Not enough %s--------------------------",AssetID));
}

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

if ( subinput.equals("x1") ) {
  MakeTheBuyOrder(ERC20_BENZ,USDT_ASSET_ID);
}
public static void MakeTheBuyOrder(String AssetID, String BaseAssetID) {
  System.out.print(String.format("Please input the %s price of %s: ",AssetID,BaseAssetID));
  String pinput = System.console().readLine();
  System.out.println(pinput);

  System.out.print(String.format("Please input the %s amount: ",BaseAssetID));
  String aminput = System.console().readLine();
  System.out.println(aminput);
  float amountf = Float.valueOf(aminput.trim()).floatValue();

  String OrderMemo = GenerateOrderMemo("B",AssetID,pinput);
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  // UUID usdtUUID         =  UUID.fromString(USDT_ASSET_ID);
  // String memoTarget     = encodeUUID(usdtUUID);
  System.out.println("------------------Ocean.one--EXCHANGE----------------------------");
  System.out.println(OrderMemo);
  JsonObject asset = mixinApiUser.getAsset(BaseAssetID);
  System.out.println(asset);
  System.out.println(asset.get("balance").getAsFloat());
  if ( asset.get("balance").getAsFloat()  > 0 && asset.get("balance").getAsFloat() >= amountf ) {
      JsonObject transInfo = mixinApiUser.transfer(BaseAssetID, OCEANONE_BOT,
                                                   aminput,
                                                   OrderMemo);
       System.out.println(String.format("--------------%s Transfer To EXCHANGE Information---------",BaseAssetID));
       System.out.println(transInfo);
       System.out.println(String.format("---Order is %s: ------",transInfo.get("trace_id").getAsString()));
    } else System.out.println(String.format("----------------Not enough %s--------------------------",BaseAssetID));
}

Read price list of currency

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

if ( subinput.equals("x") ) {
  FetchOceanMarketInfos(ERC20_BENZ,XIN_ASSET_ID);
}
private static void FetchOceanMarketInfos(String targetAssetID, String baseAssetID) {
  OkHttpClient client = new OkHttpClient();
  String baseUrl = "https://events.ocean.one/markets/%s-%s/book ";
  String fullUrl = String.format(baseUrl,targetAssetID,baseAssetID);
  // String fullUrl = baseUrl + url;
  System.out.println(fullUrl);
  Request request = new Request.Builder()
                             .url(fullUrl)
                             .build();
  try {
     Response response = client.newCall(request).execute();
     if (!response.isSuccessful()) {
       throw new IOException("Unexpected code " + response);
     }
     String res = response.body().string();
     // System.out.println(res);
     JsonParser parser = new JsonParser();
     JsonElement jsonTree = parser.parse(res);
     JsonObject orders;
     orders =  jsonTree.getAsJsonObject();
     JsonArray asksOrders = orders.get("data").getAsJsonObject().
                                   get("data").getAsJsonObject().
                                   get("asks").getAsJsonArray();
     JsonArray bidsOrders = orders.get("data").getAsJsonObject().
                                   get("data").getAsJsonObject().
                                   get("bids").getAsJsonArray();
     // System.out.println(orders.get("data").getAsJsonObject().get("data").getAsJsonObject().get("bids").getAsJsonArray());
     System.out.println("--Side--Price--Amount--Funds---");
     asksOrders.forEach((element) ->  {
        JsonObject jsonObj = element.getAsJsonObject();
        System.out.println(jsonObj.get("side").getAsString() + " " +
                           jsonObj.get("price").getAsString() + " " +
                           jsonObj.get("amount").getAsString() + " " +
                           jsonObj.get("funds").getAsString() );
     });
     bidsOrders.forEach((element) ->  {
        JsonObject jsonObj = element.getAsJsonObject();
        System.out.println(jsonObj.get("side").getAsString() + " " +
                           jsonObj.get("price").getAsString() + " " +
                           jsonObj.get("amount").getAsString() + " " +
                           jsonObj.get("funds").getAsString() );
     });
     System.out.println("----endo--of--btc/usdt----");
     return;
   } catch(Exception e) { e.printStackTrace(); }
   return;
}

Erc20 related operation instructions

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

Complete code