Skip to content

Commit

Permalink
Merge pull request #4831 from donald-jackson/kraken-network-deposit-s…
Browse files Browse the repository at this point in the history
…upport

[kraken] add different network support for currency deposit addresses
  • Loading branch information
timmolter authored Apr 20, 2024
2 parents 4abdcb2 + 1136d61 commit 74c9e34
Show file tree
Hide file tree
Showing 17 changed files with 595 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.knowm.xchange.exceptions;

import java.util.List;
import lombok.Getter;

/** Exception indicating a requested deposit address has multiple networks and network required */
@Getter
public class DepositAddressAmbiguousException extends ExchangeException {
private final List<String> networks;

public DepositAddressAmbiguousException(List<String> networks) {
super("Deposit Address Not Found");
this.networks = networks;
}

public DepositAddressAmbiguousException(List<String> networks, String message) {
super(message);
this.networks = networks;
}

public DepositAddressAmbiguousException(List<String> networks, String message, Throwable cause) {
super(message, cause);
this.networks = networks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.knowm.xchange.exceptions;

/** Exception indicating a deposit address could not be created */
public class DepositAddressCreationException extends ExchangeException {

public DepositAddressCreationException() {
super("Deposit Address Could Not Be Created");
}

public DepositAddressCreationException(String message) {
super(message);
}

public DepositAddressCreationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.knowm.xchange.exceptions;

/** Exception indicating a requested deposit address was not found */
public class DepositAddressNotFoundException extends ExchangeException {

public DepositAddressNotFoundException() {
super("Deposit Address Not Found");
}

public DepositAddressNotFoundException(String message) {
super(message);
}

public DepositAddressNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.BaseService;
import org.knowm.xchange.service.account.params.RequestDepositAddressParams;
import org.knowm.xchange.service.trade.params.DefaultWithdrawFundsParams;
import org.knowm.xchange.service.trade.params.TradeHistoryParams;
import org.knowm.xchange.service.trade.params.WithdrawFundsParams;
Expand Down Expand Up @@ -125,6 +126,24 @@ default String requestDepositAddress(Currency currency, String... args) throws I
throw new NotYetImplementedForExchangeException("requestDepositAddress");
}

/**
* Request a digital currency address to fund this account. Allows to fund the exchange account
* with digital currency from an external address
*
* @param params The deposit address request parameters
* @return the internal deposit address to send funds to
* @throws ExchangeException - Indication that the exchange reported some kind of error with the
* request or response
* @throws NotAvailableFromExchangeException - Indication that the exchange does not support the
* requested function or data
* @throws NotYetImplementedForExchangeException - Indication that the exchange supports the
* requested function or data, but it has not yet been implemented
* @throws IOException - Indication that a networking error occurred while fetching JSON data
*/
default String requestDepositAddress(RequestDepositAddressParams params) throws IOException {
return requestDepositAddress(params.getCurrency(), params.getExtraArguments());
}

/**
* Request a digital currency address to fund this account. Allows to fund the exchange account
* with digital currency from an external address
Expand All @@ -144,6 +163,25 @@ default AddressWithTag requestDepositAddressData(Currency currency, String... ar
throw new NotYetImplementedForExchangeException("requestDepositAddressData");
}

/**
* Request a digital currency address to fund this account. Allows to fund the exchange account
* with digital currency from an external address
*
* @param params The deposit address request parameters
* @return the internal deposit address to send funds to
* @throws ExchangeException - Indication that the exchange reported some kind of error with the
* request or response
* @throws NotAvailableFromExchangeException - Indication that the exchange does not support the
* requested function or data
* @throws NotYetImplementedForExchangeException - Indication that the exchange supports the
* requested function or data, but it has not yet been implemented
* @throws IOException - Indication that a networking error occurred while fetching JSON data
*/
default AddressWithTag requestDepositAddressData(RequestDepositAddressParams params)
throws IOException {
return requestDepositAddressData(params.getCurrency(), params.getExtraArguments());
}

/**
* Create {@link TradeHistoryParams} object specific to this exchange. Object created by this
* method may be used to discover supported and required {@link
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.knowm.xchange.service.account.params;

import lombok.Builder;
import lombok.Value;
import lombok.experimental.NonFinal;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.currency.Currency;

@Value
@NonFinal
@Builder
@Jacksonized
public class DefaultRequestDepositAddressParams implements RequestDepositAddressParams {
Currency currency;
String network;

@Builder.Default boolean newAddress = false;

String[] extraArguments;

public static DefaultRequestDepositAddressParams create(Currency currency, String... args) {
return DefaultRequestDepositAddressParams.builder()
.currency(currency)
.extraArguments(args)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.knowm.xchange.service.account.params;

import org.knowm.xchange.currency.Currency;

public interface RequestDepositAddressParams {
Currency getCurrency();

String getNetwork();

boolean isNewAddress();

String[] getExtraArguments();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.Order.OrderStatus;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.AddressWithTag;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.account.Fee;
import org.knowm.xchange.dto.account.FundingRecord;
Expand Down Expand Up @@ -332,8 +333,12 @@ public static OrderType adaptOrderType(KrakenType krakenType) {
return krakenType.equals(KrakenType.BUY) ? OrderType.BID : OrderType.ASK;
}

public static String adaptKrakenDepositAddress(KrakenDepositAddress[] krakenDepositAddress) {
return krakenDepositAddress[0].getAddress();
public static AddressWithTag adaptKrakenDepositAddress(
KrakenDepositAddress[] krakenDepositAddress) {
return AddressWithTag.builder()
.address(krakenDepositAddress[0].getAddress())
.addressTag(krakenDepositAddress[0].getTag())
.build();
}

public static String adaptOrderId(KrakenOrderResponse orderResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.account.AccountInfo;
import org.knowm.xchange.dto.account.AddressWithTag;
import org.knowm.xchange.dto.account.Fee;
import org.knowm.xchange.dto.account.FundingRecord;
import org.knowm.xchange.dto.account.Wallet;
import org.knowm.xchange.exceptions.DepositAddressCreationException;
import org.knowm.xchange.exceptions.DepositAddressNotFoundException;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.kraken.KrakenAdapters;
import org.knowm.xchange.kraken.KrakenUtils;
Expand All @@ -22,6 +25,8 @@
import org.knowm.xchange.kraken.dto.account.KrakenTradeBalanceInfo;
import org.knowm.xchange.kraken.dto.account.LedgerType;
import org.knowm.xchange.service.account.AccountService;
import org.knowm.xchange.service.account.params.DefaultRequestDepositAddressParams;
import org.knowm.xchange.service.account.params.RequestDepositAddressParams;
import org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamsTimeSpan;
import org.knowm.xchange.service.trade.params.DefaultWithdrawFundsParams;
import org.knowm.xchange.service.trade.params.HistoryParamsFundingType;
Expand Down Expand Up @@ -89,50 +94,88 @@ public String withdrawFunds(WithdrawFundsParams params) throws IOException {
throw new IllegalStateException("Don't know how to withdraw: " + params);
}

@Override
public AddressWithTag requestDepositAddressData(Currency currency, String... args)
throws IOException {
return requestDepositAddressData(DefaultRequestDepositAddressParams.create(currency, args));
}

@Override
public String requestDepositAddress(Currency currency, String... args) throws IOException {
return requestDepositAddressData(DefaultRequestDepositAddressParams.create(currency, args))
.getAddress();
}

@Override
public String requestDepositAddress(RequestDepositAddressParams requestDepositAddressParams)
throws IOException {
return requestDepositAddressData(requestDepositAddressParams).getAddress();
}

@Override
public AddressWithTag requestDepositAddressData(
RequestDepositAddressParams requestDepositAddressParams) throws IOException {
Currency currency = requestDepositAddressParams.getCurrency();
boolean newAddress = requestDepositAddressParams.isNewAddress();
String depositMethod = null;

KrakenDepositAddress[] depositAddresses;
if (Currency.BTC.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Bitcoin", false);
depositAddresses = getDepositAddresses(currency.toString(), "Bitcoin", newAddress);
} else if (Currency.LTC.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Litecoin", false);
depositAddresses = getDepositAddresses(currency.toString(), "Litecoin", newAddress);
} else if (Currency.ETH.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Ethereum (ERC20)", false);
depositAddresses = getDepositAddresses(currency.toString(), "Ethereum (ERC20)", newAddress);
} else if (Currency.ZEC.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Zcash (Transparent)", false);
depositAddresses =
getDepositAddresses(currency.toString(), "Zcash (Transparent)", newAddress);
} else if (Currency.ADA.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "ADA", false);
depositAddresses = getDepositAddresses(currency.toString(), "ADA", newAddress);
} else if (Currency.XMR.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Monero", false);
depositAddresses = getDepositAddresses(currency.toString(), "Monero", newAddress);
} else if (Currency.XRP.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Ripple XRP", false);
depositAddresses = getDepositAddresses(currency.toString(), "Ripple XRP", newAddress);
} else if (Currency.XLM.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Stellar XLM", false);
depositAddresses = getDepositAddresses(currency.toString(), "Stellar XLM", newAddress);
} else if (Currency.BCH.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Bitcoin Cash", false);
depositAddresses = getDepositAddresses(currency.toString(), "Bitcoin Cash", newAddress);
} else if (Currency.REP.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "REP", false);
depositAddresses = getDepositAddresses(currency.toString(), "REP", newAddress);
} else if (Currency.USD.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "SynapsePay (US Wire)", false);
depositAddresses =
getDepositAddresses(currency.toString(), "SynapsePay (US Wire)", newAddress);
} else if (Currency.XDG.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Dogecoin", false);
depositAddresses = getDepositAddresses(currency.toString(), "Dogecoin", newAddress);
} else if (Currency.MLN.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "MLN", false);
depositAddresses = getDepositAddresses(currency.toString(), "MLN", newAddress);
} else if (Currency.GNO.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "GNO", false);
depositAddresses = getDepositAddresses(currency.toString(), "GNO", newAddress);
} else if (Currency.QTUM.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "QTUM", false);
depositAddresses = getDepositAddresses(currency.toString(), "QTUM", newAddress);
} else if (Currency.XTZ.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "XTZ", false);
depositAddresses = getDepositAddresses(currency.toString(), "XTZ", newAddress);
} else if (Currency.ATOM.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Cosmos", false);
depositAddresses = getDepositAddresses(currency.toString(), "Cosmos", newAddress);
} else if (Currency.EOS.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "EOS", false);
depositAddresses = getDepositAddresses(currency.toString(), "EOS", newAddress);
} else if (Currency.DASH.equals(currency)) {
depositAddresses = getDepositAddresses(currency.toString(), "Dash", false);
depositAddresses = getDepositAddresses(currency.toString(), "Dash", newAddress);
} else {
throw new RuntimeException("Not implemented yet, Kraken works only for BTC and LTC");
depositMethod = findDepositMethod(currency, requestDepositAddressParams.getNetwork());
depositAddresses = getDepositAddresses(currency.toString(), depositMethod, newAddress);
}

if (depositAddresses.length == 0 && !newAddress) {
throw new DepositAddressNotFoundException(
String.format("No deposit addresses found for %s method: %s", currency, depositMethod));
}

if (depositAddresses.length == 0) {
throw new DepositAddressCreationException(
String.format(
"Deposit address could not be created for %s method: %s", currency, depositMethod));
}

return KrakenAdapters.adaptKrakenDepositAddress(depositAddresses);
}

Expand Down
Loading

0 comments on commit 74c9e34

Please sign in to comment.