From 3207a8227c35ce8ecb157ad455f52bf4adfe6404 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 15 Aug 2023 11:43:43 +0800 Subject: [PATCH] pkg/exchange: add QueryOrder api --- ..._request.go => get_open_orders_request.go} | 0 .../get_order_histories_request_requestgen.go | 13 +++++++ .../bybitapi/get_order_history_request.go | 7 ++-- pkg/exchange/bybit/exchange.go | 34 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) rename pkg/exchange/bybit/bybitapi/{get_open_order_request.go => get_open_orders_request.go} (100%) diff --git a/pkg/exchange/bybit/bybitapi/get_open_order_request.go b/pkg/exchange/bybit/bybitapi/get_open_orders_request.go similarity index 100% rename from pkg/exchange/bybit/bybitapi/get_open_order_request.go rename to pkg/exchange/bybit/bybitapi/get_open_orders_request.go diff --git a/pkg/exchange/bybit/bybitapi/get_order_histories_request_requestgen.go b/pkg/exchange/bybit/bybitapi/get_order_histories_request_requestgen.go index 4fafe108b..479d9eff4 100644 --- a/pkg/exchange/bybit/bybitapi/get_order_histories_request_requestgen.go +++ b/pkg/exchange/bybit/bybitapi/get_order_histories_request_requestgen.go @@ -28,6 +28,11 @@ func (g *GetOrderHistoriesRequest) OrderId(orderId string) *GetOrderHistoriesReq return g } +func (g *GetOrderHistoriesRequest) OrderLinkId(orderLinkId string) *GetOrderHistoriesRequest { + g.orderLinkId = &orderLinkId + return g +} + func (g *GetOrderHistoriesRequest) OrderFilter(orderFilter string) *GetOrderHistoriesRequest { g.orderFilter = &orderFilter return g @@ -93,6 +98,14 @@ func (g *GetOrderHistoriesRequest) GetQueryParameters() (url.Values, error) { params["orderId"] = orderId } else { } + // check orderLinkId field -> json key orderLinkId + if g.orderLinkId != nil { + orderLinkId := *g.orderLinkId + + // assign parameter of orderLinkId + params["orderLinkId"] = orderLinkId + } else { + } // check orderFilter field -> json key orderFilter if g.orderFilter != nil { orderFilter := *g.orderFilter diff --git a/pkg/exchange/bybit/bybitapi/get_order_history_request.go b/pkg/exchange/bybit/bybitapi/get_order_history_request.go index 7d46c8eff..7731bc6af 100644 --- a/pkg/exchange/bybit/bybitapi/get_order_history_request.go +++ b/pkg/exchange/bybit/bybitapi/get_order_history_request.go @@ -9,14 +9,15 @@ import ( //go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Result //go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Result -//go:generate GetRequest -url "/v5/order/history" -type GetOrderHistoriesRequest -responseDataType .OpenOrdersResponse +//go:generate GetRequest -url "/v5/order/history" -type GetOrderHistoriesRequest -responseDataType .OrdersResponse type GetOrderHistoriesRequest struct { client requestgen.AuthenticatedAPIClient category Category `param:"category,query" validValues:"spot"` - symbol *string `param:"symbol,query"` - orderId *string `param:"orderId,query"` + symbol *string `param:"symbol,query"` + orderId *string `param:"orderId,query"` + orderLinkId *string `param:"orderLinkId,query"` // orderFilter supports 3 types of Order: // 1. active order, 2. StopOrder: conditional order, 3. tpslOrder: spot TP/SL order // Normal spot: return Order active order by default diff --git a/pkg/exchange/bybit/exchange.go b/pkg/exchange/bybit/exchange.go index 01ba110ff..94f0ce4e4 100644 --- a/pkg/exchange/bybit/exchange.go +++ b/pkg/exchange/bybit/exchange.go @@ -2,6 +2,7 @@ package bybit import ( "context" + "errors" "fmt" "strconv" "time" @@ -183,6 +184,39 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [ return orders, nil } +func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error) { + if len(q.OrderID) == 0 && len(q.ClientOrderID) == 0 { + return nil, errors.New("one of OrderID/ClientOrderID is required parameter") + } + + if len(q.OrderID) != 0 && len(q.ClientOrderID) != 0 { + return nil, errors.New("only accept one parameter of OrderID/ClientOrderID") + } + + req := e.client.NewGetOrderHistoriesRequest() + if len(q.Symbol) != 0 { + req.Symbol(q.Symbol) + } + + if len(q.OrderID) != 0 { + req.OrderId(q.OrderID) + } + + if len(q.ClientOrderID) != 0 { + req.OrderLinkId(q.ClientOrderID) + } + + res, err := req.Do(ctx) + if err != nil { + return nil, fmt.Errorf("failed to query order, queryConfig: %+v, err: %w", q, err) + } + if len(res.List) != 1 { + return nil, fmt.Errorf("unexpected order length, queryConfig: %+v", q) + } + + return toGlobalOrder(res.List[0]) +} + func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) { if len(order.Market.Symbol) == 0 { return nil, fmt.Errorf("order.Market.Symbol is required: %+v", order)