Added support for the newly created 'get_all_asset_holders' API call, part of the 'asset' API

master
Nelson R. Perez 2017-01-25 16:00:33 -05:00
parent 2b225a7b8d
commit fc9915ab78
5 changed files with 145 additions and 2 deletions

View File

@ -72,6 +72,7 @@ public class Main {
// test.testGetTradeHistory();
// test.testAssetSerialization();
// test.testGetMarketHistory();
test.testGetAccountBalances();
// test.testGetAccountBalances();
test.testGetAssetHoldersCount();
}
}

View File

@ -9,6 +9,7 @@ public class RPC {
public static final String CALL_NETWORK_BROADCAST = "network_broadcast";
public static final String CALL_HISTORY = "history";
public static final String CALL_DATABASE = "database";
public static final String CALL_ASSET = "asset";
public static final String CALL_GET_ACCOUNT_BY_NAME = "get_account_by_name";
public static final String CALL_GET_ACCOUNTS = "get_accounts";
public static final String CALL_GET_DYNAMIC_GLOBAL_PROPERTIES = "get_dynamic_global_properties";
@ -25,4 +26,5 @@ public class RPC {
public static final String CALL_GET_LIMIT_ORDERS = "get_limit_orders";
public static final String CALL_GET_TRADE_HISTORY = "get_trade_history";
public static final String CALL_GET_MARKET_HISTORY = "get_market_history";
public static final String CALL_GET_ALL_ASSET_HOLDERS = "get_all_asset_holders";
}

View File

@ -14,6 +14,7 @@ import de.bitsharesmunich.graphenej.test.NaiveSSLContext;
import com.neovisionaries.ws.client.*;
import de.bitsharesmunich.graphenej.api.*;
import org.bitcoinj.core.*;
import org.spongycastle.asn1.x509.Holder;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import javax.net.ssl.SSLContext;
@ -33,6 +34,7 @@ import java.util.logging.Logger;
*/
public class Test {
public static final String AMAZON_WITNESS = "ws://54.91.97.99:8090";
public static final String WITNESS_URL = "api://api.devling.xyz:8088";
public static final String OPENLEDGER_WITNESS_URL = "wss://bitshares.openledger.info/api";
public static final String BLOCK_PAY_DE = "wss://de.blockpay.ch:8089";
@ -1180,4 +1182,43 @@ public class Test {
System.out.println("IOException. Msg: " + e.getMessage());
}
}
public void testGetAssetHoldersCount(){
SSLContext context = null;
WitnessResponseListener listener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
System.out.println("onSuccess");
List<HoldersCount> holdersCountList = (List<HoldersCount>) response.result;
for(HoldersCount holdersCount : holdersCountList){
System.out.println(String.format("Asset %s has %d holders", holdersCount.asset.getObjectId(), holdersCount.count));
}
}
@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(AMAZON_WITNESS);
mWebSocket.addListener(new GetAllAssetHolders(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

@ -1,7 +1,84 @@
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;
import com.neovisionaries.ws.client.WebSocketFrame;
import de.bitsharesmunich.graphenej.RPC;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.*;
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/25/17.
*/
public class GetAllAssetHolders {
public class GetAllAssetHolders extends BaseGrapheneHandler {
private final static int LOGIN_ID = 1;
private final static int GET_ASSET_API_ID = 2;
private final static int GET_ALL_ASSET_HOLDERS_COUNT = 3;
private int currentId = 1;
private int assetApiId = -1;
public GetAllAssetHolders(WitnessResponseListener listener) {
super(listener);
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> loginParams = new ArrayList<>();
loginParams.add(null);
loginParams.add(null);
ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, currentId);
websocket.sendText(loginCall.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
if(frame.isTextFrame())
System.out.println("<<< "+frame.getPayloadText());
String response = frame.getPayloadText();
Gson gson = new Gson();
BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
if(baseResponse.error != null){
mListener.onError(baseResponse.error);
websocket.disconnect();
}else {
currentId++;
ArrayList<Serializable> emptyParams = new ArrayList<>();
if (baseResponse.id == LOGIN_ID) {
ApiCall networkApiIdCall = new ApiCall(1, RPC.CALL_ASSET, emptyParams, RPC.VERSION, currentId);
websocket.sendText(networkApiIdCall.toJsonString());
}else if(baseResponse.id == GET_ASSET_API_ID){
Type ApiIdResponse = new TypeToken<WitnessResponse<Integer>>() {}.getType();
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
assetApiId = witnessResponse.result;
ApiCall apiCall = new ApiCall(assetApiId, RPC.CALL_GET_ALL_ASSET_HOLDERS, emptyParams, RPC.VERSION, currentId);
websocket.sendText(apiCall.toJsonString());
} else if (baseResponse.id == GET_ALL_ASSET_HOLDERS_COUNT) {
Type AssetTokenHolders = new TypeToken<WitnessResponse<List<HoldersCount>>>(){}.getType();
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(HoldersCount.class, new HoldersCount.HoldersCountDeserializer());
WitnessResponse<List<HoldersCount>> witnessResponse = builder.create().fromJson(response, AssetTokenHolders);
mListener.onSuccess(witnessResponse);
websocket.disconnect();
}else{
System.out.println("current id: "+currentId);
}
}
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
if(frame.isTextFrame()){
System.out.println(">>> "+frame.getPayloadText());
}
}
}

View File

@ -1,7 +1,29 @@
package de.bitsharesmunich.graphenej.models;
import com.google.gson.*;
import de.bitsharesmunich.graphenej.Asset;
import java.lang.reflect.Type;
/**
* Created by nelson on 1/25/17.
*/
public class HoldersCount {
public static final String KEY_ASSET_ID = "asset_id";
public static final String KEY_COUNT = "count";
public Asset asset;
public long count;
public static class HoldersCountDeserializer implements JsonDeserializer<HoldersCount> {
@Override
public HoldersCount deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
HoldersCount holdersCount = new HoldersCount();
holdersCount.asset = new Asset(jsonObject.get(KEY_ASSET_ID).getAsString());
holdersCount.count = jsonObject.get(KEY_COUNT).getAsLong();
return holdersCount;
}
}
}