From 4d214659d3a61bb23d28d26846e93d2950210f5f Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Fri, 13 Jan 2017 17:13:37 -0500 Subject: [PATCH] Added support for the 'get_account_balances' API call --- .../graphenej/AssetAmount.java | 6 +- .../de/bitsharesmunich/graphenej/Main.java | 3 +- .../de/bitsharesmunich/graphenej/RPC.java | 1 + .../de/bitsharesmunich/graphenej/Test.java | 43 +++++++++++- .../graphenej/api/GetAccountBalances.java | 69 +++++++++++++++++++ .../graphenej/api/GetLimitOrders.java | 3 +- .../graphenej/api/GetObjects.java | 3 +- .../api/GetRelativeAccountHistory.java | 2 +- .../graphenej/api/GetRequiredFees.java | 2 +- .../api/TransactionBroadcastSequence.java | 2 +- 10 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java diff --git a/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java b/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java index f4b0dad..e0c4d7d 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java +++ b/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java @@ -60,7 +60,7 @@ public class AssetAmount implements ByteSerializable, JsonSerializable { @Override public String toJsonString() { GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetSerializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmountSerializer()); return gsonBuilder.create().toJson(this); } @@ -75,7 +75,7 @@ public class AssetAmount implements ByteSerializable, JsonSerializable { /** * Custom serializer used to translate this object into the JSON-formatted entry we need for a transaction. */ - public static class AssetSerializer implements JsonSerializer { + public static class AssetAmountSerializer implements JsonSerializer { @Override public JsonElement serialize(AssetAmount assetAmount, Type type, JsonSerializationContext jsonSerializationContext) { @@ -86,7 +86,7 @@ public class AssetAmount implements ByteSerializable, JsonSerializable { } } - public static class AssetDeserializer implements JsonDeserializer { + public static class AssetAmountDeserializer implements JsonDeserializer { @Override public AssetAmount deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { diff --git a/src/main/java/de/bitsharesmunich/graphenej/Main.java b/src/main/java/de/bitsharesmunich/graphenej/Main.java index d9fa4e8..83bce15 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Main.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Main.java @@ -66,11 +66,12 @@ public class Main { // test.testGetRelativeAccountHistory(); // test.testLookupAssetSymbols(); // test.testListAssets(); - test.testGetObjects(); +// test.testGetObjects(); // test.testGetBlockHeader(); // test.testGetLimitOrders(); // test.testGetTradeHistory(); // test.testAssetSerialization(); // test.testGetMarketHistory(); + test.testGetAccountBalances(); } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/RPC.java b/src/main/java/de/bitsharesmunich/graphenej/RPC.java index 0a2dae5..279f72a 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/RPC.java +++ b/src/main/java/de/bitsharesmunich/graphenej/RPC.java @@ -19,6 +19,7 @@ public class RPC { public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts"; public static final String CALL_LIST_ASSETS = "list_assets"; public static final String GET_OBJECTS = "get_objects"; + public static final String GET_ACCOUNT_BALANCES = "get_account_balances"; public static final String CALL_LOOKUP_ASSET_SYMBOLS = "lookup_asset_symbols"; public static final String CALL_GET_BLOCK_HEADER = "get_block_header"; public static final String CALL_GET_LIMIT_ORDERS = "get_limit_orders"; diff --git a/src/main/java/de/bitsharesmunich/graphenej/Test.java b/src/main/java/de/bitsharesmunich/graphenej/Test.java index d1da231..986f1bd 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Test.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Test.java @@ -18,13 +18,13 @@ import org.spongycastle.crypto.digests.RIPEMD160Digest; import javax.net.ssl.SSLContext; import java.io.*; +import java.lang.reflect.Array; import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.NoSuchAlgorithmException; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -276,7 +276,7 @@ public class Test { Type AccountLookupResponse = new TypeToken>>() { }.getType(); GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, AccountLookupResponse); for (AssetAmount assetAmount : witnessResponse.result) { System.out.println("asset : " + assetAmount.toJsonString()); @@ -1141,4 +1141,43 @@ public class Test { byte[] bytes = byteArrayOutputStream.toByteArray(); System.out.println("serialized: "+Util.bytesToHex(bytes)); } + + public void testGetAccountBalances(){ + SSLContext context = null; + + WitnessResponseListener listener = new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + }; + + try { + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); + + // Set the custom SSL context. + factory.setSSLContext(context); + + WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_FR); + + UserAccount account = new UserAccount("1.2.138632"); + Asset asset = new Asset("1.3.121"); //USD + ArrayList assetList = new ArrayList<>(); + assetList.add(asset); + mWebSocket.addListener(new GetAccountBalances(account, assetList, listener)); + 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/de/bitsharesmunich/graphenej/api/GetAccountBalances.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java new file mode 100644 index 0000000..d7aeda4 --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java @@ -0,0 +1,69 @@ +package de.bitsharesmunich.graphenej.api; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketFrame; +import de.bitsharesmunich.graphenej.Asset; +import de.bitsharesmunich.graphenej.AssetAmount; +import de.bitsharesmunich.graphenej.RPC; +import de.bitsharesmunich.graphenej.UserAccount; +import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; +import de.bitsharesmunich.graphenej.models.ApiCall; +import de.bitsharesmunich.graphenej.models.WitnessResponse; + +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 1/13/17. + */ +public class GetAccountBalances extends BaseGrapheneHandler { + + private UserAccount mUserAccount; + private List mAssetList; + + public GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { + super(listener); + this.mUserAccount = userAccount; + this.mAssetList = assets; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList params = new ArrayList<>(); + ArrayList assetList = new ArrayList<>(); + for(Asset asset : mAssetList){ + assetList.add(asset.getObjectId()); + } + params.add(mUserAccount.getObjectId()); + params.add(assetList); + ApiCall apiCall = new ApiCall(0, RPC.GET_ACCOUNT_BALANCES, params, RPC.VERSION, 0); + websocket.sendText(apiCall.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()){ + System.out.println("<< "+frame.getPayloadText()); + } + String response = frame.getPayloadText(); + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); + + Type WitnessResponseType = new TypeToken>>(){}.getType(); + WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, WitnessResponseType); + mListener.onSuccess(witnessResponse); + websocket.disconnect(); + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()){ + System.out.println(">> "+frame.getPayloadText()); + } + } +} diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java index 062aa88..d018661 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -1,6 +1,5 @@ package de.bitsharesmunich.graphenej.api; -import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.neovisionaries.ws.client.WebSocket; @@ -58,7 +57,7 @@ public class GetLimitOrders extends WebSocketAdapter { GsonBuilder builder = new GsonBuilder(); Type GetLimitOrdersResponse = new TypeToken>>() {}.getType(); - builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); WitnessResponse> witnessResponse = builder.create().fromJson(response, GetLimitOrdersResponse); if (witnessResponse.error != null) { diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java index ea83150..33347da 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java @@ -4,7 +4,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketFrame; -import de.bitsharesmunich.graphenej.Asset; import de.bitsharesmunich.graphenej.AssetAmount; import de.bitsharesmunich.graphenej.RPC; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -50,7 +49,7 @@ public class GetObjects extends BaseGrapheneHandler { GsonBuilder gsonBuilder = new GsonBuilder(); //TODO: Uncomment this line after the deserializer is implemented. - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); // gsonBuilder.registerTypeAdapter(BitAssetData.class, new BitAssetData.BitAssetDeserializer()); // Only homogeneus array is currently supported diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java index 548b133..1887a56 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -119,7 +119,7 @@ public class GetRelativeAccountHistory extends WebSocketAdapter { Type RelativeAccountHistoryResponse = new TypeToken>>(){}.getType(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); WitnessResponse> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse); mListener.onSuccess(transfersResponse); websocket.disconnect(); diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java index 98c5909..f82b950 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java @@ -53,7 +53,7 @@ public class GetRequiredFees extends WebSocketAdapter { Type GetRequiredFeesResponse = new TypeToken>>(){}.getType(); GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, GetRequiredFeesResponse); if(witnessResponse.error != null){ diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java b/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java index c92137a..748d0b2 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java @@ -115,7 +115,7 @@ public class TransactionBroadcastSequence extends WebSocketAdapter { }else if(baseResponse.id == GET_REQUIRED_FEES){ Type GetRequiredFeesResponse = new TypeToken>>(){}.getType(); GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); WitnessResponse> requiredFeesResponse = gsonBuilder.create().fromJson(response, GetRequiredFeesResponse); // Setting fees