Scheme 1: Exchange coins through ExinCore API

Exincore provides a Mixin Network-based coin exchange API.

You can pay USDT to ExinCore, and ExinCore will transfer the bitcoins you buy to you at the lowest price and most favorable transaction fee. Every transaction is anonymous and can be verified on the chain. The details of the transaction are only known to you and ExinCore!

ExinCore doesn’t know who you are, it just knows your UUID.

Preliminary knowledge:

You first need to create a robot, the method in tutorial 1.

Installing dependency packages

As we explained in tutorial 1, we need to rely on mixin-Java-SDK, which you should have installed first. Here we will install the other packages. Add the following dependencies to mvnrepository build.gradle. KTS dependencies:

  implementation("Com. Google. Guava: guava: 27.0.1 - jre")
  implementation("Commons - the codec: the Commons - the codec: 1.11")
  implementation("Com. Auth0: Java - JWT: 3.8.0." ")
  compile(files("libs/mixin-java-sdk.jar"))
  implementation("Com. Squareup. Okhttp3: okhttp: 3.12.1")
  implementation("Com. Squareup. Okio: okio: 2.2.2." ")
  implementation("Com. Google. Code. Gson: gson: 2.8.5")
  implementation("Org. Bouncycastle: bcprov - jdk16:1.46")
  implementation("Org.apache.com mons: the Commons - the CSV: 1.6")
  implementation("Org. Msgpack: msgpack - core: 0.8.16")
  implementation("Com. Fasterxml. Jackson. Core: Jackson - databind: 2.9.8")
  implementation("Com. Fasterxml. Jackson. Core: Jackson - core: 2.9.8")
  implementation("Org. Jetbrains. Kotlin: kotlin - stdlib: 1.3.30")
Copy the code

Charge coins into the Mixin Network and read out its balance.

BTC, USDT, EOS, ETH and so on can be traded through ExinCore API. Here is a demonstration of using USDT to buy BTC or using BTC to buy USDT. Check your wallet address before making a transaction. The complete steps are as follows:

  • Check bitcoin or USDT balance, wallet address. And write down the address of the wallet.
  • From a third party exchange or your cold wallet, charge the coins to the above wallet address.
  • Check the balance of currency again to see whether the account is accounted. (The arrival time of bitcoin is five blocks high, about 100 minutes).

Bitcoin and USDT recharge address is the same.

  private static final String BTC_ASSET_ID     = "c6d0c728-2624-429b-8e0d-d9d19b6592fa";
  private static final String EOS_ASSET_ID     = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
  private static final String USDT_ASSET_ID    = "815b0b1a-2764-3736-8faa-42d694fa620a";

  MixinAPI mixinApiUser = generateAPI_FromCSV();
  JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
  System.out.println(asset);
  System.out.println("------------------------BTC------Information---------------------------");
  System.out.println("The BTC wallet address is " + asset.get("public_key").getAsString());
  System.out.println("The BTC wallet balance is " + asset.get("balance").getAsString());
  System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
Copy the code

Check ExinCore market for price information

How to query ExinCore market price information? If you want to buy bitcoin and sell USDT, the base currency is USDT. If you want to buy USDT and sell Bitcoin, then the base currency is bitcoin.

  if ( input.equals("8") ) {
   JsonArray res = FetchExinOneMarketInfos(USDT_ASSET_ID);
   System.out.println("--exchange_asset--exchange_asset_symbol/base_asset_symbol--price--minimum--maximum--exchanges--");
   // System.out.println(res);
   res.forEach((element) ->  {
      JsonObject jsonObj = element.getAsJsonObject();
      System.out.println(jsonObj.get("exchange_asset").getAsString() + "" +
                         jsonObj.get("exchange_asset_symbol").getAsString() + "/" +
                         jsonObj.get("base_asset_symbol").getAsString() + "" +
                         jsonObj.get("price").getAsString() + "" +
                         jsonObj.get("minimum_amount").getAsString() + "" +
                         jsonObj.get("maximum_amount").getAsString() + "" +
                         jsonObj.get("exchanges").getAsString() );
   });
   System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
  }
  private static JsonArray FetchExinOneMarketInfos(String url) {
    OkHttpClient client = new OkHttpClient();
    String baseUrl = "https://exinone.com/exincore/markets?base_asset=";
    String fullUrl = baseUrl + url;
    Request request = new Request.Builder()
                               .url(fullUrl)
                               .build();
    try {
       Response response = client.newCall(request).execute();
       if(! response.isSuccessful()) {throw new IOException("Unexpected code " + response);
       }
       return processJsonObjectWithDataOrError(response.body().string());
     } catch(Exception e) { e.printStackTrace(); }
     return null;
  }
  public static JsonArray processJsonObjectWithDataOrError(String res) {
   JsonParser parser = new JsonParser();
   JsonElement jsonTree = parser.parse(res);
   if ( jsonTree.isJsonObject() ) {
     if ( jsonTree.getAsJsonObject().get("data") != null ) {
        return  jsonTree.getAsJsonObject().get("data").getAsJsonArray(); }}return null;
  }
Copy the code

Before trading, create a Memo!

In chapter 2, Java Bitcoin development tutorial based on Mixin Network: The robot accepts bitcoin and immediately returns it to the user. We have learned to return bitcoin to the user. Here, in addition to giving ExinCore payment coins, we also tell him what we want to buy coins, which will be saved in the Memo.

public static String encodeUUID(UUID uuid) {
  try {
    byte[] byteUuid = asBytes(uuid);
    MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
    packer.writePayload(byteUuid);
    packer.close();
    byte[] packedData = packer.toByteArray();
    byte[] prex = { (byte)129, (byte)161.65, (byte)196.16 };
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    output.write(prex);
    output.write(packedData);
    byte[] out = output.toByteArray();
    return Base64.getEncoder().encodeToString(out);
  } catch (Exception e) { e.printStackTrace(); }
    return null;
}
Copy the code

The complete process of coin exchange

When transferring coins to ExinCore, write the memo into the coins you want to buy, otherwise, ExinCore will directly refund the coins to you! If you want to sell bitcoin and buy USDT, the call is as follows:

private static final String EXIN_BOT         = "61103d28-3ac2-44a2-ae34-bd956070dab1";
private static final String BTC_ASSET_ID     = "c6d0c728-2624-429b-8e0d-d9d19b6592fa";
private static final String EOS_ASSET_ID     = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
private static final String USDT_ASSET_ID    = "815b0b1a-2764-3736-8faa-42d694fa620a";
private static final String BTC_WALLET_ADDR  = "14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C";
private static final String MASTER_UUID      = "0b4f49dc-8fb4-4539-9a89-fb3afc613747";

if ( input.equals("5") ) {
 MixinAPI mixinApiUser = generateAPI_FromCSV();
 UUID usdtUUID         =  UUID.fromString(USDT_ASSET_ID);
 String memoTarget     = encodeUUID(usdtUUID);
 System.out.println("------------------------USDT-BTC-EXCHANGE----------------------------");
 System.out.println(memoTarget);
 JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
 System.out.println(asset);
 System.out.println(asset.get("balance").getAsFloat());
 if ( (asset.get("balance").getAsFloat() * 10000) > =1 ) {
     JsonObject transInfo = mixinApiUser.transfer(BTC_ASSET_ID, EXIN_BOT,
                                                "0.0001",memoTarget);
     System.out.println("------------------------BTC Transfer To EXCHANGE Information----------------------");
     System.out.println(transInfo);
     System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
  } else System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
}
Copy the code

If you want to sell USDT and buy bitcoin, the call is as follows:

if ( input.equals("6") ) {
 MixinAPI mixinApiUser = generateAPI_FromCSV();
 UUID btcUUID          =  UUID.fromString(BTC_ASSET_ID);
 String memoTarget     = encodeUUID(btcUUID);
 System.out.println(memoTarget);
 System.out.println("-------------------------BTC-USDT-EXCHANGE----------------------------");
 JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
 System.out.println(asset);
 if ( asset.get("balance").getAsFloat() >= 1 ) {
     JsonObject transInfo = mixinApiUser.transfer(USDT_ASSET_ID, EXIN_BOT,
                                                "1",memoTarget);
     System.out.println("------------------------USDT-BTC EXCHANGE Information----------------------");
     System.out.println(transInfo);
     System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
  } else System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
}
Copy the code

After the transaction is completed, Exincore will transfer the currency you need to your account. Also, Exincore will record the transaction price, transaction fee and other information in memo. You just need to press the following way to unlock it!

  • GetSnapshots reads the transaction history of your wallet.
if ( input.equals("Seven") ) {
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  String transDatetime  = "";
  String assetUUID      = "";
  if ( mixinApiUser.getClientID().equals("091651f2-19c3-34f0-b45e-724ff203d921") ) {
    transDatetime = "The 2019-04-19 T06:53:22. 593529 z";
    assetUUID     = USDT_ASSET_ID;
  } else {
    System.out.print("Input the transaction Date time (eg: 2019-04-19T06:53:22.593529z):);
    transDatetime = System.console().readLine();
  }
  JsonArray snapshots = mixinApiUser.getSnapshots(assetUUID,3,transDatetime,"ASC");
  // System.out.println(snapshots);
  snapshots.forEach((element) ->  {
     JsonObject jsonObj = element.getAsJsonObject();
     if ( jsonObj.get("amount").getAsFloat() > 0 && jsonObj.get("data") != null ) {
       System.out.println(jsonObj.get("data").getAsString() );
       try {
       byte[] encoded = Base64.getDecoder().decode(jsonObj.get("data").getAsString());
       MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(encoded);
       Value memoVal = unpacker.unpackValue();
       if ( memoVal.isMapValue()) {
         Map<Value, Value> map = memoVal.asMapValue().map();
         System.out.println(map.size());
         if ( map.get(ValueFactory.newString("C")).asIntegerValue().asInt() == 1000 ) {
           System.out.println("Exchange successful" + " Code: " +
                              map.get(ValueFactory.newString("C")).asIntegerValue());
           System.out.println("Price is " + map.get(ValueFactory.newString("P")).asStringValue());
           System.out.println("Fee is " + map.get(ValueFactory.newString("F")).asStringValue());
           System.out.println("Type is " + map.get(ValueFactory.newString("T")).asStringValue());
           ByteBuffer AssetBinValue = map.get(ValueFactory.newString("FA")).asRawValue().asByteBuffer();
           System.out.println("Fee is asset UUID is " + ByteBufferAsUuid(AssetBinValue));
           ByteBuffer TraceBinValue = map.get(ValueFactory.newString("O")).asRawValue().asByteBuffer();
           System.out.println("The trace id is "+ ByteBufferAsUuid(TraceBinValue)); }}}catch(Exception e) { e.printStackTrace(); }}}); }Copy the code

A successful transaction is as follows:

Make your choose(eg: q forExit!) : 7 7 -------------------------get-Snapshots--------------------------------------------- hqFDzQPooVCnNTIyNi4wM6FGqTAuMDAxMDQ1MqJGQcQQgVsLGidkNzaPqkLWlPpiCqFUoVKhT8QQPAsBVqKtQ5uS67M/ZTNleg== Exchange successful  Code: 1000 Price is 5226.03 Fee is 0.0010452 Type is R Fee is asset UUID is 815b0B1A-2764-3736-8FAA-42D694FA620a The trace ID is 3c0b0156-a2ad-439b-92eb-b33f6533657a
---------------------end-of-get-Snapshots---------------------------------------------
Copy the code

Read the balance of coins

By reading the balance of the currency, to confirm the transaction situation!

MixinAPI mixinApiUser = generateAPI_FromCSV();
JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
Copy the code

Source code execution

Compile and execute, then start trading.

  • Gradle Build builds projects.
  • Java-cp runs the project.
java -cp .:build/libs/bitcoin_wallet-java.jar:libs/* bitcoin_wallet.java.App
Copy the code

Note: It cannot be run using Gradle run because the system.console ().readline () we use is not supported by Gradle and can only be run using Java -cp!

List of commands when this code is executed:

  • 1: Create Bitcoin Wallet and update PIN
  • 2: Read Bitcoin balance & address
  • 3: Read USDT balance & address
  • 4: Read EOS balance & address
  • tbb:Transfer BTC from Bot to Wallet
  • tbm:Transfer BTC from Wallet to Master
  • teb:Transfer EOS from Bot to Wallet
  • tem:Transfer EOS from Wallet to Master
  • tub:Transfer USDT from Bot to Wallet
  • tum:Transfer USDT from Wallet to Master
  • 5: pay 0.0001 BTC buy USDT
  • 6: pay $1 USDT buy BTC
  • 7: Read Snapshots
  • 8: Fetch market price(USDT)
  • 9: Fetch market price(BTC)
  • v: Verify Wallet Pin
  • wb: Withdraw BTC
  • we: WitchDraw EOS
  • a: Read All Assets Infos
  • q: Exit Make your choose(eg: q for Exit!) :

The complete code

Solution Two: List your order on Ocean.One exchange