Implemented GetBlock API call

This commit is contained in:
Kostya Shpachenko 2017-12-12 16:36:50 +02:00
parent ff856e7e2c
commit 74767f2d1d
3 changed files with 180 additions and 0 deletions

View file

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

View file

@ -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<String, List<String>> headers) throws Exception {
ArrayList<Serializable> 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<Serializable> 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<WitnessResponse<Integer>>() {}.getType();
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
Integer apiId = witnessResponse.result;
ArrayList<Serializable> 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<WitnessResponse<Block>>(){}.getType();
gson = new GsonBuilder()
.registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer())
.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer())
.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer())
.create();
WitnessResponse<Block> 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());
}
}

View file

@ -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<Transaction> 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<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}
}