Skip to content

Commit 956da12

Browse files
committed
feat: add clientOID field to LimitOrder and MarketOrder
1 parent eb00557 commit 956da12

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

hyperliquid/consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package hyperliquid
22

3-
const GLOBAL_DEBUG = false // Defualt debug that is used in all tests
3+
const GLOBAL_DEBUG = false // Default debug that is used in all tests
44

55
// Execution constants
66
const DEFAULT_SLIPPAGE = 0.005 // 0.5% default slippage

hyperliquid/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo, isSpot bool
6060
SizePx: FloatToWire(req.Sz, maxDecimals, info.SzDecimals),
6161
ReduceOnly: req.ReduceOnly,
6262
OrderType: OrderTypeToWire(req.OrderType),
63+
Cloid: req.Cloid,
6364
}
6465
}
6566

hyperliquid/exchange_service.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type IExchangeAPI interface {
1414
// Open orders
1515
BulkOrders(requests []OrderRequest, grouping Grouping) (*PlaceOrderResponse, error)
1616
Order(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error)
17-
MarketOrder(coin string, size float64, slippage *float64) (*PlaceOrderResponse, error)
18-
LimitOrder(orderType string, coin string, size float64, px float64, isBuy bool, reduceOnly bool) (*PlaceOrderResponse, error)
17+
MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*PlaceOrderResponse, error)
18+
LimitOrder(orderType string, coin string, size float64, px float64, isBuy bool, reduceOnly bool, clientOID ...string) (*PlaceOrderResponse, error)
1919

2020
// Order management
2121
CancelOrderByOID(coin string, orderID int) (any, error)
@@ -98,7 +98,7 @@ func (api *ExchangeAPI) SlippagePriceSpot(coin string, isBuy bool, slippage floa
9898
// MarketOrder("BTC", 0.1, nil) // Buy 0.1 BTC
9999
// MarketOrder("BTC", -0.1, nil) // Sell 0.1 BTC
100100
// MarketOrder("BTC", 0.1, &slippage) // Buy 0.1 BTC with slippage
101-
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64) (*PlaceOrderResponse, error) {
101+
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*PlaceOrderResponse, error) {
102102
slpg := GetSlippage(slippage)
103103
isBuy := IsBuy(size)
104104
finalPx := api.SlippagePrice(coin, isBuy, slpg)
@@ -115,6 +115,9 @@ func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64
115115
OrderType: orderType,
116116
ReduceOnly: false,
117117
}
118+
if len(clientOID) > 0 {
119+
orderRequest.Cloid = clientOID[0]
120+
}
118121
return api.Order(orderRequest, GroupingNa)
119122
}
120123

@@ -150,7 +153,7 @@ func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *flo
150153
// Order type can be Gtc, Ioc, Alo.
151154
// Size determines the amount of the coin to buy/sell.
152155
// See the constants TifGtc, TifIoc, TifAlo.
153-
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool) (*PlaceOrderResponse, error) {
156+
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool, clientOID ...string) (*PlaceOrderResponse, error) {
154157
// check if the order type is valid
155158
if orderType != TifGtc && orderType != TifIoc && orderType != TifAlo {
156159
return nil, APIError{Message: fmt.Sprintf("Invalid order type: %s. Available types: %s, %s, %s", orderType, TifGtc, TifIoc, TifAlo)}
@@ -168,6 +171,9 @@ func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64,
168171
OrderType: orderTypeZ,
169172
ReduceOnly: reduceOnly,
170173
}
174+
if len(clientOID) > 0 {
175+
orderRequest.Cloid = clientOID[0]
176+
}
171177
return api.Order(orderRequest, GroupingNa)
172178
}
173179

hyperliquid/exchange_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type OrderRequest struct {
2828
LimitPx float64 `json:"limit_px"`
2929
OrderType OrderType `json:"order_type"`
3030
ReduceOnly bool `json:"reduce_only"`
31+
Cloid string `json:"cloid,omitempty"`
3132
}
3233

3334
type OrderType struct {
@@ -160,7 +161,8 @@ type CancelResponseStatuses struct {
160161
}
161162

162163
type RestingStatus struct {
163-
OrderId int `json:"oid"`
164+
OrderId int `json:"oid"`
165+
Cloid string `json:"cloid,omitempty"`
164166
}
165167

166168
type CloseRequest struct {
@@ -175,6 +177,7 @@ type FilledStatus struct {
175177
OrderId int `json:"oid"`
176178
AvgPx float64 `json:"avgPx,string"`
177179
TotalSz float64 `json:"totalSz,string"`
180+
Cloid string `json:"cloid,omitempty"`
178181
}
179182

180183
type Liquidation struct {

hyperliquid/hyperliquid_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,46 @@ func TestHyperliquid_MakeSomeTradingLogic(t *testing.T) {
7070
}
7171
t.Logf("LimitOrder(TifGtc, ETH, -0.01, 5000.1, true): %v", res3)
7272

73+
res4, err := client.LimitOrder(TifGtc, "ETH", 0.01, 1234.1, false, "0x1234567890abcdef1234567890abcdef")
74+
if err != nil {
75+
if err != nil {
76+
t.Errorf("Error: %v", err)
77+
}
78+
}
79+
t.Logf("LimitOrder(TifIoc, ETH, 0.01, 1234.1, false, 0x1234567890abcdef1234567890abcdef): %v", res4)
80+
7381
// Get all ordres
74-
res4, err := client.GetAccountOpenOrders()
82+
res5, err := client.GetAccountOpenOrders()
7583
if err != nil {
7684
t.Errorf("Error: %v", err)
7785
}
78-
t.Logf("GetAccountOpenOrders(): %v", res4)
86+
t.Logf("GetAccountOpenOrders(): %v", res5)
7987

8088
// Close all orders
81-
res5, err := client.CancelAllOrders()
89+
res6, err := client.CancelAllOrders()
8290
if err != nil {
8391
t.Errorf("Error: %v", err)
8492
}
85-
t.Logf("CancelAllOrders(): %v", res5)
93+
t.Logf("CancelAllOrders(): %v", res6)
8694

8795
// Make market order
88-
res6, err := client.MarketOrder("ETH", 0.01, nil)
96+
res7, err := client.MarketOrder("ETH", 0.01, nil)
8997
if err != nil {
9098
t.Errorf("Error: %v", err)
9199
}
92-
t.Logf("MarketOrder(ETH, 0.01, nil): %v", res6)
100+
t.Logf("MarketOrder(ETH, 0.01, nil): %v", res7)
93101

94102
// Close position
95-
res7, err := client.ClosePosition("ETH")
103+
res8, err := client.ClosePosition("ETH")
96104
if err != nil {
97105
t.Errorf("Error: %v", err)
98106
}
99-
t.Logf("ClosePosition(ETH): %v", res7)
107+
t.Logf("ClosePosition(ETH): %v", res8)
100108

101109
// Get account balance
102-
res8, err := client.GetAccountState()
110+
res9, err := client.GetAccountState()
103111
if err != nil {
104112
t.Errorf("Error: %v", err)
105113
}
106-
t.Logf("GetAccountState(): %v", res8)
114+
t.Logf("GetAccountState(): %v", res9)
107115
}

0 commit comments

Comments
 (0)