Added support for the 'get_account_balances' API call

This commit is contained in:
Nelson R. Perez 2017-01-13 17:13:37 -05:00
parent a835d9cee4
commit 4d214659d3
10 changed files with 121 additions and 13 deletions

View file

@ -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<AssetAmount> {
public static class AssetAmountSerializer implements JsonSerializer<AssetAmount> {
@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<AssetAmount> {
public static class AssetAmountDeserializer implements JsonDeserializer<AssetAmount> {
@Override
public AssetAmount deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

View file

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

View file

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

View file

@ -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<WitnessResponse<List<AssetAmount>>>() {
}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
WitnessResponse<List<AssetAmount>> 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<Asset> 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());
}
}
}

View file

@ -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<Asset> mAssetList;
public GetAccountBalances(UserAccount userAccount, List<Asset> assets, WitnessResponseListener listener) {
super(listener);
this.mUserAccount = userAccount;
this.mAssetList = assets;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> params = new ArrayList<>();
ArrayList<Serializable> 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<WitnessResponse<List<AssetAmount>>>(){}.getType();
WitnessResponse<List<AssetAmount>> 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());
}
}
}

View file

@ -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<WitnessResponse<List<LimitOrder>>>() {}.getType();
builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer());
WitnessResponse<List<LimitOrder>> witnessResponse = builder.create().fromJson(response, GetLimitOrdersResponse);
if (witnessResponse.error != null) {

View file

@ -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

View file

@ -119,7 +119,7 @@ public class GetRelativeAccountHistory extends WebSocketAdapter {
Type RelativeAccountHistoryResponse = new TypeToken<WitnessResponse<List<HistoricalTransfer>>>(){}.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<List<HistoricalTransfer>> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse);
mListener.onSuccess(transfersResponse);
websocket.disconnect();

View file

@ -53,7 +53,7 @@ public class GetRequiredFees extends WebSocketAdapter {
Type GetRequiredFeesResponse = new TypeToken<WitnessResponse<List<AssetAmount>>>(){}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
WitnessResponse<List<AssetAmount>> witnessResponse = gsonBuilder.create().fromJson(response, GetRequiredFeesResponse);
if(witnessResponse.error != null){

View file

@ -115,7 +115,7 @@ public class TransactionBroadcastSequence extends WebSocketAdapter {
}else if(baseResponse.id == GET_REQUIRED_FEES){
Type GetRequiredFeesResponse = new TypeToken<WitnessResponse<List<AssetAmount>>>(){}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
WitnessResponse<List<AssetAmount>> requiredFeesResponse = gsonBuilder.create().fromJson(response, GetRequiredFeesResponse);
// Setting fees