From 74767f2d1d1c63f81e459400d315d2d117bcd461 Mon Sep 17 00:00:00 2001 From: Kostya Shpachenko Date: Tue, 12 Dec 2017 16:36:50 +0200 Subject: [PATCH] Implemented GetBlock API call --- .../main/java/cy/agorise/graphenej/RPC.java | 1 + .../cy/agorise/graphenej/api/GetBlock.java | 108 ++++++++++++++++++ .../cy/agorise/graphenej/models/Block.java | 71 ++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 graphenej/src/main/java/cy/agorise/graphenej/api/GetBlock.java create mode 100644 graphenej/src/main/java/cy/agorise/graphenej/models/Block.java diff --git a/graphenej/src/main/java/cy/agorise/graphenej/RPC.java b/graphenej/src/main/java/cy/agorise/graphenej/RPC.java index e1f42c0..ea6c509 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/RPC.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/RPC.java @@ -25,6 +25,7 @@ public class RPC { public static final String GET_ACCOUNT_BALANCES = "get_account_balances"; 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_BLOCK = "get_block"; 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"; diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/GetBlock.java b/graphenej/src/main/java/cy/agorise/graphenej/api/GetBlock.java new file mode 100644 index 0000000..2cef872 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/GetBlock.java @@ -0,0 +1,108 @@ +package cy.agorise.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 cy.agorise.graphenej.AssetAmount; +import cy.agorise.graphenej.RPC; +import cy.agorise.graphenej.Transaction; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.ApiCall; +import cy.agorise.graphenej.models.BaseResponse; +import cy.agorise.graphenej.models.Block; +import cy.agorise.graphenej.models.WitnessResponse; +import cy.agorise.graphenej.operations.TransferOperation; + +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class GetBlock extends BaseGrapheneHandler { + + private final static int LOGIN_ID = 1; + private final static int GET_DATABASE_ID = 2; + private final static int GET_BLOCK_ID = 3; + + private long blockNumber; + private WitnessResponseListener mListener; + + private int currentId = LOGIN_ID; + + private boolean mOneTime; + + public GetBlock(long blockNumber, boolean oneTime, WitnessResponseListener listener){ + super(listener); + this.blockNumber = blockNumber; + this.mOneTime = oneTime; + this.mListener = listener; + } + + public GetBlock(long blockNumber, WitnessResponseListener listener){ + this(blockNumber, true, 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 { + String response = frame.getPayloadText(); + System.out.println("<<< "+response); + + Gson gson = new Gson(); + BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class); + if(baseResponse.error != null){ + mListener.onError(baseResponse.error); + if(mOneTime){ + websocket.disconnect(); + } + } else { + currentId++; + ArrayList emptyParams = new ArrayList<>(); + if (baseResponse.id == LOGIN_ID) { + ApiCall getDatabaseId = new ApiCall(1, RPC.CALL_DATABASE, emptyParams, RPC.VERSION, currentId); + websocket.sendText(getDatabaseId.toJsonString()); + } else if (baseResponse.id == GET_DATABASE_ID) { + Type ApiIdResponse = new TypeToken>() {}.getType(); + WitnessResponse witnessResponse = gson.fromJson(response, ApiIdResponse); + Integer apiId = witnessResponse.result; + + ArrayList params = new ArrayList<>(); + String blockNum = String.format("%d", this.blockNumber); + params.add(blockNum); + + ApiCall loginCall = new ApiCall(apiId, RPC.CALL_GET_BLOCK, params, RPC.VERSION, currentId); + websocket.sendText(loginCall.toJsonString()); + } else if (baseResponse.id == GET_BLOCK_ID) { + Type BlockResponse = new TypeToken>(){}.getType(); + gson = new GsonBuilder() + .registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer()) + .registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()) + .registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()) + .create(); + WitnessResponse blockResponse = gson.fromJson(response, BlockResponse); + mListener.onSuccess(blockResponse); + if (mOneTime) { + websocket.disconnect(); + } + } + } + + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()) + System.out.println(">>> "+frame.getPayloadText()); + } +} diff --git a/graphenej/src/main/java/cy/agorise/graphenej/models/Block.java b/graphenej/src/main/java/cy/agorise/graphenej/models/Block.java new file mode 100644 index 0000000..625d9d4 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/models/Block.java @@ -0,0 +1,71 @@ +package cy.agorise.graphenej.models; + +import cy.agorise.graphenej.Transaction; + +import java.util.List; + +public class Block { + private String previous; + private String timestamp; + private String witness; + private String transaction_merkle_root; + private Object[] extensions; + private String witness_signature; + private List transactions; + + public String getPrevious() { + return previous; + } + + public void setPrevious(String previous) { + this.previous = previous; + } + + public String getTimestamp() { + return timestamp; + } + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + + public String getWitness() { + return witness; + } + + public void setWitness(String witness) { + this.witness = witness; + } + + public String getTransaction_merkle_root() { + return transaction_merkle_root; + } + + public void setTransaction_merkle_root(String transaction_merkle_root) { + this.transaction_merkle_root = transaction_merkle_root; + } + + public Object[] getExtensions() { + return extensions; + } + + public void setExtensions(Object[] extensions) { + this.extensions = extensions; + } + + public String getWitness_signature() { + return witness_signature; + } + + public void setWitness_signature(String witness_signature) { + this.witness_signature = witness_signature; + } + + public List getTransactions() { + return transactions; + } + + public void setTransactions(List transactions) { + this.transactions = transactions; + } +}