成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Java比特幣開發(fā)教程:在開放式交易所限價(jià)買賣比特幣

lanffy / 2597人閱讀

摘要:如果你想限定價(jià)格買賣,或者買賣一些不支持的資產(chǎn),你需要。完整的步驟如下檢查比特幣或的余額,錢包地址。從第三方交易所或者你的冷錢包中,將幣充到上述錢包地址。比特幣的到帳時(shí)間是個(gè)區(qū)塊的高度,約分鐘。


上一章介紹了Exincore,你可以1秒完成資產(chǎn)的市價(jià)買賣。如果你想限定價(jià)格買賣,或者買賣一些exincore不支持的資產(chǎn),你需要OceanOne。

方案二: 掛單Ocean.One交易所

Ocean.one是基于Mixin Network的去中心化交易所,它中心化交易所一樣友好。
你可以在OceanOne上交易任何資產(chǎn),只需要將你的幣轉(zhuǎn)給OceanOne, 將交易信息寫在交易的memo里,OceanOne會(huì)在市場(chǎng)里列出你的交易需求,
交易成功后,會(huì)將目標(biāo)幣轉(zhuǎn)入到你的MixinNetwork帳上,它有三大特點(diǎn)與優(yōu)勢(shì):

不需要在OceanOne注冊(cè)

不需要存幣到交易所

支持所有Mixin Network上能夠轉(zhuǎn)賬的資產(chǎn),所有的ERC20 EOS代幣。

預(yù)備知識(shí):

你先需要?jiǎng)?chuàng)建一個(gè)機(jī)器人, 方法在 教程一.

安裝依賴包

正如教程一里我們介紹過的, 我們需要依賴 mixin-java-sdk, 你應(yīng)該先安裝過它了,zhang/mixin_labs-java-bot/blob/master/README.md).

Install required packages

第四課, 在上一課中已經(jīng)安裝好了.

充幣到 Mixin Network, 并讀出它的余額.

此處演示用 USDT購買BTC 或者 用BTC購買USDT。交易前,先檢查一下錢包地址。
完整的步驟如下:

檢查比特幣或USDT的余額,錢包地址。并記下錢包地址。

從第三方交易所或者你的冷錢包中,將幣充到上述錢包地址。

再檢查一下幣的余額,看到帳與否。(比特幣的到帳時(shí)間是5個(gè)區(qū)塊的高度,約100分鐘)。

比特幣與USDT的充值地址是一樣的。

  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("-----------------------------------------------------------------------");
取得Ocean.one的市場(chǎng)價(jià)格信息

如何來查詢Ocean.one市場(chǎng)的價(jià)格信息呢?你要先了解你交易的基礎(chǔ)幣是什么,如果你想買比特幣,賣出USDT,那么基礎(chǔ)貨幣就是USDT;如果你想買USDT,賣出比特幣,那么基礎(chǔ)貨幣就是比特幣.

if ( subinput.equals("1") ) {
  FetchOceanMarketInfos(BTC_ASSET_ID,USDT_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;
}
交易前,創(chuàng)建一個(gè)Memo!

在第二章里,基于Mixin Network的 Java 比特幣開發(fā)教程: 機(jī)器人接受比特幣并立即退還用戶, 我們學(xué)習(xí)過轉(zhuǎn)帳,這兒我們介紹如何告訴Ocean.one,我們給它轉(zhuǎn)帳的目的是什么,信息全部放在memo里.

Side 方向,"B" 或者 "A", "B"是購買, "A"是出售.

AssetUUID 目標(biāo)虛擬資產(chǎn)的UUID.

Price 價(jià)格,如果操作方向是"B", 價(jià)格就是AssetUUID的價(jià)格; 如果操作方向是"B", 價(jià)格就是轉(zhuǎn)給Ocean.one幣的價(jià)格.

public static String GenerateOrderMemo(String Side, String AssetUUID, String Price) {
  try {
    MessageBufferPacker m = MessagePack.newDefaultBufferPacker();
    UUID myAssetUUID  =  UUID.fromString(AssetUUID);
    Value map = ValueFactory.newMap(ValueFactory.newString("S"), ValueFactory.newString(Side),
                                    ValueFactory.newString("A"), ValueFactory.newBinary(asBytes(myAssetUUID)),
                                    ValueFactory.newString("P"), ValueFactory.newString(Price),
                                    ValueFactory.newString("T"), ValueFactory.newString("L") );
    m.packValue(map);
    return Base64.getEncoder().encodeToString(m.toByteArray());
  } catch (Exception e) { e.printStackTrace(); }
    return "";
}
出售BTC的例子

轉(zhuǎn)打算出售的BTC給Ocean.one(OCEANONE_BOT),將你打算換回來的目標(biāo)虛擬資產(chǎn)的UUID放入memo.

private static final String OCEANONE_BOT     = "aaff5bef-42fb-4c9f-90e0-29f69176b7d4";
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 ( subinput.equals("s1") ) {
  System.out.print("Please input the BTC price base USDT: ");
  String pinput = System.console().readLine();
  System.out.println(pinput);

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

  String buyMemo = GenerateOrderMemo("A",USDT_ASSET_ID,pinput);
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  // UUID usdtUUID         =  UUID.fromString(USDT_ASSET_ID);
  // String memoTarget     = encodeUUID(usdtUUID);
  System.out.println("------------------Ocean.one-USDT-BTC-EXCHANGE----------------------------");
  System.out.println(buyMemo);
  JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
  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(BTC_ASSET_ID, OCEANONE_BOT,
                                                 aminput,
                                                 buyMemo);
      System.out.println("------------------------BTC Transfer To EXCHANGE Information----------------------");
      System.out.println(transInfo);
      System.out.println("-----------------------------------------------------------------------");
   } else System.out.println("----------------Not enough BTC--------------------------------------------");
}

