get Assets and getLimitOrder

This commit is contained in:
Henry Varona 2016-12-12 23:27:16 -04:00
parent 62aa363bc4
commit 0f607657de
7 changed files with 287 additions and 4 deletions

View file

@ -4,6 +4,9 @@ package com.luminiasoft.bitshares;
* Created by nelson on 11/9/16. * Created by nelson on 11/9/16.
*/ */
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);

View file

@ -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();
} }
} }

View 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;
}
}

View file

@ -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";
} }

View file

@ -706,12 +706,92 @@ public class Test {
PublicKey from = new PublicKey((new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0).getPrivateKey())); 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()); 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(); Memo sendMemo = new MemoBuilder().setFromKey(from).setToKey(to).setMessage("test message").build();
JsonElement memoJson = sendMemo.toJsonObject(); JsonElement memoJson = sendMemo.toJsonObject();
System.out.println("generated Json : " + memoJson.toString()); 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())); 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());
}
} }
} }

View 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();
}
}

View file

@ -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();
}
}