Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Refactor examples and README #422

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ 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:

1. [`BinanceApiRestClient`](https://github.com/joaopsilva/binance-java-api/blob/master/src/main/java/com/binance/api/client/BinanceApiRestClient.java), a synchronous/blocking [Binance API](https://www.binance.com/restapipub.html) client;
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Trade> trades(final Trade... trades) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
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;

import java.io.IOException;
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
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.
*/
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 -> {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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.
*/
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
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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.
*/
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading