From e09b4fa5fb2d457d1504b24565991b36b77fcc40 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 31 Dec 2021 01:50:56 +0800 Subject: [PATCH] kucoin: rewrite cancel all orders request --- examples/kucoin/orders.go | 22 +++++++- .../cancel_all_order_request_requestgen.go | 34 +++++++++++- pkg/exchange/kucoin/kucoinapi/trade.go | 54 +++++-------------- 3 files changed, 65 insertions(+), 45 deletions(-) diff --git a/examples/kucoin/orders.go b/examples/kucoin/orders.go index 26c303b70..7a5405c5c 100644 --- a/examples/kucoin/orders.go +++ b/examples/kucoin/orders.go @@ -3,10 +3,11 @@ package main import ( "context" - "github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + + "github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi" ) func init() { @@ -16,6 +17,7 @@ func init() { cancelOrderCmd.Flags().String("client-order-id", "", "client order id") cancelOrderCmd.Flags().String("order-id", "", "order id") + cancelOrderCmd.Flags().Bool("all", false, "cancel all") ordersCmd.AddCommand(cancelOrderCmd) placeOrderCmd.Flags().String("symbol", "", "symbol") @@ -145,8 +147,24 @@ var cancelOrderCmd = &cobra.Command{ SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - req := client.TradeService.NewCancelOrderRequest() + cancelAll, err := cmd.Flags().GetBool("all") + if err != nil { + return err + } + + if cancelAll { + req := client.TradeService.NewCancelAllOrderRequest() + req.TradeType("TRADE") + response, err := req.Do(context.Background()) + if err != nil { + return err + } + logrus.Infof("cancel all order response: %+v", response) + return nil + } + + req := client.TradeService.NewCancelOrderRequest() orderID, err := cmd.Flags().GetString("order-id") if err != nil { return err diff --git a/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_requestgen.go b/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_requestgen.go index 052a5fd06..0f0f2a55c 100644 --- a/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_requestgen.go +++ b/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_requestgen.go @@ -1,8 +1,9 @@ -// Code generated by "requestgen -type CancelAllOrderRequest"; DO NOT EDIT. +// Code generated by "requestgen -method DELETE -responseType .APIResponse -responseDataField Data -url /api/v1/orders -type CancelAllOrderRequest -responseDataType .CancelOrderResponse"; DO NOT EDIT. package kucoinapi import ( + "context" "encoding/json" "fmt" "net/url" @@ -107,3 +108,34 @@ func (r *CancelAllOrderRequest) GetSlugsMap() (map[string]string, error) { return slugs, nil } + +func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { + + params, err := r.GetParameters() + if err != nil { + return nil, err + } + query := url.Values{} + + apiURL := "/api/v1/orders" + + req, err := r.client.NewAuthenticatedRequest(ctx, "DELETE", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := r.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + var data CancelOrderResponse + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return &data, nil +} diff --git a/pkg/exchange/kucoin/kucoinapi/trade.go b/pkg/exchange/kucoin/kucoinapi/trade.go index 7322d5d14..ba57f41e3 100644 --- a/pkg/exchange/kucoin/kucoinapi/trade.go +++ b/pkg/exchange/kucoin/kucoinapi/trade.go @@ -2,6 +2,7 @@ package kucoinapi //go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data //go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data +//go:generate -command DeleteRequest requestgen -method DELETE -responseType .APIResponse -responseDataField Data import ( "context" @@ -255,13 +256,6 @@ type PlaceOrderRequest struct { timeInForce *TimeInForceType `param:"timeInForce,required"` } -//go:generate requestgen -type CancelOrderRequest -type CancelOrderRequest struct { - client *RestClient - - orderID *string `param:"orderID"` - clientOrderID *string `param:"clientOrderID"` -} type CancelOrderResponse struct { CancelledOrderIDs []string `json:"cancelledOrderIds,omitempty"` @@ -271,6 +265,15 @@ type CancelOrderResponse struct { ClientOrderID string `json:"clientOid,omitempty"` } +//go:generate requestgen -type CancelOrderRequest +type CancelOrderRequest struct { + client requestgen.AuthenticatedAPIClient + + orderID *string `param:"orderID"` + clientOrderID *string `param:"clientOrderID"` +} + + func (r *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { if r.orderID == nil && r.clientOrderID == nil { return nil, errors.New("either orderID or clientOrderID is required for canceling order") @@ -310,47 +313,14 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, erro return apiResponse.Data, nil } -//go:generate requestgen -type CancelAllOrderRequest +//go:generate DeleteRequest -url /api/v1/orders -type CancelAllOrderRequest -responseDataType .CancelOrderResponse type CancelAllOrderRequest struct { - client *RestClient + client requestgen.AuthenticatedAPIClient symbol *string `param:"symbol"` tradeType *string `param:"tradeType"` } -func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { - params, err := r.GetParametersQuery() - if err != nil { - return nil, err - } - - req, err := r.client.NewAuthenticatedRequest(ctx, "DELETE", "/api/v1/orders", params, nil) - if err != nil { - return nil, err - } - - response, err := r.client.SendRequest(req) - if err != nil { - return nil, err - } - - var apiResponse struct { - Code string `json:"code"` - Message string `json:"msg"` - Data *CancelOrderResponse `json:"data"` - } - - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err - } - - if apiResponse.Data == nil { - return nil, errors.New("api error: [" + apiResponse.Code + "] " + apiResponse.Message) - } - - return apiResponse.Data, nil -} - // Request via this endpoint to place 5 orders at the same time. // The order type must be a limit order of the same symbol. // The interface currently only supports spot trading