From 996d678a454bb77592f07062859a65fea09942e6 Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Fri, 6 Dec 2024 01:17:47 +0100 Subject: [PATCH 1/3] [gateio] Fix getting of open limit order details --- .../knowm/xchange/gateio/GateioAdapters.java | 27 +++++++++------- .../service/GateioTradeServiceTest.java | 20 ++++++++++++ .../api_v4_spot_orders-open-limit-order.json | 32 +++++++++++++++++++ .../api_v4_spot_orders-open-limit-order.json | 15 +++++++++ .../src/test/resources/rest/spot.http | 13 ++++++++ 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-open-limit-order.json create mode 100644 xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-open-limit-order.json diff --git a/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/GateioAdapters.java b/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/GateioAdapters.java index 9a29de2a953..1ee986e6fd0 100644 --- a/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/GateioAdapters.java +++ b/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/GateioAdapters.java @@ -151,16 +151,21 @@ public Order toOrder(GateioOrder gateioOrder) { throw new IllegalArgumentException("Can't map " + gateioOrder.getType()); } - if (orderType == OrderType.BID) { - builder.cumulativeAmount(gateioOrder.getFilledTotalQuote()); - } else if (orderType == OrderType.ASK) { - BigDecimal filledAssetAmount = - gateioOrder - .getFilledTotalQuote() - .divide(gateioOrder.getAvgDealPrice(), MathContext.DECIMAL32); - builder.cumulativeAmount(filledAssetAmount); - } else { - throw new IllegalArgumentException("Can't map " + orderType); + // if filled then calculate amounts + OrderStatus status = toOrderStatus(gateioOrder.getStatus()); + + if (status == OrderStatus.FILLED) { + if (orderType == OrderType.BID) { + builder.cumulativeAmount(gateioOrder.getFilledTotalQuote()); + } else if (orderType == OrderType.ASK) { + BigDecimal filledAssetAmount = + gateioOrder + .getFilledTotalQuote() + .divide(gateioOrder.getAvgDealPrice(), MathContext.DECIMAL32); + builder.cumulativeAmount(filledAssetAmount); + } else { + throw new IllegalArgumentException("Can't map " + orderType); + } } return builder @@ -168,7 +173,7 @@ public Order toOrder(GateioOrder gateioOrder) { .originalAmount(gateioOrder.getAmount()) .userReference(gateioOrder.getClientOrderId()) .timestamp(Date.from(gateioOrder.getCreatedAt())) - .orderStatus(toOrderStatus(gateioOrder.getStatus())) + .orderStatus(status) .averagePrice(gateioOrder.getAvgDealPrice()) .fee(gateioOrder.getFee()) .build(); diff --git a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java index 4fba1c32c26..a3e09991507 100644 --- a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java +++ b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java @@ -142,6 +142,26 @@ void sell_order_details() throws IOException { assertThat(orders).first().usingRecursiveComparison().isEqualTo(expected); } + @Test + void open_limit_order_details() throws IOException { + LimitOrder expected = + new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USDT) + .id("745504484392") + .limitPrice(new BigDecimal("80000")) + .timestamp(Date.from(Instant.parse("2024-12-05T23:46:54.447Z"))) + .originalAmount(new BigDecimal("0.00012")) + .orderStatus(OrderStatus.OPEN) + .fee(BigDecimal.ZERO) + .userReference("web") + .build(); + + Collection orders = + gateioTradeService.getOrder( + new DefaultQueryOrderParamInstrument(CurrencyPair.BTC_USDT, "745504484392")); + assertThat(orders).hasSize(1); + assertThat(orders).first().usingRecursiveComparison().isEqualTo(expected); + } + @Test void trade_history() throws IOException { UserTrades userTrades = diff --git a/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-open-limit-order.json b/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-open-limit-order.json new file mode 100644 index 00000000000..74944c8b00f --- /dev/null +++ b/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-open-limit-order.json @@ -0,0 +1,32 @@ +{ + "id": "745504484392", + "text": "web", + "amend_text": "-", + "create_time": "1733442414", + "update_time": "1733442414", + "create_time_ms": 1733442414447, + "update_time_ms": 1733442414447, + "status": "open", + "currency_pair": "BTC_USDT", + "type": "limit", + "account": "spot", + "side": "buy", + "amount": "0.00012", + "price": "80000", + "time_in_force": "gtc", + "iceberg": "0", + "left": "0.00012", + "filled_amount": "0", + "fill_price": "0", + "filled_total": "0", + "fee": "0", + "fee_currency": "BTC", + "point_fee": "0", + "gt_fee": "0", + "gt_maker_fee": "0", + "gt_taker_fee": "0", + "gt_discount": false, + "rebated_fee": "0", + "rebated_fee_currency": "USDT", + "finish_as": "open" +} \ No newline at end of file diff --git a/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-open-limit-order.json b/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-open-limit-order.json new file mode 100644 index 00000000000..010b58799de --- /dev/null +++ b/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-open-limit-order.json @@ -0,0 +1,15 @@ +{ + "id" : "f8252706-fe0c-4407-b415-02ae3999fc40", + "name" : "api_v4_spot_orders_745504484392", + "request" : { + "url" : "/api/v4/spot/orders/745504484392?currency_pair=BTC_USDT", + "method" : "GET" + }, + "response" : { + "status" : 200, + "bodyFileName" : "api_v4_spot_orders-open-limit-order.json" + }, + "uuid" : "f8252706-fe0c-4407-b415-02ae3999fc40", + "persistent" : true, + "insertionIndex" : 38 +} \ No newline at end of file diff --git a/xchange-gateio-v4/src/test/resources/rest/spot.http b/xchange-gateio-v4/src/test/resources/rest/spot.http index f4e0c246a4c..a6fb2ad4795 100644 --- a/xchange-gateio-v4/src/test/resources/rest/spot.http +++ b/xchange-gateio-v4/src/test/resources/rest/spot.http @@ -83,3 +83,16 @@ SIGN: {{sign}} Timestamp: {{timestamp}} Content-Type: application/json + +### Get a single order +< {% + import {gen_sign} from 'sign.js' + gen_sign("GET", request); +%} + +GET {{api_v4}}/spot/orders/745504484392?currency_pair=BTC_USDT +KEY: {{api_key}} +SIGN: {{sign}} +Timestamp: {{timestamp}} +Content-Type: application/json + From 38833273aab371133be1335e6db33338d649b822 Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Fri, 6 Dec 2024 01:18:28 +0100 Subject: [PATCH 2/3] [gateio] Implement getting of open orders --- .../gateio/service/GateioTradeService.java | 14 ++++++++ .../service/GateioTradeServiceTest.java | 21 ++++++++++++ .../resources/__files/api_v4_spot_orders.json | 34 +++++++++++++++++++ .../mappings/api_v4_spot_orders.json | 15 ++++++++ 4 files changed, 84 insertions(+) create mode 100644 xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders.json create mode 100644 xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders.json diff --git a/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/service/GateioTradeService.java b/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/service/GateioTradeService.java index 4cd26795ad8..f86e2421b26 100644 --- a/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/service/GateioTradeService.java +++ b/xchange-gateio-v4/src/main/java/org/knowm/xchange/gateio/service/GateioTradeService.java @@ -11,6 +11,7 @@ import org.knowm.xchange.dto.marketdata.Trades.TradeSortType; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.dto.trade.MarketOrder; +import org.knowm.xchange.dto.trade.OpenOrders; import org.knowm.xchange.dto.trade.UserTrade; import org.knowm.xchange.dto.trade.UserTrades; import org.knowm.xchange.gateio.GateioAdapters; @@ -23,7 +24,9 @@ import org.knowm.xchange.service.trade.TradeService; import org.knowm.xchange.service.trade.params.CancelOrderParams; import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams; +import org.knowm.xchange.service.trade.params.InstrumentParam; import org.knowm.xchange.service.trade.params.TradeHistoryParams; +import org.knowm.xchange.service.trade.params.orders.OpenOrdersParams; import org.knowm.xchange.service.trade.params.orders.OrderQueryParamInstrument; import org.knowm.xchange.service.trade.params.orders.OrderQueryParams; @@ -33,6 +36,17 @@ public GateioTradeService(GateioExchange exchange) { super(exchange); } + @Override + public OpenOrders getOpenOrders(OpenOrdersParams params) throws IOException { + Validate.isInstanceOf(InstrumentParam.class, params); + Instrument instrument = ((InstrumentParam) params).getInstrument(); + List limitOrders = listOrders(instrument, OrderStatus.OPEN).stream() + .map(GateioAdapters::toOrder) + .map(LimitOrder.class::cast) + .collect(Collectors.toList()); + return new OpenOrders(limitOrders); + } + @Override public String placeMarketOrder(MarketOrder marketOrder) throws IOException { try { diff --git a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java index a3e09991507..165868fb703 100644 --- a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java +++ b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceTest.java @@ -16,6 +16,7 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.dto.trade.MarketOrder; +import org.knowm.xchange.dto.trade.OpenOrders; import org.knowm.xchange.dto.trade.UserTrade; import org.knowm.xchange.dto.trade.UserTrades; import org.knowm.xchange.exceptions.FundsExceededException; @@ -24,6 +25,7 @@ import org.knowm.xchange.gateio.dto.trade.Role; import org.knowm.xchange.gateio.service.params.GateioTradeHistoryParams; import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams; +import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument; import org.knowm.xchange.service.trade.params.orders.DefaultQueryOrderParamInstrument; class GateioTradeServiceTest extends GateioExchangeWiremock { @@ -162,6 +164,25 @@ void open_limit_order_details() throws IOException { assertThat(orders).first().usingRecursiveComparison().isEqualTo(expected); } + @Test + void open_orders() throws IOException { + LimitOrder expected = + new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USDT) + .id("745504484392") + .limitPrice(new BigDecimal("80000")) + .timestamp(Date.from(Instant.parse("2024-12-05T23:46:54.447Z"))) + .originalAmount(new BigDecimal("0.00012")) + .orderStatus(OrderStatus.OPEN) + .fee(BigDecimal.ZERO) + .userReference("web") + .build(); + + OpenOrders openOrders = + gateioTradeService.getOpenOrders(new DefaultOpenOrdersParamInstrument(CurrencyPair.BTC_USDT)); + assertThat(openOrders.getOpenOrders()).hasSize(1); + assertThat(openOrders.getOpenOrders()).first().usingRecursiveComparison().isEqualTo(expected); + } + @Test void trade_history() throws IOException { UserTrades userTrades = diff --git a/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders.json b/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders.json new file mode 100644 index 00000000000..fb8fb3c0a31 --- /dev/null +++ b/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders.json @@ -0,0 +1,34 @@ +[ + { + "id": "745504484392", + "text": "web", + "amend_text": "-", + "create_time": "1733442414", + "update_time": "1733442414", + "create_time_ms": 1733442414447, + "update_time_ms": 1733442414447, + "status": "open", + "currency_pair": "BTC_USDT", + "type": "limit", + "account": "spot", + "side": "buy", + "amount": "0.00012", + "price": "80000", + "time_in_force": "gtc", + "iceberg": "0", + "left": "0.00012", + "filled_amount": "0", + "fill_price": "0", + "filled_total": "0", + "fee": "0", + "fee_currency": "BTC", + "point_fee": "0", + "gt_fee": "0", + "gt_maker_fee": "0", + "gt_taker_fee": "0", + "gt_discount": false, + "rebated_fee": "0", + "rebated_fee_currency": "USDT", + "finish_as": "open" + } +] \ No newline at end of file diff --git a/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders.json b/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders.json new file mode 100644 index 00000000000..43d49b50f95 --- /dev/null +++ b/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders.json @@ -0,0 +1,15 @@ +{ + "id" : "703035f1-28e6-4e58-8af5-7a486fe294bb", + "name" : "api_v4_spot_orders", + "request" : { + "url" : "/api/v4/spot/orders?currency_pair=BTC_USDT&status=open", + "method" : "GET" + }, + "response" : { + "status" : 200, + "bodyFileName" : "api_v4_spot_orders.json" + }, + "uuid" : "703035f1-28e6-4e58-8af5-7a486fe294bb", + "persistent" : true, + "insertionIndex" : 37 +} \ No newline at end of file From 80a2b3944236129fd5586da3c3665ef0a137266a Mon Sep 17 00:00:00 2001 From: Dmitri Karpovich Date: Fri, 6 Dec 2024 01:32:40 +0100 Subject: [PATCH 3/3] [gateio] Remove redundant test --- .../service/GateioTradeServiceRawTest.java | 12 ------- .../__files/api_v4_spot_orders-btc-open.json | 34 ------------------- .../mappings/api_v4_spot_orders-btc-open.json | 15 -------- 3 files changed, 61 deletions(-) delete mode 100644 xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-btc-open.json delete mode 100644 xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-btc-open.json diff --git a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceRawTest.java b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceRawTest.java index 130221821be..d6a797e7e53 100644 --- a/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceRawTest.java +++ b/xchange-gateio-v4/src/test/java/org/knowm/xchange/gateio/service/GateioTradeServiceRawTest.java @@ -5,11 +5,9 @@ import java.io.IOException; import java.math.BigDecimal; import java.time.Instant; -import java.util.List; import org.junit.jupiter.api.Test; import org.knowm.xchange.currency.Currency; import org.knowm.xchange.currency.CurrencyPair; -import org.knowm.xchange.dto.Order.OrderStatus; import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.gateio.GateioExchangeWiremock; import org.knowm.xchange.gateio.dto.account.GateioOrder; @@ -49,16 +47,6 @@ class GateioTradeServiceRawTest extends GateioExchangeWiremock { .finishAs("filled") .build(); - @Test - void listOrders() throws IOException { - List orders = - gateioTradeServiceRaw.listOrders(CurrencyPair.BTC_USDT, OrderStatus.OPEN); - - assertThat(orders).hasSize(1); - assertThat(orders.get(0).getId()).isEqualTo("339440374909"); - assertThat(orders.get(0).getStatus()).isEqualTo("open"); - } - @Test void valid_market_buy_order() throws IOException { GateioOrder gateioOrder = diff --git a/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-btc-open.json b/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-btc-open.json deleted file mode 100644 index 710f1a2b209..00000000000 --- a/xchange-gateio-v4/src/test/resources/__files/api_v4_spot_orders-btc-open.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - { - "id": "339440374909", - "text": "3", - "amend_text": "-", - "create_time": "1685391045", - "update_time": "1685391045", - "create_time_ms": 1685391045952, - "update_time_ms": 1685391045952, - "status": "open", - "currency_pair": "BTC_USDT", - "type": "limit", - "account": "spot", - "side": "buy", - "amount": "0.0001", - "price": "22053", - "biz_info": "-", - "time_in_force": "gtc", - "iceberg": "0", - "left": "0.0001", - "fill_price": "0", - "filled_total": "0", - "fee": "0", - "fee_currency": "BTC", - "point_fee": "0", - "gt_fee": "0", - "gt_maker_fee": "0", - "gt_taker_fee": "0", - "gt_discount": false, - "rebated_fee": "0", - "rebated_fee_currency": "USDT", - "finish_as": "open" - } -] \ No newline at end of file diff --git a/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-btc-open.json b/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-btc-open.json deleted file mode 100644 index ad26c081532..00000000000 --- a/xchange-gateio-v4/src/test/resources/mappings/api_v4_spot_orders-btc-open.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "id" : "8597ffe9-f28d-40d6-9a47-5f378dd53515", - "name" : "api_v4_spot_orders", - "request" : { - "url" : "/api/v4/spot/orders?currency_pair=BTC_USDT&status=open", - "method" : "GET" - }, - "response" : { - "status" : 200, - "bodyFileName" : "api_v4_spot_orders-btc-open.json" - }, - "uuid" : "8597ffe9-f28d-40d6-9a47-5f378dd53515", - "persistent" : true, - "insertionIndex" : 16 -} \ No newline at end of file