Skip to content

Commit

Permalink
[coinex] Add cancelling of orders
Browse files Browse the repository at this point in the history
  • Loading branch information
bigscoop committed Dec 13, 2024
1 parent 038860c commit 40d5af3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ CoinexResponse<CoinexOrder> createOrder(
CoinexOrder coinexOrder)
throws IOException, CoinexException;

@POST
@Path("v2/spot/cancel-order")
@Consumes(MediaType.APPLICATION_JSON)
CoinexResponse<CoinexOrder> cancelOrder(
@HeaderParam("X-COINEX-KEY") String apiKey,
@HeaderParam("X-COINEX-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@HeaderParam("X-COINEX-SIGN") ParamsDigest signer,
CoinexCancelOrderRequest coinexCancelOrderRequest)
throws IOException, CoinexException;

@GET
@Path("v2/spot/order-status")
CoinexResponse<CoinexOrder> orderStatus(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.knowm.xchange.coinex.dto.trade;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.coinex.config.converter.CurrencyPairToStringConverter;
import org.knowm.xchange.coinex.dto.account.CoinexMarketType;
import org.knowm.xchange.instrument.Instrument;

@Data
@Builder
@Jacksonized
public class CoinexCancelOrderRequest {

@JsonProperty("market")
@JsonSerialize(converter = CurrencyPairToStringConverter.class)
Instrument instrument;

@JsonProperty("market_type")
private CoinexMarketType marketType;

@JsonProperty("order_id")
private Long orderId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,26 @@ public Collection<Order> getOrder(OrderQueryParams... orderQueryParams) throws I
throw CoinexErrorAdapter.adapt(e);
}
}

@Override
public boolean cancelOrder(CancelOrderParams orderParams) throws IOException {
Validate.isInstanceOf(DefaultCancelOrderByInstrumentAndIdParams.class, orderParams);
DefaultCancelOrderByInstrumentAndIdParams params =
(DefaultCancelOrderByInstrumentAndIdParams) orderParams;

try {
CoinexOrder order = cancelOrder(Long.valueOf(params.getOrderId()), params.getInstrument());
return Objects.equals(order.getOrderId(), Long.valueOf(params.getOrderId()));
} catch (CoinexException e) {
throw CoinexErrorAdapter.adapt(e);
}

}

@Override
public Class[] getRequiredCancelOrderParamClasses() {
return new Class[] {DefaultCancelOrderByInstrumentAndIdParams.class};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public CoinexOrder createOrder(CoinexOrder coinexOrder) throws IOException {
.getData();
}

public CoinexOrder cancelOrder(Long orderId, Instrument instrument) throws IOException {
CoinexCancelOrderRequest request = CoinexCancelOrderRequest.builder()
.orderId(orderId)
.instrument(instrument)
.marketType(CoinexMarketType.SPOT)
.build();
return coinexAuthenticated
.cancelOrder(apiKey, exchange.getNonceFactory(), coinexV2ParamsDigest, request)
.getData();
}

public CoinexOrder orderStatus(Instrument instrument, String orderId) throws IOException {
String market = CoinexAdapters.toString(instrument);
return coinexAuthenticated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.service.trade.TradeService;
import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams;
import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument;

class CoinexTradeServiceTest extends CoinexExchangeWiremock {
Expand Down Expand Up @@ -38,4 +39,11 @@ void filtered_open_orders() throws IOException {
.isEqualTo(CurrencyPair.BTC_USDT);
}

@Test
void valid_cancel_order() throws IOException {
boolean actual = tradeService.cancelOrder(new DefaultCancelOrderByInstrumentAndIdParams(CurrencyPair.BTC_USDT, "136215219959"));
assertThat(actual).isTrue();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"code": 0,
"data": {
"amount": "0.00005263",
"base_fee": "0",
"ccy": "BTC",
"client_id": "",
"created_at": 1734126246894,
"discount_fee": "0",
"filled_amount": "0",
"filled_value": "0",
"last_fill_amount": "0",
"last_fill_price": "0",
"maker_fee_rate": "0.0018",
"market": "BTCUSDT",
"market_type": "SPOT",
"order_id": 136215219959,
"price": "95000.99",
"quote_fee": "0",
"side": "buy",
"taker_fee_rate": "0.0018",
"type": "limit",
"unfilled_amount": "0.00005263",
"updated_at": 1734126246894
},
"message": "OK"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id" : "339fb318-39e7-49de-9c83-452a2a524977",
"name" : "v2_spot_cancel-order",
"request" : {
"url" : "/v2/spot/cancel-order",
"method" : "POST",
"bodyPatterns" : [ {
"equalToJson" : "{\"market\":\"BTCUSDT\",\"market_type\":\"SPOT\",\"order_id\":136215219959}",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
},
"response" : {
"status" : 200,
"bodyFileName" : "v2_spot_cancel-order.json"
},
"uuid" : "339fb318-39e7-49de-9c83-452a2a524977",
"persistent" : true,
"insertionIndex" : 4
}

0 comments on commit 40d5af3

Please sign in to comment.