From f8cb55ffb5c4a928600fb3332cf50574727a0ded Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Tue, 13 Dec 2016 00:21:40 -0500 Subject: [PATCH] Added basic support for the 'lookup_asset_symbols' API call --- .../de/bitsharesmunich/graphenej/Asset.java | 15 +++ .../graphenej/AssetOptions.java | 14 +++ .../de/bitsharesmunich/graphenej/Main.java | 4 +- .../de/bitsharesmunich/graphenej/RPC.java | 1 + .../de/bitsharesmunich/graphenej/Test.java | 94 +++++++++++++++++-- .../graphenej/TransferOperation.java | 2 +- .../api/GetRelativeAccountHistory.java | 2 +- .../graphenej/api/LookupAssetSymbols.java | 59 ++++++++++++ .../graphenej/models/HistoricalTransfer.java | 1 + 9 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/bitsharesmunich/graphenej/AssetOptions.java create mode 100644 src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java diff --git a/src/main/java/de/bitsharesmunich/graphenej/Asset.java b/src/main/java/de/bitsharesmunich/graphenej/Asset.java index 27faf17..261f4e4 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Asset.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Asset.java @@ -4,8 +4,23 @@ package de.bitsharesmunich.graphenej; * Created by nelson on 11/9/16. */ public class Asset extends GrapheneObject { + private String id; + private String symbol; + private int precision; + private String issuer; + private String dynamic_asset_data_id; + private AssetOptions options; public Asset(String id) { super(id); + this.id = id; + } + + public String getSymbol(){ + return this.symbol; + } + + public String getId(){ + return this.id; } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/AssetOptions.java b/src/main/java/de/bitsharesmunich/graphenej/AssetOptions.java new file mode 100644 index 0000000..08503cf --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/AssetOptions.java @@ -0,0 +1,14 @@ +package de.bitsharesmunich.graphenej; + +/** + * Created by nelson on 12/13/16. + */ +public class AssetOptions { + private String max_supply; + private long market_fee_percent; + private String max_market_fee; + private long issuer_permissions; + private int flags; + //TODO: Implement core_exchange_rate, whitelist_authorities, blacklist_authorities, whitelist_markets, blacklist_markets and extensions + private String description; +} diff --git a/src/main/java/de/bitsharesmunich/graphenej/Main.java b/src/main/java/de/bitsharesmunich/graphenej/Main.java index a3833ac..2caba9a 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Main.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Main.java @@ -58,6 +58,8 @@ public class Main { // test.testImportBinFile(); // test.testLookupAccounts(); // test.testLookupAccounts(); - test.testDecodeMemo(); +// test.testDecodeMemo(); +// test.testGetRelativeAccountHistory(); + test.testLookupAssetSymbols(); } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/RPC.java b/src/main/java/de/bitsharesmunich/graphenej/RPC.java index 2d10295..cbcfe15 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/RPC.java +++ b/src/main/java/de/bitsharesmunich/graphenej/RPC.java @@ -16,4 +16,5 @@ public class RPC { 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_LOOKUP_ACCOUNTS = "lookup_accounts"; + public static final String CALL_LOOKUP_ASSET_SYMBOLS = "lookup_asset_symbols"; } diff --git a/src/main/java/de/bitsharesmunich/graphenej/Test.java b/src/main/java/de/bitsharesmunich/graphenej/Test.java index 95464d6..b7e3a9a 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Test.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Test.java @@ -1,7 +1,6 @@ package de.bitsharesmunich.graphenej; -import de.bitsharesmunich.graphenej.models.ApiCall; -import de.bitsharesmunich.graphenej.models.BaseResponse; +import de.bitsharesmunich.graphenej.models.*; import de.bitsharesmunich.graphenej.objects.Memo; import com.google.common.primitives.UnsignedLong; import com.google.gson.Gson; @@ -15,8 +14,6 @@ import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.objects.MemoBuilder; import de.bitsharesmunich.graphenej.test.NaiveSSLContext; import com.neovisionaries.ws.client.*; -import de.bitsharesmunich.graphenej.models.AccountProperties; -import de.bitsharesmunich.graphenej.models.WitnessResponse; import de.bitsharesmunich.graphenej.api.*; import org.bitcoinj.core.*; import org.spongycastle.crypto.digests.RIPEMD160Digest; @@ -462,8 +459,7 @@ public class Test { brainKey = new BrainKey(suggestion, 0); } else { System.out.println("Using brain key: " + Main.BILTHON_5_BRAIN_KEY); -// brainKey = new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0); - brainKey = new BrainKey("CONCOCT BALOW JINJILI UNOILED MESOBAR REEST BREATH OOCYST MOUSLE HOGWARD STOLLEN ASH", 0); + brainKey = new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0); } ECKey key = brainKey.getPrivateKey(); System.out.println("Private key..................: " + Util.bytesToHex(key.getSecretBytes())); @@ -745,4 +741,90 @@ public class Test { 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())); } + + public void testGetRelativeAccountHistory(){ + WitnessResponseListener listener = new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + List transactionHistory = (List) response.result; + System.out.println("Number of transactions: "+transactionHistory.size()); + for(HistoricalTransfer historical : transactionHistory){ + if(historical.op != null){ + TransferOperation op = historical.op; + System.out.println("from: "+op.getFrom().getObjectId()+", to: "+op.getTo().getObjectId()+", amount: "+op.getAssetAmount().getAmount()+", block #: "+historical.block_num); + } + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + }; + + 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 GetRelativeAccountHistory(new UserAccount("1.2.140994"), 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()); + } + } + + public void testLookupAssetSymbols(){ + WitnessResponseListener listener = new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + WitnessResponse> resp = response; + for(Asset asset : resp.result){ + System.out.println("Asset: "+asset.getId()+", Symbol: "+asset.getSymbol()); + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + }; + + 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); + + ArrayList assets = new ArrayList<>(); + assets.add(new Asset("1.3.0")); + assets.add(new Asset("1.3.120")); + assets.add(new Asset("1.3.121")); + mWebSocket.addListener(new LookupAssetSymbols(assets, 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/TransferOperation.java b/src/main/java/de/bitsharesmunich/graphenej/TransferOperation.java index f0d8d5b..ae9b050 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/TransferOperation.java +++ b/src/main/java/de/bitsharesmunich/graphenej/TransferOperation.java @@ -55,7 +55,7 @@ public class TransferOperation extends BaseOperation { return this.to; } - public AssetAmount getAmount(){ + public AssetAmount getAssetAmount(){ return this.amount; } diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java index c2f0cb6..076683f 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -83,7 +83,7 @@ public class GetRelativeAccountHistory extends WebSocketAdapter { ArrayList loginParams = new ArrayList<>(); loginParams.add(null); loginParams.add(null); - ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, "2.0", currentId); + ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, currentId); websocket.sendText(loginCall.toJsonString()); } diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java b/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java new file mode 100644 index 0000000..489e403 --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -0,0 +1,59 @@ +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.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketFrame; +import de.bitsharesmunich.graphenej.Asset; +import de.bitsharesmunich.graphenej.RPC; +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 12/12/16. + */ +public class LookupAssetSymbols extends WebSocketAdapter { + private WitnessResponseListener mListener; + private List assets; + + public LookupAssetSymbols(List assets, WitnessResponseListener listener){ + this.assets = assets; + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList params = new ArrayList<>(); + ArrayList subArray = new ArrayList<>(); + for(Asset asset : this.assets){ + subArray.add(asset.getObjectId()); + params.add(subArray); + } + ApiCall loginCall = new ApiCall(0, RPC.CALL_LOOKUP_ASSET_SYMBOLS, params, RPC.VERSION, 0); + websocket.sendText(loginCall.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + String response = frame.getPayloadText(); + System.out.println("<<< "+response); + GsonBuilder gsonBuilder = new GsonBuilder(); + Type LookupAssetSymbolsResponse = new TypeToken>>(){}.getType(); + WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse); + mListener.onSuccess(witnessResponse); + } + + @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/HistoricalTransfer.java b/src/main/java/de/bitsharesmunich/graphenej/models/HistoricalTransfer.java index 8c5cfeb..899a459 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/models/HistoricalTransfer.java +++ b/src/main/java/de/bitsharesmunich/graphenej/models/HistoricalTransfer.java @@ -2,6 +2,7 @@ package de.bitsharesmunich.graphenej.models; import de.bitsharesmunich.graphenej.TransferOperation; + /** * This class offers support to deserialization of transfer operations received by the API * method get_relative_account_history.