diff --git a/src/main/java/de/bitsharesmunich/graphenej/MarketTrade.java b/src/main/java/de/bitsharesmunich/graphenej/MarketTrade.java new file mode 100644 index 0000000..a4b45a5 --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/MarketTrade.java @@ -0,0 +1,12 @@ +package de.bitsharesmunich.graphenej; + +/** + * + * @author henry + */ +public class MarketTrade { + public String date; + public double price; + public double amount; + public double value; +} diff --git a/src/main/java/de/bitsharesmunich/graphenej/Test.java b/src/main/java/de/bitsharesmunich/graphenej/Test.java index 6e471a7..d79f744 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Test.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Test.java @@ -896,7 +896,7 @@ public class Test { WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE); - mWebSocket.addListener(new GetLimitOrders("1.3.0", "1.3.562", 100, new WitnessResponseListener() { + mWebSocket.addListener(new GetLimitOrders("1.3.0", "1.3.120", 100, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { List orders = (List) response.result; @@ -927,4 +927,45 @@ public class Test { System.out.println("IOException. Msg: " + e.getMessage()); } } + + void testGetTradeHistory() { + 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); + + Calendar from = Calendar.getInstance(); + from.roll(Calendar.DAY_OF_MONTH, false); + from.roll(Calendar.DAY_OF_MONTH, false); + Calendar to = Calendar.getInstance(); + to.roll(Calendar.DAY_OF_MONTH, false); + + mWebSocket.addListener(new GetTradeHistory("BTS", "EUR", "20161215T0130000", "20161212T233000",100, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + List orders = (List) response.result; + for(MarketTrade markeTrade : orders){ + System.out.println("At " + markeTrade.date + " amount " + markeTrade.amount + " value " + markeTrade.value + " price " + markeTrade.price); + } + } + + @Override + public void onError(BaseResponse.Error error) { + } + })); + 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/api/GetBlockHeader.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java index 08bf9a7..3844a2b 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java @@ -6,7 +6,6 @@ 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 com.sun.tools.internal.ws.processor.model.Block; import de.bitsharesmunich.graphenej.RPC; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.*; diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java new file mode 100644 index 0000000..9f01c03 --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java @@ -0,0 +1,97 @@ +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.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFrame; +import de.bitsharesmunich.graphenej.MarketTrade; +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; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Created by nelson on 11/15/16. + */ +public class GetTradeHistory extends WebSocketAdapter { + + private String a; + private String b; + private String toTime; + private String fromTime; + private int limit; + private WitnessResponseListener mListener; + + public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener mListener) { + this.a = a; + this.b = b; + this.toTime = toTime; + this.fromTime = fromTime; + this.limit = limit; + this.mListener = mListener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList<>(); + accountParams.add(this.a); + accountParams.add(this.b); + accountParams.add(this.toTime); + accountParams.add(this.fromTime); + accountParams.add(this.limit); + + ApiCall getAccountByName = new ApiCall(0, "get_trade_history", accountParams, RPC.VERSION, 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + if (frame.isTextFrame()) { + System.out.println("<<< " + frame.getPayloadText()); + } + try { + String response = frame.getPayloadText(); + GsonBuilder builder = new GsonBuilder(); + + Type GetTradeHistoryResponse = new TypeToken>>() { + }.getType(); + WitnessResponse> witnessResponse = builder.create().fromJson(response, GetTradeHistoryResponse); + if (witnessResponse.error != null) { + this.mListener.onError(witnessResponse.error); + } else { + this.mListener.onSuccess(witnessResponse); + } + } catch (Exception e) { + e.printStackTrace(); + } + websocket.disconnect(); + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if (frame.isTextFrame()) { + System.out.println(">>> " + frame.getPayloadText()); + } + } + + @Override + public void onError(WebSocket websocket, WebSocketException cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } + + @Override + public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { + mListener.onError(new BaseResponse.Error(cause.getMessage())); + websocket.disconnect(); + } +}