Added basic support for the 'lookup_asset_symbols' API call

This commit is contained in:
Nelson R. Perez 2016-12-13 00:21:40 -05:00
parent 3b1897f083
commit f8cb55ffb5
9 changed files with 183 additions and 9 deletions

View file

@ -4,8 +4,23 @@ package de.bitsharesmunich.graphenej;
* Created by nelson on 11/9/16. * Created by nelson on 11/9/16.
*/ */
public class Asset extends GrapheneObject { 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) { public Asset(String id) {
super(id); super(id);
this.id = id;
}
public String getSymbol(){
return this.symbol;
}
public String getId(){
return this.id;
} }
} }

View file

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

View file

@ -58,6 +58,8 @@ public class Main {
// test.testImportBinFile(); // test.testImportBinFile();
// test.testLookupAccounts(); // test.testLookupAccounts();
// test.testLookupAccounts(); // test.testLookupAccounts();
test.testDecodeMemo(); // test.testDecodeMemo();
// test.testGetRelativeAccountHistory();
test.testLookupAssetSymbols();
} }
} }

View file

@ -16,4 +16,5 @@ public class RPC {
public static final String CALL_GET_KEY_REFERENCES = "get_key_references"; 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_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts"; public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts";
public static final String CALL_LOOKUP_ASSET_SYMBOLS = "lookup_asset_symbols";
} }

View file

@ -1,7 +1,6 @@
package de.bitsharesmunich.graphenej; package de.bitsharesmunich.graphenej;
import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.*;
import de.bitsharesmunich.graphenej.models.BaseResponse;
import de.bitsharesmunich.graphenej.objects.Memo; import de.bitsharesmunich.graphenej.objects.Memo;
import com.google.common.primitives.UnsignedLong; import com.google.common.primitives.UnsignedLong;
import com.google.gson.Gson; 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.objects.MemoBuilder;
import de.bitsharesmunich.graphenej.test.NaiveSSLContext; import de.bitsharesmunich.graphenej.test.NaiveSSLContext;
import com.neovisionaries.ws.client.*; import com.neovisionaries.ws.client.*;
import de.bitsharesmunich.graphenej.models.AccountProperties;
import de.bitsharesmunich.graphenej.models.WitnessResponse;
import de.bitsharesmunich.graphenej.api.*; import de.bitsharesmunich.graphenej.api.*;
import org.bitcoinj.core.*; import org.bitcoinj.core.*;
import org.spongycastle.crypto.digests.RIPEMD160Digest; import org.spongycastle.crypto.digests.RIPEMD160Digest;
@ -462,8 +459,7 @@ public class Test {
brainKey = new BrainKey(suggestion, 0); brainKey = new BrainKey(suggestion, 0);
} else { } else {
System.out.println("Using brain key: " + Main.BILTHON_5_BRAIN_KEY); System.out.println("Using brain key: " + Main.BILTHON_5_BRAIN_KEY);
// brainKey = new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0); 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);
} }
ECKey key = brainKey.getPrivateKey(); ECKey key = brainKey.getPrivateKey();
System.out.println("Private key..................: " + Util.bytesToHex(key.getSecretBytes())); 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("generated Json : " + memoJson.toString());
// System.out.println("Decode Memo : " + Memo.decodeMessage(from, to, memoJson.getAsJsonObject().get("message").getAsString(), memoJson.getAsJsonObject().get("nonce").getAsString())); // 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<HistoricalTransfer> transactionHistory = (List<HistoricalTransfer>) 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<List<Asset>> 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<Asset> 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());
}
}
} }

View file

@ -55,7 +55,7 @@ public class TransferOperation extends BaseOperation {
return this.to; return this.to;
} }
public AssetAmount getAmount(){ public AssetAmount getAssetAmount(){
return this.amount; return this.amount;
} }

View file

@ -83,7 +83,7 @@ public class GetRelativeAccountHistory extends WebSocketAdapter {
ArrayList<Serializable> loginParams = new ArrayList<>(); ArrayList<Serializable> loginParams = new ArrayList<>();
loginParams.add(null); loginParams.add(null);
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()); websocket.sendText(loginCall.toJsonString());
} }

View file

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

View file

@ -2,6 +2,7 @@ package de.bitsharesmunich.graphenej.models;
import de.bitsharesmunich.graphenej.TransferOperation; import de.bitsharesmunich.graphenej.TransferOperation;
/** /**
* This class offers support to deserialization of transfer operations received by the API * This class offers support to deserialization of transfer operations received by the API
* method get_relative_account_history. * method get_relative_account_history.