Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [bitget] fix post only order #1577

Merged
merged 1 commit into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/exchange/bitget/convert.go
Expand Up @@ -225,6 +225,27 @@ func unfilledOrderToGlobalOrder(order v2.UnfilledOrder) (*types.Order, error) {
}, nil
}

func fallbackPostOnlyOrder(order types.SubmitOrder, orderId string) (*types.Order, error) {
intOrderId, err := strconv.ParseUint(orderId, 10, 64)
if err != nil {
return nil, err
}
now := time.Now()

return &types.Order{
SubmitOrder: order,
Exchange: types.ExchangeBitget,
OrderID: intOrderId,
UUID: orderId,
Status: types.OrderStatusNew,
OriginalStatus: "FALLBACK_STATUS",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a debug message

ExecutedQuantity: fixedpoint.Zero,
IsWorking: true,
CreationTime: types.Time(now),
UpdateTime: types.Time(now),
}, nil
}

func toGlobalOrder(order v2.OrderDetail) (*types.Order, error) {
side, err := toGlobalSideType(order.Side)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/exchange/bitget/exchange.go
Expand Up @@ -378,6 +378,14 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr
}

if len(ordersResp) != 1 {
// 2023/03/12 If it's a maker order and there is a corresponding order to be executed, then the order will be canceled,
// you can receive the status immediately from the websocket, but the RestAPI requires at least 200ms waiting time.
//
// Therefore, We don't want to waste time waiting for him, so we choose to manually enter the order
// information and send it back.
if order.Type == types.OrderTypeLimitMaker {
return fallbackPostOnlyOrder(order, orderId)
}
return nil, fmt.Errorf("unexpected length of history orders, expecting: 1, given: %d, ids: %s", len(ordersResp), orderId)
}

Expand Down
32 changes: 27 additions & 5 deletions pkg/exchange/bitget/exchange_test.go
Expand Up @@ -646,6 +646,14 @@ func TestExchange_SubmitOrder(t *testing.T) {
})

t.Run("Limit Maker order", func(t *testing.T) {
emptyApiResp := v2.APIResponse{
Code: "00000",
Message: "",
Data: nil,
}
rawEmptyApiResp, err := json.Marshal(emptyApiResp)
assert.NoError(err)

transport := &httptesting.MockTransport{}
ex.client.HttpClient.Transport = transport

Expand All @@ -672,22 +680,36 @@ func TestExchange_SubmitOrder(t *testing.T) {
return httptesting.BuildResponseString(http.StatusOK, string(placeOrderFile)), nil
})

unfilledFile, err := os.ReadFile("bitgetapi/v2/testdata/get_unfilled_orders_request_limit_order.json")
assert.NoError(err)

transport.GET(openOrderUrl, func(req *http.Request) (*http.Response, error) {
query := req.URL.Query()
assert.Len(query, 1)
assert.Contains(query, "orderId")
assert.Equal(query["orderId"], []string{strconv.FormatUint(expOrder.OrderID, 10)})
return httptesting.BuildResponseString(http.StatusOK, string(unfilledFile)), nil
return httptesting.BuildResponseString(http.StatusOK, string(rawEmptyApiResp)), nil
})

transport.GET(historyOrderUrl, func(req *http.Request) (*http.Response, error) {
query := req.URL.Query()
assert.Len(query, 1)
assert.Contains(query, "orderId")
assert.Equal(query["orderId"], []string{strconv.FormatUint(expOrder.OrderID, 10)})
return httptesting.BuildResponseString(http.StatusOK, string(rawEmptyApiResp)), nil
})

reqLimitOrder2 := reqLimitOrder
reqLimitOrder2.Type = types.OrderTypeLimitMaker
acct, err := ex.SubmitOrder(context.Background(), reqLimitOrder2)
assert.NoError(err)
assert.Equal(expOrder, acct)

expOrder2 := *expOrder
expOrder2.OriginalStatus = "FALLBACK_STATUS"
expOrder2.Status = types.OrderStatusNew
expOrder2.IsWorking = true
expOrder2.Type = types.OrderTypeLimitMaker
expOrder2.SubmitOrder = reqLimitOrder2
acct.CreationTime = expOrder2.CreationTime
acct.UpdateTime = expOrder2.UpdateTime
assert.Equal(&expOrder2, acct)
})

t.Run("Market order", func(t *testing.T) {
Expand Down