diff --git a/hyperliquid/convert.go b/hyperliquid/convert.go index 5966393..7553154 100644 --- a/hyperliquid/convert.go +++ b/hyperliquid/convert.go @@ -62,15 +62,25 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo, isSpot bool OrderType: OrderTypeToWire(req.OrderType), } } -func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo) ModifyOrderWire { + +func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo, isSpot bool) ModifyOrderWire { info := meta[req.Coin] + var assetId, maxDecimals int + if isSpot { + // https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/asset-ids + assetId = info.AssetId + 10000 + maxDecimals = SPOT_MAX_DECIMALS + } else { + assetId = info.AssetId + maxDecimals = PERP_MAX_DECIMALS + } return ModifyOrderWire{ OrderId: req.OrderId, Order: OrderWire{ - Asset: info.AssetId, + Asset: assetId, IsBuy: req.IsBuy, - LimitPx: FloatToWire(req.LimitPx, nil), - SizePx: FloatToWire(req.Sz, &info.SzDecimals), + LimitPx: FloatToWire(req.LimitPx, maxDecimals, info.SzDecimals), + SizePx: FloatToWire(req.Sz, maxDecimals, info.SzDecimals), ReduceOnly: req.ReduceOnly, OrderType: OrderTypeToWire(req.OrderType), }, diff --git a/hyperliquid/exchange_service.go b/hyperliquid/exchange_service.go index 0eb92b1..96461e1 100644 --- a/hyperliquid/exchange_service.go +++ b/hyperliquid/exchange_service.go @@ -274,11 +274,11 @@ func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderR // Bulk modify orders // https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders -func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest) (*PlaceOrderResponse, error) { +func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*PlaceOrderResponse, error) { wires := []ModifyOrderWire{} for _, req := range modifyRequests { - wires = append(wires, ModifyOrderRequestToWire(req, api.meta)) + wires = append(wires, ModifyOrderRequestToWire(req, api.meta, isSpot)) } action := ModifyOrderAction{ Type: "batchModify", diff --git a/hyperliquid/exchange_test.go b/hyperliquid/exchange_test.go index 13ec140..3581ec6 100644 --- a/hyperliquid/exchange_test.go +++ b/hyperliquid/exchange_test.go @@ -1,6 +1,7 @@ package hyperliquid import ( + "log" "math" "os" "testing" @@ -200,3 +201,59 @@ func TestExchageAPI_TestMarketOrderSpot(testing *testing.T) { testing.Errorf("res.Response.Data.Statuses[0].Filled.AvgPx = %v", avgPrice) } } + +func TestExchangeAPI_TestModifyOrder(t *testing.T) { + exchangeAPI := GetExchangeAPI() + size := 0.005 + coin := "ETH" + px := 2000.0 + res, err := exchangeAPI.LimitOrder(TifGtc, coin, size, px, false) + if err != nil { + t.Errorf("MakeLimit() error = %v", err) + } + t.Logf("MakeLimit() = %v", res) + openOrders, err := exchangeAPI.infoAPI.GetOpenOrders(exchangeAPI.AccountAddress()) + if err != nil { + t.Errorf("GetAccountOpenOrders() error = %v", err) + } + t.Logf("GetAccountOpenOrders() = %v", openOrders) + orderOpened := false + for _, order := range *openOrders { + if order.Coin == coin && order.Sz == size && order.LimitPx == px { + orderOpened = true + break + } + } + log.Printf("Order ID: %v", res.Response.Data.Statuses[0].Resting.OrderId) + if !orderOpened { + t.Errorf("Order not found: %+v", openOrders) + } + time.Sleep(5 * time.Second) // wait to execute order + // modify order + newPx := 2500.0 + orderType := OrderType{ + Limit: &LimitOrderType{ + Tif: TifGtc, + }, + } + modifyOrderRequest := ModifyOrderRequest{ + OrderId: res.Response.Data.Statuses[0].Resting.OrderId, + Coin: coin, + Sz: size, + LimitPx: newPx, + OrderType: orderType, + IsBuy: true, + ReduceOnly: false, + } + modifyRes, err := exchangeAPI.BulkModifyOrders([]ModifyOrderRequest{modifyOrderRequest}, false) + if err != nil { + t.Errorf("ModifyOrder() error = %v", err) + } + t.Logf("ModifyOrder() = %+v", modifyRes) + time.Sleep(5 * time.Second) // wait to execute order + cancelRes, err := exchangeAPI.CancelAllOrders() + if err != nil { + t.Errorf("CancelAllOrders() error = %v", err) + } + t.Logf("CancelAllOrders() = %v", cancelRes) +}