Skip to content

Commit 0e4c1ff

Browse files
Merge pull request #20 from itay747/parseOrderError
Exchange API: Fix order response unmarshall errors via unified `OrderResponse`.
2 parents 8fef9c2 + a07f0c4 commit 0e4c1ff

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

hyperliquid/exchange_service.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ type IExchangeAPI interface {
1212
IClient
1313

1414
// Open orders
15-
BulkOrders(requests []OrderRequest, grouping Grouping) (*PlaceOrderResponse, error)
16-
Order(request OrderRequest, grouping Grouping) (*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)
15+
BulkOrders(requests []OrderRequest, grouping Grouping) (*OrderResponse, error)
16+
Order(request OrderRequest, grouping Grouping) (*OrderResponse, error)
17+
MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*OrderResponse, error)
18+
LimitOrder(orderType string, coin string, size float64, px float64, isBuy bool, reduceOnly bool, clientOID ...string) (*OrderResponse, error)
1919

2020
// Order management
2121
CancelOrderByOID(coin string, orderID int) (any, error)
2222
CancelOrderByCloid(coin string, clientOID string) (any, error)
2323
BulkCancelOrders(cancels []CancelOidWire) (any, error)
2424
CancelAllOrdersByCoin(coin string) (any, error)
2525
CancelAllOrders() (any, error)
26-
ClosePosition(coin string) (*PlaceOrderResponse, error)
26+
ClosePosition(coin string) (*OrderResponse, error)
2727

