Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions hyperliquid/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
4 changes: 2 additions & 2 deletions hyperliquid/exchange_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions hyperliquid/exchange_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyperliquid

import (
"log"
"math"
"os"
"testing"
Expand Down Expand Up @@ -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)
}