From e2937acb28c8ff78691bc2505d497499def3ccd3 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 13 Dec 2021 02:08:18 +0800 Subject: [PATCH] apply requestgen for CancelOrderRequest and CancelAllOrderRequest --- pkg/exchange/ftx/rest.go | 1 + .../cancel_all_order_request_accessors.go | 21 ++++++++++--- .../cancel_order_request_accessors.go | 21 ++++++++++--- .../list_orders_request_accessors.go | 31 +++++++++++++------ .../place_order_request_accessors.go | 31 +++++++++++++++---- pkg/exchange/kucoin/kucoinapi/trade.go | 13 +++----- 6 files changed, 83 insertions(+), 35 deletions(-) diff --git a/pkg/exchange/ftx/rest.go b/pkg/exchange/ftx/rest.go index 5812bebb6..2c45fda65 100644 --- a/pkg/exchange/ftx/rest.go +++ b/pkg/exchange/ftx/rest.go @@ -177,6 +177,7 @@ func (r *restRequest) newAuthenticatedRequest(ctx context.Context) (*http.Reques if err != nil { return nil, err } + ts := strconv.FormatInt(timestamp(), 10) p := fmt.Sprintf("%s%s%s", ts, r.m, u.Path) if len(r.q) > 0 { diff --git a/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_accessors.go b/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_accessors.go index 5be06ca82..10e5c91a4 100644 --- a/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_accessors.go +++ b/pkg/exchange/kucoin/kucoinapi/cancel_all_order_request_accessors.go @@ -3,6 +3,7 @@ package kucoinapi import ( + "encoding/json" "fmt" "net/url" ) @@ -17,31 +18,32 @@ func (r *CancelAllOrderRequest) TradeType(tradeType string) *CancelAllOrderReque return r } -func (r *CancelAllOrderRequest) getParameters() (map[string]interface{}, error) { +func (r *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error) { var params = map[string]interface{}{} // check symbol field -> json key symbol if r.symbol != nil { symbol := *r.symbol + // assign parameter of symbol params["symbol"] = symbol - } else { } // check tradeType field -> json key tradeType if r.tradeType != nil { tradeType := *r.tradeType + // assign parameter of tradeType params["tradeType"] = tradeType - } else { } + return params, nil } -func (r *CancelAllOrderRequest) getQuery() (url.Values, error) { +func (r *CancelAllOrderRequest) GetParametersQuery() (url.Values, error) { query := url.Values{} - params, err := r.getParameters() + params, err := r.GetParameters() if err != nil { return query, err } @@ -52,3 +54,12 @@ func (r *CancelAllOrderRequest) getQuery() (url.Values, error) { return query, nil } + +func (r *CancelAllOrderRequest) GetParametersJSON() ([]byte, error) { + params, err := r.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} diff --git a/pkg/exchange/kucoin/kucoinapi/cancel_order_request_accessors.go b/pkg/exchange/kucoin/kucoinapi/cancel_order_request_accessors.go index 5865dad21..aa322b268 100644 --- a/pkg/exchange/kucoin/kucoinapi/cancel_order_request_accessors.go +++ b/pkg/exchange/kucoin/kucoinapi/cancel_order_request_accessors.go @@ -3,6 +3,7 @@ package kucoinapi import ( + "encoding/json" "fmt" "net/url" ) @@ -17,31 +18,32 @@ func (c *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderReq return c } -func (c *CancelOrderRequest) getParameters() (map[string]interface{}, error) { +func (c *CancelOrderRequest) GetParameters() (map[string]interface{}, error) { var params = map[string]interface{}{} // check orderID field -> json key orderID if c.orderID != nil { orderID := *c.orderID + // assign parameter of orderID params["orderID"] = orderID - } else { } // check clientOrderID field -> json key clientOrderID if c.clientOrderID != nil { clientOrderID := *c.clientOrderID + // assign parameter of clientOrderID params["clientOrderID"] = clientOrderID - } else { } + return params, nil } -func (c *CancelOrderRequest) getQuery() (url.Values, error) { +func (c *CancelOrderRequest) GetParametersQuery() (url.Values, error) { query := url.Values{} - params, err := c.getParameters() + params, err := c.GetParameters() if err != nil { return query, err } @@ -52,3 +54,12 @@ func (c *CancelOrderRequest) getQuery() (url.Values, error) { return query, nil } + +func (c *CancelOrderRequest) GetParametersJSON() ([]byte, error) { + params, err := c.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} diff --git a/pkg/exchange/kucoin/kucoinapi/list_orders_request_accessors.go b/pkg/exchange/kucoin/kucoinapi/list_orders_request_accessors.go index b1e5ae765..99bc701a8 100644 --- a/pkg/exchange/kucoin/kucoinapi/list_orders_request_accessors.go +++ b/pkg/exchange/kucoin/kucoinapi/list_orders_request_accessors.go @@ -3,6 +3,7 @@ package kucoinapi import ( + "encoding/json" "fmt" "net/url" "strconv" @@ -44,7 +45,7 @@ func (r *ListOrdersRequest) EndAt(endAt time.Time) *ListOrdersRequest { return r } -func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) { +func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) { var params = map[string]interface{}{} // check status field -> json key status @@ -60,16 +61,16 @@ func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) { } + // assign parameter of status params["status"] = status - } else { } // check symbol field -> json key symbol if r.symbol != nil { symbol := *r.symbol + // assign parameter of symbol params["symbol"] = symbol - } else { } // check side field -> json key side @@ -85,50 +86,51 @@ func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) { } + // assign parameter of side params["side"] = side - } else { } // check orderType field -> json key type if r.orderType != nil { orderType := *r.orderType + // assign parameter of orderType params["type"] = orderType - } else { } // check tradeType field -> json key tradeType if r.tradeType != nil { tradeType := *r.tradeType + // assign parameter of tradeType params["tradeType"] = tradeType - } else { } // check startAt field -> json key startAt if r.startAt != nil { startAt := *r.startAt + // assign parameter of startAt // convert time.Time to milliseconds time params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10) - } else { } // check endAt field -> json key endAt if r.endAt != nil { endAt := *r.endAt + // assign parameter of endAt // convert time.Time to milliseconds time params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10) - } else { } + return params, nil } -func (r *ListOrdersRequest) getQuery() (url.Values, error) { +func (r *ListOrdersRequest) GetParametersQuery() (url.Values, error) { query := url.Values{} - params, err := r.getParameters() + params, err := r.GetParameters() if err != nil { return query, err } @@ -139,3 +141,12 @@ func (r *ListOrdersRequest) getQuery() (url.Values, error) { return query, nil } + +func (r *ListOrdersRequest) GetParametersJSON() ([]byte, error) { + params, err := r.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} diff --git a/pkg/exchange/kucoin/kucoinapi/place_order_request_accessors.go b/pkg/exchange/kucoin/kucoinapi/place_order_request_accessors.go index 04be6f89d..75671b3bc 100644 --- a/pkg/exchange/kucoin/kucoinapi/place_order_request_accessors.go +++ b/pkg/exchange/kucoin/kucoinapi/place_order_request_accessors.go @@ -3,6 +3,7 @@ package kucoinapi import ( + "encoding/json" "fmt" "github.com/google/uuid" "net/url" @@ -48,7 +49,7 @@ func (r *PlaceOrderRequest) TimeInForce(timeInForce TimeInForceType) *PlaceOrder return r } -func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { +func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) { var params = map[string]interface{}{} // check clientOrderID field -> json key clientOid @@ -59,10 +60,14 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { return params, fmt.Errorf("clientOid is required, empty string given") } + // assign parameter of clientOrderID params["clientOid"] = clientOrderID } else { + + // assign default of clientOrderID clientOrderID := uuid.New().String() + // assign parameter of clientOrderID params["clientOid"] = clientOrderID } @@ -73,24 +78,27 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { return params, fmt.Errorf("symbol is required, empty string given") } + // assign parameter of symbol params["symbol"] = symbol // check tag field -> json key tag if r.tag != nil { tag := *r.tag + // assign parameter of tag params["tag"] = tag - } else { } // check side field -> json key side side := r.side + // assign parameter of side params["side"] = side // check orderType field -> json key ordType orderType := r.orderType + // assign parameter of orderType params["ordType"] = orderType // check size field -> json key size @@ -100,14 +108,15 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { return params, fmt.Errorf("size is required, empty string given") } + // assign parameter of size params["size"] = size // check price field -> json key price if r.price != nil { price := *r.price + // assign parameter of price params["price"] = price - } else { } // check timeInForce field -> json key timeInForce @@ -118,16 +127,17 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { return params, fmt.Errorf("timeInForce is required, empty string given") } + // assign parameter of timeInForce params["timeInForce"] = timeInForce - } else { } + return params, nil } -func (r *PlaceOrderRequest) getQuery() (url.Values, error) { +func (r *PlaceOrderRequest) GetParametersQuery() (url.Values, error) { query := url.Values{} - params, err := r.getParameters() + params, err := r.GetParameters() if err != nil { return query, err } @@ -138,3 +148,12 @@ func (r *PlaceOrderRequest) getQuery() (url.Values, error) { return query, nil } + +func (r *PlaceOrderRequest) GetParametersJSON() ([]byte, error) { + params, err := r.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} diff --git a/pkg/exchange/kucoin/kucoinapi/trade.go b/pkg/exchange/kucoin/kucoinapi/trade.go index 5a50c11a6..cc039ec41 100644 --- a/pkg/exchange/kucoin/kucoinapi/trade.go +++ b/pkg/exchange/kucoin/kucoinapi/trade.go @@ -6,7 +6,6 @@ import ( "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/google/uuid" "github.com/pkg/errors" ) @@ -98,7 +97,7 @@ type OrderListPage struct { } func (r *ListOrdersRequest) Do(ctx context.Context) (*OrderListPage, error) { - params, err := r.getQuery() + params, err := r.GetParametersQuery() if err != nil { return nil, err } @@ -164,7 +163,7 @@ type PlaceOrderRequest struct { } func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) { - payload, err := r.getParameters() + payload, err := r.GetParameters() if err != nil { return nil, err } @@ -260,7 +259,7 @@ type CancelAllOrderRequest struct { } func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { - params, err := r.getQuery() + params, err := r.GetParametersQuery() if err != nil { return nil, err } @@ -315,15 +314,11 @@ func (r *BatchPlaceOrderRequest) Add(reqs ...*PlaceOrderRequest) *BatchPlaceOrde func (r *BatchPlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) { var orderList []map[string]interface{} for _, req := range r.reqs { - params, err := req.getParameters() + params, err := req.GetParameters() if err != nil { return nil, err } - if _, ok := params["clientOid"]; !ok { - params["clientOid"] = uuid.New().String() - } - orderList = append(orderList, params) }