From 439b402ade48d9ee4017a57798b540200f95088b Mon Sep 17 00:00:00 2001 From: itay <4023066+itay747@users.noreply.github.com> Date: Mon, 13 Jan 2025 01:54:48 -0500 Subject: [PATCH] Exchange API: Added bulk modify orders --- hyperliquid/convert.go | 14 ++++++++++++++ hyperliquid/exchange_service.go | 27 +++++++++++++++++++++++++++ hyperliquid/exchange_types.go | 23 +++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/hyperliquid/convert.go b/hyperliquid/convert.go index 75a282a..8be476e 100644 --- a/hyperliquid/convert.go +++ b/hyperliquid/convert.go @@ -53,6 +53,20 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo) OrderWire { OrderType: OrderTypeToWire(req.OrderType), } } +func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo) ModifyOrderWire { + info := meta[req.Coin] + return ModifyOrderWire{ + OrderId: req.OrderId, + Order: OrderWire{ + Asset: info.AssetId, + IsBuy: req.IsBuy, + LimitPx: FloatToWire(req.LimitPx, nil), + SizePx: FloatToWire(req.Sz, &info.SzDecimals), + ReduceOnly: req.ReduceOnly, + OrderType: OrderTypeToWire(req.OrderType), + }, + } +} func OrderTypeToWire(orderType OrderType) OrderTypeWire { if orderType.Limit != nil { diff --git a/hyperliquid/exchange_service.go b/hyperliquid/exchange_service.go index 3b6f573..e8ada9e 100644 --- a/hyperliquid/exchange_service.go +++ b/hyperliquid/exchange_service.go @@ -213,6 +213,33 @@ func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderR return MakeUniversalRequest[CancelOrderResponse](api, request) } +// 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) { + wires := []ModifyOrderWire{} + + for _, req := range modifyRequests { + wires = append(wires, ModifyOrderRequestToWire(req, api.meta)) + } + action := ModifyOrderAction{ + Type: "batchModify", + Modifies: wires, + } + + timestamp := GetNonce() + vVal, rVal, sVal, signErr := api.SignL1Action(action, timestamp) + if signErr != nil { + return nil, signErr + } + request := ExchangeRequest{ + Action: action, + Nonce: timestamp, + Signature: ToTypedSig(rVal, sVal, vVal), + VaultAddress: nil, + } + return MakeUniversalRequest[PlaceOrderResponse](api, request) +} + // Cancel exact order by OID func (api *ExchangeAPI) CancelOrderByOID(coin string, orderID int64) (*CancelOrderResponse, error) { return api.BulkCancelOrders([]CancelOidWire{{Asset: api.meta[coin].AssetId, Oid: int(orderID)}}) diff --git a/hyperliquid/exchange_types.go b/hyperliquid/exchange_types.go index 69af1c6..d303118 100644 --- a/hyperliquid/exchange_types.go +++ b/hyperliquid/exchange_types.go @@ -73,6 +73,29 @@ type OrderWire struct { OrderType OrderTypeWire `msgpack:"t" json:"t"` Cloid string `msgpack:"c,omitempty" json:"c,omitempty"` } +type ModifyResponse struct { + Status string `json:"status"` + Response PlaceOrderInnerResponse `json:"response"` +} +type ModifyOrderWire struct { + OrderId int `msgpack:"oid" json:"oid"` + Order OrderWire `msgpack:"order" json:"order"` +} +type ModifyOrderAction struct { + Type string `msgpack:"type" json:"type"` + Modifies []ModifyOrderWire `msgpack:"modifies" json:"modifies"` +} + +type ModifyOrderRequest struct { + OrderId int `json:"oid"` + Coin string `json:"coin"` + IsBuy bool `json:"is_buy"` + Sz float64 `json:"sz"` + LimitPx float64 `json:"limit_px"` + OrderType OrderType `json:"order_type"` + ReduceOnly bool `json:"reduce_only"` + Cloid string `json:"cloid,omitempty"` +} type OrderTypeWire struct { Limit *LimitOrderType `json:"limit,omitempty" msgpack:"limit,omitempty"`