Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/github.com/knowm/XChange/d…
Browse files Browse the repository at this point in the history
…evelop' into PR/bybit-concurrent-fix
  • Loading branch information
rizer1980 committed Jun 16, 2024
2 parents 27ab637 + 341810d commit 750b814
Show file tree
Hide file tree
Showing 41 changed files with 1,373 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import org.knowm.xchange.bitstamp.dto.BitstampException;
import org.knowm.xchange.bitstamp.dto.BitstampTransferBalanceResponse;
import org.knowm.xchange.bitstamp.dto.account.BitstampBalance;
import org.knowm.xchange.bitstamp.dto.account.BitstampDepositAddress;
import org.knowm.xchange.bitstamp.dto.account.BitstampRippleDepositAddress;
import org.knowm.xchange.bitstamp.dto.account.BitstampWithdrawal;
import org.knowm.xchange.bitstamp.dto.account.DepositTransaction;
import org.knowm.xchange.bitstamp.dto.account.WithdrawalFee;
import org.knowm.xchange.bitstamp.dto.account.WithdrawalRequest;
import org.knowm.xchange.bitstamp.dto.trade.BitstampCancelAllOrdersResponse;
import org.knowm.xchange.bitstamp.dto.trade.BitstampOrder;
Expand Down Expand Up @@ -646,7 +648,7 @@ BitstampTransferBalanceResponse transferSubAccountBalanceToMain(
@POST
@Path("withdrawal-requests/")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
WithdrawalRequest[] getWithdrawalRequests(
List<WithdrawalRequest> getWithdrawalRequests(
@HeaderParam("X-Auth") String apiKey,
@HeaderParam("X-Auth-Signature") ParamsDigest signer,
@HeaderParam("X-Auth-Nonce") SynchronizedValueFactory<String> nonce,
Expand All @@ -655,6 +657,16 @@ WithdrawalRequest[] getWithdrawalRequests(
@FormParam("timedelta") Long timeDelta)
throws BitstampException, IOException;

@POST
@Path("fees/withdrawal/")
List<WithdrawalFee> getWithdrawalFees(
@HeaderParam("X-Auth") String apiKey,
@HeaderParam("X-Auth-Signature") ParamsDigest signer,
@HeaderParam("X-Auth-Nonce") SynchronizedValueFactory<String> nonce,
@HeaderParam("X-Auth-Timestamp") SynchronizedValueFactory<String> timeStamp,
@HeaderParam("X-Auth-Version") String version)
throws BitstampException, IOException;

@POST
@Path("withdrawal/open/")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.knowm.xchange.bitstamp;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.knowm.xchange.BaseExchange;
Expand Down Expand Up @@ -68,8 +68,8 @@ public SynchronizedValueFactory<String> getTimestampFactory() {
public void remoteInit() throws IOException, ExchangeException {
BitstampMarketDataServiceRaw dataService =
(BitstampMarketDataServiceRaw) this.marketDataService;
BitstampPairInfo[] bitstampPairInfos = dataService.getTradingPairsInfo();
List<BitstampPairInfo> bitstampPairInfos = dataService.getTradingPairsInfo();
exchangeMetaData =
BitstampAdapters.adaptMetaData(Arrays.asList(bitstampPairInfos), exchangeMetaData);
BitstampAdapters.adaptMetaData(bitstampPairInfos, exchangeMetaData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BitstampTransaction[] getTransactions(

@GET
@Path("trading-pairs-info/")
BitstampPairInfo[] getTradingPairsInfo() throws IOException, BitstampException;
List<BitstampPairInfo> getTradingPairsInfo() throws IOException, BitstampException;

class Pair {
public final CurrencyPair pair;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.knowm.xchange.bitstamp.dto.account;

import java.math.BigDecimal;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.currency.Currency;

@Jacksonized
@Value
@Builder
public class WithdrawalFee {

String network;

BigDecimal fee;

Currency currency;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.knowm.xchange.bitstamp.dto.account.BitstampRippleDepositAddress;
import org.knowm.xchange.bitstamp.dto.account.BitstampWithdrawal;
import org.knowm.xchange.bitstamp.dto.account.DepositTransaction;
import org.knowm.xchange.bitstamp.dto.account.WithdrawalFee;
import org.knowm.xchange.bitstamp.dto.account.WithdrawalRequest;
import org.knowm.xchange.bitstamp.dto.trade.BitstampUserTransaction;
import org.knowm.xchange.client.ExchangeRestProxyBuilder;
Expand Down Expand Up @@ -349,16 +350,27 @@ public List<DepositTransaction> getUnconfirmedDeposits() throws IOException {
public List<WithdrawalRequest> getWithdrawalRequests(Long timeDelta) throws IOException {

try {
final List<WithdrawalRequest> response =
Arrays.asList(
bitstampAuthenticatedV2.getWithdrawalRequests(
return bitstampAuthenticatedV2.getWithdrawalRequests(
apiKeyForV2Requests,
signatureCreatorV2,
uuidNonceFactory,
timestampFactory,
API_VERSION,
timeDelta));
return response;
timeDelta);
} catch (BitstampException e) {
throw handleError(e);
}
}

public List<WithdrawalFee> getWithdrawalFees() throws IOException {

try {
return bitstampAuthenticatedV2.getWithdrawalFees(
apiKeyForV2Requests,
signatureCreatorV2,
uuidNonceFactory,
timestampFactory,
API_VERSION);
} catch (BitstampException e) {
throw handleError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public BitstampTransaction[] getTransactions(CurrencyPair pair, @Nullable Bitsta
}
}

public BitstampPairInfo[] getTradingPairsInfo() throws IOException {
public List<BitstampPairInfo> getTradingPairsInfo() throws IOException {
try {
return bitstampV2.getTradingPairsInfo();
} catch (BitstampException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.knowm.xchange.bitstamp.dto.account;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import org.junit.Test;

public class WithdrawalFeesJSONTest {

@Test
public void testUnmarshal() throws IOException {

// Read in the JSON from the example resources
InputStream is =
WithdrawalFeesJSONTest.class.getResourceAsStream(
"/org/knowm/xchange/bitstamp/dto/account/withdrawal-fees.json");

// Use Jackson to parse it
ObjectMapper mapper = new ObjectMapper();

List<WithdrawalFee> withdrawalFees =
mapper.readValue(
is,
mapper.getTypeFactory().constructCollectionType(List.class, WithdrawalFee.class));

assertThat(withdrawalFees.size()).isEqualTo(1);
assertThat(withdrawalFees.get(0).getNetwork()).isEqualTo("bitcoin");
assertThat(withdrawalFees.get(0).getCurrency().getCurrencyCode()).isEqualToIgnoringCase("btc");
assertThat(withdrawalFees.get(0).getFee()).isEqualTo(new BigDecimal("0.00015000"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"currency": "btc",
"fee": "0.00015000",
"network": "bitcoin"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import org.knowm.xchange.bybit.dto.trade.BybitAmendOrderPayload;
import org.knowm.xchange.bybit.dto.trade.BybitCancelOrderPayload;
import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.account.allcoins.BybitAllCoinsBalance;
Expand Down Expand Up @@ -106,6 +107,19 @@ BybitResult<BybitOrderResponse> placeLimitOrder(
BybitPlaceOrderPayload payload)
throws IOException,BybitException;

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/v5/order/cancel-order">API</a>
*/
@POST
@Path("/order/cancel")
@Consumes(MediaType.APPLICATION_JSON)
BybitResult<BybitOrderResponse> cancelOrder(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
BybitCancelOrderPayload payload)
throws IOException,BybitException;

/**
* @apiSpec <https://bybit-exchange.github.io/docs/v5/order/amend-order">API</a>
*/
Expand All @@ -118,4 +132,5 @@ BybitResult<BybitOrderResponse> amendOrder(
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
BybitAmendOrderPayload payload)
throws IOException,BybitException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.knowm.xchange.bybit.dto.trade;

import lombok.Getter;
import org.knowm.xchange.bybit.Bybit;
import org.knowm.xchange.bybit.dto.BybitCategory;

@Getter
public class BybitCancelOrderPayload {

private BybitCategory category;
private String symbol;
private String orderId;
private String orderLinkId;
private String orderFilter;

public BybitCancelOrderPayload(BybitCategory category, String symbol, String orderId, String orderLinkId) {
this.category = category;
this.symbol = symbol;
this.orderId = orderId;
this.orderLinkId = orderLinkId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
import org.knowm.xchange.bybit.BybitAdapters;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTicker;
import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTickers;
import org.knowm.xchange.bybit.dto.trade.BybitOrderResponse;
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetail;
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.StopOrder;
import org.knowm.xchange.service.trade.TradeService;

public class BybitTradeService extends BybitTradeServiceRaw implements TradeService {
Expand Down Expand Up @@ -95,4 +92,15 @@ public String amendOrder(Order order) throws IOException {
return "";
}


public String cancelOrder(Order order) throws IOException {
BybitCategory category = BybitAdapters.getCategory(order.getInstrument());
BybitResult<BybitOrderResponse> response = cancelOrder(category,
convertToBybitSymbol(order.getInstrument()), order.getId(), order.getUserReference());
if (response != null) {
return response.getResult().getOrderId();
} else
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigDecimal;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.bybit.dto.trade.BybitCancelOrderPayload;
import org.knowm.xchange.bybit.dto.trade.BybitAmendOrderPayload;
import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
Expand Down Expand Up @@ -88,4 +89,19 @@ public BybitResult<BybitOrderResponse> amendOrder(BybitCategory category, String
return amendOrder;
}


public BybitResult<BybitOrderResponse> cancelOrder(BybitCategory category,String symbol,
String orderId, String orderLinkId) throws IOException {
BybitCancelOrderPayload payload = new BybitCancelOrderPayload(category, symbol, orderId, orderLinkId);
BybitResult<BybitOrderResponse> cancelOrder =
bybitAuthenticated.cancelOrder(
apiKey,
signatureCreator,
nonceFactory,
payload);
if (!cancelOrder.isSuccess()) {
throw createBybitExceptionFromResult(cancelOrder);
}
return cancelOrder;
}
}
Loading

0 comments on commit 750b814

Please sign in to comment.