From 0f607657deefaef4405d405c22e9a2579eb8e0e3 Mon Sep 17 00:00:00 2001 From: Henry Varona Date: Mon, 12 Dec 2016 23:27:16 -0400 Subject: [PATCH] get Assets and getLimitOrder --- .../java/com/luminiasoft/bitshares/Asset.java | 3 + .../java/com/luminiasoft/bitshares/Main.java | 4 +- .../com/luminiasoft/bitshares/Market.java | 28 ++++++ .../java/com/luminiasoft/bitshares/RPC.java | 2 + .../java/com/luminiasoft/bitshares/Test.java | 86 +++++++++++++++++- .../luminiasoft/bitshares/ws/GetAsset.java | 87 +++++++++++++++++++ .../bitshares/ws/getLimitOrders.java | 81 +++++++++++++++++ 7 files changed, 287 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/luminiasoft/bitshares/Market.java create mode 100644 src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java create mode 100644 src/main/java/com/luminiasoft/bitshares/ws/getLimitOrders.java diff --git a/src/main/java/com/luminiasoft/bitshares/Asset.java b/src/main/java/com/luminiasoft/bitshares/Asset.java index e6a1103..8c0e1e7 100644 --- a/src/main/java/com/luminiasoft/bitshares/Asset.java +++ b/src/main/java/com/luminiasoft/bitshares/Asset.java @@ -4,6 +4,9 @@ package com.luminiasoft.bitshares; * Created by nelson on 11/9/16. */ public class Asset extends GrapheneObject { + + public int precision; + public String symbol; public Asset(String id) { super(id); diff --git a/src/main/java/com/luminiasoft/bitshares/Main.java b/src/main/java/com/luminiasoft/bitshares/Main.java index 40c8ddc..b243f7b 100644 --- a/src/main/java/com/luminiasoft/bitshares/Main.java +++ b/src/main/java/com/luminiasoft/bitshares/Main.java @@ -69,6 +69,8 @@ public class Main { // test.testCreateBinFile(); // test.testImportBinFile(); //test.testLookupAccounts(); - test.testDecodeMemo(); + //test.testDecodeMemo(); + test.testGetAsset(); + test.testGetALimitOrders(); } } diff --git a/src/main/java/com/luminiasoft/bitshares/Market.java b/src/main/java/com/luminiasoft/bitshares/Market.java new file mode 100644 index 0000000..90179f9 --- /dev/null +++ b/src/main/java/com/luminiasoft/bitshares/Market.java @@ -0,0 +1,28 @@ +package com.luminiasoft.bitshares; + + +/** + * + * @author henry + */ +public class Market { + + public String id; + public String expiration; + public String seller; + public long for_sale; + public long deferred_fee; + public Price sell_price; + + public class Price { + + public AmountPrice base; + public AmountPrice quote; + } + + public class AmountPrice { + + public String asset_id; + public long amount; + } +} diff --git a/src/main/java/com/luminiasoft/bitshares/RPC.java b/src/main/java/com/luminiasoft/bitshares/RPC.java index f7db64b..e37577a 100644 --- a/src/main/java/com/luminiasoft/bitshares/RPC.java +++ b/src/main/java/com/luminiasoft/bitshares/RPC.java @@ -16,4 +16,6 @@ public class RPC { public static final String CALL_GET_KEY_REFERENCES = "get_key_references"; public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history"; public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts"; + public static final String CALL_GET_ASSET = "get_assets"; + public static final String CALL_GET_LIMIT_ORDERS = "get_limit_orders"; } diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index 86a5d20..274b13f 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -706,12 +706,92 @@ public class Test { PublicKey from = new PublicKey((new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0).getPrivateKey())); PublicKey to = new PublicKey(new BrainKey(Main.BILTHON_5_BRAIN_KEY, 0).getPrivateKey()); - + Memo sendMemo = new MemoBuilder().setFromKey(from).setToKey(to).setMessage("test message").build(); - + JsonElement memoJson = sendMemo.toJsonObject(); System.out.println("generated Json : " + memoJson.toString()); System.out.println("Decode Memo : " + Memo.decodeMessage(from, to, memoJson.getAsJsonObject().get("message").getAsString(), memoJson.getAsJsonObject().get("nonce").getAsString())); - + + } + + void testGetAsset() { + SSLContext context = null; + try { + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); + + // Set the custom SSL context. + factory.setSSLContext(context); + + WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE); + + mWebSocket.addListener(new GetAsset("1.3.562", new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + if (response.result.getClass() == ArrayList.class) { + ArrayList list = (ArrayList) response.result; + + for (Object listObject : list) { + if (listObject.getClass() == Asset.class) { + Asset asset = (Asset) listObject; + System.out.println(" " + asset.symbol + " " + asset.precision); + } + } + } + } + + @Override + public void onError(BaseResponse.Error error) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + })); + mWebSocket.connect(); + + } catch (NoSuchAlgorithmException e) { + System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IOException. Msg: " + e.getMessage()); + } + } + + void testGetALimitOrders() { + SSLContext context = null; + try { + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); + + // Set the custom SSL context. + factory.setSSLContext(context); + + WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE); + + mWebSocket.addListener(new getLimitOrders("1.3.0", "1.3.562", 100, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + if (response.result.getClass() == ArrayList.class) { + ArrayList list = (ArrayList) response.result; + if (list.get(0).getClass() == Market.class) { + System.out.println(" " + ((Market)list.get(0)).sell_price.base.amount/((Market)list.get(0)).sell_price.quote.amount); + } + } + } + + @Override + public void onError(BaseResponse.Error error) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + })); + mWebSocket.connect(); + + } catch (NoSuchAlgorithmException e) { + System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IOException. Msg: " + e.getMessage()); + } } } diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java new file mode 100644 index 0000000..f16d5df --- /dev/null +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java @@ -0,0 +1,87 @@ +package com.luminiasoft.bitshares.ws; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.Asset; +import com.luminiasoft.bitshares.RPC; +import com.luminiasoft.bitshares.interfaces.JsonSerializable; +import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; +import com.luminiasoft.bitshares.models.AccountProperties; +import com.luminiasoft.bitshares.models.ApiCall; +import com.luminiasoft.bitshares.models.BaseResponse; +import com.luminiasoft.bitshares.models.WitnessResponse; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFrame; + +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Created by nelson on 11/15/16. + */ +public class GetAsset extends WebSocketAdapter { + + private String assetName; + private WitnessResponseListener mListener; + + public GetAsset(String assetName, WitnessResponseListener listener){ + this.assetName = assetName; + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList<>(); + ArrayList assetList = new ArrayList(); + assetList.add(new JsonSerializable() { + @Override + public String toJsonString() { + return assetName; + } + + @Override + public JsonElement toJsonObject() { + return new JsonParser().parse(assetName); + } + }); + accountParams.add(assetList); + ApiCall getAccountByName = new ApiCall(0, RPC.CALL_GET_ASSET, accountParams, "2.0", 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + String response = frame.getPayloadText(); + Gson gson = new Gson(); + + Type getAssetResponse = new TypeToken>>(){}.getType(); + WitnessResponse> witnessResponse = gson.fromJson(response, getAssetResponse); + + if(witnessResponse.error != null){ + this.mListener.onError(witnessResponse.error); + }else{ + this.mListener.onSuccess(witnessResponse); + } + + websocket.disconnect(); + } + + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } + + @Override + public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } +} diff --git a/src/main/java/com/luminiasoft/bitshares/ws/getLimitOrders.java b/src/main/java/com/luminiasoft/bitshares/ws/getLimitOrders.java new file mode 100644 index 0000000..a418dd2 --- /dev/null +++ b/src/main/java/com/luminiasoft/bitshares/ws/getLimitOrders.java @@ -0,0 +1,81 @@ +package com.luminiasoft.bitshares.ws; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.Market; +import com.luminiasoft.bitshares.RPC; +import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; +import com.luminiasoft.bitshares.models.AccountProperties; +import com.luminiasoft.bitshares.models.ApiCall; +import com.luminiasoft.bitshares.models.BaseResponse; +import com.luminiasoft.bitshares.models.WitnessResponse; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFrame; + +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Created by nelson on 11/15/16. + */ +public class getLimitOrders extends WebSocketAdapter { + + private String a; + private String b; + private int limit; + private WitnessResponseListener mListener; + + public getLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { + this.a = a; + this.b = b; + this.limit = limit; + this.mListener = mListener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList<>(); + accountParams.add(this.a); + accountParams.add(this.b); + accountParams.add(this.limit); + ApiCall getAccountByName = new ApiCall(0, RPC.CALL_GET_LIMIT_ORDERS, accountParams, "2.0", 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + try { + String response = frame.getPayloadText(); + Gson gson = new Gson(); + + Type GetLimitiOrdersResponse = new TypeToken>>() { + }.getType(); + WitnessResponse> witnessResponse = gson.fromJson(response, GetLimitiOrdersResponse); + if (witnessResponse.error != null) { + this.mListener.onError(witnessResponse.error); + } else { + this.mListener.onSuccess(witnessResponse); + } + } catch (Exception e) { + e.printStackTrace(); + } + websocket.disconnect(); + } + + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } + + @Override + public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } +}