get Assets and getLimitOrder
This commit is contained in:
parent
62aa363bc4
commit
0f607657de
7 changed files with 287 additions and 4 deletions
|
@ -5,6 +5,9 @@ package com.luminiasoft.bitshares;
|
||||||
*/
|
*/
|
||||||
public class Asset extends GrapheneObject {
|
public class Asset extends GrapheneObject {
|
||||||
|
|
||||||
|
public int precision;
|
||||||
|
public String symbol;
|
||||||
|
|
||||||
public Asset(String id) {
|
public Asset(String id) {
|
||||||
super(id);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,8 @@ public class Main {
|
||||||
// test.testCreateBinFile();
|
// test.testCreateBinFile();
|
||||||
// test.testImportBinFile();
|
// test.testImportBinFile();
|
||||||
//test.testLookupAccounts();
|
//test.testLookupAccounts();
|
||||||
test.testDecodeMemo();
|
//test.testDecodeMemo();
|
||||||
|
test.testGetAsset();
|
||||||
|
test.testGetALimitOrders();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
28
src/main/java/com/luminiasoft/bitshares/Market.java
Normal file
28
src/main/java/com/luminiasoft/bitshares/Market.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,4 +16,6 @@ public class RPC {
|
||||||
public static final String CALL_GET_KEY_REFERENCES = "get_key_references";
|
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_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
|
||||||
public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts";
|
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";
|
||||||
}
|
}
|
||||||
|
|
|
@ -714,4 +714,84 @@ public class Test {
|
||||||
System.out.println("Decode Memo : " + Memo.decodeMessage(from, to, memoJson.getAsJsonObject().get("message").getAsString(), memoJson.getAsJsonObject().get("nonce").getAsString()));
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
87
src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java
Normal file
87
src/main/java/com/luminiasoft/bitshares/ws/GetAsset.java
Normal file
|
@ -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<String, List<String>> headers) throws Exception {
|
||||||
|
ArrayList<Serializable> accountParams = new ArrayList<>();
|
||||||
|
ArrayList<Serializable> 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<WitnessResponse<ArrayList<Asset>>>(){}.getType();
|
||||||
|
WitnessResponse<ArrayList<Asset>> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<String, List<String>> headers) throws Exception {
|
||||||
|
ArrayList<Serializable> 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<WitnessResponse<ArrayList<Market>>>() {
|
||||||
|
}.getType();
|
||||||
|
WitnessResponse<ArrayList<Market>> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue