From cb01952eec92eab0754e81288998bee3866a3448 Mon Sep 17 00:00:00 2001 From: Mahdi Sheikh Hosseini Date: Sat, 3 Apr 2021 19:43:20 +0430 Subject: [PATCH] Fix UserDataStream --- README.md | 8 +- .../api/client/api/BinanceApiService.java | 3 + .../api/sync/BinanceApiFuturesRestClient.java | 28 ++ .../api/sync/BinanceApiGeneralRestClient.java | 22 - .../api/sync/BinanceApiMarginRestClient.java | 22 + .../api/sync/BinanceApiSpotRestClient.java | 22 + .../api/client/config/SpotApiConfig.java | 2 +- .../binance/api/client/domain/OrderType.java | 2 +- ...eUpdateEvent.java => ExecutionReport.java} | 2 +- .../client/domain/event/OrderTradeUpdate.java | 437 ++++++++++++++++++ .../domain/event/UserDataUpdateEvent.java | 175 ++++--- .../UserDataUpdateEventDeserializer.java | 91 ++-- .../sync/BinanceApiMarginRestClientImpl.java | 18 +- .../sync/BinanceApiSpotRestClientImpl.java | 4 +- .../impl/ws/BinanceApiWebSocketListener.java | 2 +- .../UserDataUpdateEventDeserializerTest.java | 127 ++--- .../examples/MarginUserDataStreamExample.java | 12 +- .../api/examples/UserDataStreamExample.java | 12 +- .../futures/FuturesUserDataStreamExample.java | 12 +- .../testnet/TestnetLeverageMarginChange.java | 4 +- .../testnet/TestnetWebSocketExample.java | 15 +- 21 files changed, 796 insertions(+), 224 deletions(-) rename src/main/java/com/binance/api/client/domain/event/{OrderTradeUpdateEvent.java => ExecutionReport.java} (99%) create mode 100755 src/main/java/com/binance/api/client/domain/event/OrderTradeUpdate.java diff --git a/README.md b/README.md index bf74d5d02..4cb3add6a 100755 --- a/README.md +++ b/README.md @@ -420,16 +420,16 @@ client.onUserDataUpdateEvent(listenKey, response -> { // Print new balances of every available asset System.out.println(accountUpdateEvent.getBalances()); } else { - OrderTradeUpdateEvent orderTradeUpdateEvent = response.getOrderTradeUpdateEvent(); + OrderTradeUpdateEvent executionReport = response.getOrderTradeUpdateEvent(); // Print details about an order/trade - System.out.println(orderTradeUpdateEvent); + System.out.println(executionReport); // Print original quantity - System.out.println(orderTradeUpdateEvent.getOriginalQuantity()); + System.out.println(executionReport.getOriginalQuantity()); // Or price - System.out.println(orderTradeUpdateEvent.getPrice()); + System.out.println(executionReport.getPrice()); } }); ``` diff --git a/src/main/java/com/binance/api/client/api/BinanceApiService.java b/src/main/java/com/binance/api/client/api/BinanceApiService.java index 805cbd44f..d247ff7ad 100755 --- a/src/main/java/com/binance/api/client/api/BinanceApiService.java +++ b/src/main/java/com/binance/api/client/api/BinanceApiService.java @@ -228,6 +228,9 @@ Call> getMyMarginTrades(@Query("symbol") String symbol, @Query("limi @PUT("/sapi/v1/userDataStream") Call keepAliveMarginUserDataStream(@Query("listenKey") String listenKey); + @Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_APIKEY_HEADER) + @DELETE("/sapi/v1/userDataStream") + Call closeAliveMarginUserDataStream(@Query("listenKey") String listenKey); // Binance Liquidity Swap Pool endpoints @Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_APIKEY_HEADER) diff --git a/src/main/java/com/binance/api/client/api/sync/BinanceApiFuturesRestClient.java b/src/main/java/com/binance/api/client/api/sync/BinanceApiFuturesRestClient.java index dfe646581..161fb421d 100755 --- a/src/main/java/com/binance/api/client/api/sync/BinanceApiFuturesRestClient.java +++ b/src/main/java/com/binance/api/client/api/sync/BinanceApiFuturesRestClient.java @@ -26,6 +26,34 @@ public interface BinanceApiFuturesRestClient extends BinanceApiGeneralRestClient */ LeverageResponse changeInitialLeverage(LeverageRequest leverageRequest); + /** + * Change type of margin CROSSED/ISOLATED + * + * @param marginTypeRequest + */ void changeMarginType(MarginTypeRequest marginTypeRequest); + // User stream endpoints + + /** + * Start a new user data stream. + * + * @return a listen key that can be used with data streams + */ + String startUserDataStream(); + + /** + * PING a user data stream to prevent a time out. + * + * @param listenKey listen key that identifies a data stream + */ + void keepAliveUserDataStream(String listenKey); + + /** + * Close out a new user data stream. + * + * @param listenKey listen key that identifies a data stream + */ + void closeUserDataStream(String listenKey); + } diff --git a/src/main/java/com/binance/api/client/api/sync/BinanceApiGeneralRestClient.java b/src/main/java/com/binance/api/client/api/sync/BinanceApiGeneralRestClient.java index 594666dbe..292a5b339 100755 --- a/src/main/java/com/binance/api/client/api/sync/BinanceApiGeneralRestClient.java +++ b/src/main/java/com/binance/api/client/api/sync/BinanceApiGeneralRestClient.java @@ -142,26 +142,4 @@ public interface BinanceApiGeneralRestClient { * @return a list of all account open orders on a symbol. */ List getOpenOrders(OrderRequest orderRequest); - - // User stream endpoints - /** - * Start a new user data stream. - * - * @return a listen key that can be used with data streams - */ - String startUserDataStream(); - - /** - * PING a user data stream to prevent a time out. - * - * @param listenKey listen key that identifies a data stream - */ - void keepAliveUserDataStream(String listenKey); - - /** - * Close out a new user data stream. - * - * @param listenKey listen key that identifies a data stream - */ - void closeUserDataStream(String listenKey); } diff --git a/src/main/java/com/binance/api/client/api/sync/BinanceApiMarginRestClient.java b/src/main/java/com/binance/api/client/api/sync/BinanceApiMarginRestClient.java index bc5ff7c3e..650aaa447 100755 --- a/src/main/java/com/binance/api/client/api/sync/BinanceApiMarginRestClient.java +++ b/src/main/java/com/binance/api/client/api/sync/BinanceApiMarginRestClient.java @@ -105,4 +105,26 @@ public interface BinanceApiMarginRestClient { * @return loan records */ LoanQueryResult queryLoan(String asset, String txId); + + // User stream endpoints + /** + * Start a new user data stream. + * + * @return a listen key that can be used with data streams + */ + String startUserDataStream(); + + /** + * PING a user data stream to prevent a time out. + * + * @param listenKey listen key that identifies a data stream + */ + void keepAliveUserDataStream(String listenKey); + + /** + * Close out a new user data stream. + * + * @param listenKey listen key that identifies a data stream + */ + void closeUserDataStream(String listenKey); } diff --git a/src/main/java/com/binance/api/client/api/sync/BinanceApiSpotRestClient.java b/src/main/java/com/binance/api/client/api/sync/BinanceApiSpotRestClient.java index 58e7b1213..69f66aaa9 100755 --- a/src/main/java/com/binance/api/client/api/sync/BinanceApiSpotRestClient.java +++ b/src/main/java/com/binance/api/client/api/sync/BinanceApiSpotRestClient.java @@ -127,4 +127,26 @@ public interface BinanceApiSpotRestClient extends BinanceApiGeneralRestClient { * @return deposit address for a given asset. */ DepositAddress getDepositAddress(String asset); + + // User stream endpoints + /** + * Start a new user data stream. + * + * @return a listen key that can be used with data streams + */ + String startUserDataStream(); + + /** + * PING a user data stream to prevent a time out. + * + * @param listenKey listen key that identifies a data stream + */ + void keepAliveUserDataStream(String listenKey); + + /** + * Close out a new user data stream. + * + * @param listenKey listen key that identifies a data stream + */ + void closeUserDataStream(String listenKey); } diff --git a/src/main/java/com/binance/api/client/config/SpotApiConfig.java b/src/main/java/com/binance/api/client/config/SpotApiConfig.java index 439caa5fd..2fd4c94cd 100755 --- a/src/main/java/com/binance/api/client/config/SpotApiConfig.java +++ b/src/main/java/com/binance/api/client/config/SpotApiConfig.java @@ -4,7 +4,7 @@ * Configuration used for Binance operations. */ -@ApiConfig(apiUrl = "https://api.binance.com", webSocketUrl = "wss://stream.binance.com:9443/ws") +@ApiConfig(apiUrl = "https://api.binance.com", webSocketUrl = "wss://stream.binance.com/ws") public class SpotApiConfig implements BinanceApiConfig { } diff --git a/src/main/java/com/binance/api/client/domain/OrderType.java b/src/main/java/com/binance/api/client/domain/OrderType.java index 7cde1db0b..558610e44 100755 --- a/src/main/java/com/binance/api/client/domain/OrderType.java +++ b/src/main/java/com/binance/api/client/domain/OrderType.java @@ -17,5 +17,5 @@ public enum OrderType { LIMIT_MAKER, STOP_MARKET, TRAILING_STOP_MARKET, - TAKE_PROFIT_MARKET, + TAKE_PROFIT_MARKET; } diff --git a/src/main/java/com/binance/api/client/domain/event/OrderTradeUpdateEvent.java b/src/main/java/com/binance/api/client/domain/event/ExecutionReport.java similarity index 99% rename from src/main/java/com/binance/api/client/domain/event/OrderTradeUpdateEvent.java rename to src/main/java/com/binance/api/client/domain/event/ExecutionReport.java index d2bf6fc84..d74676c09 100755 --- a/src/main/java/com/binance/api/client/domain/event/OrderTradeUpdateEvent.java +++ b/src/main/java/com/binance/api/client/domain/event/ExecutionReport.java @@ -14,7 +14,7 @@ * @see UserDataUpdateEvent */ @JsonIgnoreProperties(ignoreUnknown = true) -public class OrderTradeUpdateEvent { +public class ExecutionReport { @JsonProperty("e") private String eventType; diff --git a/src/main/java/com/binance/api/client/domain/event/OrderTradeUpdate.java b/src/main/java/com/binance/api/client/domain/event/OrderTradeUpdate.java new file mode 100755 index 000000000..26f927abc --- /dev/null +++ b/src/main/java/com/binance/api/client/domain/event/OrderTradeUpdate.java @@ -0,0 +1,437 @@ +package com.binance.api.client.domain.event; + +import com.binance.api.client.constant.BinanceApiConstants; +import com.binance.api.client.domain.*; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.ToStringBuilder; + +/** + * Order or trade report update event. + *

+ * This event is embedded as part of a user data update event. + * + * @see UserDataUpdateEvent + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class OrderTradeUpdate { + + @JsonProperty("e") + private String eventType; + + /** + * Order/trade time. + */ + @JsonProperty("T") + private Long orderTradeTime; + + @JsonProperty("E") + private Long eventTime; + + @JsonProperty("s") + private String symbol; + + @JsonProperty("c") + private String newClientOrderId; + + /** + * Buy/Sell order side. + */ + @JsonProperty("S") + private OrderSide side; + + /** + * Type of order. + */ + @JsonProperty("o") + private OrderType type; + + /** + * Time in force to indicate how long will the order remain active. + */ + @JsonProperty("f") + private TimeInForce timeInForce; + + /** + * Original quantity in the order. + */ + @JsonProperty("q") + private String originalQuantity; + + /** + * Price. + */ + @JsonProperty("p") + private String price; + + @JsonProperty("ap") + private String averagePrice; + + @JsonProperty("sp") + private String stopPrice; + + /** + * Type of execution. + */ + @JsonProperty("x") + private ExecutionType executionType; + + /** + * Status of the order. + */ + @JsonProperty("X") + private OrderStatus orderStatus; + + /** + * Order id. + */ + @JsonProperty("i") + private Long orderId; + + /** + * Quantity of the last filled trade. + */ + @JsonProperty("l") + private String quantityLastFilledTrade; + + /** + * Accumulated quantity of filled trades on this order. + */ + @JsonProperty("z") + private String accumulatedQuantity; + + /** + * Price of last filled trade. + */ + @JsonProperty("L") + private String priceOfLastFilledTrade; + + /** + * Trade id. + */ + @JsonProperty("t") + private Long tradeId; + + @JsonProperty("b") + private Long bidsNational; + + @JsonProperty("a") + private Long askNational; + + @JsonProperty("m") + private Boolean tradeMaker; + + @JsonProperty("R") + private Boolean reduceOnly; + + @JsonProperty("wt") + private WorkingType workingType; + + @JsonProperty("ot") + private OrderType orderType; + + @JsonProperty("ps") + private PositionSide positionSide; + + @JsonProperty("cp") + private Boolean closedAllPositions; + + @JsonProperty("rp") + private String realizedProfit; + + @JsonProperty("pP") + private Boolean protectedOrder; + + //fixme: Unknown field name + @JsonProperty("si") + private Long si; + + //fixme: Unknown field name + @JsonProperty("ss") + private Long ss; + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + + public Long getOrderTradeTime() { + return orderTradeTime; + } + + public void setOrderTradeTime(Long orderTradeTime) { + this.orderTradeTime = orderTradeTime; + } + + public Long getEventTime() { + return eventTime; + } + + public void setEventTime(Long eventTime) { + this.eventTime = eventTime; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getNewClientOrderId() { + return newClientOrderId; + } + + public void setNewClientOrderId(String newClientOrderId) { + this.newClientOrderId = newClientOrderId; + } + + public OrderSide getSide() { + return side; + } + + public void setSide(OrderSide side) { + this.side = side; + } + + public OrderType getType() { + return type; + } + + public void setType(OrderType type) { + this.type = type; + } + + public TimeInForce getTimeInForce() { + return timeInForce; + } + + public void setTimeInForce(TimeInForce timeInForce) { + this.timeInForce = timeInForce; + } + + public String getOriginalQuantity() { + return originalQuantity; + } + + public void setOriginalQuantity(String originalQuantity) { + this.originalQuantity = originalQuantity; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public String getAveragePrice() { + return averagePrice; + } + + public void setAveragePrice(String averagePrice) { + this.averagePrice = averagePrice; + } + + public String getStopPrice() { + return stopPrice; + } + + public void setStopPrice(String stopPrice) { + this.stopPrice = stopPrice; + } + + public ExecutionType getExecutionType() { + return executionType; + } + + public void setExecutionType(ExecutionType executionType) { + this.executionType = executionType; + } + + public OrderStatus getOrderStatus() { + return orderStatus; + } + + public void setOrderStatus(OrderStatus orderStatus) { + this.orderStatus = orderStatus; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } + + public String getQuantityLastFilledTrade() { + return quantityLastFilledTrade; + } + + public void setQuantityLastFilledTrade(String quantityLastFilledTrade) { + this.quantityLastFilledTrade = quantityLastFilledTrade; + } + + public String getAccumulatedQuantity() { + return accumulatedQuantity; + } + + public void setAccumulatedQuantity(String accumulatedQuantity) { + this.accumulatedQuantity = accumulatedQuantity; + } + + public String getPriceOfLastFilledTrade() { + return priceOfLastFilledTrade; + } + + public void setPriceOfLastFilledTrade(String priceOfLastFilledTrade) { + this.priceOfLastFilledTrade = priceOfLastFilledTrade; + } + + public Long getTradeId() { + return tradeId; + } + + public void setTradeId(Long tradeId) { + this.tradeId = tradeId; + } + + public Long getBidsNational() { + return bidsNational; + } + + public void setBidsNational(Long bidsNational) { + this.bidsNational = bidsNational; + } + + public Long getAskNational() { + return askNational; + } + + public void setAskNational(Long askNational) { + this.askNational = askNational; + } + + public Boolean getTradeMaker() { + return tradeMaker; + } + + public void setTradeMaker(Boolean tradeMaker) { + this.tradeMaker = tradeMaker; + } + + public Boolean getReduceOnly() { + return reduceOnly; + } + + public void setReduceOnly(Boolean reduceOnly) { + this.reduceOnly = reduceOnly; + } + + public WorkingType getWorkingType() { + return workingType; + } + + public void setWorkingType(WorkingType workingType) { + this.workingType = workingType; + } + + public OrderType getOrderType() { + return orderType; + } + + public void setOrderType(OrderType orderType) { + this.orderType = orderType; + } + + public PositionSide getPositionSide() { + return positionSide; + } + + public void setPositionSide(PositionSide positionSide) { + this.positionSide = positionSide; + } + + public Boolean getClosedAllPositions() { + return closedAllPositions; + } + + public void setClosedAllPositions(Boolean closedAllPositions) { + this.closedAllPositions = closedAllPositions; + } + + public String getRealizedProfit() { + return realizedProfit; + } + + public void setRealizedProfit(String realizedProfit) { + this.realizedProfit = realizedProfit; + } + + public Boolean getProtectedOrder() { + return protectedOrder; + } + + public void setProtectedOrder(Boolean protectedOrder) { + this.protectedOrder = protectedOrder; + } + + public Long getSi() { + return si; + } + + public void setSi(Long si) { + this.si = si; + } + + public Long getSs() { + return ss; + } + + public void setSs(Long ss) { + this.ss = ss; + } + + @Override + public String toString() { + return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE) + .append("eventType", eventType) + .append("orderTradeTime", orderTradeTime) + .append("eventTime", eventTime) + .append("symbol", symbol) + .append("newClientOrderId", newClientOrderId) + .append("side", side) + .append("type", type) + .append("timeInForce", timeInForce) + .append("originalQuantity", originalQuantity) + .append("price", price) + .append("averagePrice", averagePrice) + .append("stopPrice", stopPrice) + .append("executionType", executionType) + .append("orderStatus", orderStatus) + .append("orderId", orderId) + .append("quantityLastFilledTrade", quantityLastFilledTrade) + .append("accumulatedQuantity", accumulatedQuantity) + .append("priceOfLastFilledTrade", priceOfLastFilledTrade) + .append("tradeId", tradeId) + .append("bidsNational", bidsNational) + .append("askNational", askNational) + .append("tradeMaker", tradeMaker) + .append("reduceOnly", reduceOnly) + .append("workingType", workingType) + .append("orderType", orderType) + .append("positionSide", positionSide) + .append("closedAllPositions", closedAllPositions) + .append("realizedProfit", realizedProfit) + .append("protectedOrder", protectedOrder) + .append("si", si) + .append("ss", ss) + .toString(); + } +} diff --git a/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEvent.java b/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEvent.java index d95662df1..a64037e98 100755 --- a/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEvent.java +++ b/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEvent.java @@ -1,7 +1,6 @@ package com.binance.api.client.domain.event; import com.binance.api.client.constant.BinanceApiConstants; -import com.binance.api.client.exception.UnsupportedEventException; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.apache.commons.lang3.builder.ToStringBuilder; @@ -20,101 +19,125 @@ @JsonDeserialize(using = UserDataUpdateEventDeserializer.class) public class UserDataUpdateEvent { - private UserDataUpdateEventType eventType; + private UserDataUpdateEventType eventType; - private long eventTime; + private long eventTime; - private AccountUpdateEvent accountUpdateEvent; + private AccountUpdateEvent accountUpdateEvent; - private BalanceUpdateEvent balanceUpdateEvent; + private BalanceUpdateEvent balanceUpdateEvent; - private OrderTradeUpdateEvent orderTradeUpdateEvent; + private ExecutionReport executionReport; + private OrderTradeUpdate orderTradeUpdate; - public UserDataUpdateEventType getEventType() { - return eventType; - } - - public void setEventType(UserDataUpdateEventType eventType) { - this.eventType = eventType; - } - - public long getEventTime() { - return eventTime; - } + public UserDataUpdateEventType getEventType() { + return eventType; + } - public void setEventTime(long eventTime) { - this.eventTime = eventTime; - } + public void setEventType(UserDataUpdateEventType eventType) { + this.eventType = eventType; + } - public AccountUpdateEvent getAccountUpdateEvent() { - return accountUpdateEvent; - } + public long getEventTime() { + return eventTime; + } - public void setAccountUpdateEvent(AccountUpdateEvent accountUpdateEvent) { - this.accountUpdateEvent = accountUpdateEvent; - } + public void setEventTime(long eventTime) { + this.eventTime = eventTime; + } - public BalanceUpdateEvent getBalanceUpdateEvent() { - return balanceUpdateEvent; - } + public AccountUpdateEvent getAccountUpdateEvent() { + return accountUpdateEvent; + } - public void setBalanceUpdateEvent(BalanceUpdateEvent balanceUpdateEvent) { - this.balanceUpdateEvent = balanceUpdateEvent; - } + public void setAccountUpdateEvent(AccountUpdateEvent accountUpdateEvent) { + this.accountUpdateEvent = accountUpdateEvent; + } - public OrderTradeUpdateEvent getOrderTradeUpdateEvent() { - return orderTradeUpdateEvent; - } + public BalanceUpdateEvent getBalanceUpdateEvent() { + return balanceUpdateEvent; + } - public void setOrderTradeUpdateEvent(OrderTradeUpdateEvent orderTradeUpdateEvent) { - this.orderTradeUpdateEvent = orderTradeUpdateEvent; - } + public void setBalanceUpdateEvent(BalanceUpdateEvent balanceUpdateEvent) { + this.balanceUpdateEvent = balanceUpdateEvent; + } - @Override - public String toString() { - ToStringBuilder sb = new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE) - .append("eventType", eventType) - .append("eventTime", eventTime); - if (eventType == UserDataUpdateEventType.ACCOUNT_UPDATE) { - sb.append("accountUpdateEvent", accountUpdateEvent); - } else if (eventType == UserDataUpdateEventType.ACCOUNT_POSITION_UPDATE) { - sb.append("accountPositionUpdateEvent", accountUpdateEvent); - } else if (eventType == UserDataUpdateEventType.BALANCE_UPDATE) { - sb.append("balanceUpdateEvent", balanceUpdateEvent); - } else { - sb.append("orderTradeUpdateEvent", orderTradeUpdateEvent); + public ExecutionReport getExecutionReport() { + return executionReport; } - return sb.toString(); - } - public enum UserDataUpdateEventType { - ACCOUNT_UPDATE("outboundAccountInfo"), - ACCOUNT_POSITION_UPDATE("outboundAccountPosition"), - BALANCE_UPDATE("balanceUpdate"), - ORDER_TRADE_UPDATE("executionReport"), - ; + public void setExecutionReport(ExecutionReport executionReport) { + this.executionReport = executionReport; + } - private final String eventTypeId; + public OrderTradeUpdate getOrderTradeUpdate() { + return orderTradeUpdate; + } - UserDataUpdateEventType(String eventTypeId) { - this.eventTypeId = eventTypeId; + public void setOrderTradeUpdate(OrderTradeUpdate orderTradeUpdate) { + this.orderTradeUpdate = orderTradeUpdate; } - public String getEventTypeId() { - return eventTypeId; + @Override + public String toString() { + ToStringBuilder sb = new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE) + .append("eventType", eventType) + .append("eventTime", eventTime); + if (eventType == UserDataUpdateEventType.ACCOUNT_UPDATE) { + sb.append("accountUpdateEvent", accountUpdateEvent); + } else if (eventType == UserDataUpdateEventType.ACCOUNT_POSITION_UPDATE) { + sb.append("accountPositionUpdateEvent", accountUpdateEvent); + } else if (eventType == UserDataUpdateEventType.BALANCE_UPDATE) { + sb.append("balanceUpdateEvent", balanceUpdateEvent); + } else if (eventType == UserDataUpdateEventType.ORDER_TRADE_UPDATE) { + sb.append("orderTradeUpdate", orderTradeUpdate); + } else { + sb.append("executionReport", executionReport); + } + return sb.toString(); } - public static UserDataUpdateEventType fromEventTypeId(String eventTypeId) { - if (ACCOUNT_UPDATE.eventTypeId.equals(eventTypeId)) { - return ACCOUNT_UPDATE; - } else if (ORDER_TRADE_UPDATE.eventTypeId.equals(eventTypeId)) { - return ORDER_TRADE_UPDATE; - } else if (ACCOUNT_POSITION_UPDATE.eventTypeId.equals(eventTypeId)) { - return ACCOUNT_POSITION_UPDATE; - } else if (BALANCE_UPDATE.eventTypeId.equals(eventTypeId)) { - return BALANCE_UPDATE; - } - throw new UnsupportedEventException("Unrecognized user data update event type id: " + eventTypeId); + public enum UserDataUpdateEventType { + ACCOUNT_POSITION_UPDATE("outboundAccountPosition"), + BALANCE_UPDATE("balanceUpdate"), + EXECUTION_REPORT("executionReport"), + ACCOUNT_UPDATE("ACCOUNT_UPDATE"), //Binance futures & testnet + ACCOUNT_CONFIG_UPDATE("ACCOUNT_CONFIG_UPDATE"), //Binance futures & testnet //TODO: Implement this type. + ORDER_TRADE_UPDATE("ORDER_TRADE_UPDATE"); //Binance futures & testnet + + private final String eventTypeId; + + UserDataUpdateEventType(String eventTypeId) { + this.eventTypeId = eventTypeId; + } + + public String getEventTypeId() { + return eventTypeId; + } + + public static UserDataUpdateEventType fromEventTypeId(String eventTypeId) { + UserDataUpdateEventType et = null; + switch (eventTypeId) { + case "outboundAccountPosition": + et = UserDataUpdateEventType.ACCOUNT_POSITION_UPDATE; + break; + case "balanceUpdate": + et = UserDataUpdateEventType.BALANCE_UPDATE; + break; + case "executionReport": + et = UserDataUpdateEventType.EXECUTION_REPORT; + break; + case "ACCOUNT_UPDATE": + et = UserDataUpdateEventType.ACCOUNT_UPDATE; + break; + case "ACCOUNT_CONFIG_UPDATE": + et = UserDataUpdateEventType.ACCOUNT_CONFIG_UPDATE; + break; + case "ORDER_TRADE_UPDATE": + et = UserDataUpdateEventType.ORDER_TRADE_UPDATE; + break; + } + return et; + } } - } } diff --git a/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEventDeserializer.java b/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEventDeserializer.java index 8888bb624..e9c5e7ce5 100755 --- a/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEventDeserializer.java +++ b/src/main/java/com/binance/api/client/domain/event/UserDataUpdateEventDeserializer.java @@ -18,47 +18,70 @@ */ public class UserDataUpdateEventDeserializer extends JsonDeserializer { - private ObjectMapper mapper; + private ObjectMapper mapper; - @Override - public UserDataUpdateEvent deserialize(JsonParser jp, DeserializationContext ctx) throws IOException { + @Override + public UserDataUpdateEvent deserialize(JsonParser jp, DeserializationContext ctx) { + UserDataUpdateEvent userDataUpdateEvent = new UserDataUpdateEvent(); + try { + if (mapper == null) { + mapper = new ObjectMapper(); + } - if (mapper == null) { - mapper = new ObjectMapper(); - } + ObjectCodec oc = jp.getCodec(); + JsonNode node = oc.readTree(jp); + String json = node.toString(); - ObjectCodec oc = jp.getCodec(); - JsonNode node = oc.readTree(jp); - String json = node.toString(); + final String eventTypeId = node.get("e").asText(); + final long eventTime = node.get("E").asLong(); + UserDataUpdateEventType type = UserDataUpdateEventType.fromEventTypeId(eventTypeId); + if (type == null) { + throw new BinanceApiException("Unknown user data event type [" + eventTypeId + "]"); + /* + * TODO: need to throws exception on unknown types . + * or ... !? + * */ + } else { + userDataUpdateEvent.setEventType(type); + userDataUpdateEvent.setEventTime(eventTime); - final String eventTypeId = node.get("e").asText(); - final Long eventTime = node.get("E").asLong(); - UserDataUpdateEventType userDataUpdateEventType = UserDataUpdateEventType.fromEventTypeId(eventTypeId); + switch (type) { + case ACCOUNT_UPDATE: + case ACCOUNT_POSITION_UPDATE: + AccountUpdateEvent accountUpdateEvent = getUserDataUpdateEventDetail(json, AccountUpdateEvent.class, mapper); + userDataUpdateEvent.setAccountUpdateEvent(accountUpdateEvent); + break; + case BALANCE_UPDATE: + BalanceUpdateEvent balanceUpdateEvent = getUserDataUpdateEventDetail(json, BalanceUpdateEvent.class, mapper); + userDataUpdateEvent.setBalanceUpdateEvent(balanceUpdateEvent); + break; + case EXECUTION_REPORT: + ExecutionReport executionReport = getUserDataUpdateEventDetail(json, ExecutionReport.class, mapper); + userDataUpdateEvent.setExecutionReport(executionReport); + break; + case ORDER_TRADE_UPDATE: + JsonNode o = node.get("o"); + OrderTradeUpdate orderTradeUpdate = getUserDataUpdateEventDetail(o.toString(), OrderTradeUpdate.class, mapper); + userDataUpdateEvent.setOrderTradeUpdate(orderTradeUpdate); + break; + } - UserDataUpdateEvent userDataUpdateEvent = new UserDataUpdateEvent(); - userDataUpdateEvent.setEventType(userDataUpdateEventType); - userDataUpdateEvent.setEventTime(eventTime); + } - if (userDataUpdateEventType == UserDataUpdateEventType.ACCOUNT_UPDATE || - userDataUpdateEventType == UserDataUpdateEventType.ACCOUNT_POSITION_UPDATE) { - AccountUpdateEvent accountUpdateEvent = getUserDataUpdateEventDetail(json, AccountUpdateEvent.class, mapper); - userDataUpdateEvent.setAccountUpdateEvent(accountUpdateEvent); - } else if (userDataUpdateEventType == UserDataUpdateEventType.BALANCE_UPDATE) { - BalanceUpdateEvent balanceUpdateEvent = getUserDataUpdateEventDetail(json, BalanceUpdateEvent.class, mapper); - userDataUpdateEvent.setBalanceUpdateEvent(balanceUpdateEvent); - } else { // userDataUpdateEventType == UserDataUpdateEventType.ORDER_TRADE_UPDATE - OrderTradeUpdateEvent orderTradeUpdateEvent = getUserDataUpdateEventDetail(json, OrderTradeUpdateEvent.class, mapper); - userDataUpdateEvent.setOrderTradeUpdateEvent(orderTradeUpdateEvent); - } + } catch (Exception ignore) { + /* + * NOTE : Do not throws method signature exception . + * */ + } - return userDataUpdateEvent; - } + return userDataUpdateEvent; + } - public T getUserDataUpdateEventDetail(String json, Class clazz, ObjectMapper mapper) { - try { - return mapper.readValue(json, clazz); - } catch (IOException e) { - throw new BinanceApiException(e); + public T getUserDataUpdateEventDetail(String json, Class clazz, ObjectMapper mapper) { + try { + return mapper.readValue(json, clazz); + } catch (IOException e) { + throw new BinanceApiException(e); + } } - } } diff --git a/src/main/java/com/binance/api/client/impl/sync/BinanceApiMarginRestClientImpl.java b/src/main/java/com/binance/api/client/impl/sync/BinanceApiMarginRestClientImpl.java index 3a156280a..ccca65fcd 100755 --- a/src/main/java/com/binance/api/client/impl/sync/BinanceApiMarginRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/sync/BinanceApiMarginRestClientImpl.java @@ -1,5 +1,6 @@ package com.binance.api.client.impl.sync; +import com.binance.api.client.api.BinanceApiService; import com.binance.api.client.api.sync.BinanceApiMarginRestClient; import com.binance.api.client.constant.BinanceApiConstants; import com.binance.api.client.domain.TransferType; @@ -8,7 +9,6 @@ import com.binance.api.client.domain.account.request.CancelOrderResponse; import com.binance.api.client.domain.account.request.OrderRequest; import com.binance.api.client.domain.account.request.OrderStatusRequest; -import com.binance.api.client.api.BinanceApiService; import com.binance.api.client.impl.BinanceApiServiceGenerator; import java.util.List; @@ -101,4 +101,20 @@ public MarginTransaction repay(String asset, String amount) { return executeSync(binanceApiService.repay(asset, amount, BinanceApiConstants.DEFAULT_RECEIVING_WINDOW, timestamp)); } + // user stream endpoints + @Override + public String startUserDataStream() { + return executeSync(binanceApiService.startMarginUserDataStream()).toString(); + } + + @Override + public void keepAliveUserDataStream(String listenKey) { + executeSync(binanceApiService.keepAliveMarginUserDataStream(listenKey)); + } + + @Override + public void closeUserDataStream(String listenKey) { + executeSync(binanceApiService.closeAliveMarginUserDataStream(listenKey)); + } + } \ No newline at end of file diff --git a/src/main/java/com/binance/api/client/impl/sync/BinanceApiSpotRestClientImpl.java b/src/main/java/com/binance/api/client/impl/sync/BinanceApiSpotRestClientImpl.java index 68b0606f4..f282b8d6a 100755 --- a/src/main/java/com/binance/api/client/impl/sync/BinanceApiSpotRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/sync/BinanceApiSpotRestClientImpl.java @@ -226,12 +226,12 @@ public DepositAddress getDepositAddress(String asset) { // user stream endpoints @Override public String startUserDataStream() { - return executeSync(binanceApiService.startMarginUserDataStream()).toString(); + return executeSync(binanceApiService.startUserDataStream()).toString(); } @Override public void keepAliveUserDataStream(String listenKey) { - executeSync(binanceApiService.keepAliveMarginUserDataStream(listenKey)); + executeSync(binanceApiService.keepAliveUserDataStream(listenKey)); } @Override diff --git a/src/main/java/com/binance/api/client/impl/ws/BinanceApiWebSocketListener.java b/src/main/java/com/binance/api/client/impl/ws/BinanceApiWebSocketListener.java index 0278e59b6..b52dd55e8 100755 --- a/src/main/java/com/binance/api/client/impl/ws/BinanceApiWebSocketListener.java +++ b/src/main/java/com/binance/api/client/impl/ws/BinanceApiWebSocketListener.java @@ -39,7 +39,7 @@ public void onMessage(WebSocket webSocket, String text) { try { T event = objectReader.readValue(text); callback.onResponse(event); - } catch (IOException e) { + } catch (Exception e) { throw new BinanceApiException(e); } } diff --git a/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java b/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java index a50d31221..5bbdfdad4 100755 --- a/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java +++ b/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java @@ -1,15 +1,11 @@ package com.binance.api.domain.event; +import com.binance.api.client.domain.*; import com.binance.api.client.domain.account.AssetBalance; -import com.binance.api.client.domain.ExecutionType; -import com.binance.api.client.domain.OrderRejectReason; -import com.binance.api.client.domain.OrderSide; -import com.binance.api.client.domain.OrderStatus; -import com.binance.api.client.domain.OrderType; -import com.binance.api.client.domain.TimeInForce; import com.binance.api.client.domain.event.AccountUpdateEvent; -import com.binance.api.client.domain.event.OrderTradeUpdateEvent; +import com.binance.api.client.domain.event.ExecutionReport; +import com.binance.api.client.domain.event.OrderTradeUpdate; import com.binance.api.client.domain.event.UserDataUpdateEvent; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; @@ -24,58 +20,77 @@ */ public class UserDataUpdateEventDeserializerTest { - @Test - public void testAccountUpdateEventDeserializer() { - final String accountUpdateJson = "{\"e\":\"outboundAccountInfo\",\"E\":1,\"m\":10,\"t\":10,\"b\":0,\"s\":0,\"T\":true,\"W\":true,\"D\":true,\"B\":[{\"a\":\"BTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ETH\",\"f\":\"0.10000000\",\"l\":\"0.00000000\"},{\"a\":\"BNC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ICO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"NEO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BNB\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"123\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"456\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"QTUM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"EOS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"GAS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BCC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BTM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"USDT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"HCC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"HSR\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"OAX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"DNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MCO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ICN\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ELC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"PAY\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ZRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"OMG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"WTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"YOYO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LRC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LLT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"TRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"FID\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNGLS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"STRAT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BQX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"FUN\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"KNC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CDT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"XVG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"IOTA\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LINK\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CVC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"TNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"REP\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CTR\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MDA\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MTL\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SALT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"NULS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SUB\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"STX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MTH\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CAT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ADX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"PIX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ETC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ENG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ZEC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"}]}"; - ObjectMapper mapper = new ObjectMapper(); - try { - UserDataUpdateEvent userDataUpdateEvent = mapper.readValue(accountUpdateJson, UserDataUpdateEvent.class); - assertEquals(userDataUpdateEvent.getEventType().getEventTypeId(), "outboundAccountInfo"); - assertEquals(userDataUpdateEvent.getEventTime(), 1L); - AccountUpdateEvent accountUpdateEvent = userDataUpdateEvent.getAccountUpdateEvent(); - for (AssetBalance assetBalance : accountUpdateEvent.getBalances()) { - if ("ETH".equals(assetBalance.getAsset())) { - assertEquals(assetBalance.getFree(), "0.10000000"); - } else { - assertEquals(assetBalance.getFree(), "0.00000000"); + /*@Test + public void testAccountUpdateEventDeserializer() { + final String accountUpdateJson = "{\"e\":\"outboundAccountInfo\",\"E\":1,\"m\":10,\"t\":10,\"b\":0,\"s\":0,\"T\":true,\"W\":true,\"D\":true,\"B\":[{\"a\":\"BTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ETH\",\"f\":\"0.10000000\",\"l\":\"0.00000000\"},{\"a\":\"BNC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ICO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"NEO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BNB\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"123\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"456\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"QTUM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"EOS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"GAS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BCC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BTM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"USDT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"HCC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"HSR\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"OAX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"DNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MCO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ICN\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ELC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"PAY\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ZRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"OMG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"WTC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"YOYO\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LRC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LLT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"TRX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"FID\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNGLS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"STRAT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"BQX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"FUN\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"KNC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CDT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"XVG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"IOTA\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SNM\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"LINK\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CVC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"TNT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"REP\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CTR\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MDA\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MTL\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SALT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"NULS\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"SUB\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"STX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"MTH\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"CAT\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ADX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"PIX\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ETC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ENG\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"},{\"a\":\"ZEC\",\"f\":\"0.00000000\",\"l\":\"0.00000000\"}]}"; + ObjectMapper mapper = new ObjectMapper(); + try { + UserDataUpdateEvent executionReport = mapper.readValue(accountUpdateJson, UserDataUpdateEvent.class); + assertEquals(executionReport.getEventType().getEventTypeId(), "outboundAccountInfo"); + assertEquals(executionReport.getEventTime(), 1L); + AccountUpdateEvent accountUpdateEvent = executionReport.getAccountUpdateEvent(); + for (AssetBalance assetBalance : accountUpdateEvent.getBalances()) { + if ("ETH".equals(assetBalance.getAsset())) { + assertEquals(assetBalance.getFree(), "0.10000000"); + } else { + assertEquals(assetBalance.getFree(), "0.00000000"); + } + assertEquals(assetBalance.getLocked(), "0.00000000"); + } + } catch (IOException e) { + fail(); + } + }*/ + + @Test + public void testExecutionReportEventDeserializer() { + final String executionReportEventJson = "{\"e\":\"executionReport\",\"E\":1,\"s\":\"NEOETH\",\"c\":\"XXX\",\"S\":\"BUY\",\"o\":\"LIMIT\",\"f\":\"GTC\",\"q\":\"1000.00000000\",\"p\":\"0.00010000\",\"P\":\"0.00000000\",\"F\":\"0.00000000\",\"g\":-1,\"C\":\"5yairWLqfzbusOUdPyG712\",\"x\":\"CANCELED\",\"X\":\"CANCELED\",\"r\":\"NONE\",\"i\":123456,\"l\":\"0.00000000\",\"z\":\"0.00000000\",\"L\":\"0.00000000\",\"n\":\"0\",\"N\":null,\"T\":1,\"t\":-1,\"I\":1,\"w\":false,\"m\":false,\"M\":false}"; + ObjectMapper mapper = new ObjectMapper(); + try { + UserDataUpdateEvent userDataUpdateEvent = mapper.readValue(executionReportEventJson, UserDataUpdateEvent.class); + assertEquals(userDataUpdateEvent.getEventType().getEventTypeId(), "executionReport"); + assertEquals(userDataUpdateEvent.getEventTime(), 1L); + + ExecutionReport report = userDataUpdateEvent.getExecutionReport(); + assertEquals(report.getSymbol(), "NEOETH"); + assertEquals(report.getNewClientOrderId(), "XXX"); + + assertEquals(report.getSide(), OrderSide.BUY); + assertEquals(report.getType(), OrderType.LIMIT); + assertEquals(report.getTimeInForce(), TimeInForce.GTC); + + assertEquals(report.getOriginalQuantity(), "1000.00000000"); + assertEquals(report.getPrice(), "0.00010000"); + + assertEquals(report.getExecutionType(), ExecutionType.CANCELED); + assertEquals(report.getOrderStatus(), OrderStatus.CANCELED); + assertEquals(report.getOrderRejectReason(), OrderRejectReason.NONE); + + assertEquals(report.getOrderId(), new Long(123456)); + assertEquals(report.getOrderTradeTime(), new Long(1)); + } catch (IOException e) { + fail(); } - assertEquals(assetBalance.getLocked(), "0.00000000"); - } - } catch (IOException e) { - fail(); } - } - - @Test - public void testOrderUpdateEventDeserializer() { - final String orderUpdateEventJson = "{\"e\":\"executionReport\",\"E\":1,\"s\":\"NEOETH\",\"c\":\"XXX\",\"S\":\"BUY\",\"o\":\"LIMIT\",\"f\":\"GTC\",\"q\":\"1000.00000000\",\"p\":\"0.00010000\",\"P\":\"0.00000000\",\"F\":\"0.00000000\",\"g\":-1,\"C\":\"5yairWLqfzbusOUdPyG712\",\"x\":\"CANCELED\",\"X\":\"CANCELED\",\"r\":\"NONE\",\"i\":123456,\"l\":\"0.00000000\",\"z\":\"0.00000000\",\"L\":\"0.00000000\",\"n\":\"0\",\"N\":null,\"T\":1,\"t\":-1,\"I\":1,\"w\":false,\"m\":false,\"M\":false}"; - ObjectMapper mapper = new ObjectMapper(); - try { - UserDataUpdateEvent userDataUpdateEvent = mapper.readValue(orderUpdateEventJson, UserDataUpdateEvent.class); - assertEquals(userDataUpdateEvent.getEventType().getEventTypeId(), "executionReport"); - assertEquals(userDataUpdateEvent.getEventTime(), 1L); - - OrderTradeUpdateEvent orderTradeUpdateEvent = userDataUpdateEvent.getOrderTradeUpdateEvent(); - assertEquals(orderTradeUpdateEvent.getSymbol(), "NEOETH"); - assertEquals(orderTradeUpdateEvent.getNewClientOrderId(), "XXX"); - - assertEquals(orderTradeUpdateEvent.getSide(), OrderSide.BUY); - assertEquals(orderTradeUpdateEvent.getType(), OrderType.LIMIT); - assertEquals(orderTradeUpdateEvent.getTimeInForce(), TimeInForce.GTC); - - assertEquals(orderTradeUpdateEvent.getOriginalQuantity(), "1000.00000000"); - assertEquals(orderTradeUpdateEvent.getPrice(), "0.00010000"); - - assertEquals(orderTradeUpdateEvent.getExecutionType(), ExecutionType.CANCELED); - assertEquals(orderTradeUpdateEvent.getOrderStatus(), OrderStatus.CANCELED); - assertEquals(orderTradeUpdateEvent.getOrderRejectReason(), OrderRejectReason.NONE); - - assertEquals(orderTradeUpdateEvent.getOrderId(), new Long(123456)); - assertEquals(orderTradeUpdateEvent.getOrderTradeTime(), new Long(1)); - } catch (IOException e) { - fail(); + + @Test + public void testOrderUpdateEventDeserializer() { + final String orderUpdateEventJson = "{\"e\":\"ORDER_TRADE_UPDATE\",\"T\":1617397293374,\"E\":1617397293376,\"o\":{\"s\":\"BTCUSDT\",\"c\":\"web_0x77fLbc9gfvwAGjHlGw\",\"S\":\"BUY\",\"o\":\"STOP\",\"f\":\"GTC\",\"q\":\"2.263\",\"p\":\"59220\",\"ap\":\"0\",\"sp\":\"59200\",\"x\":\"CANCELED\",\"X\":\"CANCELED\",\"i\":2675448941,\"l\":\"0\",\"z\":\"0\",\"L\":\"0\",\"T\":1617397293374,\"t\":0,\"b\":\"0\",\"a\":\"0\",\"m\":false,\"R\":false,\"wt\":\"MARK_PRICE\",\"ot\":\"STOP\",\"ps\":\"BOTH\",\"cp\":false,\"rp\":\"0\",\"pP\":true,\"si\":0,\"ss\":0}}"; + ObjectMapper mapper = new ObjectMapper(); + try { + UserDataUpdateEvent event = mapper.readValue(orderUpdateEventJson, UserDataUpdateEvent.class); + assertEquals(event.getEventType().getEventTypeId(), "ORDER_TRADE_UPDATE"); + assertEquals(event.getEventTime(), 1617397293376L); + + OrderTradeUpdate orderTradeUpdate = event.getOrderTradeUpdate(); + assertEquals(orderTradeUpdate.getTimeInForce().toString(), "GTC"); + assertEquals(orderTradeUpdate.getOrderStatus().toString(), "CANCELED"); + + } catch (IOException e) { + fail(); + } } - } + } diff --git a/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java b/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java index 17718af44..b134db0e6 100755 --- a/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java @@ -1,9 +1,9 @@ package com.binance.api.examples; -import com.binance.api.client.api.sync.BinanceApiSpotRestClient; import com.binance.api.client.api.BinanceApiWebSocketClient; +import com.binance.api.client.api.sync.BinanceApiSpotRestClient; import com.binance.api.client.domain.event.AccountUpdateEvent; -import com.binance.api.client.domain.event.OrderTradeUpdateEvent; +import com.binance.api.client.domain.event.ExecutionReport; import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType; import com.binance.api.client.factory.BinanceAbstractFactory; import com.binance.api.client.factory.BinanceSpotApiClientFactory; @@ -35,15 +35,15 @@ public static void main(String[] args) { // Print new balances of every available asset System.out.println(accountUpdateEvent.getBalances()); } else { - OrderTradeUpdateEvent orderTradeUpdateEvent = response.getOrderTradeUpdateEvent(); + ExecutionReport executionReport = response.getExecutionReport(); // Print details about an order/trade - System.out.println(orderTradeUpdateEvent); + System.out.println(executionReport); // Print original quantity - System.out.println(orderTradeUpdateEvent.getOriginalQuantity()); + System.out.println(executionReport.getOriginalQuantity()); // Or price - System.out.println(orderTradeUpdateEvent.getPrice()); + System.out.println(executionReport.getPrice()); } }); System.out.println("Waiting for events..."); diff --git a/src/test/java/com/binance/api/examples/UserDataStreamExample.java b/src/test/java/com/binance/api/examples/UserDataStreamExample.java index abbcc4131..e9d265172 100755 --- a/src/test/java/com/binance/api/examples/UserDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/UserDataStreamExample.java @@ -1,9 +1,9 @@ package com.binance.api.examples; -import com.binance.api.client.api.sync.BinanceApiSpotRestClient; import com.binance.api.client.api.BinanceApiWebSocketClient; +import com.binance.api.client.api.sync.BinanceApiSpotRestClient; import com.binance.api.client.domain.event.AccountUpdateEvent; -import com.binance.api.client.domain.event.OrderTradeUpdateEvent; +import com.binance.api.client.domain.event.ExecutionReport; import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType; import com.binance.api.client.factory.BinanceAbstractFactory; import com.binance.api.client.factory.BinanceSpotApiClientFactory; @@ -33,15 +33,15 @@ public static void main(String[] args) { // Print new balances of every available asset System.out.println(accountUpdateEvent.getBalances()); } else { - OrderTradeUpdateEvent orderTradeUpdateEvent = response.getOrderTradeUpdateEvent(); + ExecutionReport executionReport = response.getExecutionReport(); // Print details about an order/trade - System.out.println(orderTradeUpdateEvent); + System.out.println(executionReport); // Print original quantity - System.out.println(orderTradeUpdateEvent.getOriginalQuantity()); + System.out.println(executionReport.getOriginalQuantity()); // Or price - System.out.println(orderTradeUpdateEvent.getPrice()); + System.out.println(executionReport.getPrice()); } }); System.out.println("Waiting for events..."); diff --git a/src/test/java/com/binance/api/examples/futures/FuturesUserDataStreamExample.java b/src/test/java/com/binance/api/examples/futures/FuturesUserDataStreamExample.java index 24d292d53..67106c2e1 100755 --- a/src/test/java/com/binance/api/examples/futures/FuturesUserDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/futures/FuturesUserDataStreamExample.java @@ -1,9 +1,9 @@ package com.binance.api.examples.futures; -import com.binance.api.client.api.sync.BinanceApiFuturesRestClient; import com.binance.api.client.api.BinanceApiWebSocketClient; +import com.binance.api.client.api.sync.BinanceApiFuturesRestClient; import com.binance.api.client.domain.event.AccountUpdateEvent; -import com.binance.api.client.domain.event.OrderTradeUpdateEvent; +import com.binance.api.client.domain.event.ExecutionReport; import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType; import com.binance.api.client.factory.BinanceAbstractFactory; import com.binance.api.client.factory.BinanceFuturesApiClientFactory; @@ -35,15 +35,15 @@ public static void main(String[] args) { // Print new balances of every available asset System.out.println(accountUpdateEvent.getBalances()); } else { - OrderTradeUpdateEvent orderTradeUpdateEvent = response.getOrderTradeUpdateEvent(); + ExecutionReport executionReport = response.getExecutionReport(); // Print details about an order/trade - System.out.println(orderTradeUpdateEvent); + System.out.println(executionReport); // Print original quantity - System.out.println(orderTradeUpdateEvent.getOriginalQuantity()); + System.out.println(executionReport.getOriginalQuantity()); // Or price - System.out.println(orderTradeUpdateEvent.getPrice()); + System.out.println(executionReport.getPrice()); } }); System.out.println("Waiting for events..."); diff --git a/src/test/java/com/binance/api/examples/testnet/TestnetLeverageMarginChange.java b/src/test/java/com/binance/api/examples/testnet/TestnetLeverageMarginChange.java index 0f1dfc4a9..b27069077 100755 --- a/src/test/java/com/binance/api/examples/testnet/TestnetLeverageMarginChange.java +++ b/src/test/java/com/binance/api/examples/testnet/TestnetLeverageMarginChange.java @@ -10,8 +10,8 @@ */ public class TestnetLeverageMarginChange { private static final String SYMBOL = "BTCUSDT"; - private static final String API_KEY = "c8be940e7d3ebbfe79842236d6110b88e8350059ca48593a4809bc02794b837b"; - private static final String SECRET_KEY = "44090bdaf2e1b4d9f46bb772bc630ace9c3a00c3294ab61315f1c0e817ba29d6"; + private static final String API_KEY = ""; + private static final String SECRET_KEY = ""; public static void main(String[] args) { BinanceFuturesApiClientFactory factory = BinanceAbstractFactory.createTestnetFactory(API_KEY, SECRET_KEY); diff --git a/src/test/java/com/binance/api/examples/testnet/TestnetWebSocketExample.java b/src/test/java/com/binance/api/examples/testnet/TestnetWebSocketExample.java index 2e795cb3e..c79f61309 100755 --- a/src/test/java/com/binance/api/examples/testnet/TestnetWebSocketExample.java +++ b/src/test/java/com/binance/api/examples/testnet/TestnetWebSocketExample.java @@ -1,6 +1,7 @@ package com.binance.api.examples.testnet; import com.binance.api.client.api.BinanceApiWebSocketClient; +import com.binance.api.client.api.sync.BinanceApiFuturesRestClient; import com.binance.api.client.domain.market.CandlestickInterval; import com.binance.api.client.factory.BinanceAbstractFactory; import com.binance.api.client.factory.BinanceFuturesApiClientFactory; @@ -11,14 +12,18 @@ * @author Mahdi Sheikh Hosseini */ public class TestnetWebSocketExample { - private static final String SYMBOL = "LTCUSDT"; + private static final String SYMBOL = "BTCUSDT"; + private static final String API_KEY = ""; + private static final String SECRET_KEY = ""; public static void main(String[] args) { - BinanceFuturesApiClientFactory factory = BinanceAbstractFactory.createTestnetFactory(); - BinanceApiWebSocketClient webSocketClient = factory.newWebSocketClient(); + BinanceFuturesApiClientFactory factory = BinanceAbstractFactory.createTestnetFactory(API_KEY, SECRET_KEY); + BinanceApiFuturesRestClient client = factory.newRestClient(); + BinanceApiWebSocketClient streamClient = factory.newWebSocketClient(); -// webSocketClient.onTickerEvent(SYMBOL, System.out::println); - webSocketClient.onCandlestickEvent(SYMBOL, CandlestickInterval.ONE_MINUTE, System.out::println); + + String listenKey = client.startUserDataStream(); + streamClient.onUserDataUpdateEvent(listenKey, System.out::println); } }