Skip to content

Commit eb00557

Browse files
Merge pull request #15 from Logarithm-Labs/patch-modify-order
Patch ModifyOrderRequestToWire
2 parents 47c78e7 + 3c739a8 commit eb00557

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

hyperliquid/convert.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,25 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo, isSpot bool
6262
OrderType: OrderTypeToWire(req.OrderType),
6363
}
6464
}
65-
func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo) ModifyOrderWire {
65+
66+
func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo, isSpot bool) ModifyOrderWire {
6667
info := meta[req.Coin]
68+
var assetId, maxDecimals int
69+
if isSpot {
70+
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/asset-ids
71+
assetId = info.AssetId + 10000
72+
maxDecimals = SPOT_MAX_DECIMALS
73+
} else {
74+
assetId = info.AssetId
75+
maxDecimals = PERP_MAX_DECIMALS
76+
}
6777
return ModifyOrderWire{
6878
OrderId: req.OrderId,
6979
Order: OrderWire{
70-
Asset: info.AssetId,
80+
Asset: assetId,
7181
IsBuy: req.IsBuy,
72-
LimitPx: FloatToWire(req.LimitPx, nil),
73-
SizePx: FloatToWire(req.Sz, &info.SzDecimals),
82+
LimitPx: FloatToWire(req.LimitPx, maxDecimals, info.SzDecimals),
83+
SizePx: FloatToWire(req.Sz, maxDecimals, info.SzDecimals),
7484
ReduceOnly: req.ReduceOnly,
7585
OrderType: OrderTypeToWire(req.OrderType),
7686
},

hyperliquid/exchange_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderR
274274

275275
// Bulk modify orders
276276
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
277-
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest) (*PlaceOrderResponse, error) {
277+
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*PlaceOrderResponse, error) {
278278
wires := []ModifyOrderWire{}
279279

280280
for _, req := range modifyRequests {
281-
wires = append(wires, ModifyOrderRequestToWire(req, api.meta))
281+
wires = append(wires, ModifyOrderRequestToWire(req, api.meta, isSpot))
282282
}
283283
action := ModifyOrderAction{
284284
Type: "batchModify",

hyperliquid/exchange_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hyperliquid
22

33
import (
4+
"log"
45
"math"
56
"os"
67
"testing"
@@ -200,3 +201,59 @@ func TestExchageAPI_TestMarketOrderSpot(testing *testing.T) {
200201
testing.Errorf("res.Response.Data.Statuses[0].Filled.AvgPx = %v", avgPrice)
201202
}
202203
}
204+
205+
func TestExchangeAPI_TestModifyOrder(t *testing.T) {
206+
exchangeAPI := GetExchangeAPI()
207+
size := 0.005
208+
coin := "ETH"
209+
px := 2000.0
210+
res, err := exchangeAPI.LimitOrder(TifGtc, coin, size, px, false)
211+
if err != nil {
212+
t.Errorf("MakeLimit() error = %v", err)
213+
}
214+
t.Logf("MakeLimit() = %v", res)
215+
openOrders, err := exchangeAPI.infoAPI.GetOpenOrders(exchangeAPI.AccountAddress())
216+
if err != nil {
217+
t.Errorf("GetAccountOpenOrders() error = %v", err)
218+
}
219+
t.Logf("GetAccountOpenOrders() = %v", openOrders)
220+
orderOpened := false
221+
for _, order := range *openOrders {
222+
if order.Coin == coin && order.Sz == size && order.LimitPx == px {
223+
orderOpened = true
224+
break
225+
}
226+
}
227+
log.Printf("Order ID: %v", res.Response.Data.Statuses[0].Resting.OrderId)
228+
if !orderOpened {
229+
t.Errorf("Order not found: %+v", openOrders)
230+
}
231+
time.Sleep(5 * time.Second) // wait to execute order
232+
// modify order
233+
newPx := 2500.0
234+
orderType := OrderType{
235+
Limit: &LimitOrderType{
236+
Tif: TifGtc,
237+
},
238+
}
239+
modifyOrderRequest := ModifyOrderRequest{
240+
OrderId: res.Response.Data.Statuses[0].Resting.OrderId,
241+
Coin: coin,
242+
Sz: size,
243+
LimitPx: newPx,
244+
OrderType: orderType,
245+
IsBuy: true,
246+
ReduceOnly: false,
247+
}
248+
modifyRes, err := exchangeAPI.BulkModifyOrders([]ModifyOrderRequest{modifyOrderRequest}, false)
249+
if err != nil {
250+
t.Errorf("ModifyOrder() error = %v", err)
251+
}
252+
t.Logf("ModifyOrder() = %+v", modifyRes)
253+
time.Sleep(5 * time.Second) // wait to execute order
254+
cancelRes, err := exchangeAPI.CancelAllOrders()
255+
if err != nil {
256+
t.Errorf("CancelAllOrders() error = %v", err)
257+
}
258+
t.Logf("CancelAllOrders() = %v", cancelRes)
259+
}

0 commit comments

Comments
 (0)