From bd29d92944d4a13eaa25ef05802ff9a2e76887ad Mon Sep 17 00:00:00 2001 From: antlen Date: Wed, 5 Jan 2022 14:41:22 +0900 Subject: [PATCH 1/7] issue #409 API should allow integration with ExecutorService to allow better control of client threading. --- .../BinanceApiAsyncExecutorRestClient.java | 309 ++++++++++++++++++ .../api/client/BinanceApiClientFactory.java | 16 + ...BinanceApiAsyncExecutorRestClientImpl.java | 234 +++++++++++++ .../BinanceApiAsyncRestClientAdapter.java | 198 +++++++++++ 4 files changed, 757 insertions(+) create mode 100755 src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java create mode 100644 src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java create mode 100644 src/main/java/com/binance/api/client/impl/BinanceApiAsyncRestClientAdapter.java diff --git a/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java b/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java new file mode 100755 index 000000000..dbd23cd1b --- /dev/null +++ b/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java @@ -0,0 +1,309 @@ +package com.binance.api.client; + +import com.binance.api.client.domain.account.*; +import com.binance.api.client.domain.account.request.*; +import com.binance.api.client.domain.event.ListenKey; +import com.binance.api.client.domain.general.Asset; +import com.binance.api.client.domain.general.ExchangeInfo; +import com.binance.api.client.domain.general.ServerTime; +import com.binance.api.client.domain.market.*; + +import java.util.List; +import java.util.concurrent.Future; + +/** + * Binance API facade, supporting asynchronous/non-blocking access Binance's REST API using ExecutorServices to + * handling the request and response threading. + */ +public interface BinanceApiAsyncExecutorRestClient { + + // General endpoints + + /** + * Test connectivity to the Rest API. + * @return + */ + Future ping(BinanceApiCallback callback); + + /** + * Check server time. + * @return + */ + Future getServerTime(BinanceApiCallback callback); + + /** + * Current exchange trading rules and symbol information + * + * @return + */ + Future getExchangeInfo(BinanceApiCallback callback); + + /** + * ALL supported assets and whether or not they can be withdrawn. + * + * @return + */ + Future> getAllAssets(BinanceApiCallback> callback); + + // Market Data endpoints + + /** + * Get order book of a symbol (asynchronous) + * + * @param symbol ticker symbol (e.g. ETHBTC) + * @param limit depth of the order book (max 100) + * @param callback the callback that handles the response + * + * @return + */ + Future getOrderBook(String symbol, Integer limit, BinanceApiCallback callback); + + /** + * Get recent trades (up to last 500). Weight: 1 + * + * @param symbol ticker symbol (e.g. ETHBTC) + * @param limit of last trades (Default 500; max 1000.) + * @param callback the callback that handles the response + * + * @return + */ + Future> getTrades(String symbol, Integer limit, BinanceApiCallback> callback); + + /** + * Get older trades. Weight: 5 + * + * @param symbol ticker symbol (e.g. ETHBTC) + * @param limit of last trades (Default 500; max 1000.) + * @param fromId TradeId to fetch from. Default gets most recent trades. + * @param callback the callback that handles the response + * + * @return + */ + Future> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback); + + /** + * Get compressed, aggregate trades. Trades that fill at the time, from the same order, with + * the same price will have the quantity aggregated. + * + * If both startTime and endTime are sent, limitshould not + * be sent AND the distance between startTime and endTime must be less than 24 hours. + * + * @param symbol symbol to aggregate (mandatory) + * @param fromId ID to get aggregate trades from INCLUSIVE (optional) + * @param limit Default 500; max 1000 (optional) + * @param startTime Timestamp in ms to get aggregate trades from INCLUSIVE (optional). + * @param endTime Timestamp in ms to get aggregate trades until INCLUSIVE (optional). + * @param callback the callback that handles the response + * @return a list of aggregate trades for the given symbol + */ + Future> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); + + /** + * Return the most recent aggregate trades for symbol + * + * @see #getAggTrades(String, String, Integer, Long, Long, BinanceApiCallback) + */ + Future> getAggTrades(String symbol, BinanceApiCallback> callback); + + /** + * Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. + * + * @param symbol symbol to aggregate (mandatory) + * @param interval candlestick interval (mandatory) + * @param limit Default 500; max 1000 (optional) + * @param startTime Timestamp in ms to get candlestick bars from INCLUSIVE (optional). + * @param endTime Timestamp in ms to get candlestick bars until INCLUSIVE (optional). + * @param callback the callback that handles the response containing a candlestick bar for the given symbol and interval + */ + Future> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); + + /** + * Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. + * + * @see #getCandlestickBars(String, CandlestickInterval, BinanceApiCallback) + */ + Future> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback); + + /** + * Get 24 hour price change statistics (asynchronous). + * + * @param symbol ticker symbol (e.g. ETHBTC) + * @param callback the callback that handles the response + */ + Future get24HrPriceStatistics(String symbol, BinanceApiCallback callback); + + /** + * Get 24 hour price change statistics for all symbols (asynchronous). + * + * @param callback the callback that handles the response + */ + Future> getAll24HrPriceStatistics(BinanceApiCallback> callback); + + /** + * Get Latest price for all symbols (asynchronous). + * + * @param callback the callback that handles the response + */ + Future> getAllPrices(BinanceApiCallback> callback); + + /** + * Get latest price for symbol (asynchronous). + * + * @param symbol ticker symbol (e.g. ETHBTC) + * @param callback the callback that handles the response + */ + Future getPrice(String symbol , BinanceApiCallback callback); + + /** + * Get best price/qty on the order book for all symbols (asynchronous). + * + * @param callback the callback that handles the response + */ + Future> getBookTickers(BinanceApiCallback> callback); + + // Account endpoints + + /** + * Send in a new order (asynchronous) + * + * @param order the new order to submit. + * @param callback the callback that handles the response + */ + Future newOrder(NewOrder order, BinanceApiCallback callback); + + /** + * Test new order creation and signature/recvWindow long. Creates and validates a new order but does not send it into the matching engine. + * + * @param order the new TEST order to submit. + * @param callback the callback that handles the response + */ + Future newOrderTest(NewOrder order, BinanceApiCallback callback); + + /** + * Check an order's status (asynchronous). + * + * @param orderStatusRequest order status request parameters + * @param callback the callback that handles the response + */ + Future getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback); + + /** + * Cancel an active order (asynchronous). + * + * @param cancelOrderRequest order status request parameters + * @param callback the callback that handles the response + */ + Future cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback); + + /** + * Get all open orders on a symbol (asynchronous). + * + * @param orderRequest order request parameters + * @param callback the callback that handles the response + */ + Future> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback); + + /** + * Get all account orders; active, canceled, or filled. + * + * @param orderRequest order request parameters + * @param callback the callback that handles the response + */ + Future> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback); + + /** + * Get current account information (async). + */ + Future getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback); + + /** + * Get current account information using default parameters (async). + */ + Future getAccount(BinanceApiCallback callback); + + /** + * Get trades for a specific account and symbol. + * + * @param symbol symbol to get trades from + * @param limit default 500; max 1000 + * @param fromId TradeId to fetch from. Default gets most recent trades. + * @param callback the callback that handles the response with a list of trades + */ + Future> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback); + + /** + * Get trades for a specific account and symbol. + * + * @param symbol symbol to get trades from + * @param limit default 500; max 1000 + * @param callback the callback that handles the response with a list of trades + */ + Future> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback); + + /** + * Get trades for a specific account and symbol. + * + * @param symbol symbol to get trades from + * @param callback the callback that handles the response with a list of trades + */ + Future> getMyTrades(String symbol, BinanceApiCallback> callback); + + /** + * Submit a withdraw request. + * + * Enable Withdrawals option has to be active in the API settings. + * + * @param asset asset symbol to withdraw + * @param address address to withdraw to + * @param amount amount to withdraw + * @param name description/alias of the address + * @param addressTag Secondary address identifier for coins like XRP,XMR etc. + */ + Future withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback); + + /** + * Fetch account deposit history. + * + * @param callback the callback that handles the response and returns the deposit history + */ + Future getDepositHistory(String asset, BinanceApiCallback callback); + + /** + * Fetch account withdraw history. + * + * @param callback the callback that handles the response and returns the withdraw history + */ + Future getWithdrawHistory(String asset, BinanceApiCallback callback); + + /** + * Fetch deposit address. + * + * @param callback the callback that handles the response and returns the deposit address + */ + Future getDepositAddress(String asset, BinanceApiCallback callback); + + // User stream endpoints + + /** + * Start a new user data stream. + * + * @param callback the callback that handles the response which contains a listenKey + */ + Future startUserDataStream(BinanceApiCallback callback); + + /** + * PING a user data stream to prevent a time out. + * + * @param listenKey listen key that identifies a data stream + * @param callback the callback that handles the response which contains a listenKey + */ + Future keepAliveUserDataStream(String listenKey, BinanceApiCallback callback); + + /** + * Close out a new user data stream. + * + * @param listenKey listen key that identifies a data stream + * @param callback the callback that handles the response which contains a listenKey + */ + Future closeUserDataStream(String listenKey, BinanceApiCallback callback); +} \ No newline at end of file diff --git a/src/main/java/com/binance/api/client/BinanceApiClientFactory.java b/src/main/java/com/binance/api/client/BinanceApiClientFactory.java index 7ba5551bb..26f197370 100755 --- a/src/main/java/com/binance/api/client/BinanceApiClientFactory.java +++ b/src/main/java/com/binance/api/client/BinanceApiClientFactory.java @@ -2,6 +2,9 @@ import com.binance.api.client.impl.*; import com.binance.api.client.config.BinanceApiConfig; + +import java.util.concurrent.ExecutorService; + import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient; /** @@ -101,6 +104,19 @@ public BinanceApiRestClient newRestClient() { return new BinanceApiRestClientImpl(apiKey, secret); } + /** + * Creates a new asynchronous/non-blocking REST client that uses Executor Services for handling + * the request and response threading. + * + * @param requestService + * @param responseService + * @return + */ + public BinanceApiAsyncExecutorRestClient newAsyncExecutorRestClient(ExecutorService requestService, + ExecutorService responseService) { + return new BinanceApiAsyncExecutorRestClientImpl(newRestClient(), requestService, responseService); + } + /** * Creates a new asynchronous/non-blocking REST client. */ diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java new file mode 100644 index 000000000..da7905e34 --- /dev/null +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java @@ -0,0 +1,234 @@ +package com.binance.api.client.impl; + +import com.binance.api.client.BinanceApiAsyncExecutorRestClient; +import com.binance.api.client.BinanceApiCallback; +import com.binance.api.client.BinanceApiRestClient; +import com.binance.api.client.domain.account.*; +import com.binance.api.client.domain.account.request.*; +import com.binance.api.client.domain.event.ListenKey; +import com.binance.api.client.domain.general.Asset; +import com.binance.api.client.domain.general.ExchangeInfo; +import com.binance.api.client.domain.general.ServerTime; +import com.binance.api.client.domain.market.*; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.function.Supplier; + +/** + * Implementation of BinanceApiAsyncExecutorRestClient using ExecutorServices for handling request and responses. + */ +public class BinanceApiAsyncExecutorRestClientImpl implements BinanceApiAsyncExecutorRestClient { + private final BinanceApiRestClient client; + private final ExecutorService requestService; + private final ExecutorService responseService; + + public BinanceApiAsyncExecutorRestClientImpl(BinanceApiRestClient client, ExecutorService requestService, ExecutorService responseService) { + this.client = client; + this.requestService = requestService; + this.responseService = responseService; + } + + private Future invoke(BinanceApiCallback callback, Supplier t){ + return requestService.submit(() -> { + try { + T v = t.get(); + responseService.submit(() -> callback.onResponse(v)); + return v; + }catch (Exception e){ + responseService.submit(() -> callback.onFailure(e)); + throw e; + } + }); + } + + @Override + public Future ping(BinanceApiCallback callback) { + return invoke(callback,() -> { + client.ping(); + return null; + }); + } + + @Override + public Future getServerTime(BinanceApiCallback callback) { + return invoke(callback, () -> { + ServerTime t = new ServerTime(); + t.setServerTime(client.getServerTime()); + return t; + }); + } + + @Override + public Future getExchangeInfo(BinanceApiCallback callback) { + return invoke(callback, () -> client.getExchangeInfo()); + } + + @Override + public Future> getAllAssets(BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAllAssets()); + } + + @Override + public Future getOrderBook(String symbol, Integer limit, BinanceApiCallback callback) { + return invoke(callback, () -> client.getOrderBook(symbol,limit)); + } + + @Override + public Future> getTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getTrades(symbol, limit)); + } + + @Override + public Future> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getHistoricalTrades(symbol, limit, fromId)); + } + + @Override + public Future> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAggTrades(symbol,fromId,limit,startTime,endTime)); + } + + @Override + public Future> getAggTrades(String symbol, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAggTrades(symbol)); + } + + @Override + public Future> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getCandlestickBars(symbol,interval,limit,startTime, endTime)); + } + + @Override + public Future> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getCandlestickBars(symbol, interval)); + } + + @Override + public Future get24HrPriceStatistics(String symbol, BinanceApiCallback callback) { + return invoke(callback, () -> client.get24HrPriceStatistics(symbol)); + } + + @Override + public Future> getAll24HrPriceStatistics(BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAll24HrPriceStatistics()); + } + + @Override + public Future> getAllPrices(BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAllPrices()); + } + + @Override + public Future getPrice(String symbol, BinanceApiCallback callback) { + return invoke(callback, () -> client.getPrice(symbol)); + } + + @Override + public Future> getBookTickers(BinanceApiCallback> callback) { + return invoke(callback, () -> client.getBookTickers()); + } + + @Override + public Future newOrder(NewOrder order, BinanceApiCallback callback) { + return invoke(callback, () -> client.newOrder(order)); + } + + @Override + public Future newOrderTest(NewOrder order, BinanceApiCallback callback) { + return invoke(callback, () -> { + client.newOrderTest(order); + return null; + }); + } + + @Override + public Future getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback) { + return invoke(callback, () -> client.getOrderStatus(orderStatusRequest)); + } + + @Override + public Future cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback) { + return invoke(callback, () -> client.cancelOrder(cancelOrderRequest)); + } + + @Override + public Future> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getOpenOrders(orderRequest)); + } + + @Override + public Future> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getAllOrders(orderRequest)); + } + + @Override + public Future getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback) { + return invoke(callback, () -> client.getAccount(recvWindow, timestamp)); + } + + @Override + public Future getAccount(BinanceApiCallback callback) { + return invoke(callback, () -> client.getAccount()); + } + + @Override + public Future> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getMyTrades(symbol, limit, fromId, recvWindow, timestamp)); + } + + @Override + public Future> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getMyTrades(symbol, limit)); + } + + @Override + public Future> getMyTrades(String symbol, BinanceApiCallback> callback) { + return invoke(callback, () -> client.getMyTrades(symbol)); + } + + @Override + public Future withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback) { + return invoke(callback, () -> client.withdraw(asset, address, amount, name, addressTag)); + } + + @Override + public Future getDepositHistory(String asset, BinanceApiCallback callback) { + return invoke(callback, () -> client.getDepositHistory(asset)); + } + + @Override + public Future getWithdrawHistory(String asset, BinanceApiCallback callback) { + return invoke(callback, () -> client.getWithdrawHistory(asset)); + } + + @Override + public Future getDepositAddress(String asset, BinanceApiCallback callback) { + return invoke(callback, () -> client.getDepositAddress(asset)); + } + + @Override + public Future startUserDataStream(BinanceApiCallback callback) { + return invoke(callback, () -> { + client.startUserDataStream(); + return null; + }); + } + + @Override + public Future keepAliveUserDataStream(String listenKey, BinanceApiCallback callback) { + return invoke(callback, () -> { + client.keepAliveUserDataStream(listenKey); + return null; + }); + } + + @Override + public Future closeUserDataStream(String listenKey, BinanceApiCallback callback) { + return invoke(callback, () -> { + client.closeUserDataStream(listenKey); + return null; + }); + } +} diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncRestClientAdapter.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncRestClientAdapter.java new file mode 100644 index 000000000..e37dcec9a --- /dev/null +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncRestClientAdapter.java @@ -0,0 +1,198 @@ +package com.binance.api.client.impl; + +import com.binance.api.client.BinanceApiAsyncExecutorRestClient; +import com.binance.api.client.BinanceApiAsyncRestClient; +import com.binance.api.client.BinanceApiCallback; +import com.binance.api.client.domain.account.*; +import com.binance.api.client.domain.account.request.*; +import com.binance.api.client.domain.event.ListenKey; +import com.binance.api.client.domain.general.Asset; +import com.binance.api.client.domain.general.ExchangeInfo; +import com.binance.api.client.domain.general.ServerTime; +import com.binance.api.client.domain.market.*; + +import java.util.List; + +/** + * Adapter to make calls to a BinanceApiAsyncExecutorRestClient using the BinanceApiAsyncRestClient + * interface. This allows existing code to migrate to use BinanceApiAsyncExecutorRestClient with dedicated + * ExecutorServices without a major lift. + * If you are writing new code, consider to use BinanceApiAsyncExecutorRestClient directly. + */ +public class BinanceApiAsyncRestClientAdapter implements BinanceApiAsyncRestClient { + private BinanceApiAsyncExecutorRestClient delegate; + + public BinanceApiAsyncRestClientAdapter(BinanceApiAsyncExecutorRestClient delegate) { + this.delegate = delegate; + } + + @Override + public void ping(BinanceApiCallback callback) { + delegate.ping(callback); + } + + @Override + public void getServerTime(BinanceApiCallback callback) { + delegate.getServerTime(callback); + } + + @Override + public void getExchangeInfo(BinanceApiCallback callback) { + delegate.getExchangeInfo(callback); + } + + @Override + public void getAllAssets(BinanceApiCallback> callback) { + delegate.getAllAssets(callback); + } + + @Override + public void getOrderBook(String symbol, Integer limit, BinanceApiCallback callback) { + delegate.getOrderBook(symbol, limit,callback); + } + + @Override + public void getTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + delegate.getTrades(symbol, limit,callback); + } + + @Override + public void getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback) { + delegate.getHistoricalTrades(symbol, limit, fromId,callback); + } + + @Override + public void getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + delegate.getAggTrades(symbol, fromId,limit, startTime, endTime, callback); + } + + @Override + public void getAggTrades(String symbol, BinanceApiCallback> callback) { + delegate.getAggTrades(symbol, callback); + } + + @Override + public void getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + delegate.getCandlestickBars(symbol, interval, limit, startTime, endTime,callback); + } + + @Override + public void getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback) { + delegate.getCandlestickBars(symbol, interval,callback); + } + + @Override + public void get24HrPriceStatistics(String symbol, BinanceApiCallback callback) { + delegate.get24HrPriceStatistics(symbol,callback); + } + + @Override + public void getAll24HrPriceStatistics(BinanceApiCallback> callback) { + delegate.getAll24HrPriceStatistics(callback); + } + + @Override + public void getAllPrices(BinanceApiCallback> callback) { + delegate.getAllPrices(callback); + } + + @Override + public void getPrice(String symbol, BinanceApiCallback callback) { + delegate.getPrice(symbol,callback); + } + + @Override + public void getBookTickers(BinanceApiCallback> callback) { + delegate.getBookTickers(callback); + } + + @Override + public void newOrder(NewOrder order, BinanceApiCallback callback) { + delegate.newOrder(order,callback); + } + + @Override + public void newOrderTest(NewOrder order, BinanceApiCallback callback) { + delegate.newOrderTest(order,callback); + } + + @Override + public void getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback) { + delegate.getOrderStatus(orderStatusRequest,callback); + } + + @Override + public void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback) { + delegate.cancelOrder(cancelOrderRequest,callback); + } + + @Override + public void getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback) { + delegate.getOpenOrders(orderRequest,callback); + } + + @Override + public void getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback) { + delegate.getAllOrders(orderRequest,callback); + } + + @Override + public void getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback) { + delegate.getAccount(recvWindow,timestamp,callback); + } + + @Override + public void getAccount(BinanceApiCallback callback) { + delegate.getAccount(callback); + } + + @Override + public void getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback) { + delegate.getMyTrades(symbol, limit,fromId, recvWindow, timestamp,callback); + } + + @Override + public void getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + delegate.getMyTrades(symbol, limit,callback); + } + + @Override + public void getMyTrades(String symbol, BinanceApiCallback> callback) { + delegate.getMyTrades(symbol, callback); + } + + @Override + public void withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback) { + delegate.withdraw(asset, address,amount, name, addressTag,callback); + } + + @Override + public void getDepositHistory(String asset, BinanceApiCallback callback) { + delegate.getDepositHistory(asset, callback); + } + + @Override + public void getWithdrawHistory(String asset, BinanceApiCallback callback) { + delegate.getWithdrawHistory(asset, callback); + } + + @Override + public void getDepositAddress(String asset, BinanceApiCallback callback) { + delegate.getDepositAddress(asset, callback); + } + + @Override + public void startUserDataStream(BinanceApiCallback callback) { + delegate.startUserDataStream(callback); + } + + @Override + public void keepAliveUserDataStream(String listenKey, BinanceApiCallback callback) { + delegate.keepAliveUserDataStream(listenKey, callback); + } + + @Override + public void closeUserDataStream(String listenKey, BinanceApiCallback callback) { + delegate.closeUserDataStream(listenKey, callback); + } +} From d2677160d036a6b34a726e13bb0d453e71281093 Mon Sep 17 00:00:00 2001 From: antlen Date: Mon, 10 Jan 2022 19:19:22 +0900 Subject: [PATCH 2/7] use CompletableFuture rather than Future --- .../BinanceApiAsyncExecutorRestClient.java | 71 +++++++------- ...BinanceApiAsyncExecutorRestClientImpl.java | 94 ++++++++++--------- 2 files changed, 83 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java b/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java index dbd23cd1b..ca35f2c51 100755 --- a/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java +++ b/src/main/java/com/binance/api/client/BinanceApiAsyncExecutorRestClient.java @@ -7,9 +7,8 @@ import com.binance.api.client.domain.general.ExchangeInfo; import com.binance.api.client.domain.general.ServerTime; import com.binance.api.client.domain.market.*; - import java.util.List; -import java.util.concurrent.Future; +import java.util.concurrent.CompletableFuture; /** * Binance API facade, supporting asynchronous/non-blocking access Binance's REST API using ExecutorServices to @@ -23,27 +22,27 @@ public interface BinanceApiAsyncExecutorRestClient { * Test connectivity to the Rest API. * @return */ - Future ping(BinanceApiCallback callback); + CompletableFuture ping(BinanceApiCallback callback); /** * Check server time. * @return */ - Future getServerTime(BinanceApiCallback callback); + CompletableFuture getServerTime(BinanceApiCallback callback); /** * Current exchange trading rules and symbol information * * @return */ - Future getExchangeInfo(BinanceApiCallback callback); + CompletableFuture getExchangeInfo(BinanceApiCallback callback); /** * ALL supported assets and whether or not they can be withdrawn. * * @return */ - Future> getAllAssets(BinanceApiCallback> callback); + CompletableFuture> getAllAssets(BinanceApiCallback> callback); // Market Data endpoints @@ -56,7 +55,7 @@ public interface BinanceApiAsyncExecutorRestClient { * * @return */ - Future getOrderBook(String symbol, Integer limit, BinanceApiCallback callback); + CompletableFuture getOrderBook(String symbol, Integer limit, BinanceApiCallback callback); /** * Get recent trades (up to last 500). Weight: 1 @@ -67,7 +66,7 @@ public interface BinanceApiAsyncExecutorRestClient { * * @return */ - Future> getTrades(String symbol, Integer limit, BinanceApiCallback> callback); + CompletableFuture> getTrades(String symbol, Integer limit, BinanceApiCallback> callback); /** * Get older trades. Weight: 5 @@ -79,7 +78,7 @@ public interface BinanceApiAsyncExecutorRestClient { * * @return */ - Future> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback); + CompletableFuture> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback); /** * Get compressed, aggregate trades. Trades that fill at the time, from the same order, with @@ -96,14 +95,14 @@ public interface BinanceApiAsyncExecutorRestClient { * @param callback the callback that handles the response * @return a list of aggregate trades for the given symbol */ - Future> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); + CompletableFuture> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); /** * Return the most recent aggregate trades for symbol * * @see #getAggTrades(String, String, Integer, Long, Long, BinanceApiCallback) */ - Future> getAggTrades(String symbol, BinanceApiCallback> callback); + CompletableFuture> getAggTrades(String symbol, BinanceApiCallback> callback); /** * Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. @@ -115,14 +114,14 @@ public interface BinanceApiAsyncExecutorRestClient { * @param endTime Timestamp in ms to get candlestick bars until INCLUSIVE (optional). * @param callback the callback that handles the response containing a candlestick bar for the given symbol and interval */ - Future> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); + CompletableFuture> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback); /** * Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. * * @see #getCandlestickBars(String, CandlestickInterval, BinanceApiCallback) */ - Future> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback); + CompletableFuture> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback); /** * Get 24 hour price change statistics (asynchronous). @@ -130,21 +129,21 @@ public interface BinanceApiAsyncExecutorRestClient { * @param symbol ticker symbol (e.g. ETHBTC) * @param callback the callback that handles the response */ - Future get24HrPriceStatistics(String symbol, BinanceApiCallback callback); + CompletableFuture get24HrPriceStatistics(String symbol, BinanceApiCallback callback); /** * Get 24 hour price change statistics for all symbols (asynchronous). * * @param callback the callback that handles the response */ - Future> getAll24HrPriceStatistics(BinanceApiCallback> callback); + CompletableFuture> getAll24HrPriceStatistics(BinanceApiCallback> callback); /** * Get Latest price for all symbols (asynchronous). * * @param callback the callback that handles the response */ - Future> getAllPrices(BinanceApiCallback> callback); + CompletableFuture> getAllPrices(BinanceApiCallback> callback); /** * Get latest price for symbol (asynchronous). @@ -152,14 +151,14 @@ public interface BinanceApiAsyncExecutorRestClient { * @param symbol ticker symbol (e.g. ETHBTC) * @param callback the callback that handles the response */ - Future getPrice(String symbol , BinanceApiCallback callback); + CompletableFuture getPrice(String symbol , BinanceApiCallback callback); /** * Get best price/qty on the order book for all symbols (asynchronous). * * @param callback the callback that handles the response */ - Future> getBookTickers(BinanceApiCallback> callback); + CompletableFuture> getBookTickers(BinanceApiCallback> callback); // Account endpoints @@ -169,7 +168,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param order the new order to submit. * @param callback the callback that handles the response */ - Future newOrder(NewOrder order, BinanceApiCallback callback); + CompletableFuture newOrder(NewOrder order, BinanceApiCallback callback); /** * Test new order creation and signature/recvWindow long. Creates and validates a new order but does not send it into the matching engine. @@ -177,7 +176,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param order the new TEST order to submit. * @param callback the callback that handles the response */ - Future newOrderTest(NewOrder order, BinanceApiCallback callback); + CompletableFuture newOrderTest(NewOrder order, BinanceApiCallback callback); /** * Check an order's status (asynchronous). @@ -185,7 +184,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param orderStatusRequest order status request parameters * @param callback the callback that handles the response */ - Future getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback); + CompletableFuture getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback); /** * Cancel an active order (asynchronous). @@ -193,7 +192,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param cancelOrderRequest order status request parameters * @param callback the callback that handles the response */ - Future cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback); + CompletableFuture cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback); /** * Get all open orders on a symbol (asynchronous). @@ -201,7 +200,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param orderRequest order request parameters * @param callback the callback that handles the response */ - Future> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback); + CompletableFuture> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback); /** * Get all account orders; active, canceled, or filled. @@ -209,17 +208,17 @@ public interface BinanceApiAsyncExecutorRestClient { * @param orderRequest order request parameters * @param callback the callback that handles the response */ - Future> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback); + CompletableFuture> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback); /** * Get current account information (async). */ - Future getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback); + CompletableFuture getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback); /** * Get current account information using default parameters (async). */ - Future getAccount(BinanceApiCallback callback); + CompletableFuture getAccount(BinanceApiCallback callback); /** * Get trades for a specific account and symbol. @@ -229,7 +228,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param fromId TradeId to fetch from. Default gets most recent trades. * @param callback the callback that handles the response with a list of trades */ - Future> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback); + CompletableFuture> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback); /** * Get trades for a specific account and symbol. @@ -238,7 +237,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param limit default 500; max 1000 * @param callback the callback that handles the response with a list of trades */ - Future> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback); + CompletableFuture> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback); /** * Get trades for a specific account and symbol. @@ -246,7 +245,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param symbol symbol to get trades from * @param callback the callback that handles the response with a list of trades */ - Future> getMyTrades(String symbol, BinanceApiCallback> callback); + CompletableFuture> getMyTrades(String symbol, BinanceApiCallback> callback); /** * Submit a withdraw request. @@ -259,28 +258,28 @@ public interface BinanceApiAsyncExecutorRestClient { * @param name description/alias of the address * @param addressTag Secondary address identifier for coins like XRP,XMR etc. */ - Future withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback); + CompletableFuture withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback); /** * Fetch account deposit history. * * @param callback the callback that handles the response and returns the deposit history */ - Future getDepositHistory(String asset, BinanceApiCallback callback); + CompletableFuture getDepositHistory(String asset, BinanceApiCallback callback); /** * Fetch account withdraw history. * * @param callback the callback that handles the response and returns the withdraw history */ - Future getWithdrawHistory(String asset, BinanceApiCallback callback); + CompletableFuture getWithdrawHistory(String asset, BinanceApiCallback callback); /** * Fetch deposit address. * * @param callback the callback that handles the response and returns the deposit address */ - Future getDepositAddress(String asset, BinanceApiCallback callback); + CompletableFuture getDepositAddress(String asset, BinanceApiCallback callback); // User stream endpoints @@ -289,7 +288,7 @@ public interface BinanceApiAsyncExecutorRestClient { * * @param callback the callback that handles the response which contains a listenKey */ - Future startUserDataStream(BinanceApiCallback callback); + CompletableFuture startUserDataStream(BinanceApiCallback callback); /** * PING a user data stream to prevent a time out. @@ -297,7 +296,7 @@ public interface BinanceApiAsyncExecutorRestClient { * @param listenKey listen key that identifies a data stream * @param callback the callback that handles the response which contains a listenKey */ - Future keepAliveUserDataStream(String listenKey, BinanceApiCallback callback); + CompletableFuture keepAliveUserDataStream(String listenKey, BinanceApiCallback callback); /** * Close out a new user data stream. @@ -305,5 +304,5 @@ public interface BinanceApiAsyncExecutorRestClient { * @param listenKey listen key that identifies a data stream * @param callback the callback that handles the response which contains a listenKey */ - Future closeUserDataStream(String listenKey, BinanceApiCallback callback); + CompletableFuture closeUserDataStream(String listenKey, BinanceApiCallback callback); } \ No newline at end of file diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java index da7905e34..d079a41b0 100644 --- a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java @@ -10,10 +10,9 @@ import com.binance.api.client.domain.general.ExchangeInfo; import com.binance.api.client.domain.general.ServerTime; import com.binance.api.client.domain.market.*; - import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.function.Supplier; /** @@ -30,21 +29,24 @@ public BinanceApiAsyncExecutorRestClientImpl(BinanceApiRestClient client, Execut this.responseService = responseService; } - private Future invoke(BinanceApiCallback callback, Supplier t){ - return requestService.submit(() -> { - try { - T v = t.get(); - responseService.submit(() -> callback.onResponse(v)); + private CompletableFuture invoke(BinanceApiCallback callback, Supplier t){ + CompletableFuture f = CompletableFuture.supplyAsync(t, requestService); + if(callback != null){ + f = f.thenApplyAsync(v -> { + callback.onResponse(v); return v; - }catch (Exception e){ - responseService.submit(() -> callback.onFailure(e)); - throw e; - } - }); + }, responseService); + + f.exceptionallyAsync(throwable -> { + callback.onFailure(throwable); + return null; + }, responseService); + } + return f; } @Override - public Future ping(BinanceApiCallback callback) { + public CompletableFuture ping(BinanceApiCallback callback) { return invoke(callback,() -> { client.ping(); return null; @@ -52,7 +54,7 @@ public Future ping(BinanceApiCallback callback) { } @Override - public Future getServerTime(BinanceApiCallback callback) { + public CompletableFuture getServerTime(BinanceApiCallback callback) { return invoke(callback, () -> { ServerTime t = new ServerTime(); t.setServerTime(client.getServerTime()); @@ -61,82 +63,82 @@ public Future getServerTime(BinanceApiCallback callback) } @Override - public Future getExchangeInfo(BinanceApiCallback callback) { + public CompletableFuture getExchangeInfo(BinanceApiCallback callback) { return invoke(callback, () -> client.getExchangeInfo()); } @Override - public Future> getAllAssets(BinanceApiCallback> callback) { + public CompletableFuture> getAllAssets(BinanceApiCallback> callback) { return invoke(callback, () -> client.getAllAssets()); } @Override - public Future getOrderBook(String symbol, Integer limit, BinanceApiCallback callback) { + public CompletableFuture getOrderBook(String symbol, Integer limit, BinanceApiCallback callback) { return invoke(callback, () -> client.getOrderBook(symbol,limit)); } @Override - public Future> getTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + public CompletableFuture> getTrades(String symbol, Integer limit, BinanceApiCallback> callback) { return invoke(callback, () -> client.getTrades(symbol, limit)); } @Override - public Future> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback) { + public CompletableFuture> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback> callback) { return invoke(callback, () -> client.getHistoricalTrades(symbol, limit, fromId)); } @Override - public Future> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + public CompletableFuture> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { return invoke(callback, () -> client.getAggTrades(symbol,fromId,limit,startTime,endTime)); } @Override - public Future> getAggTrades(String symbol, BinanceApiCallback> callback) { + public CompletableFuture> getAggTrades(String symbol, BinanceApiCallback> callback) { return invoke(callback, () -> client.getAggTrades(symbol)); } @Override - public Future> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { + public CompletableFuture> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback> callback) { return invoke(callback, () -> client.getCandlestickBars(symbol,interval,limit,startTime, endTime)); } @Override - public Future> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback) { + public CompletableFuture> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback> callback) { return invoke(callback, () -> client.getCandlestickBars(symbol, interval)); } @Override - public Future get24HrPriceStatistics(String symbol, BinanceApiCallback callback) { + public CompletableFuture get24HrPriceStatistics(String symbol, BinanceApiCallback callback) { return invoke(callback, () -> client.get24HrPriceStatistics(symbol)); } @Override - public Future> getAll24HrPriceStatistics(BinanceApiCallback> callback) { + public CompletableFuture> getAll24HrPriceStatistics(BinanceApiCallback> callback) { return invoke(callback, () -> client.getAll24HrPriceStatistics()); } @Override - public Future> getAllPrices(BinanceApiCallback> callback) { + public CompletableFuture> getAllPrices(BinanceApiCallback> callback) { return invoke(callback, () -> client.getAllPrices()); } @Override - public Future getPrice(String symbol, BinanceApiCallback callback) { + public CompletableFuture getPrice(String symbol, BinanceApiCallback callback) { return invoke(callback, () -> client.getPrice(symbol)); } @Override - public Future> getBookTickers(BinanceApiCallback> callback) { + public CompletableFuture> getBookTickers(BinanceApiCallback> callback) { return invoke(callback, () -> client.getBookTickers()); } @Override - public Future newOrder(NewOrder order, BinanceApiCallback callback) { + public CompletableFuture newOrder(NewOrder order, BinanceApiCallback callback) { return invoke(callback, () -> client.newOrder(order)); } @Override - public Future newOrderTest(NewOrder order, BinanceApiCallback callback) { + public CompletableFuture newOrderTest(NewOrder order, BinanceApiCallback callback) { return invoke(callback, () -> { client.newOrderTest(order); return null; @@ -144,72 +146,72 @@ public Future newOrderTest(NewOrder order, BinanceApiCallback callba } @Override - public Future getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback) { + public CompletableFuture getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback callback) { return invoke(callback, () -> client.getOrderStatus(orderStatusRequest)); } @Override - public Future cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback) { + public CompletableFuture cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback callback) { return invoke(callback, () -> client.cancelOrder(cancelOrderRequest)); } @Override - public Future> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback) { + public CompletableFuture> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback> callback) { return invoke(callback, () -> client.getOpenOrders(orderRequest)); } @Override - public Future> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback) { + public CompletableFuture> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback> callback) { return invoke(callback, () -> client.getAllOrders(orderRequest)); } @Override - public Future getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback) { + public CompletableFuture getAccount(Long recvWindow, Long timestamp, BinanceApiCallback callback) { return invoke(callback, () -> client.getAccount(recvWindow, timestamp)); } @Override - public Future getAccount(BinanceApiCallback callback) { + public CompletableFuture getAccount(BinanceApiCallback callback) { return invoke(callback, () -> client.getAccount()); } @Override - public Future> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback) { + public CompletableFuture> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback> callback) { return invoke(callback, () -> client.getMyTrades(symbol, limit, fromId, recvWindow, timestamp)); } @Override - public Future> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback) { + public CompletableFuture> getMyTrades(String symbol, Integer limit, BinanceApiCallback> callback) { return invoke(callback, () -> client.getMyTrades(symbol, limit)); } @Override - public Future> getMyTrades(String symbol, BinanceApiCallback> callback) { + public CompletableFuture> getMyTrades(String symbol, BinanceApiCallback> callback) { return invoke(callback, () -> client.getMyTrades(symbol)); } @Override - public Future withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback) { + public CompletableFuture withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback callback) { return invoke(callback, () -> client.withdraw(asset, address, amount, name, addressTag)); } @Override - public Future getDepositHistory(String asset, BinanceApiCallback callback) { + public CompletableFuture getDepositHistory(String asset, BinanceApiCallback callback) { return invoke(callback, () -> client.getDepositHistory(asset)); } @Override - public Future getWithdrawHistory(String asset, BinanceApiCallback callback) { + public CompletableFuture getWithdrawHistory(String asset, BinanceApiCallback callback) { return invoke(callback, () -> client.getWithdrawHistory(asset)); } @Override - public Future getDepositAddress(String asset, BinanceApiCallback callback) { + public CompletableFuture getDepositAddress(String asset, BinanceApiCallback callback) { return invoke(callback, () -> client.getDepositAddress(asset)); } @Override - public Future startUserDataStream(BinanceApiCallback callback) { + public CompletableFuture startUserDataStream(BinanceApiCallback callback) { return invoke(callback, () -> { client.startUserDataStream(); return null; @@ -217,7 +219,7 @@ public Future startUserDataStream(BinanceApiCallback callb } @Override - public Future keepAliveUserDataStream(String listenKey, BinanceApiCallback callback) { + public CompletableFuture keepAliveUserDataStream(String listenKey, BinanceApiCallback callback) { return invoke(callback, () -> { client.keepAliveUserDataStream(listenKey); return null; @@ -225,7 +227,7 @@ public Future keepAliveUserDataStream(String listenKey, BinanceApiCallback } @Override - public Future closeUserDataStream(String listenKey, BinanceApiCallback callback) { + public CompletableFuture closeUserDataStream(String listenKey, BinanceApiCallback callback) { return invoke(callback, () -> { client.closeUserDataStream(listenKey); return null; From 013430befad66e8c780c60b91693b6141326c754 Mon Sep 17 00:00:00 2001 From: antlen Date: Mon, 10 Jan 2022 20:37:01 +0900 Subject: [PATCH 3/7] manually put on the reponse service --- .../client/impl/BinanceApiAsyncExecutorRestClientImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java index d079a41b0..9abbeda8b 100644 --- a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java @@ -37,10 +37,10 @@ private CompletableFuture invoke(BinanceApiCallback callback, Supplier return v; }, responseService); - f.exceptionallyAsync(throwable -> { - callback.onFailure(throwable); + f.exceptionally(throwable -> { + responseService.submit(()->callback.onFailure(throwable)); return null; - }, responseService); + }); } return f; } From 92aafa3064dc6bb4b6287a1a0f7f82d3e06240f0 Mon Sep 17 00:00:00 2001 From: antlen Date: Sun, 15 May 2022 10:20:42 +0900 Subject: [PATCH 4/7] my binance changes --- pom.xml | 6 +++--- .../com/binance/api/client/domain/general/FilterType.java | 1 + .../client/impl/BinanceApiAsyncExecutorRestClientImpl.java | 2 ++ .../api/client/impl/BinanceApiWebSocketClientImpl.java | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 67e7af532..e6b62a87e 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.binance.api binance-api-client - 1.0.1 + 1.0.1-el The MIT License @@ -53,8 +53,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.8 - 1.8 + 14 + 14 diff --git a/src/main/java/com/binance/api/client/domain/general/FilterType.java b/src/main/java/com/binance/api/client/domain/general/FilterType.java index 57c2931db..beda8d142 100755 --- a/src/main/java/com/binance/api/client/domain/general/FilterType.java +++ b/src/main/java/com/binance/api/client/domain/general/FilterType.java @@ -19,6 +19,7 @@ public enum FilterType { MARKET_LOT_SIZE, MAX_NUM_ICEBERG_ORDERS, MAX_POSITION, + TRAILING_DELTA, // Exchange EXCHANGE_MAX_NUM_ORDERS, diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java index 9abbeda8b..180d8fbf5 100644 --- a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java @@ -19,6 +19,7 @@ * Implementation of BinanceApiAsyncExecutorRestClient using ExecutorServices for handling request and responses. */ public class BinanceApiAsyncExecutorRestClientImpl implements BinanceApiAsyncExecutorRestClient { + private final BinanceApiRestClient client; private final ExecutorService requestService; private final ExecutorService responseService; @@ -38,6 +39,7 @@ private CompletableFuture invoke(BinanceApiCallback callback, Supplier }, responseService); f.exceptionally(throwable -> { + throwable.printStackTrace(); responseService.submit(()->callback.onFailure(throwable)); return null; }); diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java index 4d68fb436..c7509d5da 100755 --- a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java @@ -30,7 +30,8 @@ public BinanceApiWebSocketClientImpl(OkHttpClient client) { public Closeable onDepthEvent(String symbols, BinanceApiCallback callback) { final String channel = Arrays.stream(symbols.split(",")) .map(String::trim) - .map(s -> String.format("%s@depth", s)) + .map(s -> String.format("%s@depth@100ms", s)) + //.map(s -> String.format("%s@depth", s)) .collect(Collectors.joining("/")); return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, DepthEvent.class)); } @@ -75,7 +76,8 @@ public Closeable onAllMarketTickersEvent(BinanceApiCallback> c public Closeable onBookTickerEvent(String symbols, BinanceApiCallback callback) { final String channel = Arrays.stream(symbols.split(",")) .map(String::trim) - .map(s -> String.format("%s@bookTicker", s)) + .map(s -> String.format("%s@bookTicker@100ms", s)) + // .map(s -> String.format("%s@bookTicker", s)) .collect(Collectors.joining("/")); return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, BookTickerEvent.class)); } From 88029e15b229ca7b63e3ebaad09c3aea6a5887cd Mon Sep 17 00:00:00 2001 From: antlen Date: Fri, 29 Jul 2022 17:41:48 +0900 Subject: [PATCH 5/7] small changes --- .../com/binance/api/client/domain/event/UserDataUpdateEvent.java | 1 + .../api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) 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 062565790..fd41dbad4 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 @@ -123,4 +123,5 @@ public static UserDataUpdateEventType fromEventTypeId(String eventTypeId) { throw new UnsupportedEventException("Unrecognized user data update event type id: " + eventTypeId); } } + } diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java index 180d8fbf5..0fbe95d56 100644 --- a/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiAsyncExecutorRestClientImpl.java @@ -39,7 +39,6 @@ private CompletableFuture invoke(BinanceApiCallback callback, Supplier }, responseService); f.exceptionally(throwable -> { - throwable.printStackTrace(); responseService.submit(()->callback.onFailure(throwable)); return null; }); From 2a159e359ff920544646850fe552a3864cbc0ae7 Mon Sep 17 00:00:00 2001 From: antlen Date: Wed, 10 Aug 2022 18:21:53 +0900 Subject: [PATCH 6/7] new enum value --- .../java/com/binance/api/client/domain/general/FilterType.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/binance/api/client/domain/general/FilterType.java b/src/main/java/com/binance/api/client/domain/general/FilterType.java index beda8d142..a15416982 100755 --- a/src/main/java/com/binance/api/client/domain/general/FilterType.java +++ b/src/main/java/com/binance/api/client/domain/general/FilterType.java @@ -20,6 +20,7 @@ public enum FilterType { MAX_NUM_ICEBERG_ORDERS, MAX_POSITION, TRAILING_DELTA, + PERCENT_PRICE_BY_SIDE, // Exchange EXCHANGE_MAX_NUM_ORDERS, From 50538d3fee2490fd0eb02f36f7a7b767a3c34d33 Mon Sep 17 00:00:00 2001 From: antlen Date: Tue, 22 Aug 2023 17:59:11 +0900 Subject: [PATCH 7/7] new event type --- .../com/binance/api/client/domain/general/FilterType.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/binance/api/client/domain/general/FilterType.java b/src/main/java/com/binance/api/client/domain/general/FilterType.java index a15416982..8e960f5c5 100755 --- a/src/main/java/com/binance/api/client/domain/general/FilterType.java +++ b/src/main/java/com/binance/api/client/domain/general/FilterType.java @@ -24,5 +24,7 @@ public enum FilterType { // Exchange EXCHANGE_MAX_NUM_ORDERS, - EXCHANGE_MAX_ALGO_ORDERS + EXCHANGE_MAX_ALGO_ORDERS, + + NOTIONAL } \ No newline at end of file