From 1cbd76b010f55c16ef6ee412e8fba3d374c29dfc Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Tue, 30 Apr 2024 16:12:18 +0200 Subject: [PATCH 1/3] [binance] Fix call with wrong parameters --- .../xchange/binance/service/BinanceMarketDataService.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 15f3b6a01e8..52cf9f5aa3b 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 @@ -87,6 +87,12 @@ public List getTickers(Params params) throws IOException { } } + @Override + public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws IOException { + return getOrderBook((Instrument) currencyPair, args); + } + + @Override public OrderBook getOrderBook(Instrument instrument, Object... args) throws IOException { return getBinanceOrderBook(instrument, args); From ed33773726bace910c0a8dd92398c5a8a101a9cf Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Tue, 30 Apr 2024 16:12:29 +0200 Subject: [PATCH 2/3] [binance] Add authorized request for idea http client --- xchange-binance/.gitignore | 1 + xchange-binance/README.md | 11 +++++++++++ .../example.http-client.private.env.json | 6 ++++++ xchange-binance/src/test/resources/rest/sign.js | 16 ++++++++++++++++ .../src/test/resources/rest/wallet.http | 8 ++++++++ 5 files changed, 42 insertions(+) create mode 100644 xchange-binance/.gitignore create mode 100644 xchange-binance/README.md create mode 100644 xchange-binance/example.http-client.private.env.json create mode 100644 xchange-binance/src/test/resources/rest/sign.js create mode 100644 xchange-binance/src/test/resources/rest/wallet.http diff --git a/xchange-binance/.gitignore b/xchange-binance/.gitignore new file mode 100644 index 00000000000..42c7d2cd0eb --- /dev/null +++ b/xchange-binance/.gitignore @@ -0,0 +1 @@ +http-client.private.env.json \ No newline at end of file diff --git a/xchange-binance/README.md b/xchange-binance/README.md new file mode 100644 index 00000000000..6f4d4742fa7 --- /dev/null +++ b/xchange-binance/README.md @@ -0,0 +1,11 @@ +## Using IntelliJ Idea HTTP client + +There are *.http files stored in `src/test/resources/rest` that can be used with IntelliJ Idea HTTP Client. + +Some requests need authorization, so the api credentials have to be stored in `http-client.private.env.json` in module's root. Sample content can be found in `example.http-client.private.env.json` + +> [!CAUTION] +> Never commit your api credentials to the repository! + + +[HTTP Client documentation](https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html) \ No newline at end of file diff --git a/xchange-binance/example.http-client.private.env.json b/xchange-binance/example.http-client.private.env.json new file mode 100644 index 00000000000..977c974ecbe --- /dev/null +++ b/xchange-binance/example.http-client.private.env.json @@ -0,0 +1,6 @@ +{ + "default": { + "api_key": "replace_me", + "api_secret": "replace_me" + } +} \ No newline at end of file diff --git a/xchange-binance/src/test/resources/rest/sign.js b/xchange-binance/src/test/resources/rest/sign.js new file mode 100644 index 00000000000..d4461c6b044 --- /dev/null +++ b/xchange-binance/src/test/resources/rest/sign.js @@ -0,0 +1,16 @@ +export function gen_sign(request) { + const pattern = RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"); + const url = request.url.tryGetSubstituted(); + const matches = url.match(pattern); + + const timestamp = Math.floor(Date.now()).toFixed(); + const query = matches[7] || ""; + + const payloadToSign = query.replace("&signature={{signature}}", "").replace("{{timestamp}}", timestamp); + + const apiSecret = request.environment.get("api_secret"); + const sign = crypto.hmac.sha256().withTextSecret(apiSecret).updateWithText(payloadToSign).digest().toHex(); + + request.variables.set("timestamp", timestamp); + request.variables.set("signature", sign); +} \ No newline at end of file diff --git a/xchange-binance/src/test/resources/rest/wallet.http b/xchange-binance/src/test/resources/rest/wallet.http new file mode 100644 index 00000000000..7d458981556 --- /dev/null +++ b/xchange-binance/src/test/resources/rest/wallet.http @@ -0,0 +1,8 @@ +### All Coins' Information (USER_DATA) +< {% + import {gen_sign} from 'sign.js' + gen_sign(request); +%} + +GET {{api_host}}/sapi/v1/capital/config/getall?timestamp={{timestamp}}&signature={{signature}} +X-MBX-APIKEY: {{api_key}} From f13b32dd1c11f61d642fa608b166d5244213c5e1 Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Tue, 30 Apr 2024 16:12:41 +0200 Subject: [PATCH 3/3] [binance] Add currency infos endpoint --- .../xchange/binance/BinanceAuthenticated.java | 22 ++- .../converter/StringToCurrencyConverter.java | 13 ++ .../dto/account/BinanceCurrencyInfo.java | 138 ++++++++++++++++++ .../service/BinanceAccountServiceRaw.java | 21 ++- .../account/AccountServiceIntegration.java | 12 ++ 5 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 xchange-binance/src/main/java/org/knowm/xchange/binance/config/converter/StringToCurrencyConverter.java create mode 100644 xchange-binance/src/main/java/org/knowm/xchange/binance/dto/account/BinanceCurrencyInfo.java diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceAuthenticated.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceAuthenticated.java index 3d6f88aa8c7..eddeaee6ccb 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceAuthenticated.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/BinanceAuthenticated.java @@ -16,7 +16,16 @@ import java.util.List; import java.util.Map; import org.knowm.xchange.binance.dto.BinanceException; -import org.knowm.xchange.binance.dto.account.*; +import org.knowm.xchange.binance.dto.account.AssetDetail; +import org.knowm.xchange.binance.dto.account.AssetDividendResponse; +import org.knowm.xchange.binance.dto.account.BinanceAccountInformation; +import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo; +import org.knowm.xchange.binance.dto.account.BinanceDeposit; +import org.knowm.xchange.binance.dto.account.BinanceWithdraw; +import org.knowm.xchange.binance.dto.account.DepositAddress; +import org.knowm.xchange.binance.dto.account.TransferHistory; +import org.knowm.xchange.binance.dto.account.TransferSubUserHistory; +import org.knowm.xchange.binance.dto.account.WithdrawResponse; import org.knowm.xchange.binance.dto.trade.BinanceCancelledOrder; import org.knowm.xchange.binance.dto.trade.BinanceDustLog; import org.knowm.xchange.binance.dto.trade.BinanceListenKey; @@ -329,6 +338,17 @@ BinanceDustLog getDustLog( @QueryParam(SIGNATURE) ParamsDigest signature) throws IOException, BinanceException; + + @GET + @Path("/sapi/v1/capital/config/getall") + List getCurrencyInfos( + @QueryParam("recvWindow") Long recvWindow, + @QueryParam("timestamp") SynchronizedValueFactory timestamp, + @HeaderParam(X_MBX_APIKEY) String apiKey, + @QueryParam(SIGNATURE) ParamsDigest signature) + throws IOException, BinanceException; + + /** * Submit a withdraw request. * diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/config/converter/StringToCurrencyConverter.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/config/converter/StringToCurrencyConverter.java new file mode 100644 index 00000000000..d524e0ad889 --- /dev/null +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/config/converter/StringToCurrencyConverter.java @@ -0,0 +1,13 @@ +package org.knowm.xchange.binance.config.converter; + +import com.fasterxml.jackson.databind.util.StdConverter; +import org.knowm.xchange.currency.Currency; + +/** Converts string value {@code Currency} */ +public class StringToCurrencyConverter extends StdConverter { + + @Override + public Currency convert(String value) { + return Currency.getInstance(value); + } +} diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/account/BinanceCurrencyInfo.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/account/BinanceCurrencyInfo.java new file mode 100644 index 00000000000..99b7ada13c4 --- /dev/null +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/account/BinanceCurrencyInfo.java @@ -0,0 +1,138 @@ +package org.knowm.xchange.binance.dto.account; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.math.BigDecimal; +import java.util.List; +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; +import org.knowm.xchange.binance.config.converter.StringToCurrencyConverter; +import org.knowm.xchange.currency.Currency; + +@Data +@Builder +@Jacksonized +public class BinanceCurrencyInfo { + + @JsonProperty("coin") + @JsonDeserialize(converter = StringToCurrencyConverter.class) + private Currency currency; + + @JsonProperty("depositAllEnable") + private Boolean depositEnabled; + + @JsonProperty("withdrawAllEnable") + private Boolean withdrawEnabled; + + @JsonProperty("name") + private String name; + + @JsonProperty("free") + private BigDecimal free; + + @JsonProperty("locked") + private BigDecimal locked; + + @JsonProperty("freeze") + private BigDecimal freeze; + + @JsonProperty("withdrawing") + private BigDecimal withdrawing; + + @JsonProperty("ipoing") + private BigDecimal ipoing; + + @JsonProperty("ipoable") + private BigDecimal ipoable; + + @JsonProperty("storage") + private BigDecimal storage; + + @JsonProperty("isLegalMoney") + private Boolean isLegalMoney; + + @JsonProperty("trading") + private Boolean trading; + + @JsonProperty("networkList") + private List networks; + + + @Data + @Builder + @Jacksonized + public static class Network { + + @JsonProperty("network") + private String id; + + @JsonProperty("coin") + @JsonDeserialize(converter = StringToCurrencyConverter.class) + private Currency currency; + + @JsonProperty("withdrawIntegerMultiple") + private BigDecimal withdrawIntegerMultiple; + + @JsonProperty("isDefault") + private Boolean isDefault; + + @JsonProperty("depositEnable") + private Boolean depositEnabled; + + @JsonProperty("withdrawEnable") + private Boolean withdrawEnabled; + + @JsonProperty("depositDesc") + private String depositDesc; + + @JsonProperty("withdrawDesc") + private String withdrawDesc; + + @JsonProperty("specialTips") + private String specialTips; + + @JsonProperty("name") + private String name; + + @JsonProperty("resetAddressStatus") + private Boolean resetAddressStatus; + + @JsonProperty("addressRegex") + private String addressRegex; + + @JsonProperty("memoRegex") + private String memoRegex; + + @JsonProperty("withdrawFee") + private BigDecimal withdrawFee; + + @JsonProperty("withdrawMin") + private BigDecimal withdrawMin; + + @JsonProperty("withdrawMax") + private BigDecimal withdrawMax; + + @JsonProperty("minConfirm") + private Integer minConfirm; + + @JsonProperty("unLockConfirm") + private Integer unLockConfirm; + + @JsonProperty("sameAddress") + private Boolean sameAddress; + + @JsonProperty("estimatedArrivalTime") + private Integer estimatedArrivalTime; + + @JsonProperty("busy") + private Boolean busy; + + @JsonProperty("contractAddressUrl") + private String contractAddressUrl; + + @JsonProperty("contractAddress") + private String contractAddress; + + } +} diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountServiceRaw.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountServiceRaw.java index db07456d675..b4a766a73f3 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountServiceRaw.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/service/BinanceAccountServiceRaw.java @@ -10,7 +10,16 @@ import org.knowm.xchange.binance.BinanceAdapters; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.dto.BinanceException; -import org.knowm.xchange.binance.dto.account.*; +import org.knowm.xchange.binance.dto.account.AssetDetail; +import org.knowm.xchange.binance.dto.account.AssetDividendResponse; +import org.knowm.xchange.binance.dto.account.BinanceAccountInformation; +import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo; +import org.knowm.xchange.binance.dto.account.BinanceDeposit; +import org.knowm.xchange.binance.dto.account.BinanceWithdraw; +import org.knowm.xchange.binance.dto.account.DepositAddress; +import org.knowm.xchange.binance.dto.account.TransferHistory; +import org.knowm.xchange.binance.dto.account.TransferSubUserHistory; +import org.knowm.xchange.binance.dto.account.WithdrawResponse; import org.knowm.xchange.binance.dto.account.futures.BinanceFutureAccountInformation; import org.knowm.xchange.client.ResilienceRegistries; import org.knowm.xchange.currency.Currency; @@ -30,6 +39,16 @@ public BinanceAccountInformation account() throws BinanceException, IOException .call(); } + + public List currencyInfos() throws BinanceException, IOException { + return decorateApiCall( + () -> binance.getCurrencyInfos(getRecvWindow(), getTimestampFactory(), apiKey, signatureCreator)) + .withRetry(retry("currencyInfo")) + .withRateLimiter(rateLimiter(REQUEST_WEIGHT_RATE_LIMITER), 5) + .call(); + } + + public BinanceFutureAccountInformation futuresAccount() throws BinanceException, IOException { return decorateApiCall( () -> diff --git a/xchange-binance/src/test/java/org/knowm/xchange/binance/service/account/AccountServiceIntegration.java b/xchange-binance/src/test/java/org/knowm/xchange/binance/service/account/AccountServiceIntegration.java index 1039d09aa63..a60c034fe82 100644 --- a/xchange-binance/src/test/java/org/knowm/xchange/binance/service/account/AccountServiceIntegration.java +++ b/xchange-binance/src/test/java/org/knowm/xchange/binance/service/account/AccountServiceIntegration.java @@ -1,5 +1,7 @@ package org.knowm.xchange.binance.service.account; +import static org.assertj.core.api.Assertions.assertThat; + import java.math.BigDecimal; import java.util.List; import java.util.Map; @@ -11,6 +13,7 @@ import org.junit.Test; import org.knowm.xchange.binance.BinanceExchangeIntegration; import org.knowm.xchange.binance.dto.account.AssetDetail; +import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo; import org.knowm.xchange.binance.dto.account.BinanceDeposit; import org.knowm.xchange.binance.dto.account.TransferHistory; import org.knowm.xchange.binance.service.BinanceAccountService; @@ -48,6 +51,15 @@ public void testAssetDetail() throws Exception { Assert.assertFalse(assetDetails.isEmpty()); } + + @Test + public void testCurrencyInfos() throws Exception { + assumeProduction(); + List currencyInfos = accountService.currencyInfos(); + assertThat(currencyInfos).isNotEmpty(); + } + + @Test public void testMetaData() {