diff --git a/pkg/exchange/okex/exchange.go b/pkg/exchange/okex/exchange.go index d7757618f..292741de7 100644 --- a/pkg/exchange/okex/exchange.go +++ b/pkg/exchange/okex/exchange.go @@ -65,26 +65,40 @@ var log = logrus.WithFields(logrus.Fields{ var ErrSymbolRequired = errors.New("symbol is a required parameter") type Exchange struct { - key, secret, passphrase string + key, secret, passphrase, brokerId string client *okexapi.RestClient timeNowFunc func() time.Time } -func New(key, secret, passphrase string) *Exchange { +type Option func(exchange *Exchange) + +func WithBrokerId(id string) Option { + return func(exchange *Exchange) { + exchange.brokerId = id + } +} + +func New(key, secret, passphrase string, opts ...Option) *Exchange { client := okexapi.NewClient() if len(key) > 0 && len(secret) > 0 { client.Auth(key, secret, passphrase) } - return &Exchange{ + ex := &Exchange{ key: key, secret: secret, passphrase: passphrase, client: client, timeNowFunc: time.Now, } + + for _, o := range opts { + o(ex) + } + + return ex } func (e *Exchange) Name() types.ExchangeName { @@ -263,6 +277,10 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*t orderReq.ClientOrderID(order.ClientOrderID) } + if len(e.brokerId) != 0 { + orderReq.Tag(e.brokerId) + } + timeNow := time.Now() orders, err := orderReq.Do(ctx) if err != nil { diff --git a/pkg/exchange/okex/okexapi/place_order_request.go b/pkg/exchange/okex/okexapi/place_order_request.go index 3f8799285..ad0edcc32 100644 --- a/pkg/exchange/okex/okexapi/place_order_request.go +++ b/pkg/exchange/okex/okexapi/place_order_request.go @@ -42,7 +42,7 @@ type PlaceOrderRequest struct { // A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters. clientOrderID *string `param:"clOrdId"` - // A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters. + // A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 16 characters. tag *string `param:"tag"` // "buy" or "sell" diff --git a/pkg/exchange/okex/okexapi/place_order_request_requestgen.go b/pkg/exchange/okex/okexapi/place_order_request_requestgen.go index 3303d5961..f1badcba1 100644 --- a/pkg/exchange/okex/okexapi/place_order_request_requestgen.go +++ b/pkg/exchange/okex/okexapi/place_order_request_requestgen.go @@ -284,15 +284,29 @@ func (r *PlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) { } var apiResponse APIResponse - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err + + type responseUnmarshaler interface { + Unmarshal(data []byte) error + } + + if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok { + if err := unmarshaler.Unmarshal(response.Body); err != nil { + return nil, err + } + } else { + // The line below checks the content type, however, some API server might not send the correct content type header, + // Hence, this is commented for backward compatibility + // response.IsJSON() + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } } type responseValidator interface { Validate() error } - validator, ok := interface{}(apiResponse).(responseValidator) - if ok { + + if validator, ok := interface{}(&apiResponse).(responseValidator); ok { if err := validator.Validate(); err != nil { return nil, err }