如果你是打算買BTC,操作如下:

if ( subinput.equals("b1") ) {
  System.out.print("Please input the BTC price base USDT: ");
  String pinput = System.console().readLine();
  System.out.println(pinput);

  System.out.print("Please input the USDT amount: ");
  String aminput = System.console().readLine();
  System.out.println(aminput);
  float amountf = Float.valueOf(aminput.trim()).floatValue();
  String buyMemo = GenerateOrderMemo("B",BTC_ASSET_ID,pinput);
  MixinAPI mixinApiUser = generateAPI_FromCSV();
  // UUID usdtUUID         =  UUID.fromString(USDT_ASSET_ID);
  // String memoTarget     = encodeUUID(usdtUUID);
  System.out.println("------------------Ocean.one-USDT-BTC-EXCHANGE----------------------------");
  System.out.println(buyMemo);
  JsonObject asset = mixinApiUser.getAsset(USDT_ASSET_ID);
  System.out.println(asset);
  System.out.println(asset.get("balance").getAsFloat());
  if ( asset.get("balance").getAsFloat()  >= 1 && asset.get("balance").getAsFloat() >= amountf ) {
      JsonObject transInfo = mixinApiUser.transfer(USDT_ASSET_ID, OCEANONE_BOT,
                                                 aminput,
                                                 buyMemo);
      System.out.println("------------------------BTC Transfer To EXCHANGE Information----------------------");
      System.out.println(transInfo);
      System.out.println("-----------------------------------------------------------------------");
   } else System.out.println("----------------Not enough USDT--------------------------------------------");
}

一個(gè)成功的掛單如下:

------------------Ocean.one--EXCHANGE----------------------------
hKFToUKhQcQQyUrIj0ZxOXa2CgkGTxgR6KFQojExoVShTA==
{"type":"asset","asset_id":"815b0b1a-2764-3736-8faa-42d694fa620a","chain_id":"c6d0c728-2624-429b-8e0d-d9d19b6592fa","symbol":"USDT","name":"Tether USD","icon_url":"https://images.mixin.one/ndNBEpObYs7450U08oAOMnSEPzN66SL8Mh-f2pPWBDeWaKbXTPUIdrZph7yj8Z93Rl8uZ16m7Qjz-E-9JFKSsJ-F=s128","balance":"2","public_key":"15RNBb5GzoXWRoEMT5MJL3pqzSjdPoXf6J","account_name":"","account_tag":"","price_btc":"0.00018747","price_usd":"0.98731224","change_btc":"0.021985715750932774","change_usd":"-0.01843154053058167","asset_key":"815b0b1a-2764-3736-8faa-42d694fa620a","confirmations":6,"capitalization":0}
2.0
--------------815b0b1a-2764-3736-8faa-42d694fa620a Transfer To EXCHANGE Information---------
{"type":"transfer","snapshot_id":"f526fd18-c3d2-4a6d-a0f0-3a720e93c48e","opponent_id":"aaff5bef-42fb-4c9f-90e0-29f69176b7d4","asset_id":"815b0b1a-2764-3736-8faa-42d694fa620a","amount":"-2","trace_id":"6cf37ca6-4efa-4e78-bea1-53c5c5d54d2b","memo":"hKFToUKhQcQQyUrIj0ZxOXa2CgkGTxgR6KFQojExoVShTA==","created_at":"2019-04-26T01:56:00.803572612Z","counter_user_id":"0b4f49dc-8fb4-4539-9a89-fb3afc613747"}
---Order is 6cf37ca6-4efa-4e78-bea1-53c5c5d54d2b: ------
取消掛單

Ocean.one將trace_id當(dāng)做訂單,比如上面的例子, 6cf37ca6-4efa-4e78-bea1-53c5c5d54d2b 就是訂單號(hào),我們用他來取消訂單。

public static String GenerateOrderCancelMemo(String myUuid) {
  try {
    MessageBufferPacker m = MessagePack.newDefaultBufferPacker();
    UUID AssetUUID  =  UUID.fromString(myUuid);
    Value map = ValueFactory.newMap(
                                    ValueFactory.newString("O"), ValueFactory.newBinary(asBytes(AssetUUID)) );
    m.packValue(map);
    return Base64.getEncoder().encodeToString(m.toByteArray());
  } catch (Exception e) { e.printStackTrace(); }
    return "";
}
通過讀取資產(chǎn)余額,來確認(rèn)到帳情況

