Added support for the 'get_assets' API call and updated the Asset class accordingly
This commit is contained in:
parent
ef50a9f7ef
commit
7cd0a76e2e
9 changed files with 330 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
|||
package de.bitsharesmunich.graphenej;
|
||||
|
||||
import com.google.common.primitives.UnsignedLong;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
@ -12,6 +13,14 @@ public class Asset extends GrapheneObject {
|
|||
public static final String KEY_SYMBOL = "symbol";
|
||||
public static final String KEY_PRECISION = "precision";
|
||||
public static final String KEY_ISSUER = "issuer";
|
||||
public static final String KEY_OPTIONS = "options";
|
||||
public static final String KEY_MAX_SUPPLY = "max_supply";
|
||||
public static final String KEY_MARKET_FEE_PERCENT = "market_fee_percent";
|
||||
public static final String KEY_MARKET_FEE = "max_market_fee";
|
||||
public static final String KEY_ISSUER_PERMISSIONS = "issuer_permissions";
|
||||
public static final String KEY_FLAGS = "flags";
|
||||
public static final String KEY_CORE_EXCHANGE_RATE = "core_exchange_rate";
|
||||
public static final String KEY_DESCRIPTION = "description";
|
||||
public static final String KEY_DYNAMIC_ASSET_DATA_ID = "dynamic_asset_data_id";
|
||||
|
||||
private String symbol;
|
||||
|
@ -83,6 +92,14 @@ public class Asset extends GrapheneObject {
|
|||
return description;
|
||||
}
|
||||
|
||||
public void setAssetOptions(AssetOptions options){
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public AssetOptions getAssetOptions(){
|
||||
return this.options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
|
@ -97,6 +114,14 @@ public class Asset extends GrapheneObject {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is a witness-fed asset and all issuer permissions flags are set.
|
||||
* @return: True if the asset is a smartcoin.
|
||||
*/
|
||||
public boolean isSmartcoin(){
|
||||
return this.options.getFlags() == 128 && options.getIssuerPermissions() == 511;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom deserializer used to instantiate a simple version of the Asset class from the response of the
|
||||
* 'lookup_asset_symbols' API call.
|
||||
|
@ -110,7 +135,21 @@ public class Asset extends GrapheneObject {
|
|||
String symbol = object.get(KEY_SYMBOL).getAsString();
|
||||
int precision = object.get(KEY_PRECISION).getAsInt();
|
||||
String issuer = object.get(KEY_ISSUER).getAsString();
|
||||
return new Asset(id, symbol, precision, issuer);
|
||||
JsonObject optionsJson = object.get(KEY_OPTIONS).getAsJsonObject();
|
||||
|
||||
// Deserializing asset options
|
||||
AssetOptions options = new AssetOptions();
|
||||
options.setMaxSupply(UnsignedLong.valueOf(optionsJson.get(KEY_MAX_SUPPLY).getAsString()));
|
||||
options.setMarketFeePercent(optionsJson.get(KEY_MARKET_FEE_PERCENT).getAsInt());
|
||||
options.setMaxMarketFee(UnsignedLong.valueOf(optionsJson.get(KEY_MARKET_FEE).getAsString()));
|
||||
options.setIssuerPermissions(optionsJson.get(KEY_ISSUER_PERMISSIONS).getAsLong());
|
||||
options.setFlags(optionsJson.get(KEY_FLAGS).getAsInt());
|
||||
|
||||
//TODO: Deserialize core_exchange_rate field
|
||||
|
||||
Asset asset = new Asset(id, symbol, precision, issuer);
|
||||
asset.setAssetOptions(options);
|
||||
return asset;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,73 @@
|
|||
package de.bitsharesmunich.graphenej;
|
||||
|
||||
import com.google.common.primitives.UnsignedLong;
|
||||
|
||||
/**
|
||||
* Created by nelson on 12/13/16.
|
||||
*/
|
||||
public class AssetOptions {
|
||||
private String max_supply;
|
||||
private UnsignedLong max_supply;
|
||||
private long market_fee_percent;
|
||||
private String max_market_fee;
|
||||
private UnsignedLong 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 Price core_exchange_rate;
|
||||
//TODO: Implement whitelist_authorities, blacklist_authorities, whitelist_markets, blacklist_markets and extensions
|
||||
private String description;
|
||||
|
||||
public UnsignedLong getMaxSupply() {
|
||||
return max_supply;
|
||||
}
|
||||
|
||||
public void setMaxSupply(UnsignedLong max_supply) {
|
||||
this.max_supply = max_supply;
|
||||
}
|
||||
|
||||
public long getMarketFeePercent() {
|
||||
return market_fee_percent;
|
||||
}
|
||||
|
||||
public void setMarketFeePercent(long market_fee_percent) {
|
||||
this.market_fee_percent = market_fee_percent;
|
||||
}
|
||||
|
||||
public UnsignedLong getMax_market_fee() {
|
||||
return max_market_fee;
|
||||
}
|
||||
|
||||
public void setMaxMarketFee(UnsignedLong max_market_fee) {
|
||||
this.max_market_fee = max_market_fee;
|
||||
}
|
||||
|
||||
public long getIssuerPermissions() {
|
||||
return issuer_permissions;
|
||||
}
|
||||
|
||||
public void setIssuerPermissions(long issuer_permissions) {
|
||||
this.issuer_permissions = issuer_permissions;
|
||||
}
|
||||
|
||||
public int getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public void setFlags(int flags) {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
public Price getCoreExchangeRate() {
|
||||
return core_exchange_rate;
|
||||
}
|
||||
|
||||
public void setCoreExchangeRate(Price core_exchange_rate) {
|
||||
this.core_exchange_rate = core_exchange_rate;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,10 +65,11 @@ public class Main {
|
|||
// test.testDecodeMemo();
|
||||
// test.testGetRelativeAccountHistory();
|
||||
// test.testLookupAssetSymbols();
|
||||
test.testListAssets();
|
||||
// test.testGetBlockHeader();
|
||||
//test.testGetLimitOrders();
|
||||
// test.testGetTradeHistory();
|
||||
// test.testAssetSerialization();
|
||||
test.testGetMarketHistory();
|
||||
// test.testGetMarketHistory();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,18 @@
|
|||
package de.bitsharesmunich.graphenej;
|
||||
|
||||
/**
|
||||
* The price struct stores asset prices in the Graphene system.
|
||||
*
|
||||
* A price is defined as a ratio between two assets, and represents a possible exchange rate
|
||||
* between those two assets. prices are generally not stored in any simplified form, i.e. a price
|
||||
* of (1000 CORE)/(20 USD) is perfectly normal.
|
||||
|
||||
* The assets within a price are labeled base and quote. Throughout the Graphene code base,
|
||||
* the convention used is that the base asset is the asset being sold, and the quote asset is
|
||||
* the asset being purchased, where the price is represented as base/quote, so in the example
|
||||
* price above the seller is looking to sell CORE asset and get USD in return.
|
||||
*
|
||||
* Note: Taken from the Graphene doxygen.
|
||||
* Created by nelson on 12/16/16.
|
||||
*/
|
||||
public class Price {
|
||||
|
|
|
@ -17,6 +17,7 @@ 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_LIST_ASSETS = "list_assets";
|
||||
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";
|
||||
|
|
|
@ -855,6 +855,47 @@ public class Test {
|
|||
}
|
||||
}
|
||||
|
||||
public void testListAssets(){
|
||||
WitnessResponseListener listener = new WitnessResponseListener() {
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
System.out.println("onSuccess");
|
||||
List<Asset> resp = (List<Asset>) response.result;
|
||||
System.out.println(String.format("Got %d assets", resp.size()));
|
||||
for(Asset asset : resp){
|
||||
if(asset.isSmartcoin())
|
||||
System.out.println("Asset: "+asset.getObjectId()+", 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);
|
||||
|
||||
mWebSocket.addListener(new ListAssets("", ListAssets.LIST_ALL, 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 testGetBlockHeader(){
|
||||
WitnessResponseListener listener = new WitnessResponseListener() {
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package de.bitsharesmunich.graphenej.api;
|
||||
|
||||
import com.neovisionaries.ws.client.WebSocket;
|
||||
import com.neovisionaries.ws.client.WebSocketAdapter;
|
||||
import com.neovisionaries.ws.client.WebSocketException;
|
||||
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
|
||||
import de.bitsharesmunich.graphenej.models.BaseResponse;
|
||||
|
||||
/**
|
||||
* Created by nelson on 1/5/17.
|
||||
*/
|
||||
public abstract class BaseGrapheneHandler extends WebSocketAdapter {
|
||||
|
||||
protected WitnessResponseListener mListener;
|
||||
|
||||
public BaseGrapheneHandler(WitnessResponseListener listener){
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||
System.out.println("onError. cause: "+cause.getMessage());
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
|
||||
System.out.println("handleCallbackError. cause: "+cause.getMessage()+", error: "+cause.getClass());
|
||||
for (StackTraceElement element : cause.getStackTrace()){
|
||||
System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber());
|
||||
}
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
117
src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java
Normal file
117
src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java
Normal file
|
@ -0,0 +1,117 @@
|
|||
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.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;
|
||||
|
||||
/**
|
||||
* WebSocketAdapter class used to send a request a 'list_assets' API call to the witness node.
|
||||
*
|
||||
* @see: <a href="http://docs.bitshares.org/development/namespaces/app.html"></a>
|
||||
*
|
||||
* The API imposes a limit of of 100 assets per request, but if the user of this class wants
|
||||
* to get a list of all assets, the LIST_ALL constant must be used as second argument in the
|
||||
* constructor. Internally we are going to perform multiple calls in order to satisfy the user's
|
||||
* request.
|
||||
*
|
||||
* Created by nelson on 1/5/17.
|
||||
*/
|
||||
public class ListAssets extends BaseGrapheneHandler {
|
||||
/**
|
||||
* Constant that must be used as argument to the constructor of this class to indicate
|
||||
* that the user wants to get all existing assets.
|
||||
*/
|
||||
public static final int LIST_ALL = -1;
|
||||
|
||||
/**
|
||||
* Internal constant used to represent the maximum limit of assets retrieved in one call.
|
||||
*/
|
||||
private final int MAX_BATCH_SIZE = 100;
|
||||
|
||||
private List<Asset> assets;
|
||||
private String lowerBound;
|
||||
private int limit;
|
||||
private int requestCounter = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lowerBoundSymbol: Lower bound of symbol names to retrieve
|
||||
* @param limit: Maximum number of assets to fetch, if the constant LIST_ALL
|
||||
* is passed, all existing assets will be retrieved.
|
||||
*/
|
||||
public ListAssets(String lowerBoundSymbol, int limit, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.lowerBound = lowerBoundSymbol;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
params.add(this.lowerBound);
|
||||
if(limit > MAX_BATCH_SIZE || limit == LIST_ALL){
|
||||
params.add(MAX_BATCH_SIZE);
|
||||
}else{
|
||||
params.add(this.limit);
|
||||
}
|
||||
ApiCall apiCall = new ApiCall(0, RPC.CALL_LIST_ASSETS, params, RPC.VERSION, 0);
|
||||
websocket.sendText(apiCall.toJsonString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
String response = frame.getPayloadText();
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
Type LookupAssetSymbolsResponse = new TypeToken<WitnessResponse<List<Asset>>>(){}.getType();
|
||||
gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer());
|
||||
WitnessResponse<List<Asset>> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse);
|
||||
if(this.limit != LIST_ALL && this.limit < MAX_BATCH_SIZE){
|
||||
// If the requested number of assets was below
|
||||
// the limit, we just call the listener.
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
}else{
|
||||
// Updating counter to keep track of how many batches we already retrieved.
|
||||
requestCounter++;
|
||||
if(this.assets == null){
|
||||
this.assets = new ArrayList<>();
|
||||
}
|
||||
this.assets.addAll(witnessResponse.result);
|
||||
|
||||
// Checking to see if we're done
|
||||
if(this.limit == LIST_ALL && witnessResponse.result.size() < MAX_BATCH_SIZE){
|
||||
// In case we requested all assets, we might be in the last round whenever
|
||||
// we got less than the requested amount.
|
||||
witnessResponse.result = this.assets;
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
}else if(this.assets.size() == this.limit){
|
||||
// We already have the required amount of assets
|
||||
witnessResponse.result = this.assets;
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
}else{
|
||||
// We still need to fetch some more assets
|
||||
this.lowerBound = this.assets.get(this.assets.size() - 1).getSymbol();
|
||||
int nextBatch = this.limit == LIST_ALL ? MAX_BATCH_SIZE : this.limit - (MAX_BATCH_SIZE * requestCounter);
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
params.add(this.lowerBound);
|
||||
params.add(nextBatch);
|
||||
ApiCall apiCall = new ApiCall(0, RPC.CALL_LIST_ASSETS, params, RPC.VERSION, 0);
|
||||
websocket.sendText(apiCall.toJsonString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,11 +4,13 @@ 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.WebSocketException;
|
||||
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.BaseResponse;
|
||||
import de.bitsharesmunich.graphenej.models.WitnessResponse;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -57,4 +59,21 @@ public class LookupAssetSymbols extends WebSocketAdapter {
|
|||
if(frame.isTextFrame())
|
||||
System.out.println(">>> "+frame.getPayloadText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||
System.out.println("onError. cause: "+cause.getMessage());
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
|
||||
System.out.println("handleCallbackError. cause: "+cause.getMessage()+", error: "+cause.getClass());
|
||||
for (StackTraceElement element : cause.getStackTrace()){
|
||||
System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber());
|
||||
}
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue