From f4ee92d94a6b138344c742edaee63ed360b4f4da Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Wed, 20 Dec 2023 14:06:01 +0300 Subject: [PATCH 1/9] binance dapi quick fix --- .../main/java/org/knowm/xchange/binance/BinanceExchange.java | 3 ++- .../org/knowm/xchange/binance/service/BinanceBaseService.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java index 9afe7f6a97c..2e9c09400b7 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java @@ -17,9 +17,10 @@ public class BinanceExchange extends BaseExchange implements Exchange { public static final String SPECIFIC_PARAM_USE_SANDBOX = "Use_Sandbox"; public static final String SPECIFIC_PARAM_USE_FUTURES_SANDBOX = "Use_Sandbox_Futures"; public static final String SPECIFIC_PARAM_FUTURES_ENABLED = "Futures_Enabled"; + public static final String SPECIFIC_PARAM_INVERSE_FUTURES_ENABLED = "Futures_Enabled"; public static final String SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED = "Portfolio_Margin_Enabled"; private static final String SPOT_URL = "https://api.binance.com"; - public static final String FUTURES_URL = "https://dapi.binance.com"; + public static final String FUTURES_URL = "https://fapi.binance.com"; public static final String INVERSE_FUTURES_URL = "https://dapi.binance.com"; public static final String PORTFOLIO_MARGIN_URL = "https://papi.binance.com"; diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java index 9c7e03d65c9..6065848e4be 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java @@ -47,7 +47,7 @@ protected BinanceBaseService( inverseFuturesSpec.setSslUri( (exchange.usingSandbox()) ? BinanceExchange.SANDBOX_FUTURES_URL - : BinanceExchange.INVERSE_FUTURES_URL); + : BinanceExchange.FUTURES_URL); this.inverseBinanceFutures = ExchangeRestProxyBuilder.forInterface( BinanceFuturesAuthenticated.class, inverseFuturesSpec) From 7e7fb54baccf8040935a3d1def5ba74de72e226d Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Mon, 6 May 2024 19:18:51 +0300 Subject: [PATCH 2/9] [binance] fix dapi error and rework binance connections --- .../xchange/binance/BinanceExchange.java | 100 ++++++++++-------- .../xchange/binance/BinanceUsExchange.java | 2 + .../xchange/binance/dto/ExchangeType.java | 12 +++ .../service/BinanceAccountService.java | 21 ++-- .../binance/service/BinanceBaseService.java | 56 +++++----- .../binance/BinanceExchangeTypeTest.java | 59 +++++++++++ .../xchange/binance/BinanceFutureTest.java | 15 +-- .../BinanceFuturesPublicStreamsTest.java | 12 ++- .../BinanceLiveFutureSubscriptionExample.java | 5 + .../BinanceStreamExchangeTypeTest.java | 82 ++++++++++++++ 10 files changed, 276 insertions(+), 88 deletions(-) create mode 100644 xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java create mode 100644 xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java create mode 100644 xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java index 2e9c09400b7..c7232ea5275 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java @@ -1,30 +1,33 @@ package org.knowm.xchange.binance; +import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; + import java.util.Map; import org.knowm.xchange.BaseExchange; import org.knowm.xchange.Exchange; import org.knowm.xchange.ExchangeSpecification; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.account.AssetDetail; import org.knowm.xchange.binance.service.BinanceAccountService; import org.knowm.xchange.binance.service.BinanceMarketDataService; import org.knowm.xchange.binance.service.BinanceTradeService; +import org.knowm.xchange.client.ExchangeRestProxyBuilder; import org.knowm.xchange.client.ResilienceRegistries; import org.knowm.xchange.exceptions.ExchangeException; import org.knowm.xchange.utils.AuthUtils; import si.mazi.rescu.SynchronizedValueFactory; public class BinanceExchange extends BaseExchange implements Exchange { - public static final String SPECIFIC_PARAM_USE_SANDBOX = "Use_Sandbox"; - public static final String SPECIFIC_PARAM_USE_FUTURES_SANDBOX = "Use_Sandbox_Futures"; - public static final String SPECIFIC_PARAM_FUTURES_ENABLED = "Futures_Enabled"; - public static final String SPECIFIC_PARAM_INVERSE_FUTURES_ENABLED = "Futures_Enabled"; - public static final String SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED = "Portfolio_Margin_Enabled"; + public static String EXCHANGE_TYPE = "Exchange_Type"; private static final String SPOT_URL = "https://api.binance.com"; public static final String FUTURES_URL = "https://fapi.binance.com"; public static final String INVERSE_FUTURES_URL = "https://dapi.binance.com"; public static final String PORTFOLIO_MARGIN_URL = "https://papi.binance.com"; + public static final String SANDBOX_SPOT_URL = "https://testnet.binance.vision"; public static final String SANDBOX_FUTURES_URL = "https://testnet.binancefuture.com"; + public static final String SANDBOX_INVERSE_FUTURES_URL = "https://testnet.binancefuture.com"; + protected static ResilienceRegistries RESILIENCE_REGISTRIES; protected SynchronizedValueFactory timestampFactory; @@ -62,13 +65,14 @@ public ResilienceRegistries getResilienceRegistries() { @Override public ExchangeSpecification getDefaultExchangeSpecification() { - ExchangeSpecification spec = new ExchangeSpecification(this.getClass()); spec.setSslUri(SPOT_URL); spec.setHost("www.binance.com"); spec.setPort(80); spec.setExchangeName("Binance"); spec.setExchangeDescription("Binance Exchange."); + spec.setExchangeSpecificParametersItem(EXCHANGE_TYPE, SPOT); + spec.setExchangeSpecificParametersItem(USE_SANDBOX, false); AuthUtils.setApiAndSecretKey(spec, "binance"); return spec; } @@ -79,21 +83,14 @@ public void applySpecification(ExchangeSpecification exchangeSpecification) { super.applySpecification(exchangeSpecification); } - public boolean isFuturesSandbox() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_USE_FUTURES_SANDBOX)); - } - public boolean isFuturesEnabled() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem(SPECIFIC_PARAM_FUTURES_ENABLED)); + return ExchangeType.FUTURES.equals(exchangeSpecification. + getExchangeSpecificParametersItem(EXCHANGE_TYPE)); } public boolean isPortfolioMarginEnabled() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED)); + return ExchangeType.PORTFOLIO_MARGIN.equals(exchangeSpecification + .getExchangeSpecificParametersItem(EXCHANGE_TYPE)); } public boolean usingSandbox() { @@ -102,35 +99,35 @@ public boolean usingSandbox() { @Override public void remoteInit() { - try { BinanceMarketDataService marketDataService = (BinanceMarketDataService) this.marketDataService; - BinanceAccountService accountService = (BinanceAccountService) getAccountService(); Map assetDetailMap = null; if (!usingSandbox() && isAuthenticated()) { assetDetailMap = accountService.getAssetDetails(); // not available in sndbox } - if (usingSandbox()) { - if (isFuturesSandbox()) { - BinanceAdapters.adaptFutureExchangeMetaData( - exchangeMetaData, marketDataService.getFutureExchangeInfo()); - } else { - exchangeMetaData = - BinanceAdapters.adaptExchangeMetaData( - marketDataService.getExchangeInfo(), assetDetailMap); - } - } else { + //hook for Binance US + if(exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE) ==null) { exchangeMetaData = BinanceAdapters.adaptExchangeMetaData( marketDataService.getExchangeInfo(), assetDetailMap); - if (isFuturesEnabled()) { - BinanceAdapters.adaptFutureExchangeMetaData( - exchangeMetaData, marketDataService.getFutureExchangeInfo()); + } else { + switch ((ExchangeType) exchangeSpecification.getExchangeSpecificParametersItem( + EXCHANGE_TYPE)) { + case SPOT: { + exchangeMetaData = + BinanceAdapters.adaptExchangeMetaData( + marketDataService.getExchangeInfo(), assetDetailMap); + break; + } + case FUTURES: { + BinanceAdapters.adaptFutureExchangeMetaData( + exchangeMetaData, marketDataService.getFutureExchangeInfo()); + break; + } } } - } catch (Exception e) { throw new ExchangeException("Failed to initialize: " + e.getMessage(), e); } @@ -142,21 +139,38 @@ protected boolean isAuthenticated() { && exchangeSpecification.getSecretKey() != null; } - /** Adjust host parameters depending on exchange specific parameters */ + /** + * Adjust host parameters depending on exchange specific parameters + */ private static void concludeHostParams(ExchangeSpecification exchangeSpecification) { - if (exchangeSpecification.getExchangeSpecificParameters() != null) { - if (enabledSandbox(exchangeSpecification)) { - exchangeSpecification.setSslUri("https://testnet.binance.vision"); - exchangeSpecification.setHost("testnet.binance.vision"); + if(exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE) != null) { + switch ((ExchangeType)exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE)) { + case SPOT: { + if (enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(SANDBOX_SPOT_URL); + break; + } + case FUTURES: { + if (!enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(FUTURES_URL); + else + exchangeSpecification.setSslUri(SANDBOX_FUTURES_URL); + break; + } + case INVERSE: { + if (!enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(INVERSE_FUTURES_URL); + else + exchangeSpecification.setSslUri(SANDBOX_INVERSE_FUTURES_URL); + break; + } } } } + private static boolean enabledSandbox(ExchangeSpecification exchangeSpecification) { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem(SPECIFIC_PARAM_USE_SANDBOX)) - || Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_USE_FUTURES_SANDBOX)); + return Boolean.TRUE.equals(exchangeSpecification.getExchangeSpecificParametersItem(USE_SANDBOX)); } + } diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java index b0caf8d8d78..d731692c18a 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java @@ -1,5 +1,7 @@ package org.knowm.xchange.binance; +import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; + import java.io.IOException; import org.knowm.xchange.ExchangeSpecification; import org.knowm.xchange.binance.service.BinanceMarketDataService; diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java new file mode 100644 index 00000000000..ed0a05cdd1f --- /dev/null +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java @@ -0,0 +1,12 @@ +package org.knowm.xchange.binance.dto; + +import lombok.Getter; + +@Getter +public enum ExchangeType { + SPOT, + FUTURES, + INVERSE, + PORTFOLIO_MARGIN; +} + diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java index 8ce0795c9f8..9483cab8ae0 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java @@ -1,5 +1,7 @@ package org.knowm.xchange.binance.service; +import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; + import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; @@ -9,6 +11,7 @@ import org.knowm.xchange.binance.BinanceErrorAdapter; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.dto.BinanceException; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.account.*; import org.knowm.xchange.binance.dto.account.BinanceMasterAccountTransferHistoryParams; import org.knowm.xchange.binance.dto.account.BinanceSubAccountTransferHistoryParams; @@ -91,25 +94,19 @@ public AccountInfo getAccountInfo() throws IOException { try { List wallets = new ArrayList<>(); List openPositions = new ArrayList<>(); - - if (exchange.usingSandbox()) { - if (exchange.isFuturesSandbox()) { - BinanceFutureAccountInformation futureAccountInformation = futuresAccount(); - wallets.add(BinanceAdapters.adaptBinanceFutureWallet(futureAccountInformation)); - openPositions.addAll( - BinanceAdapters.adaptOpenPositions(futureAccountInformation.getPositions())); - - } else { + switch (exchange.getExchangeSpecification().getExchangeSpecificParametersItem( + EXCHANGE_TYPE).toString()) { + case "SPOT": { wallets.add(BinanceAdapters.adaptBinanceSpotWallet(account())); + break; } - } else { - if (exchange.isFuturesEnabled()) { + case "FUTURES": { BinanceFutureAccountInformation futureAccountInformation = futuresAccount(); wallets.add(BinanceAdapters.adaptBinanceFutureWallet(futureAccountInformation)); openPositions.addAll( BinanceAdapters.adaptOpenPositions(futureAccountInformation.getPositions())); + break; } - wallets.add(BinanceAdapters.adaptBinanceSpotWallet(account())); } return new AccountInfo( exchange.getExchangeSpecification().getUserName(), diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java index 6065848e4be..6968dcf5b06 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java @@ -1,5 +1,6 @@ package org.knowm.xchange.binance.service; +import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; import static org.knowm.xchange.binance.BinanceResilience.REQUEST_WEIGHT_RATE_LIMITER; import java.io.IOException; @@ -7,6 +8,7 @@ import org.knowm.xchange.binance.BinanceAuthenticated; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.BinanceFuturesAuthenticated; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.meta.BinanceSystemStatus; import org.knowm.xchange.binance.dto.meta.exchangeinfo.BinanceExchangeInfo; import org.knowm.xchange.client.ExchangeRestProxyBuilder; @@ -23,8 +25,8 @@ public class BinanceBaseService extends BaseResilientExchangeService System.out.printf("last price: %s%n", t.getLast())); + Thread.sleep(3000L); + disposable.dispose(); + Thread.sleep(500L); + exchange.disconnect().blockingAwait(); + } + + private static ExchangeSpecification getSpec(ExchangeType exchangeType, boolean useSandbox) { + ExchangeSpecification exchangeSpecification; + if (exchangeType == SPOT) { + exchangeSpecification = new BinanceStreamingExchange().getDefaultExchangeSpecification(); + } else { + exchangeSpecification = new BinanceFutureStreamingExchange().getDefaultExchangeSpecification(); + } + exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); + if (useSandbox) { + exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); + } + return exchangeSpecification; + } + + private static ExchangeSpecification getSpec1(ExchangeType exchangeType, boolean useSandbox) { + ExchangeSpecification exchangeSpecification; + if (exchangeType == SPOT) { + exchangeSpecification = + new ExchangeSpecification(BinanceStreamingExchange.class); + } else { + exchangeSpecification = + new ExchangeSpecification(BinanceFutureStreamingExchange.class); + } + exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); + if (useSandbox) { + exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); + } + return exchangeSpecification; + } +} From 58274ffe58cecdbb97376e62693fecb158e9bf20 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Wed, 20 Dec 2023 14:06:01 +0300 Subject: [PATCH 3/9] binance dapi quick fix --- .../main/java/org/knowm/xchange/binance/BinanceExchange.java | 3 ++- .../org/knowm/xchange/binance/service/BinanceBaseService.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java index 9afe7f6a97c..2e9c09400b7 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java @@ -17,9 +17,10 @@ public class BinanceExchange extends BaseExchange implements Exchange { public static final String SPECIFIC_PARAM_USE_SANDBOX = "Use_Sandbox"; public static final String SPECIFIC_PARAM_USE_FUTURES_SANDBOX = "Use_Sandbox_Futures"; public static final String SPECIFIC_PARAM_FUTURES_ENABLED = "Futures_Enabled"; + public static final String SPECIFIC_PARAM_INVERSE_FUTURES_ENABLED = "Futures_Enabled"; public static final String SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED = "Portfolio_Margin_Enabled"; private static final String SPOT_URL = "https://api.binance.com"; - public static final String FUTURES_URL = "https://dapi.binance.com"; + public static final String FUTURES_URL = "https://fapi.binance.com"; public static final String INVERSE_FUTURES_URL = "https://dapi.binance.com"; public static final String PORTFOLIO_MARGIN_URL = "https://papi.binance.com"; diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java index 9c7e03d65c9..6065848e4be 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java @@ -47,7 +47,7 @@ protected BinanceBaseService( inverseFuturesSpec.setSslUri( (exchange.usingSandbox()) ? BinanceExchange.SANDBOX_FUTURES_URL - : BinanceExchange.INVERSE_FUTURES_URL); + : BinanceExchange.FUTURES_URL); this.inverseBinanceFutures = ExchangeRestProxyBuilder.forInterface( BinanceFuturesAuthenticated.class, inverseFuturesSpec) From 3626ebd329671f997ccdc5d46ad62eb40f205124 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Mon, 6 May 2024 19:18:51 +0300 Subject: [PATCH 4/9] [binance] fix dapi error and rework binance connections --- .../xchange/binance/BinanceExchange.java | 100 ++++++++++-------- .../xchange/binance/BinanceUsExchange.java | 2 + .../xchange/binance/dto/ExchangeType.java | 12 +++ .../service/BinanceAccountService.java | 21 ++-- .../binance/service/BinanceBaseService.java | 56 +++++----- .../binance/BinanceExchangeTypeTest.java | 59 +++++++++++ .../xchange/binance/BinanceFutureTest.java | 15 +-- .../BinanceFuturesPublicStreamsTest.java | 12 ++- .../BinanceLiveFutureSubscriptionExample.java | 5 + .../BinanceStreamExchangeTypeTest.java | 82 ++++++++++++++ 10 files changed, 276 insertions(+), 88 deletions(-) create mode 100644 xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java create mode 100644 xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java create mode 100644 xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java index 2e9c09400b7..c7232ea5275 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceExchange.java @@ -1,30 +1,33 @@ package org.knowm.xchange.binance; +import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; + import java.util.Map; import org.knowm.xchange.BaseExchange; import org.knowm.xchange.Exchange; import org.knowm.xchange.ExchangeSpecification; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.account.AssetDetail; import org.knowm.xchange.binance.service.BinanceAccountService; import org.knowm.xchange.binance.service.BinanceMarketDataService; import org.knowm.xchange.binance.service.BinanceTradeService; +import org.knowm.xchange.client.ExchangeRestProxyBuilder; import org.knowm.xchange.client.ResilienceRegistries; import org.knowm.xchange.exceptions.ExchangeException; import org.knowm.xchange.utils.AuthUtils; import si.mazi.rescu.SynchronizedValueFactory; public class BinanceExchange extends BaseExchange implements Exchange { - public static final String SPECIFIC_PARAM_USE_SANDBOX = "Use_Sandbox"; - public static final String SPECIFIC_PARAM_USE_FUTURES_SANDBOX = "Use_Sandbox_Futures"; - public static final String SPECIFIC_PARAM_FUTURES_ENABLED = "Futures_Enabled"; - public static final String SPECIFIC_PARAM_INVERSE_FUTURES_ENABLED = "Futures_Enabled"; - public static final String SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED = "Portfolio_Margin_Enabled"; + public static String EXCHANGE_TYPE = "Exchange_Type"; private static final String SPOT_URL = "https://api.binance.com"; public static final String FUTURES_URL = "https://fapi.binance.com"; public static final String INVERSE_FUTURES_URL = "https://dapi.binance.com"; public static final String PORTFOLIO_MARGIN_URL = "https://papi.binance.com"; + public static final String SANDBOX_SPOT_URL = "https://testnet.binance.vision"; public static final String SANDBOX_FUTURES_URL = "https://testnet.binancefuture.com"; + public static final String SANDBOX_INVERSE_FUTURES_URL = "https://testnet.binancefuture.com"; + protected static ResilienceRegistries RESILIENCE_REGISTRIES; protected SynchronizedValueFactory timestampFactory; @@ -62,13 +65,14 @@ public ResilienceRegistries getResilienceRegistries() { @Override public ExchangeSpecification getDefaultExchangeSpecification() { - ExchangeSpecification spec = new ExchangeSpecification(this.getClass()); spec.setSslUri(SPOT_URL); spec.setHost("www.binance.com"); spec.setPort(80); spec.setExchangeName("Binance"); spec.setExchangeDescription("Binance Exchange."); + spec.setExchangeSpecificParametersItem(EXCHANGE_TYPE, SPOT); + spec.setExchangeSpecificParametersItem(USE_SANDBOX, false); AuthUtils.setApiAndSecretKey(spec, "binance"); return spec; } @@ -79,21 +83,14 @@ public void applySpecification(ExchangeSpecification exchangeSpecification) { super.applySpecification(exchangeSpecification); } - public boolean isFuturesSandbox() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_USE_FUTURES_SANDBOX)); - } - public boolean isFuturesEnabled() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem(SPECIFIC_PARAM_FUTURES_ENABLED)); + return ExchangeType.FUTURES.equals(exchangeSpecification. + getExchangeSpecificParametersItem(EXCHANGE_TYPE)); } public boolean isPortfolioMarginEnabled() { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_PORTFOLIO_MARGIN_ENABLED)); + return ExchangeType.PORTFOLIO_MARGIN.equals(exchangeSpecification + .getExchangeSpecificParametersItem(EXCHANGE_TYPE)); } public boolean usingSandbox() { @@ -102,35 +99,35 @@ public boolean usingSandbox() { @Override public void remoteInit() { - try { BinanceMarketDataService marketDataService = (BinanceMarketDataService) this.marketDataService; - BinanceAccountService accountService = (BinanceAccountService) getAccountService(); Map assetDetailMap = null; if (!usingSandbox() && isAuthenticated()) { assetDetailMap = accountService.getAssetDetails(); // not available in sndbox } - if (usingSandbox()) { - if (isFuturesSandbox()) { - BinanceAdapters.adaptFutureExchangeMetaData( - exchangeMetaData, marketDataService.getFutureExchangeInfo()); - } else { - exchangeMetaData = - BinanceAdapters.adaptExchangeMetaData( - marketDataService.getExchangeInfo(), assetDetailMap); - } - } else { + //hook for Binance US + if(exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE) ==null) { exchangeMetaData = BinanceAdapters.adaptExchangeMetaData( marketDataService.getExchangeInfo(), assetDetailMap); - if (isFuturesEnabled()) { - BinanceAdapters.adaptFutureExchangeMetaData( - exchangeMetaData, marketDataService.getFutureExchangeInfo()); + } else { + switch ((ExchangeType) exchangeSpecification.getExchangeSpecificParametersItem( + EXCHANGE_TYPE)) { + case SPOT: { + exchangeMetaData = + BinanceAdapters.adaptExchangeMetaData( + marketDataService.getExchangeInfo(), assetDetailMap); + break; + } + case FUTURES: { + BinanceAdapters.adaptFutureExchangeMetaData( + exchangeMetaData, marketDataService.getFutureExchangeInfo()); + break; + } } } - } catch (Exception e) { throw new ExchangeException("Failed to initialize: " + e.getMessage(), e); } @@ -142,21 +139,38 @@ protected boolean isAuthenticated() { && exchangeSpecification.getSecretKey() != null; } - /** Adjust host parameters depending on exchange specific parameters */ + /** + * Adjust host parameters depending on exchange specific parameters + */ private static void concludeHostParams(ExchangeSpecification exchangeSpecification) { - if (exchangeSpecification.getExchangeSpecificParameters() != null) { - if (enabledSandbox(exchangeSpecification)) { - exchangeSpecification.setSslUri("https://testnet.binance.vision"); - exchangeSpecification.setHost("testnet.binance.vision"); + if(exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE) != null) { + switch ((ExchangeType)exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE)) { + case SPOT: { + if (enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(SANDBOX_SPOT_URL); + break; + } + case FUTURES: { + if (!enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(FUTURES_URL); + else + exchangeSpecification.setSslUri(SANDBOX_FUTURES_URL); + break; + } + case INVERSE: { + if (!enabledSandbox(exchangeSpecification)) + exchangeSpecification.setSslUri(INVERSE_FUTURES_URL); + else + exchangeSpecification.setSslUri(SANDBOX_INVERSE_FUTURES_URL); + break; + } } } } + private static boolean enabledSandbox(ExchangeSpecification exchangeSpecification) { - return Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem(SPECIFIC_PARAM_USE_SANDBOX)) - || Boolean.TRUE.equals( - exchangeSpecification.getExchangeSpecificParametersItem( - SPECIFIC_PARAM_USE_FUTURES_SANDBOX)); + return Boolean.TRUE.equals(exchangeSpecification.getExchangeSpecificParametersItem(USE_SANDBOX)); } + } diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java index b0caf8d8d78..d731692c18a 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceUsExchange.java @@ -1,5 +1,7 @@ package org.knowm.xchange.binance; +import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; + import java.io.IOException; import org.knowm.xchange.ExchangeSpecification; import org.knowm.xchange.binance.service.BinanceMarketDataService; diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java new file mode 100644 index 00000000000..ed0a05cdd1f --- /dev/null +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/ExchangeType.java @@ -0,0 +1,12 @@ +package org.knowm.xchange.binance.dto; + +import lombok.Getter; + +@Getter +public enum ExchangeType { + SPOT, + FUTURES, + INVERSE, + PORTFOLIO_MARGIN; +} + diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java index 8ce0795c9f8..9483cab8ae0 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java @@ -1,5 +1,7 @@ package org.knowm.xchange.binance.service; +import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; + import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; @@ -9,6 +11,7 @@ import org.knowm.xchange.binance.BinanceErrorAdapter; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.dto.BinanceException; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.account.*; import org.knowm.xchange.binance.dto.account.BinanceMasterAccountTransferHistoryParams; import org.knowm.xchange.binance.dto.account.BinanceSubAccountTransferHistoryParams; @@ -91,25 +94,19 @@ public AccountInfo getAccountInfo() throws IOException { try { List wallets = new ArrayList<>(); List openPositions = new ArrayList<>(); - - if (exchange.usingSandbox()) { - if (exchange.isFuturesSandbox()) { - BinanceFutureAccountInformation futureAccountInformation = futuresAccount(); - wallets.add(BinanceAdapters.adaptBinanceFutureWallet(futureAccountInformation)); - openPositions.addAll( - BinanceAdapters.adaptOpenPositions(futureAccountInformation.getPositions())); - - } else { + switch (exchange.getExchangeSpecification().getExchangeSpecificParametersItem( + EXCHANGE_TYPE).toString()) { + case "SPOT": { wallets.add(BinanceAdapters.adaptBinanceSpotWallet(account())); + break; } - } else { - if (exchange.isFuturesEnabled()) { + case "FUTURES": { BinanceFutureAccountInformation futureAccountInformation = futuresAccount(); wallets.add(BinanceAdapters.adaptBinanceFutureWallet(futureAccountInformation)); openPositions.addAll( BinanceAdapters.adaptOpenPositions(futureAccountInformation.getPositions())); + break; } - wallets.add(BinanceAdapters.adaptBinanceSpotWallet(account())); } return new AccountInfo( exchange.getExchangeSpecification().getUserName(), diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java index 6065848e4be..6968dcf5b06 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceBaseService.java @@ -1,5 +1,6 @@ package org.knowm.xchange.binance.service; +import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; import static org.knowm.xchange.binance.BinanceResilience.REQUEST_WEIGHT_RATE_LIMITER; import java.io.IOException; @@ -7,6 +8,7 @@ import org.knowm.xchange.binance.BinanceAuthenticated; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.BinanceFuturesAuthenticated; +import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.meta.BinanceSystemStatus; import org.knowm.xchange.binance.dto.meta.exchangeinfo.BinanceExchangeInfo; import org.knowm.xchange.client.ExchangeRestProxyBuilder; @@ -23,8 +25,8 @@ public class BinanceBaseService extends BaseResilientExchangeService System.out.printf("last price: %s%n", t.getLast())); + Thread.sleep(3000L); + disposable.dispose(); + Thread.sleep(500L); + exchange.disconnect().blockingAwait(); + } + + private static ExchangeSpecification getSpec(ExchangeType exchangeType, boolean useSandbox) { + ExchangeSpecification exchangeSpecification; + if (exchangeType == SPOT) { + exchangeSpecification = new BinanceStreamingExchange().getDefaultExchangeSpecification(); + } else { + exchangeSpecification = new BinanceFutureStreamingExchange().getDefaultExchangeSpecification(); + } + exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); + if (useSandbox) { + exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); + } + return exchangeSpecification; + } + + private static ExchangeSpecification getSpec1(ExchangeType exchangeType, boolean useSandbox) { + ExchangeSpecification exchangeSpecification; + if (exchangeType == SPOT) { + exchangeSpecification = + new ExchangeSpecification(BinanceStreamingExchange.class); + } else { + exchangeSpecification = + new ExchangeSpecification(BinanceFutureStreamingExchange.class); + } + exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); + if (useSandbox) { + exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); + } + return exchangeSpecification; + } +} From 7ca211f876bb1872182219e61c7c20df83ad7d4e Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 10 May 2024 16:00:18 +0300 Subject: [PATCH 5/9] [binance] enum connections fix --- .../knowm/xchange/binance/service/BinanceMarketDataService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceMarketDataService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceMarketDataService.java index 29c61e91374..4f435bc382b 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceMarketDataService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceMarketDataService.java @@ -58,7 +58,7 @@ public Ticker getTicker(Instrument instrument, Object... args) throws IOExceptio @Override public List getTickers(Params params) throws IOException { try { - if (this.exchange.isFuturesEnabled() || this.exchange.isFuturesSandbox()) { + if (this.exchange.isFuturesEnabled()) { return ticker24hAllProducts(true).stream().map(f -> f.toTicker(true)) .collect(Collectors.toList()); } else { From b79b782b44b9f14bfbf4c2ae34788b2f2b98302b Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 10 May 2024 16:15:47 +0300 Subject: [PATCH 6/9] [binance-stream] revert @ignore due to http error --- .../binance/BinanceFuturesPublicStreamsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceFuturesPublicStreamsTest.java b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceFuturesPublicStreamsTest.java index e359f6dcd50..9c92813f087 100644 --- a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceFuturesPublicStreamsTest.java +++ b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceFuturesPublicStreamsTest.java @@ -1,9 +1,7 @@ package info.bitrich.xchangestream.binance; -import static info.bitrich.xchangestream.binance.BinanceStreamingExchange.USE_REALTIME_BOOK_TICKER; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; -import static org.knowm.xchange.binance.BinanceExchange.FUTURES_URL; import static org.knowm.xchange.binance.dto.ExchangeType.FUTURES; import info.bitrich.xchangestream.binancefuture.BinanceFutureStreamingExchange; @@ -20,6 +18,8 @@ import org.knowm.xchange.dto.meta.InstrumentMetaData; import org.knowm.xchange.instrument.Instrument; +// Github build give http 451 error(Unavailable For Legal Reasons) +@Ignore public class BinanceFuturesPublicStreamsTest { StreamingExchange exchange; From c1157acfe3f99db7c0bdb1a981e118ed2f8445f7 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 10 May 2024 18:52:46 +0300 Subject: [PATCH 7/9] [binance] convert example to test --- .../service/BinanceAccountService.java | 10 ++-- .../binance/BinanceExchangeTypeTest.java | 52 +++++++++++++------ .../BinanceStreamExchangeTypeTest.java | 46 +++++++++++----- 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java index 9483cab8ae0..02149f32047 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountService.java @@ -1,6 +1,8 @@ package org.knowm.xchange.binance.service; import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; +import static org.knowm.xchange.binance.dto.ExchangeType.FUTURES; +import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; import java.io.IOException; import java.math.BigDecimal; @@ -94,13 +96,13 @@ public AccountInfo getAccountInfo() throws IOException { try { List wallets = new ArrayList<>(); List openPositions = new ArrayList<>(); - switch (exchange.getExchangeSpecification().getExchangeSpecificParametersItem( - EXCHANGE_TYPE).toString()) { - case "SPOT": { + switch ((ExchangeType)exchange.getExchangeSpecification().getExchangeSpecificParametersItem( + EXCHANGE_TYPE)) { + case SPOT: { wallets.add(BinanceAdapters.adaptBinanceSpotWallet(account())); break; } - case "FUTURES": { + case FUTURES: { BinanceFutureAccountInformation futureAccountInformation = futuresAccount(); wallets.add(BinanceAdapters.adaptBinanceFutureWallet(futureAccountInformation)); openPositions.addAll( diff --git a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java index 0c9956f0f6a..dca16a376b3 100644 --- a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java +++ b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java @@ -1,11 +1,13 @@ package org.knowm.xchange.binance; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.knowm.xchange.Exchange.USE_SANDBOX; import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; import static org.knowm.xchange.binance.dto.ExchangeType.FUTURES; import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; import java.io.IOException; +import org.junit.Test; import org.knowm.xchange.Exchange; import org.knowm.xchange.ExchangeFactory; import org.knowm.xchange.ExchangeSpecification; @@ -13,32 +15,50 @@ import org.knowm.xchange.currency.CurrencyPair; import org.knowm.xchange.derivative.FuturesContract; import org.knowm.xchange.dto.marketdata.Ticker; +import org.knowm.xchange.dto.marketdata.Trade; +import org.knowm.xchange.dto.marketdata.Trades; import org.knowm.xchange.instrument.Instrument; public class BinanceExchangeTypeTest { - public static void main(String[] args) throws InterruptedException, IOException { - testConnection(new CurrencyPair("ETH/USDT"), SPOT, false); - testConnection(new CurrencyPair("ETH/USDT"), SPOT, true); - testConnection(new FuturesContract("ETH/USDT/PERP"), FUTURES, false); - testConnection(new FuturesContract("ETH/USDT/PERP"), FUTURES, true); + + @Test + public void testConnections() throws InterruptedException, IOException { + testConnection(new CurrencyPair("ETH/USDT"), getSpec1(SPOT, false)); + testConnection(new CurrencyPair("ETH/USDT"), getSpec(SPOT, true)); + testConnection(new FuturesContract("ETH/USDT/PERP"), getSpec(FUTURES, false)); + testConnection(new FuturesContract("ETH/USDT/PERP"), getSpec1(FUTURES, true)); } - private static void testConnection(Instrument instrument, ExchangeType exchangeType, - boolean useSandbox) + private static void testConnection(Instrument instrument, ExchangeSpecification spec) throws InterruptedException, IOException { Exchange exchange = - ExchangeFactory.INSTANCE.createExchange(getSpec(exchangeType, useSandbox)); -// another implementation -// StreamingExchange exchange = -// StreamingExchangeFactory.INSTANCE.createExchange(getSpec1(exchangeType, useSandbox)); - System.out.printf("trades %s%n", exchange.getMarketDataService().getTrades(instrument)); - System.out.printf("ticker %s%n", exchange.getMarketDataService().getTicker(instrument)); + ExchangeFactory.INSTANCE.createExchange(spec); + Trades trades = exchange.getMarketDataService().getTrades(instrument); + for(Trade trade : trades.getTrades()) { + tradeCheck(trade, instrument); + } + Ticker ticker = exchange.getMarketDataService().getTicker(instrument); + tickerCheck(ticker, instrument); Thread.sleep(500L); } + private static void tickerCheck(Ticker ticker, Instrument instrument) { + assertThat(ticker.getInstrument()).isEqualTo(instrument); + assertThat(ticker.getHigh()).isNotNull(); + assertThat(ticker.getVolume()).isNotNull(); + assertThat(ticker.getTimestamp()).isNotNull(); + } + + private static void tradeCheck(Trade trade, Instrument instrument) { + assertThat(trade.getInstrument()).isEqualTo(instrument); + assertThat(trade.getOriginalAmount()).isNotNull(); + assertThat(trade.getPrice()).isNotNull(); + assertThat(trade.getTimestamp()).isNotNull(); + } + private static ExchangeSpecification getSpec(ExchangeType exchangeType, boolean useSandbox) { ExchangeSpecification exchangeSpecification; - exchangeSpecification = new BinanceExchange().getDefaultExchangeSpecification(); + exchangeSpecification = new BinanceExchange().getDefaultExchangeSpecification(); exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); if (useSandbox) { exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); @@ -48,8 +68,8 @@ private static ExchangeSpecification getSpec(ExchangeType exchangeType, boolean private static ExchangeSpecification getSpec1(ExchangeType exchangeType, boolean useSandbox) { ExchangeSpecification exchangeSpecification; - exchangeSpecification = - new ExchangeSpecification(BinanceExchange.class); + exchangeSpecification = + new ExchangeSpecification(BinanceExchange.class); exchangeSpecification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, exchangeType); if (useSandbox) { exchangeSpecification.setExchangeSpecificParametersItem(USE_SANDBOX, true); diff --git a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java index 15e7606f46b..3f88e293c84 100644 --- a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java +++ b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java @@ -1,5 +1,6 @@ package info.bitrich.xchangestream.binance; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.knowm.xchange.Exchange.USE_SANDBOX; import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE; import static org.knowm.xchange.binance.dto.ExchangeType.FUTURES; @@ -11,29 +12,32 @@ import info.bitrich.xchangestream.core.StreamingExchangeFactory; import io.reactivex.disposables.Disposable; import java.io.IOException; +import java.util.List; +import org.junit.Before; +import org.junit.Test; import org.knowm.xchange.ExchangeSpecification; import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.currency.CurrencyPair; import org.knowm.xchange.derivative.FuturesContract; +import org.knowm.xchange.dto.marketdata.Ticker; +import org.knowm.xchange.dto.marketdata.Trade; +import org.knowm.xchange.dto.marketdata.Trades; import org.knowm.xchange.instrument.Instrument; public class BinanceStreamExchangeTypeTest { - public static void main(String[] args) throws InterruptedException, IOException { - testConnection(new CurrencyPair("ETH/USDT"), SPOT, false); - testConnection(new CurrencyPair("ETH/USDT"), SPOT, true); - testConnection(new FuturesContract("ETH/USDT/PERP"), FUTURES, false); - testConnection(new FuturesContract("ETH/USDT/PERP"), FUTURES, true); + @Test + public void testConnections() throws InterruptedException, IOException { + testConnection(new CurrencyPair("ETH/USDT"), getSpec(SPOT, false)); + testConnection(new CurrencyPair("ETH/USDT"), getSpec1(SPOT, true)); + testConnection(new FuturesContract("ETH/USDT/PERP"), getSpec1(FUTURES, false)); + testConnection(new FuturesContract("ETH/USDT/PERP"), getSpec(FUTURES, true)); } - private static void testConnection(Instrument instrument, ExchangeType exchangeType, - boolean useSandbox) + private static void testConnection(Instrument instrument, ExchangeSpecification spec) throws InterruptedException, IOException { StreamingExchange exchange = - StreamingExchangeFactory.INSTANCE.createExchange(getSpec(exchangeType, useSandbox)); -// another implementation -// StreamingExchange exchange = -// StreamingExchangeFactory.INSTANCE.createExchange(getSpec1(exchangeType, useSandbox)); + StreamingExchangeFactory.INSTANCE.createExchange(spec); ProductSubscription subscription = ProductSubscription.create() .addOrderbook(instrument) .addTicker(instrument) @@ -41,15 +45,31 @@ private static void testConnection(Instrument instrument, ExchangeType exchangeT .addTrades(instrument) .build(); exchange.connect(subscription).blockingAwait(); - System.out.printf("trades %s%n", exchange.getMarketDataService().getTrades(instrument)); + Trades trades = exchange.getMarketDataService().getTrades(instrument); + for(Trade trade : trades.getTrades()) + tradeCheck(trade,instrument); Disposable disposable = exchange.getStreamingMarketDataService().getTicker(instrument) - .subscribe(t -> System.out.printf("last price: %s%n", t.getLast())); + .subscribe(t -> tickerCheck(t, instrument)); Thread.sleep(3000L); disposable.dispose(); Thread.sleep(500L); exchange.disconnect().blockingAwait(); } + private static void tickerCheck(Ticker ticker, Instrument instrument) { + assertThat(ticker.getInstrument()).isEqualTo(instrument); + assertThat(ticker.getHigh()).isNotNull(); + assertThat(ticker.getVolume()).isNotNull(); + assertThat(ticker.getTimestamp()).isNotNull(); + } + + private static void tradeCheck(Trade trade, Instrument instrument) { + assertThat(trade.getInstrument()).isEqualTo(instrument); + assertThat(trade.getOriginalAmount()).isNotNull(); + assertThat(trade.getPrice()).isNotNull(); + assertThat(trade.getTimestamp()).isNotNull(); + } + private static ExchangeSpecification getSpec(ExchangeType exchangeType, boolean useSandbox) { ExchangeSpecification exchangeSpecification; if (exchangeType == SPOT) { From b2f7701ebd6ac275189ee1ef47927f982594bf4a Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 10 May 2024 22:59:06 +0300 Subject: [PATCH 8/9] [binance] add @Ignore to test due of http 451 error --- .../org/knowm/xchange/binance/BinanceExchangeTypeTest.java | 6 ++++-- .../binance/BinanceStreamExchangeTypeTest.java | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java index dca16a376b3..2111bb85061 100644 --- a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java +++ b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java @@ -7,6 +7,7 @@ import static org.knowm.xchange.binance.dto.ExchangeType.SPOT; import java.io.IOException; +import org.junit.Ignore; import org.junit.Test; import org.knowm.xchange.Exchange; import org.knowm.xchange.ExchangeFactory; @@ -19,6 +20,8 @@ import org.knowm.xchange.dto.marketdata.Trades; import org.knowm.xchange.instrument.Instrument; +// Github build give http 451 error(Unavailable For Legal Reasons) +@Ignore public class BinanceExchangeTypeTest { @Test @@ -30,7 +33,7 @@ public void testConnections() throws InterruptedException, IOException { } private static void testConnection(Instrument instrument, ExchangeSpecification spec) - throws InterruptedException, IOException { + throws IOException { Exchange exchange = ExchangeFactory.INSTANCE.createExchange(spec); Trades trades = exchange.getMarketDataService().getTrades(instrument); @@ -39,7 +42,6 @@ private static void testConnection(Instrument instrument, ExchangeSpecification } Ticker ticker = exchange.getMarketDataService().getTicker(instrument); tickerCheck(ticker, instrument); - Thread.sleep(500L); } private static void tickerCheck(Ticker ticker, Instrument instrument) { diff --git a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java index 3f88e293c84..a14631624c5 100644 --- a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java +++ b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.util.List; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.knowm.xchange.ExchangeSpecification; import org.knowm.xchange.binance.dto.ExchangeType; @@ -24,6 +25,8 @@ import org.knowm.xchange.dto.marketdata.Trades; import org.knowm.xchange.instrument.Instrument; +// Github build give http 451 error(Unavailable For Legal Reasons) +@Ignore public class BinanceStreamExchangeTypeTest { @Test From 40e4a7c3219567cfc6e8e109bed99548df344cb4 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 10 May 2024 23:31:49 +0300 Subject: [PATCH 9/9] [binance] rename tests class names --- ...hangeTypeTest.java => BinanceExchangeTypeIntegration.java} | 2 +- ...ypeTest.java => BinanceStreamExchangeTypeIntegration.java} | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) rename xchange-binance/src/test/java/org/knowm/xchange/binance/{BinanceExchangeTypeTest.java => BinanceExchangeTypeIntegration.java} (98%) rename xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/{BinanceStreamExchangeTypeTest.java => BinanceStreamExchangeTypeIntegration.java} (97%) diff --git a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeIntegration.java similarity index 98% rename from xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java rename to xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeIntegration.java index 2111bb85061..dae9be0ab38 100644 --- a/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeTest.java +++ b/xchange-binance/src/test/java/org/knowm/xchange/binance/BinanceExchangeTypeIntegration.java @@ -22,7 +22,7 @@ // Github build give http 451 error(Unavailable For Legal Reasons) @Ignore -public class BinanceExchangeTypeTest { +public class BinanceExchangeTypeIntegration { @Test public void testConnections() throws InterruptedException, IOException { diff --git a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeIntegration.java similarity index 97% rename from xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java rename to xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeIntegration.java index a14631624c5..bf7f26e45ef 100644 --- a/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeTest.java +++ b/xchange-stream-binance/src/test/java/info/bitrich/xchangestream/binance/BinanceStreamExchangeTypeIntegration.java @@ -12,8 +12,6 @@ import info.bitrich.xchangestream.core.StreamingExchangeFactory; import io.reactivex.disposables.Disposable; import java.io.IOException; -import java.util.List; -import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.knowm.xchange.ExchangeSpecification; @@ -27,7 +25,7 @@ // Github build give http 451 error(Unavailable For Legal Reasons) @Ignore -public class BinanceStreamExchangeTypeTest { +public class BinanceStreamExchangeTypeIntegration { @Test public void testConnections() throws InterruptedException, IOException {