ftx: rewrite order cancel handling

This commit is contained in:
c9s 2022-03-03 14:49:25 +08:00
parent dd76cfafa4
commit 86af4d2b40

View File

@ -2,6 +2,7 @@ package ftx
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -560,18 +561,26 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) erro
logrus.WithError(err).Error("rate limit error") logrus.WithError(err).Error("rate limit error")
} }
var resp *ftxapi.APIResponse
var err error
if len(o.ClientOrderID) > 0 { if len(o.ClientOrderID) > 0 {
req := e.client.NewCancelOrderByClientOrderIdRequest(o.ClientOrderID) req := e.client.NewCancelOrderByClientOrderIdRequest(o.ClientOrderID)
_, err := req.Do(ctx) resp, err = req.Do(ctx)
if err != nil {
return err
}
} else { } else {
req := e.client.NewCancelOrderRequest(strconv.FormatUint(o.OrderID, 10)) req := e.client.NewCancelOrderRequest(strconv.FormatUint(o.OrderID, 10))
_, err := req.Do(ctx) resp, err = req.Do(ctx)
}
if err != nil {
return err
}
if !resp.Success {
v, err := unmarshalResult(resp.Result)
if err != nil { if err != nil {
return err return err
} }
return fmt.Errorf("cancel order failed: %v", v)
} }
} }
return nil return nil
@ -655,3 +664,8 @@ func (e *Exchange) Transfer(ctx context.Context, coin string, size float64, dest
} }
return resp.Result.String(), nil return resp.Result.String(), nil
} }
func unmarshalResult(result json.RawMessage) (a interface{}, err error) {
err = json.Unmarshal(result, &a)
return a, err
}