From b6ffb13bcd4df82d18358cee9fcfd9f61a6fa6cf Mon Sep 17 00:00:00 2001 From: Donald Jackson Date: Wed, 25 Sep 2024 14:26:20 +0100 Subject: [PATCH 1/4] [bitstamp] add settlements as deposit type --- .../xchange/bitstamp/dto/trade/BitstampUserTransaction.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xchange-bitstamp/src/main/java/org/knowm/xchange/bitstamp/dto/trade/BitstampUserTransaction.java b/xchange-bitstamp/src/main/java/org/knowm/xchange/bitstamp/dto/trade/BitstampUserTransaction.java index a625afa1989..7ba5c63d3ab 100644 --- a/xchange-bitstamp/src/main/java/org/knowm/xchange/bitstamp/dto/trade/BitstampUserTransaction.java +++ b/xchange-bitstamp/src/main/java/org/knowm/xchange/bitstamp/dto/trade/BitstampUserTransaction.java @@ -88,11 +88,11 @@ public TransactionType getType() { } public boolean isDeposit() { - return type == TransactionType.deposit; + return type == TransactionType.deposit || type == TransactionType.rippleDeposit || type == TransactionType.settlementTransfer; } public boolean isWithdrawal() { - return type == TransactionType.withdrawal; + return type == TransactionType.withdrawal || type == TransactionType.rippleWithdrawal; } public boolean isMarketTrade() { From 25ab79c5b284cc0472a4c75b26256db9824d2010 Mon Sep 17 00:00:00 2001 From: Donald Jackson Date: Tue, 10 Dec 2024 20:32:47 +0200 Subject: [PATCH 2/4] [binance] add network support to deposit addresses --- .../xchange/binance/BinanceAuthenticated.java | 1 + .../binance/dto/trade/TimeInForce.java | 2 + .../service/BinanceAccountService.java | 37 +++++++++++++++++-- .../service/BinanceAccountServiceRaw.java | 30 +++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) 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 b8503410818..5ffe123e37b 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 @@ -499,6 +499,7 @@ List transferSubUserHistory( @Path("/sapi/v1/capital/deposit/address") DepositAddress depositAddress( @QueryParam("coin") String coin, + @QueryParam("network") String network, @QueryParam("recvWindow") Long recvWindow, @QueryParam("timestamp") SynchronizedValueFactory timestamp, @HeaderParam(X_MBX_APIKEY) String apiKey, diff --git a/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/trade/TimeInForce.java b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/trade/TimeInForce.java index 2121ff106c5..5ab2ee73a65 100644 --- a/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/trade/TimeInForce.java +++ b/xchange-binance/src/main/java/org/knowm/xchange/binance/dto/trade/TimeInForce.java @@ -1,8 +1,10 @@ package org.knowm.xchange.binance.dto.trade; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import org.knowm.xchange.dto.Order.IOrderFlags; +@JsonTypeInfo(use = JsonTypeInfo.Id.NONE) public enum TimeInForce implements IOrderFlags { GTC, GTX, 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 fb37325ce54..f051d9f4471 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,8 +1,6 @@ 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; @@ -13,6 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.knowm.xchange.binance.BinanceAdapters; import org.knowm.xchange.binance.BinanceErrorAdapter; import org.knowm.xchange.binance.BinanceExchange; @@ -20,6 +19,8 @@ import org.knowm.xchange.binance.dto.ExchangeType; import org.knowm.xchange.binance.dto.account.AssetDetail; import org.knowm.xchange.binance.dto.account.BinanceAccountInformation; +import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo; +import org.knowm.xchange.binance.dto.account.BinanceCurrencyInfo.Network; import org.knowm.xchange.binance.dto.account.BinanceFundingHistoryParams; import org.knowm.xchange.binance.dto.account.BinanceMasterAccountTransferHistoryParams; import org.knowm.xchange.binance.dto.account.BinanceSubAccountTransferHistoryParams; @@ -38,6 +39,7 @@ import org.knowm.xchange.dto.account.Wallet; import org.knowm.xchange.instrument.Instrument; import org.knowm.xchange.service.account.AccountService; +import org.knowm.xchange.service.account.params.RequestDepositAddressParams; import org.knowm.xchange.service.trade.params.DefaultWithdrawFundsParams; import org.knowm.xchange.service.trade.params.HistoryParamsFundingType; import org.knowm.xchange.service.trade.params.RippleWithdrawFundsParams; @@ -215,7 +217,29 @@ public String requestDepositAddress(Currency currency, String... args) throws IO @Override public AddressWithTag requestDepositAddressData(Currency currency, String... args) throws IOException { - DepositAddress depositAddress = super.requestDepositAddress(currency); + return prepareAddressWithTag(super.requestDepositAddress(currency)); + } + + @Override + public AddressWithTag requestDepositAddressData(RequestDepositAddressParams requestDepositAddressParams) throws IOException { + if(StringUtils.isEmpty(requestDepositAddressParams.getNetwork())) { + return requestDepositAddressData(requestDepositAddressParams.getCurrency(), requestDepositAddressParams.getExtraArguments()); + } + + BinanceCurrencyInfo binanceCurrencyInfo = super.getCurrencyInfo(requestDepositAddressParams.getCurrency()).orElseThrow(() -> new IllegalArgumentException("Currency not supported: " + requestDepositAddressParams.getCurrency())); + + Network binanceNetwork = binanceCurrencyInfo.getNetworks() + .stream() + .filter(network -> requestDepositAddressParams.getNetwork().equals(network.getId()) || network.getId().equals(requestDepositAddressParams.getCurrency().getCurrencyCode())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Network not supported: " + requestDepositAddressParams.getNetwork())); + + DepositAddress depositAddress = super.requestDepositAddressWithNetwork(requestDepositAddressParams.getCurrency(), binanceNetwork.getId()); + + return prepareAddressWithTag(depositAddress); + } + + private static AddressWithTag prepareAddressWithTag(DepositAddress depositAddress) { String destinationTag = (depositAddress.addressTag == null || depositAddress.addressTag.isEmpty()) ? null @@ -223,6 +247,13 @@ public AddressWithTag requestDepositAddressData(Currency currency, String... arg return new AddressWithTag(depositAddress.address, destinationTag); } + @Override + public String requestDepositAddress(RequestDepositAddressParams requestDepositAddressParams) throws IOException { + return requestDepositAddressData(requestDepositAddressParams).getAddress(); + } + + + public Map getAssetDetails() throws IOException { try { return super.requestAssetDetail(); 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 4b920c56a59..1b835031b0c 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 @@ -7,6 +7,9 @@ import java.math.BigDecimal; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import org.knowm.xchange.binance.BinanceAdapters; import org.knowm.xchange.binance.BinanceExchange; import org.knowm.xchange.binance.dto.BinanceException; @@ -25,6 +28,8 @@ import org.knowm.xchange.currency.Currency; public class BinanceAccountServiceRaw extends BinanceBaseService { + private List currencyInfos; + private final Lock currencyInfoLock = new ReentrantLock(); public BinanceAccountServiceRaw( BinanceExchange exchange, ResilienceRegistries resilienceRegistries) { @@ -95,10 +100,15 @@ private WithdrawResponse withdraw( } public DepositAddress requestDepositAddress(Currency currency) throws IOException { + return requestDepositAddressWithNetwork(currency, null); + } + + public DepositAddress requestDepositAddressWithNetwork(Currency currency, String network) throws IOException { return decorateApiCall( () -> binance.depositAddress( BinanceAdapters.toSymbol(currency), + network, getRecvWindow(), getTimestampFactory(), apiKey, @@ -214,4 +224,24 @@ public List getSubUserHistory( .withRateLimiter(rateLimiter(REQUEST_WEIGHT_RATE_LIMITER)) .call(); } + + protected List getCurrencyInfoCached() throws IOException { + currencyInfoLock.lock(); + try { + if (currencyInfos == null) { + currencyInfos = currencyInfos(); + } + currencyInfos = currencyInfos(); + } finally { + currencyInfoLock.unlock(); + } + + return currencyInfos; + } + + protected Optional getCurrencyInfo(Currency currency) throws IOException { + return getCurrencyInfoCached().stream() + .filter(info -> currency.equals(info.getCurrency())) + .findFirst(); + } } From 2881a15396903df99a25d85ad07f1c6ae670975f Mon Sep 17 00:00:00 2001 From: Donald Jackson Date: Tue, 10 Dec 2024 21:09:28 +0200 Subject: [PATCH 3/4] [binance] add network support for withdrawals --- .../xchange/binance/BinanceAuthenticated.java | 1 + .../service/BinanceAccountService.java | 19 +++++++++++++++++-- .../service/BinanceAccountServiceRaw.java | 9 +++++---- .../params/NetworkWithdrawFundsParams.java | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 xchange-core/src/main/java/org/knowm/xchange/service/trade/params/NetworkWithdrawFundsParams.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 5ffe123e37b..7b6e8968977 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 @@ -371,6 +371,7 @@ WithdrawResponse withdraw( @FormParam("addressTag") String addressTag, @FormParam("amount") BigDecimal amount, @FormParam("name") String name, + @FormParam("network") String network, @FormParam("recvWindow") Long recvWindow, @FormParam("timestamp") SynchronizedValueFactory timestamp, @HeaderParam(X_MBX_APIKEY) String apiKey, 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 f051d9f4471..71330fa4752 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 @@ -42,6 +42,7 @@ import org.knowm.xchange.service.account.params.RequestDepositAddressParams; import org.knowm.xchange.service.trade.params.DefaultWithdrawFundsParams; import org.knowm.xchange.service.trade.params.HistoryParamsFundingType; +import org.knowm.xchange.service.trade.params.NetworkWithdrawFundsParams; import org.knowm.xchange.service.trade.params.RippleWithdrawFundsParams; import org.knowm.xchange.service.trade.params.TradeHistoryParamCurrency; import org.knowm.xchange.service.trade.params.TradeHistoryParamLimit; @@ -189,7 +190,19 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException { rippleParams.getCurrency().getCurrencyCode(), rippleParams.getAddress(), rippleParams.getTag(), - rippleParams.getAmount()); + rippleParams.getAmount(), + Currency.XRP.getCurrencyCode() + ); + } else if(params instanceof NetworkWithdrawFundsParams) { + NetworkWithdrawFundsParams p = (NetworkWithdrawFundsParams) params; + withdraw = + super.withdraw( + p.getCurrency().getCurrencyCode(), + p.getAddress(), + p.getAddressTag(), + p.getAmount(), + p.getNetwork() + ); } else { DefaultWithdrawFundsParams p = (DefaultWithdrawFundsParams) params; withdraw = @@ -197,7 +210,9 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException { p.getCurrency().getCurrencyCode(), p.getAddress(), p.getAddressTag(), - p.getAmount()); + p.getAmount(), + null + ); } return withdraw.getId(); } catch (BinanceException e) { 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 1b835031b0c..15fb1ee4e80 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 @@ -68,19 +68,19 @@ public WithdrawResponse withdraw(String coin, String address, BigDecimal amount) throws IOException, BinanceException { // the name parameter seams to be mandatory String name = address.length() <= 10 ? address : address.substring(0, 10); - return withdraw(coin, address, null, amount, name); + return withdraw(coin, address, null, amount, name, null); } public WithdrawResponse withdraw( - String coin, String address, String addressTag, BigDecimal amount) + String coin, String address, String addressTag, BigDecimal amount, String network) throws IOException, BinanceException { // the name parameter seams to be mandatory String name = address.length() <= 10 ? address : address.substring(0, 10); - return withdraw(coin, address, addressTag, amount, name); + return withdraw(coin, address, addressTag, amount, name, network); } private WithdrawResponse withdraw( - String coin, String address, String addressTag, BigDecimal amount, String name) + String coin, String address, String addressTag, BigDecimal amount, String name, String network) throws IOException, BinanceException { return decorateApiCall( () -> @@ -90,6 +90,7 @@ private WithdrawResponse withdraw( addressTag, amount, name, + network, getRecvWindow(), getTimestampFactory(), apiKey, diff --git a/xchange-core/src/main/java/org/knowm/xchange/service/trade/params/NetworkWithdrawFundsParams.java b/xchange-core/src/main/java/org/knowm/xchange/service/trade/params/NetworkWithdrawFundsParams.java new file mode 100644 index 00000000000..ac5d2445d13 --- /dev/null +++ b/xchange-core/src/main/java/org/knowm/xchange/service/trade/params/NetworkWithdrawFundsParams.java @@ -0,0 +1,14 @@ +package org.knowm.xchange.service.trade.params; + +import lombok.ToString; +import lombok.Value; +import lombok.experimental.NonFinal; +import lombok.experimental.SuperBuilder; + +@Value +@NonFinal +@SuperBuilder +@ToString(callSuper = true) +public class NetworkWithdrawFundsParams extends DefaultWithdrawFundsParams { + String network; +} From 66b409a04fa255a7cd51eef1f46ba6933d14b9f1 Mon Sep 17 00:00:00 2001 From: Donald Jackson Date: Wed, 11 Dec 2024 09:54:47 +0200 Subject: [PATCH 4/4] [binance] format code --- .../service/BinanceAccountService.java | 58 ++++++++++++------- .../service/BinanceAccountServiceRaw.java | 10 +++- 2 files changed, 44 insertions(+), 24 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 71330fa4752..e66451192f6 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 @@ -191,9 +191,8 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException { rippleParams.getAddress(), rippleParams.getTag(), rippleParams.getAmount(), - Currency.XRP.getCurrencyCode() - ); - } else if(params instanceof NetworkWithdrawFundsParams) { + Currency.XRP.getCurrencyCode()); + } else if (params instanceof NetworkWithdrawFundsParams) { NetworkWithdrawFundsParams p = (NetworkWithdrawFundsParams) params; withdraw = super.withdraw( @@ -201,8 +200,7 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException { p.getAddress(), p.getAddressTag(), p.getAmount(), - p.getNetwork() - ); + p.getNetwork()); } else { DefaultWithdrawFundsParams p = (DefaultWithdrawFundsParams) params; withdraw = @@ -211,8 +209,7 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException { p.getAddress(), p.getAddressTag(), p.getAmount(), - null - ); + null); } return withdraw.getId(); } catch (BinanceException e) { @@ -236,20 +233,38 @@ public AddressWithTag requestDepositAddressData(Currency currency, String... arg } @Override - public AddressWithTag requestDepositAddressData(RequestDepositAddressParams requestDepositAddressParams) throws IOException { - if(StringUtils.isEmpty(requestDepositAddressParams.getNetwork())) { - return requestDepositAddressData(requestDepositAddressParams.getCurrency(), requestDepositAddressParams.getExtraArguments()); + public AddressWithTag requestDepositAddressData( + RequestDepositAddressParams requestDepositAddressParams) throws IOException { + if (StringUtils.isEmpty(requestDepositAddressParams.getNetwork())) { + return requestDepositAddressData( + requestDepositAddressParams.getCurrency(), + requestDepositAddressParams.getExtraArguments()); } - BinanceCurrencyInfo binanceCurrencyInfo = super.getCurrencyInfo(requestDepositAddressParams.getCurrency()).orElseThrow(() -> new IllegalArgumentException("Currency not supported: " + requestDepositAddressParams.getCurrency())); - - Network binanceNetwork = binanceCurrencyInfo.getNetworks() - .stream() - .filter(network -> requestDepositAddressParams.getNetwork().equals(network.getId()) || network.getId().equals(requestDepositAddressParams.getCurrency().getCurrencyCode())) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException("Network not supported: " + requestDepositAddressParams.getNetwork())); - - DepositAddress depositAddress = super.requestDepositAddressWithNetwork(requestDepositAddressParams.getCurrency(), binanceNetwork.getId()); + BinanceCurrencyInfo binanceCurrencyInfo = + super.getCurrencyInfo(requestDepositAddressParams.getCurrency()) + .orElseThrow( + () -> + new IllegalArgumentException( + "Currency not supported: " + requestDepositAddressParams.getCurrency())); + + Network binanceNetwork = + binanceCurrencyInfo.getNetworks().stream() + .filter( + network -> + requestDepositAddressParams.getNetwork().equals(network.getId()) + || network + .getId() + .equals(requestDepositAddressParams.getCurrency().getCurrencyCode())) + .findFirst() + .orElseThrow( + () -> + new IllegalArgumentException( + "Network not supported: " + requestDepositAddressParams.getNetwork())); + + DepositAddress depositAddress = + super.requestDepositAddressWithNetwork( + requestDepositAddressParams.getCurrency(), binanceNetwork.getId()); return prepareAddressWithTag(depositAddress); } @@ -263,12 +278,11 @@ private static AddressWithTag prepareAddressWithTag(DepositAddress depositAddres } @Override - public String requestDepositAddress(RequestDepositAddressParams requestDepositAddressParams) throws IOException { + public String requestDepositAddress(RequestDepositAddressParams requestDepositAddressParams) + throws IOException { return requestDepositAddressData(requestDepositAddressParams).getAddress(); } - - public Map getAssetDetails() throws IOException { try { return super.requestAssetDetail(); 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 15fb1ee4e80..fdba371a090 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 @@ -80,7 +80,12 @@ public WithdrawResponse withdraw( } private WithdrawResponse withdraw( - String coin, String address, String addressTag, BigDecimal amount, String name, String network) + String coin, + String address, + String addressTag, + BigDecimal amount, + String name, + String network) throws IOException, BinanceException { return decorateApiCall( () -> @@ -104,7 +109,8 @@ public DepositAddress requestDepositAddress(Currency currency) throws IOExceptio return requestDepositAddressWithNetwork(currency, null); } - public DepositAddress requestDepositAddressWithNetwork(Currency currency, String network) throws IOException { + public DepositAddress requestDepositAddressWithNetwork(Currency currency, String network) + throws IOException { return decorateApiCall( () -> binance.depositAddress(