diff --git a/src/main/java/de/bitsharesmunich/graphenej/Main.java b/src/main/java/de/bitsharesmunich/graphenej/Main.java index 83bce15..8789f9d 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Main.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Main.java @@ -72,6 +72,7 @@ public class Main { // test.testGetTradeHistory(); // test.testAssetSerialization(); // test.testGetMarketHistory(); - test.testGetAccountBalances(); +// test.testGetAccountBalances(); + test.testGetAssetHoldersCount(); } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/RPC.java b/src/main/java/de/bitsharesmunich/graphenej/RPC.java index 279f72a..7e15568 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/RPC.java +++ b/src/main/java/de/bitsharesmunich/graphenej/RPC.java @@ -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"; } \ No newline at end of file diff --git a/src/main/java/de/bitsharesmunich/graphenej/Test.java b/src/main/java/de/bitsharesmunich/graphenej/Test.java index 986f1bd..f500011 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Test.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Test.java @@ -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 holdersCountList = (List) 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()); + } + } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java index 07d11db..bfb129d 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java @@ -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> headers) throws Exception { + ArrayList 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 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>() {}.getType(); + WitnessResponse 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>>(){}.getType(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(HoldersCount.class, new HoldersCount.HoldersCountDeserializer()); + WitnessResponse> 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()); + } + } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/models/HoldersCount.java b/src/main/java/de/bitsharesmunich/graphenej/models/HoldersCount.java index 762a5a4..4804a8c 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/models/HoldersCount.java +++ b/src/main/java/de/bitsharesmunich/graphenej/models/HoldersCount.java @@ -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 { + + @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; + } + } }