Check the wallet"s balance.

MixinAPI mixinApiUser = generateAPI_FromCSV();
JsonObject asset = mixinApiUser.getAsset(BTC_ASSET_ID);
源代碼執(zhí)行

編譯執(zhí)行,即可開始交易了.

gradle build 編譯項(xiàng)目.

java -cp 運(yùn)行項(xiàng)目.

java -cp .:build/libs/bitcoin_wallet-java.jar:libs/* bitcoin_wallet.java.App

注意:不能使用gradle run來運(yùn)行,因?yàn)槲覀兪褂玫?strong>System.console().readLine()不被Gradle支持,只能使用java -cp
來運(yùn)行!

本代碼執(zhí)行時(shí)的命令列表:

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

o: Ocean.One Exchange

q: Exit

Make your choose(eg: q for Exit!):

1: Orders-Book of BTC/USDT

b1: Buy BTC pay USDT

s1: Sell BTC get USDT

2: Orders-Book of EOS/USDT

b2: Buy EOS pay USDT

s2: Sell EOS get USDT

3: Orders-Book of XIN/USDT

b3: Buy XIN pay USDT

s3: Sell XIN get USDT

4: Orders-Book of XIN/BTC

b4: Buy XIN pay BTC

s4: Sell XIN get BTC

5: Orders-Book of EOS/BTC

b4: Buy EOS pay BTC

s4: Sell EOS get BTC

6: Orders-Book of SC/BTC

b6: Buy SC pay BTC

s6: Sell SC get BTC

7: Orders-Book of EOS/XIN

b7: Buy EOS pay XIN

s7: Sell EOS get XIN

8: Orders-Book of ETH/XIN

b8: Buy ETH pay XIN

s8: Sell EOS get XIN

9: Orders-Book of SC/XIN

b9: Buy SC pay XIN

s9: Sell SC get XIN

c: Cancel the order

q: Exit

完整代碼

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/24671.html

相關(guān)文章

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

    摘要:在上一課中,我們介紹了如何在交易比特幣。支持交易任何上的,包括所有的和,不需要任何手續(xù)和費(fèi)用,直接掛單即可。掌握了代幣的交易方法,就可以交易任何其他代幣了。 showImg(https://segmentfault.com/img/remote/1460000018771566?w=1200&h=659); 在上一課中,我們介紹了如何在OceanOne交易比特幣。OceanOne支持交...

    tigerZH 評(píng)論0 收藏0
  • php 比特教程大更新,包括交易和閃兌

    摘要:課程簡(jiǎn)介創(chuàng)建一個(gè)機(jī)器人機(jī)器人接受比特幣并立即退還用戶創(chuàng)建比特幣錢包買賣實(shí)時(shí)兌換買賣在自由市場(chǎng)掛單買賣買賣任意在自由市場(chǎng)掛單買賣其他編程語言比特幣開發(fā)教程如下比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 課程簡(jiǎn)介 PHP 創(chuàng)建一...

    avwu 評(píng)論0 收藏0
  • php 比特教程大更新,包括交易和閃兌

    摘要:課程簡(jiǎn)介創(chuàng)建一個(gè)機(jī)器人機(jī)器人接受比特幣并立即退還用戶創(chuàng)建比特幣錢包買賣實(shí)時(shí)兌換買賣在自由市場(chǎng)掛單買賣買賣任意在自由市場(chǎng)掛單買賣其他編程語言比特幣開發(fā)教程如下比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程比特幣開發(fā)教程 showImg(https://segmentfault.com/img/bVboFQ6?w=1200&h=659); 課程簡(jiǎn)介 PHP 創(chuàng)建一...

    andot 評(píng)論0 收藏0
  • Java比特開發(fā)教程Java編程買賣比特

    摘要:完整的步驟如下檢查比特幣或的余額,錢包地址。比特幣的到帳時(shí)間是個(gè)塊的高度,約分鐘。 showImg(https://segmentfault.com/img/remote/1460000018952144?w=1200&h=659); 方案一: 通過ExinCore API進(jìn)行幣幣交易 Exincore 提供了基于Mixin Network的幣幣交易API. 你可以支付USDT給Exi...

    xiaowugui666 評(píng)論0 收藏0
  • Java比特開發(fā)教程Java編程買賣比特

    摘要:完整的步驟如下檢查比特幣或的余額,錢包地址。比特幣的到帳時(shí)間是個(gè)塊的高度,約分鐘。 showImg(https://segmentfault.com/img/remote/1460000018952144?w=1200&h=659); 方案一: 通過ExinCore API進(jìn)行幣幣交易 Exincore 提供了基于Mixin Network的幣幣交易API. 你可以支付USDT給Exi...

    msup 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<