diff --git a/README.md b/README.md index e90c3ed4c..ec1c0c37c 100755 --- a/README.md +++ b/README.md @@ -22,6 +22,15 @@ Alternatively, you can clone this repository and run the [examples](https://gith ## Examples ### Getting Started +Please see the basic [information](https://binance-docs.github.io/apidocs/#general-api-information) about api. + +To use the examples, you need to generate `API-KEY` and `SECRET` at [API Management](https://www.binancezh.top/en/support/faq/360002502072) and replace XXX in [`PrivateConfig`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/examples/constants/PrivateConfig.java) with the appropriate values. + +```java +public static final String API_KEY = "XXX"; +public static final String SECRET_KEY = "XXX"; +``` + There are three main client classes that can be used to interact with the API: @@ -29,7 +38,7 @@ There are three main client classes that can be used to interact with the API: 2. [`BinanceApiAsyncRestClient`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/client/BinanceApiAsyncRestClient.java), an asynchronous/non-blocking [Binance API](https://www.binance.com/restapipub.html) client; 3. [`BinanceApiWebSocketClient`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java), a data streaming client using [Binance WebSocket API](https://www.binance.com/restapipub.html#wss-endpoint). -These can be instantiated through the corresponding factory method of [`BinanceApiClientFactory`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/client/BinanceApiClientFactory.java), by passing the [security parameters](https://www.binance.com/restapipub.html#user-content-endpoint-security-type) `API-KEY` and `SECRET`, which can be created at [https://www.binance.com/userCenter/createApi.html](https://www.binance.com/userCenter/createApi.html). +These can be instantiated through the corresponding factory method of [`BinanceApiClientFactory`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/client/BinanceApiClientFactory.java), by passing the [security parameters](https://www.binance.com/restapipub.html#user-content-endpoint-security-type) `API-KEY` and `SECRET`. ```java BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("API-KEY", "SECRET"); diff --git a/src/test/java/com/binance/api/client/domain/account/NewOrderResponseTest.java b/src/test/java/com/binance/api/client/domain/account/NewOrderResponseTest.java index f65e5eefe..b6fc6edde 100755 --- a/src/test/java/com/binance/api/client/domain/account/NewOrderResponseTest.java +++ b/src/test/java/com/binance/api/client/domain/account/NewOrderResponseTest.java @@ -7,9 +7,7 @@ import java.util.Collections; import java.util.List; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; /** * @see NewOrderResponse @@ -28,19 +26,19 @@ public void setUp() { @Test public void shouldHandleToStringWithNullFills() { - assertThat(newOrderResponse.toString(), containsString(",fills=")); + assertTrue(newOrderResponse.toString().contains(",fills=")); } @Test public void shouldHandleToStringWithNoFills() { newOrderResponse.setFills(Collections.emptyList()); - assertThat(newOrderResponse.toString(), containsString(",fills=")); + assertTrue(newOrderResponse.toString().contains(",fills=")); } @Test public void shouldHandleToStringWithFills() { newOrderResponse.setFills(trades(trade)); - assertThat(newOrderResponse.toString(), containsString(",fills=Trade[id=123,")); + assertTrue(newOrderResponse.toString().contains(",fills=Trade[id=123,")); } private static List trades(final Trade... trades) { diff --git a/src/test/java/com/binance/api/domain/account/WithdrawHistoryDeserializerTest.java b/src/test/java/com/binance/api/domain/account/WithdrawHistoryDeserializerTest.java index d0a99bf85..4a8ead994 100755 --- a/src/test/java/com/binance/api/domain/account/WithdrawHistoryDeserializerTest.java +++ b/src/test/java/com/binance/api/domain/account/WithdrawHistoryDeserializerTest.java @@ -8,9 +8,7 @@ import java.io.IOException; import java.util.List; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; -import static junit.framework.TestCase.fail; +import static junit.framework.TestCase.*; /** * Test deserialization of a withdraw/deposit history. 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 03ce3e00d..ee97bee62 100755 --- a/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java +++ b/src/test/java/com/binance/api/domain/event/UserDataUpdateEventDeserializerTest.java @@ -1,13 +1,8 @@ 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.UserDataUpdateEvent; @@ -70,8 +65,8 @@ public void testOrderUpdateEventDeserializer() { assertEquals(orderTradeUpdateEvent.getOrderStatus(), OrderStatus.CANCELED); assertEquals(orderTradeUpdateEvent.getOrderRejectReason(), OrderRejectReason.NONE); - assertEquals(orderTradeUpdateEvent.getOrderId(), new Long(123456)); - assertEquals(orderTradeUpdateEvent.getOrderTradeTime(), new Long(1)); + assertEquals(orderTradeUpdateEvent.getOrderId(), Long.valueOf(123456)); + assertEquals(orderTradeUpdateEvent.getOrderTradeTime(), Long.valueOf(1)); } catch (IOException e) { fail(); } diff --git a/src/test/java/com/binance/api/domain/general/ExchangeInfoDeserializerTest.java b/src/test/java/com/binance/api/domain/general/ExchangeInfoDeserializerTest.java index 94467915b..8fecc3085 100755 --- a/src/test/java/com/binance/api/domain/general/ExchangeInfoDeserializerTest.java +++ b/src/test/java/com/binance/api/domain/general/ExchangeInfoDeserializerTest.java @@ -1,14 +1,7 @@ package com.binance.api.domain.general; import com.binance.api.client.domain.OrderType; -import com.binance.api.client.domain.general.ExchangeInfo; -import com.binance.api.client.domain.general.FilterType; -import com.binance.api.client.domain.general.RateLimit; -import com.binance.api.client.domain.general.RateLimitInterval; -import com.binance.api.client.domain.general.RateLimitType; -import com.binance.api.client.domain.general.SymbolFilter; -import com.binance.api.client.domain.general.SymbolInfo; -import com.binance.api.client.domain.general.SymbolStatus; +import com.binance.api.client.domain.general.*; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; @@ -16,9 +9,7 @@ import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; /** * Test deserialization of exchange information. diff --git a/src/test/java/com/binance/api/examples/AccountEndpointsExample.java b/src/test/java/com/binance/api/examples/AccountEndpointsExample.java index 72fc68c3d..d622bf621 100755 --- a/src/test/java/com/binance/api/examples/AccountEndpointsExample.java +++ b/src/test/java/com/binance/api/examples/AccountEndpointsExample.java @@ -4,6 +4,7 @@ import com.binance.api.client.BinanceApiRestClient; import com.binance.api.client.domain.account.Account; import com.binance.api.client.domain.account.Trade; +import com.binance.api.examples.constants.PrivateConfig; import java.util.List; @@ -13,7 +14,7 @@ public class AccountEndpointsExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiRestClient client = factory.newRestClient(); // Get account balances diff --git a/src/test/java/com/binance/api/examples/AccountEndpointsExampleAsync.java b/src/test/java/com/binance/api/examples/AccountEndpointsExampleAsync.java index 5ede0526b..04b3d2d51 100755 --- a/src/test/java/com/binance/api/examples/AccountEndpointsExampleAsync.java +++ b/src/test/java/com/binance/api/examples/AccountEndpointsExampleAsync.java @@ -3,6 +3,7 @@ import com.binance.api.client.BinanceApiAsyncRestClient; import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.domain.account.Account; +import com.binance.api.examples.constants.PrivateConfig; /** * Examples on how to get account information. @@ -10,20 +11,20 @@ public class AccountEndpointsExampleAsync { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiAsyncRestClient client = factory.newAsyncRestClient(); // Get account balances (async) client.getAccount((Account response) -> System.out.println(response.getAssetBalance("ETH"))); // Get list of trades (async) - client.getMyTrades("NEOETH", response -> System.out.println(response)); + client.getMyTrades("NEOETH", System.out::println); // Get withdraw history (async) - client.getWithdrawHistory("ETH", response -> System.out.println(response)); + client.getWithdrawHistory("ETH", System.out::println); // Get deposit history (async) - client.getDepositHistory("ETH", response -> System.out.println(response)); + client.getDepositHistory("ETH", System.out::println); // Withdraw (async) client.withdraw("ETH", "0x123", "0.1", null, null, response -> {}); diff --git a/src/test/java/com/binance/api/examples/AllMarketTickersExample.java b/src/test/java/com/binance/api/examples/AllMarketTickersExample.java index 4bee760ce..01baef8a1 100755 --- a/src/test/java/com/binance/api/examples/AllMarketTickersExample.java +++ b/src/test/java/com/binance/api/examples/AllMarketTickersExample.java @@ -14,8 +14,6 @@ public static void main(String[] args) { BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(); BinanceApiWebSocketClient client = factory.newWebSocketClient(); - client.onAllMarketTickersEvent(event -> { - System.out.println(event); - }); + client.onAllMarketTickersEvent(System.out::println); } } diff --git a/src/test/java/com/binance/api/examples/DepthCacheExample.java b/src/test/java/com/binance/api/examples/DepthCacheExample.java index 25adc8c55..bb5d5a26f 100755 --- a/src/test/java/com/binance/api/examples/DepthCacheExample.java +++ b/src/test/java/com/binance/api/examples/DepthCacheExample.java @@ -11,12 +11,7 @@ import java.io.Closeable; import java.io.IOException; import java.math.BigDecimal; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.NavigableMap; -import java.util.TreeMap; +import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; diff --git a/src/test/java/com/binance/api/examples/GeneralEndpointsExample.java b/src/test/java/com/binance/api/examples/GeneralEndpointsExample.java index 760d6f26a..0578e4049 100755 --- a/src/test/java/com/binance/api/examples/GeneralEndpointsExample.java +++ b/src/test/java/com/binance/api/examples/GeneralEndpointsExample.java @@ -2,11 +2,7 @@ import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.BinanceApiRestClient; -import com.binance.api.client.domain.general.Asset; -import com.binance.api.client.domain.general.ExchangeInfo; -import com.binance.api.client.domain.general.FilterType; -import com.binance.api.client.domain.general.SymbolFilter; -import com.binance.api.client.domain.general.SymbolInfo; +import com.binance.api.client.domain.general.*; import java.util.List; diff --git a/src/test/java/com/binance/api/examples/GeneralEndpointsExampleAsync.java b/src/test/java/com/binance/api/examples/GeneralEndpointsExampleAsync.java index 96739a5c2..b4abe8517 100755 --- a/src/test/java/com/binance/api/examples/GeneralEndpointsExampleAsync.java +++ b/src/test/java/com/binance/api/examples/GeneralEndpointsExampleAsync.java @@ -2,13 +2,10 @@ import com.binance.api.client.BinanceApiAsyncRestClient; import com.binance.api.client.BinanceApiClientFactory; -import com.binance.api.client.domain.general.Asset; import com.binance.api.client.domain.general.FilterType; import com.binance.api.client.domain.general.SymbolFilter; import com.binance.api.client.domain.general.SymbolInfo; -import java.util.List; - /** * Examples on how to use the general endpoints. */ diff --git a/src/test/java/com/binance/api/examples/MarginAccountEndpointsExample.java b/src/test/java/com/binance/api/examples/MarginAccountEndpointsExample.java index 0d204fac1..83ddc8d61 100755 --- a/src/test/java/com/binance/api/examples/MarginAccountEndpointsExample.java +++ b/src/test/java/com/binance/api/examples/MarginAccountEndpointsExample.java @@ -6,6 +6,7 @@ import com.binance.api.client.domain.account.MarginAccount; import com.binance.api.client.domain.account.MarginTransaction; import com.binance.api.client.domain.account.Trade; +import com.binance.api.examples.constants.PrivateConfig; import java.util.List; @@ -15,7 +16,7 @@ public class MarginAccountEndpointsExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiMarginRestClient client = factory.newMarginRestClient(); // Get account balances diff --git a/src/test/java/com/binance/api/examples/MarginAccountEndpointsExampleAsync.java b/src/test/java/com/binance/api/examples/MarginAccountEndpointsExampleAsync.java index 07c3507be..5f75a3ac2 100755 --- a/src/test/java/com/binance/api/examples/MarginAccountEndpointsExampleAsync.java +++ b/src/test/java/com/binance/api/examples/MarginAccountEndpointsExampleAsync.java @@ -1,10 +1,9 @@ package com.binance.api.examples; import com.binance.api.client.BinanceApiAsyncMarginRestClient; -import com.binance.api.client.BinanceApiCallback; import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.domain.TransferType; -import com.binance.api.client.domain.account.MarginTransaction; +import com.binance.api.examples.constants.PrivateConfig; /** * Examples on how to get margin account information asynchronously. @@ -12,7 +11,7 @@ public class MarginAccountEndpointsExampleAsync { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiAsyncMarginRestClient client = factory.newAsyncMarginRestClient(); // Get account balances @@ -23,9 +22,7 @@ public static void main(String[] args) { }); // Get list of trades - client.getMyTrades("NEOETH", myTrades -> { - System.out.println(myTrades); - }); + client.getMyTrades("NEOETH", System.out::println); // Transfer, borrow, repay client.transfer("USDT", "1", TransferType.SPOT_TO_MARGIN, transaction -> System.out.println(transaction.getTranId())); diff --git a/src/test/java/com/binance/api/examples/MarginAccountEndpointsLoanQueryExample.java b/src/test/java/com/binance/api/examples/MarginAccountEndpointsLoanQueryExample.java index 4cc4bf01f..03390580d 100755 --- a/src/test/java/com/binance/api/examples/MarginAccountEndpointsLoanQueryExample.java +++ b/src/test/java/com/binance/api/examples/MarginAccountEndpointsLoanQueryExample.java @@ -5,6 +5,7 @@ import com.binance.api.client.domain.account.MarginTransaction; import com.binance.api.client.domain.account.MaxBorrowableQueryResult; import com.binance.api.client.domain.account.RepayQueryResult; +import com.binance.api.examples.constants.PrivateConfig; /** * Examples on how to get margin account information. @@ -12,7 +13,7 @@ public class MarginAccountEndpointsLoanQueryExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiMarginRestClient client = factory.newMarginRestClient(); MaxBorrowableQueryResult usdt = client.queryMaxBorrowable("USDT"); System.out.println(usdt.getAmount()); diff --git a/src/test/java/com/binance/api/examples/MarginOrdersExample.java b/src/test/java/com/binance/api/examples/MarginOrdersExample.java index 969ca784f..28d975316 100755 --- a/src/test/java/com/binance/api/examples/MarginOrdersExample.java +++ b/src/test/java/com/binance/api/examples/MarginOrdersExample.java @@ -11,6 +11,7 @@ import com.binance.api.client.domain.account.request.OrderRequest; import com.binance.api.client.domain.account.request.OrderStatusRequest; import com.binance.api.client.exception.BinanceApiException; +import com.binance.api.examples.constants.PrivateConfig; import java.util.List; @@ -22,7 +23,7 @@ public class MarginOrdersExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiMarginRestClient client = factory.newMarginRestClient(); // Getting list of open orders diff --git a/src/test/java/com/binance/api/examples/MarginOrdersExampleAsync.java b/src/test/java/com/binance/api/examples/MarginOrdersExampleAsync.java index b07a01792..611a7c210 100755 --- a/src/test/java/com/binance/api/examples/MarginOrdersExampleAsync.java +++ b/src/test/java/com/binance/api/examples/MarginOrdersExampleAsync.java @@ -6,6 +6,7 @@ import com.binance.api.client.domain.account.request.CancelOrderRequest; import com.binance.api.client.domain.account.request.OrderRequest; import com.binance.api.client.domain.account.request.OrderStatusRequest; +import com.binance.api.examples.constants.PrivateConfig; import static com.binance.api.client.domain.account.MarginNewOrder.limitBuy; @@ -15,22 +16,19 @@ public class MarginOrdersExampleAsync { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiAsyncMarginRestClient client = factory.newAsyncMarginRestClient(); // Getting list of open orders - client.getOpenOrders(new OrderRequest("LINKETH"), response -> System.out.println(response)); + client.getOpenOrders(new OrderRequest("LINKETH"), System.out::println); // Get status of a particular order - client.getOrderStatus(new OrderStatusRequest("LINKETH", 745262L), - response -> System.out.println(response)); + client.getOrderStatus(new OrderStatusRequest("LINKETH", 745262L), System.out::println); // Canceling an order - client.cancelOrder(new CancelOrderRequest("LINKETH", 756703L), - response -> System.out.println(response)); + client.cancelOrder(new CancelOrderRequest("LINKETH", 756703L), System.out::println); // Placing a real LIMIT order - client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"), - response -> System.out.println(response)); + client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"), System.out::println); } } diff --git a/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java b/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java index 6a27a5fa4..0db88cc5e 100755 --- a/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/MarginUserDataStreamExample.java @@ -2,11 +2,11 @@ import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.BinanceApiMarginRestClient; -import com.binance.api.client.BinanceApiRestClient; import com.binance.api.client.BinanceApiWebSocketClient; import com.binance.api.client.domain.event.AccountUpdateEvent; import com.binance.api.client.domain.event.OrderTradeUpdateEvent; import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType; +import com.binance.api.examples.constants.PrivateConfig; /** * User data stream endpoints examples. @@ -17,7 +17,7 @@ public class MarginUserDataStreamExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiMarginRestClient client = factory.newMarginRestClient(); // First, we obtain a listenKey which is required to interact with the user data stream diff --git a/src/test/java/com/binance/api/examples/MarketDataEndpointsExample.java b/src/test/java/com/binance/api/examples/MarketDataEndpointsExample.java index 592aba46a..a2971bf1d 100755 --- a/src/test/java/com/binance/api/examples/MarketDataEndpointsExample.java +++ b/src/test/java/com/binance/api/examples/MarketDataEndpointsExample.java @@ -2,13 +2,7 @@ import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.BinanceApiRestClient; -import com.binance.api.client.domain.market.AggTrade; -import com.binance.api.client.domain.market.BookTicker; -import com.binance.api.client.domain.market.Candlestick; -import com.binance.api.client.domain.market.CandlestickInterval; -import com.binance.api.client.domain.market.OrderBook; -import com.binance.api.client.domain.market.TickerPrice; -import com.binance.api.client.domain.market.TickerStatistics; +import com.binance.api.client.domain.market.*; import com.binance.api.client.exception.BinanceApiException; import java.util.List; diff --git a/src/test/java/com/binance/api/examples/MarketDataEndpointsExampleAsync.java b/src/test/java/com/binance/api/examples/MarketDataEndpointsExampleAsync.java index f27fab7ff..67c22e859 100755 --- a/src/test/java/com/binance/api/examples/MarketDataEndpointsExampleAsync.java +++ b/src/test/java/com/binance/api/examples/MarketDataEndpointsExampleAsync.java @@ -2,16 +2,10 @@ import com.binance.api.client.BinanceApiAsyncRestClient; import com.binance.api.client.BinanceApiClientFactory; -import com.binance.api.client.domain.market.AggTrade; -import com.binance.api.client.domain.market.Candlestick; import com.binance.api.client.domain.market.CandlestickInterval; import com.binance.api.client.domain.market.OrderBook; -import com.binance.api.client.domain.market.TickerPrice; -import com.binance.api.client.domain.market.TickerStatistics; import com.binance.api.client.exception.BinanceApiException; -import java.util.List; - /** * Examples on how to get market data information such as the latest price of a symbol, etc., in an async way. */ @@ -27,28 +21,24 @@ public static void main(String[] args) { }); // Getting latest price of a symbol (async) - client.get24HrPriceStatistics("NEOETH", (TickerStatistics response) -> { - System.out.println(response); - }); + client.get24HrPriceStatistics("NEOETH", System.out::println); // Getting all latest prices (async) - client.getAllPrices((List response) -> { - System.out.println(response); - }); + client.getAllPrices(System.out::println); // Getting agg trades (async) - client.getAggTrades("NEOETH", (List response) -> System.out.println(response)); + client.getAggTrades("NEOETH", System.out::println); // Weekly candlestick bars for a symbol client.getCandlestickBars("NEOETH", CandlestickInterval.WEEKLY, - (List response) -> System.out.println(response)); + System.out::println); // Book tickers (async) - client.getBookTickers(response -> System.out.println(response)); + client.getBookTickers(System.out::println); // Exception handling try { - client.getOrderBook("UNKNOWN", 10, response -> System.out.println(response)); + client.getOrderBook("UNKNOWN", 10, System.out::println); } catch (BinanceApiException e) { System.out.println(e.getError().getCode()); // -1121 System.out.println(e.getError().getMsg()); // Invalid symbol diff --git a/src/test/java/com/binance/api/examples/MarketDataStreamExample.java b/src/test/java/com/binance/api/examples/MarketDataStreamExample.java index 898d49b10..502b89606 100755 --- a/src/test/java/com/binance/api/examples/MarketDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/MarketDataStreamExample.java @@ -17,12 +17,12 @@ public static void main(String[] args) throws InterruptedException, IOException BinanceApiWebSocketClient client = BinanceApiClientFactory.newInstance().newWebSocketClient(); // Listen for aggregated trade events for ETH/BTC - client.onAggTradeEvent("ethbtc", response -> System.out.println(response)); + client.onAggTradeEvent("ethbtc", System.out::println); // Listen for changes in the order book in ETH/BTC - client.onDepthEvent("ethbtc", response -> System.out.println(response)); + client.onDepthEvent("ethbtc", System.out::println); // Obtain 1m candlesticks in real-time for ETH/BTC - client.onCandlestickEvent("ethbtc", CandlestickInterval.ONE_MINUTE, response -> System.out.println(response)); + client.onCandlestickEvent("ethbtc", CandlestickInterval.ONE_MINUTE, System.out::println); } } diff --git a/src/test/java/com/binance/api/examples/OrdersExample.java b/src/test/java/com/binance/api/examples/OrdersExample.java index a1dd9d6cd..0a8854824 100755 --- a/src/test/java/com/binance/api/examples/OrdersExample.java +++ b/src/test/java/com/binance/api/examples/OrdersExample.java @@ -6,12 +6,9 @@ import com.binance.api.client.domain.account.NewOrderResponse; import com.binance.api.client.domain.account.NewOrderResponseType; import com.binance.api.client.domain.account.Order; -import com.binance.api.client.domain.account.request.AllOrdersRequest; -import com.binance.api.client.domain.account.request.CancelOrderRequest; -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.domain.account.request.*; import com.binance.api.client.exception.BinanceApiException; +import com.binance.api.examples.constants.PrivateConfig; import java.util.List; @@ -24,7 +21,7 @@ public class OrdersExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiRestClient client = factory.newRestClient(); // Getting list of open orders @@ -41,7 +38,7 @@ public static void main(String[] args) { // Canceling an order try { - CancelOrderResponse cancelOrderResponse = client.cancelOrder(new CancelOrderRequest("LINKETH", 756762l)); + CancelOrderResponse cancelOrderResponse = client.cancelOrder(new CancelOrderRequest("LINKETH", 756762L)); System.out.println(cancelOrderResponse); } catch (BinanceApiException e) { System.out.println(e.getError().getMsg()); diff --git a/src/test/java/com/binance/api/examples/OrdersExampleAsync.java b/src/test/java/com/binance/api/examples/OrdersExampleAsync.java index 776e694a6..3baafab22 100755 --- a/src/test/java/com/binance/api/examples/OrdersExampleAsync.java +++ b/src/test/java/com/binance/api/examples/OrdersExampleAsync.java @@ -7,6 +7,7 @@ import com.binance.api.client.domain.account.request.CancelOrderRequest; import com.binance.api.client.domain.account.request.OrderRequest; import com.binance.api.client.domain.account.request.OrderStatusRequest; +import com.binance.api.examples.constants.PrivateConfig; import static com.binance.api.client.domain.account.NewOrder.limitBuy; import static com.binance.api.client.domain.account.NewOrder.marketBuy; @@ -17,22 +18,22 @@ public class OrdersExampleAsync { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiAsyncRestClient client = factory.newAsyncRestClient(); // Getting list of open orders - client.getOpenOrders(new OrderRequest("LINKETH"), response -> System.out.println(response)); + client.getOpenOrders(new OrderRequest("LINKETH"), System.out::println); // Get status of a particular order client.getOrderStatus(new OrderStatusRequest("LINKETH", 745262L), - response -> System.out.println(response)); + System.out::println); // Getting list of all orders with a limit of 10 - client.getAllOrders(new AllOrdersRequest("LINKETH").limit(10), response -> System.out.println(response)); + client.getAllOrders(new AllOrdersRequest("LINKETH").limit(10), System.out::println); // Canceling an order client.cancelOrder(new CancelOrderRequest("LINKETH", 756703L), - response -> System.out.println(response)); + System.out::println); // Placing a test LIMIT order client.newOrderTest(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"), @@ -42,7 +43,6 @@ public static void main(String[] args) { client.newOrderTest(marketBuy("LINKETH", "1000"), response -> System.out.println("Test order has succeeded.")); // Placing a real LIMIT order - client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"), - response -> System.out.println(response)); + client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"), System.out::println); } } diff --git a/src/test/java/com/binance/api/examples/SwapEndpointExample.java b/src/test/java/com/binance/api/examples/SwapEndpointExample.java index c4c32d98d..bc1c6829c 100755 --- a/src/test/java/com/binance/api/examples/SwapEndpointExample.java +++ b/src/test/java/com/binance/api/examples/SwapEndpointExample.java @@ -3,17 +3,15 @@ import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.BinanceApiSwapRestClient; import com.binance.api.client.domain.account.*; +import com.binance.api.examples.constants.PrivateConfig; import java.util.List; public class SwapEndpointExample { - public static String API_KEY = "api-key"; - public static String SECRET_KEY = "secret-key"; - public static void main(String[] args) { - BinanceApiClientFactory binanceApiClientFactory = BinanceApiClientFactory.newInstance(API_KEY, SECRET_KEY); + BinanceApiClientFactory binanceApiClientFactory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiSwapRestClient swapClient = binanceApiClientFactory.newSwapRestClient(); List pools = swapClient.listAllSwapPools(); for(Pool pool:pools) { diff --git a/src/test/java/com/binance/api/examples/TotalAccountBalanceExample.java b/src/test/java/com/binance/api/examples/TotalAccountBalanceExample.java index 5abfe04f1..fc4d3fcc1 100755 --- a/src/test/java/com/binance/api/examples/TotalAccountBalanceExample.java +++ b/src/test/java/com/binance/api/examples/TotalAccountBalanceExample.java @@ -5,6 +5,7 @@ import com.binance.api.client.constant.Util; import com.binance.api.client.domain.account.Account; import com.binance.api.client.domain.account.AssetBalance; +import com.binance.api.examples.constants.PrivateConfig; /** * Example how to get total of balances on your account @@ -13,7 +14,7 @@ public class TotalAccountBalanceExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiRestClient client = factory.newRestClient(); // Get account balances diff --git a/src/test/java/com/binance/api/examples/UserDataStreamExample.java b/src/test/java/com/binance/api/examples/UserDataStreamExample.java index b74eed727..652e77da9 100755 --- a/src/test/java/com/binance/api/examples/UserDataStreamExample.java +++ b/src/test/java/com/binance/api/examples/UserDataStreamExample.java @@ -6,6 +6,7 @@ import com.binance.api.client.domain.event.AccountUpdateEvent; import com.binance.api.client.domain.event.OrderTradeUpdateEvent; import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType; +import com.binance.api.examples.constants.PrivateConfig; /** * User data stream endpoints examples. @@ -16,7 +17,7 @@ public class UserDataStreamExample { public static void main(String[] args) { - BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET"); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY); BinanceApiRestClient client = factory.newRestClient(); // First, we obtain a listenKey which is required to interact with the user data stream diff --git a/src/test/java/com/binance/api/examples/constants/PrivateConfig.java b/src/test/java/com/binance/api/examples/constants/PrivateConfig.java new file mode 100644 index 000000000..32fe556a0 --- /dev/null +++ b/src/test/java/com/binance/api/examples/constants/PrivateConfig.java @@ -0,0 +1,6 @@ +package com.binance.api.examples.constants; + +public class PrivateConfig { + public static final String API_KEY = "XXX"; + public static final String SECRET_KEY = "XXX"; +}