diff --git a/README.md b/README.md index 44b36b2..616b192 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ The following API endpoints have been implemented - `/private/linear/order/list` Get Active Order - `/private/linear/order/cancel` Cancel Active Order - `/private/linear/order/cancel-all` Cancel All Active Orders +- `/private/linear/order/replace` Replace Active Order - `/private/linear/order/search` Query Active Order (real-time) - `/private/linear/stop-order/create` Place Conditional Order - `/private/linear/stop-order/list` Get Conditional Order diff --git a/future_usdt_perpetual_test.go b/future_usdt_perpetual_test.go index 897903c..9d3df2f 100644 --- a/future_usdt_perpetual_test.go +++ b/future_usdt_perpetual_test.go @@ -637,6 +637,75 @@ func TestCancelAllLinearStopOrder(t *testing.T) { }) } +func TestReplaceLinearOrder(t *testing.T) { + t.Run("success", func(t *testing.T) { + orderID := "test" + newPrice := 10000.0 + param := ReplaceLinearOrderParam{ + Symbol: SymbolFutureBTCUSDT, + OrderID: &orderID, + NewPrice: &newPrice, + } + + path := "/private/linear/order/replace" + method := http.MethodPost + status := http.StatusOK + respBody := ReplaceLinearOrderResponse{ + Result: ReplaceLinearOrderResult{ + OrderID: "b777573b-026e-48ee-b9ac-28c81e829ae8", + }, + } + bytesBody, err := json.Marshal(respBody) + require.NoError(t, err) + + server, teardown := testhelper.NewServer( + testhelper.WithHandlerOption(path, method, status, bytesBody), + ) + defer teardown() + + client := NewTestClient(). + WithBaseURL(server.URL). + WithAuth("test", "test") + + resp, err := client.Future().USDTPerpetual().ReplaceLinearOrder(param) + require.NoError(t, err) + + require.NotNil(t, resp) + assert.Equal(t, respBody, *resp) + }) + t.Run("authentication required", func(t *testing.T) { + orderID := "test" + newPrice := 10000.0 + param := ReplaceLinearOrderParam{ + Symbol: SymbolFutureBTCUSDT, + OrderID: &orderID, + NewPrice: &newPrice, + } + + path := "/private/linear/order/replace" + method := http.MethodPost + status := http.StatusOK + respBody := ReplaceLinearOrderResponse{ + Result: ReplaceLinearOrderResult{ + OrderID: "b777573b-026e-48ee-b9ac-28c81e829ae8", + }, + } + bytesBody, err := json.Marshal(respBody) + require.NoError(t, err) + + server, teardown := testhelper.NewServer( + testhelper.WithHandlerOption(path, method, status, bytesBody), + ) + defer teardown() + + client := NewTestClient(). + WithBaseURL(server.URL) + + _, err = client.Future().USDTPerpetual().ReplaceLinearOrder(param) + assert.Error(t, err) + }) +} + func TestQueryLinearStopOrder(t *testing.T) { t.Run("success", func(t *testing.T) { param := QueryLinearStopOrderParam{ diff --git a/integrationtest/future-usdt-perpetual/future_usdt_perpetual_test.go b/integrationtest/future-usdt-perpetual/future_usdt_perpetual_test.go index 3bcc9bc..af4ae96 100644 --- a/integrationtest/future-usdt-perpetual/future_usdt_perpetual_test.go +++ b/integrationtest/future-usdt-perpetual/future_usdt_perpetual_test.go @@ -624,3 +624,52 @@ func TestLinearTradingStop(t *testing.T) { require.NoError(t, err) } } + +func TestReplaceLinearOrder(t *testing.T) { + t.Run("ok", func(t *testing.T) { + client := bybit.NewTestClient().WithAuthFromEnv() + + symbol := bybit.SymbolFutureBTCUSDT + var orderID string + { + price := 10000.0 + res, err := client.Future().USDTPerpetual().CreateLinearOrder(bybit.CreateLinearOrderParam{ + Side: bybit.SideBuy, + Symbol: symbol, + OrderType: bybit.OrderTypeLimit, + Qty: 0.001, + TimeInForce: bybit.TimeInForceGoodTillCancel, + Price: &price, + }) + require.NoError(t, err) + orderID = res.Result.OrderID + } + { + newPrice := 11000.0 + res, err := client.Future().USDTPerpetual().ReplaceLinearOrder(bybit.ReplaceLinearOrderParam{ + Symbol: symbol, + OrderID: &orderID, + NewPrice: &newPrice, + }) + require.NoError(t, err) + goldenFilename := "./testdata/private-linear-order-replace.json" + testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + + orderID = res.Result.OrderID + } + { + _, err := client.Future().USDTPerpetual().CancelLinearOrder(bybit.CancelLinearOrderParam{ + Symbol: symbol, + OrderID: &orderID, + }) + require.NoError(t, err) + } + }) + + t.Run("auth error", func(t *testing.T) { + client := bybit.NewTestClient() + _, err := client.Future().USDTPerpetual().ReplaceLinearOrder(bybit.ReplaceLinearOrderParam{}) + require.Error(t, err) + }) +} diff --git a/integrationtest/future-usdt-perpetual/testdata/private-linear-order-replace.json b/integrationtest/future-usdt-perpetual/testdata/private-linear-order-replace.json new file mode 100644 index 0000000..1510b57 --- /dev/null +++ b/integrationtest/future-usdt-perpetual/testdata/private-linear-order-replace.json @@ -0,0 +1,3 @@ +{ + "order_id": "b777573b-026e-48ee-b9ac-28c81e829ae8" +} \ No newline at end of file