2828
// Account management
2929
Withdraw(destination string, amount float64) (*WithdrawResponse, error)
@@ -99,7 +99,7 @@ func (api *ExchangeAPI) SlippagePriceSpot(coin string, isBuy bool, slippage floa
9999
// MarketOrder("BTC", 0.1, nil) // Buy 0.1 BTC
100100
// MarketOrder("BTC", -0.1, nil) // Sell 0.1 BTC
101101
// MarketOrder("BTC", 0.1, &slippage) // Buy 0.1 BTC with slippage
102-
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*PlaceOrderResponse, error) {
102+
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*OrderResponse, error) {
103103
slpg := GetSlippage(slippage)
104104
isBuy := IsBuy(size)
105105
finalPx := api.SlippagePrice(coin, isBuy, slpg)
@@ -130,7 +130,7 @@ func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64
130130
// MarketOrderSpot("HYPE", 0.1, nil) // Buy 0.1 HYPE
131131
// MarketOrderSpot("HYPE", -0.1, nil) // Sell 0.1 HYPE
132132
// MarketOrderSpot("HYPE", 0.1, &slippage) // Buy 0.1 HYPE with slippage
133-
func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *float64) (*PlaceOrderResponse, error) {
133+
func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *float64) (*OrderResponse, error) {
134134
slpg := GetSlippage(slippage)
135135
isBuy := IsBuy(size)
136136
finalPx := api.SlippagePriceSpot(coin, isBuy, slpg)
@@ -154,7 +154,7 @@ func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *flo
154154
// Order type can be Gtc, Ioc, Alo.
155155
// Size determines the amount of the coin to buy/sell.
156156
// See the constants TifGtc, TifIoc, TifAlo.
157-
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool, clientOID ...string) (*PlaceOrderResponse, error) {
157+
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool, clientOID ...string) (*OrderResponse, error) {
158158
// check if the order type is valid
159159
if orderType != TifGtc && orderType != TifIoc && orderType != TifAlo {
160160
return nil, APIError{Message: fmt.Sprintf("Invalid order type: %s. Available types: %s, %s, %s", orderType, TifGtc, TifIoc, TifAlo)}
@@ -179,7 +179,7 @@ func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64,
179179
}
180180

181181
// Close all positions for a given coin. They are closing with a market order.
182-
func (api *ExchangeAPI) ClosePosition(coin string) (*PlaceOrderResponse, error) {
182+
func (api *ExchangeAPI) ClosePosition(coin string) (*OrderResponse, error) {
183183
// Get all positions and find the one for the coin
184184
// Then just make MarketOpen with the reverse size
185185
state, err := api.infoAPI.GetUserState(api.AccountAddress())
@@ -219,18 +219,18 @@ func (api *ExchangeAPI) ClosePosition(coin string) (*PlaceOrderResponse, error)
219219
}
220220

221221
// Place single order
222-
func (api *ExchangeAPI) Order(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error) {
222+
func (api *ExchangeAPI) Order(request OrderRequest, grouping Grouping) (*OrderResponse, error) {
223223
return api.BulkOrders([]OrderRequest{request}, grouping, false)
224224
}
225225

226226
// OrderSpot places a spot order
227-
func (api *ExchangeAPI) OrderSpot(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error) {
227+
func (api *ExchangeAPI) OrderSpot(request OrderRequest, grouping Grouping) (*OrderResponse, error) {
228228
return api.BulkOrders([]OrderRequest{request}, grouping, true)
229229
}
230230

231231
// Place orders in bulk
232232
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
233-
func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, isSpot bool) (*PlaceOrderResponse, error) {
233+
func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, isSpot bool) (*OrderResponse, error) {
234234
var wires []OrderWire
235235
var meta map[string]AssetInfo
236236
if isSpot {
@@ -254,12 +254,12 @@ func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, i
254254
Signature: ToTypedSig(r, s, v),
255255
VaultAddress: nil,
256256
}
257-
return MakeUniversalRequest[PlaceOrderResponse](api, request)
257+
return MakeUniversalRequest[OrderResponse](api, request)
258258
}
259259

260260
// Cancel order(s)
261261
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
262-
func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderResponse, error) {
262+
func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*OrderResponse, error) {
263263
timestamp := GetNonce()
264264
action := CancelOidOrderAction{
265265
Type: "cancel",
@@ -276,12 +276,12 @@ func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderR
276276
Signature: ToTypedSig(r, s, v),
277277
VaultAddress: nil,
278278
}
279-
return MakeUniversalRequest[CancelOrderResponse](api, request)
279+
return MakeUniversalRequest[OrderResponse](api, request)
280280
}
281281

282282
// Bulk modify orders
283283
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
284-
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*PlaceOrderResponse, error) {
284+
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*OrderResponse, error) {
285285
wires := []ModifyOrderWire{}
286286

287287
for _, req := range modifyRequests {
@@ -303,17 +303,17 @@ func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, is
303303
Signature: ToTypedSig(rVal, sVal, vVal),
304304
VaultAddress: nil,
305305
}
306-
return MakeUniversalRequest[PlaceOrderResponse](api, request)
306+
return MakeUniversalRequest[OrderResponse](api, request)
307307
}
308308

309309
// Cancel exact order by OID
310-
func (api *ExchangeAPI) CancelOrderByOID(coin string, orderID int64) (*CancelOrderResponse, error) {
310+
func (api *ExchangeAPI) CancelOrderByOID(coin string, orderID int64) (*OrderResponse, error) {
311311
return api.BulkCancelOrders([]CancelOidWire{{Asset: api.meta[coin].AssetId, Oid: int(orderID)}})
312312
}
313313

314314
// Cancel exact order by Client Order Id
315315
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
316-
func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*CancelOrderResponse, error) {
316+
func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*OrderResponse, error) {
317317
timestamp := GetNonce()
318318
action := CancelCloidOrderAction{
319319
Type: "cancelByCloid",
@@ -335,11 +335,11 @@ func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*Canc
335335
Signature: ToTypedSig(r, s, v),
336336
VaultAddress: nil,
337337
}
338-
return MakeUniversalRequest[CancelOrderResponse](api, request)
338+
return MakeUniversalRequest[OrderResponse](api, request)
339339
}
340340

341341
// Cancel all orders for a given coin
342-
func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*CancelOrderResponse, error) {
342+
func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*OrderResponse, error) {
343343
orders, err := api.infoAPI.GetOpenOrders(api.AccountAddress())
344344
if err != nil {
345345
api.debug("Error getting orders: %s", err)
@@ -356,7 +356,7 @@ func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*CancelOrderResponse
356356
}
357357

358358
// Cancel all open orders
359-
func (api *ExchangeAPI) CancelAllOrders() (*CancelOrderResponse, error) {
359+
func (api *ExchangeAPI) CancelAllOrders() (*OrderResponse, error) {
360360
orders, err := api.infoAPI.GetOpenOrders(api.AccountAddress())
361361
if err != nil {
362362
api.debug("Error getting orders: %s", err)

hyperliquid/exchange_types.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ type OrderWire struct {
7777
Cloid string `msgpack:"c,omitempty" json:"c,omitempty"`
7878
}
7979
type ModifyResponse struct {
80-
Status string `json:"status"`
81-
Response PlaceOrderInnerResponse `json:"response"`
80+
Status string `json:"status"`
81+
Response OrderInnerResponse `json:"response"`
8282
}
8383
type ModifyOrderWire struct {
8484
OrderId int `msgpack:"oid" json:"oid"`
@@ -111,12 +111,12 @@ type PlaceOrderAction struct {
111111
Grouping Grouping `msgpack:"grouping" json:"grouping"`
112112
}
113113

114-
type PlaceOrderResponse struct {
115-
Status string `json:"status"`
116-
Response PlaceOrderInnerResponse `json:"response"`
114+
type OrderResponse struct {
115+
Status string `json:"status"`
116+
Response OrderInnerResponse `json:"response"`
117117
}
118118

119-
type PlaceOrderInnerResponse struct {
119+
type OrderInnerResponse struct {
120120
Type string `json:"type"`
121121
Data DataResponse `json:"data"`
122122
}
@@ -156,20 +156,6 @@ type CancelCloidOrderAction struct {
156156
Cancels []CancelCloidWire `msgpack:"cancels" json:"cancels"`
157157
}
158158

159-
type CancelOrderResponse struct {
160-
Status string `json:"status"`
161-
Response InnerCancelResponse `json:"response"`
162-
}
163-
164-
type InnerCancelResponse struct {
165-
Type string `json:"type"`
166-
Data CancelResponseStatuses `json:"data"`
167-
}
168-
169-
type CancelResponseStatuses struct {
170-
Statuses []string `json:"statuses"`
171-
}
172-
173159
type RestingStatus struct {
174160
OrderId int `json:"oid"`
175161
Cloid string `json:"cloid,omitempty"`

0 commit comments

Comments
 (0)