Java bitcoin development tutorial: Java programming business bitcoin


Java bitcoin development tutorial: Java programming business bitcoin

Scheme 1: currency transaction through exincore API

Exincore provides currency transaction API based on mixin network

You can pay usdt to exincore. Exincore will transfer the bitcoin you purchased to you at the lowest price and the most favorable transaction fee. Each transaction is anonymous and can be verified on the chain. Only you and exincore know the details of the transaction!

Exincore doesn’t know who you are, it only knows your UUID

Preparatory knowledge:

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

Install dependency package

As we introduced in tutorial 1, we need to rely onmixin-java-sdkYou should have installed it first. Here we will install other software packages
Download address: mvnrepository
For dependencies of build.gradle.kts, add the following dependent packages:


Charge it to mixin network and read out its balance

Through exincore API, BTC, usdt, EOS, Eth and other transactions can be carried out. Here is a demonstration of purchasing BTC with usdt or usdt with BTC. Check your wallet address before you trade.
The complete steps are as follows:

  • Check the balance and wallet address of bitcoin or usdt. And write down the address of the wallet.
  • From a third-party exchange or your cold wallet, charge the money to the wallet address above.
  • Check the balance of the currency again to see whether the account is good or not. (bitcoin’s arrival time is 5 blocks high, about 100 minutes).

Bitcoin and usdt have the same recharge address.

  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("The BTC wallet address is " + asset.get("public_key").getAsString());
  System.out.println("The BTC wallet balance is " + asset.get("balance").getAsString());

Query price information of exincore Market

How to query the price information of exincore market? You should first understand what the base currency you are trading is. If you want to buy bitcoin and sell usdt, the base currency is usdt; if you want to buy usdt and sell bitcoin, the base currency is bitcoin

  if ( input.equals("8") ) {
   JsonArray res = FetchExinOneMarketInfos(USDT_ASSET_ID);
   // 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() );
  private static JsonArray FetchExinOneMarketInfos(String url) {
    OkHttpClient client = new OkHttpClient();
    String baseUrl = "";
    String fullUrl = baseUrl + url;
    Request request = new Request.Builder()
    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;

Create a memo before trading!

In Chapter 2, Java bitcoin development tutorial based on mixin network: robots accept bitcoin and return it to users immediately. We have learned to return bitcoin to users. Here, in addition to paying exincore, we also need to tell him what kind of currency we want to buy, and what kind of currency we want to buy will be saved in memo.

public static String encodeUUID(UUID uuid) {
  try {
    byte[] byteUuid = asBytes(uuid);
    MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
    byte[] packedData = packer.toByteArray();
    byte[] prex = { (byte)129, (byte)161, 65, (byte)196, 16 };
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] out = output.toByteArray();
    return Base64.getEncoder().encodeToString(out);
  } catch (Exception e) { e.printStackTrace(); }
    return null;

Complete process of currency transaction

When transferring currency to exincore, write the memo into the currency you want to buy. Otherwise, exincore will directly return the currency to you!
If you want to sell bitcoin to buy usdt, you can call it 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);
 JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
 if ( (asset.get("balance").getAsFloat() * 10000) >= 1 ) {
     JsonObject transInfo = mixinApiUser.transfer(BTC_ASSET_ID, EXIN_BOT,
     System.out.println("------------------------BTC Transfer To EXCHANGE Information----------------------");
  } else System.out.println("-----------------------------------------------------------------------");

If you want to sell usdt to buy bitcoin, you can call it as follows:

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

After the transaction is completed, exincore will transfer the currency you need to your account. Similarly, it will record the transaction price, transaction cost and other information in memo! You just need to untie it in the following way!

  • getSnapshotsRead the wallet’s transactions.
if ( input.equals("7") ) {
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  String transDatetime  = "";
  String assetUUID      = "";
  if ( mixinApiUser.getClientID().equals("091651f2-19c3-34f0-b45e-724ff203d921") ) {
    transDatetime = "2019-04-19T06:53:22.593529Z";
    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();
         if ( map.get(ValueFactory.newString("C")).asIntegerValue().asInt() == 1000 ) {
           System.out.println("Exchange successful" + " Code: " +
           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(); }

A successful transaction is as follows:

Make your choose(eg: q for Exit!): 7
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

Read balance in currency

Confirm the transaction by reading the balance of currency!

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

Source code execution

Compile and execute to start the transaction

  • gradle buildCompile the project
  • java -cpRun the project
java -cp .:build/libs/bitcoin_wallet-java.jar:libs/*

Note: you can’t use gradle run to run because we useSystem.console().readLine()Not supported by gradle, can only usejava -cp
To run!

Command list 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!):

Complete code

Solution Two: List your order on Ocean.One exchange