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
7 changes: 5 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ type CancelAction struct {
}

// CancelByCloidWire represents cancel by cloid item wire format
// NB: the CancelByCloidWire MUST have `asset` and not `o` like
// CancelOrderWire has
// See: https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/f19056ca1b65cc15a019d92dffa9ada887b3d808/hyperliquid/exchange.py#L305-L310
type CancelByCloidWire struct {
Asset int `json:"a" msgpack:"a"`
OrderID string `json:"cloid" msgpack:"cloid"`
Asset int `json:"asset" msgpack:"asset"`
ClientID string `json:"cloid" msgpack:"cloid"`
}

// CancelByCloidAction represents the cancel by cloid action
Expand Down
2 changes: 1 addition & 1 deletion apiresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAPIResponse_UnmarshalJSON(t *testing.T) {
{
Resting: &OrderStatusResting{
Oid: 12345678901,
ClientID: "0x00000000000000000000000000000000",
ClientID: stringPtr("0x00000000000000000000000000000000"),
},
},
},
Expand Down
20 changes: 13 additions & 7 deletions exchange_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type createOrderRequest struct {
}

type OrderStatusResting struct {
Oid int64 `json:"oid"`
ClientID string `json:"cloid"`
Status string `json:"status"`
Oid int64 `json:"oid"`
ClientID *string `json:"cloid"`
Status string `json:"status"`
}

type OrderStatusFilled struct {
Expand Down Expand Up @@ -137,10 +137,16 @@ func (e *Exchange) BulkOrders(
return nil, err
}
err = e.executeAction(action, &result)
// check if any of the statuses has an error set
for _, s := range result.Data.Statuses {
if s.Error != nil {
return result, fmt.Errorf("%s", *s.Error)
if err != nil {
return nil, err
}

if result != nil {
// check if any of the statuses has an error set
for _, s := range result.Data.Statuses {
if s.Error != nil {
return result, fmt.Errorf("%s", *s.Error)
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions exchange_orders_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (e *Exchange) BulkCancelByCloids(
) (res *APIResponse[CancelOrderResponse], err error) {
cancels := slices.Map(requests, func(req CancelOrderRequestByCloid) CancelByCloidWire {
return CancelByCloidWire{
Asset: e.info.NameToAsset(req.Coin),
OrderID: req.Cloid,
Asset: e.info.NameToAsset(req.Coin),
ClientID: req.Cloid,
}
})

Expand All @@ -96,5 +96,17 @@ func (e *Exchange) BulkCancelByCloids(
if err = e.executeAction(action, &res); err != nil {
return
}

if res == nil || !res.Ok || res.Status == "err" {
if res != nil && res.Err != "" {
return res, fmt.Errorf(res.Err)
}
return res, fmt.Errorf("cancel failed")
}

if err := res.Data.Statuses.FirstError(); err != nil {
return res, err
}

return
}
118 changes: 114 additions & 4 deletions exchange_orders_cancel_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,122 @@
package hyperliquid

import (
"log"
"testing"

"github.com/stretchr/testify/require"
)

var recordForDebug = false
var recordForDebug = true

func TestCancelByCloid(t *testing.T) {
type tc struct {
name string
cassetteName string
// If placeFirst is true, we first place a resting order and use its OID.
placeFirst bool
order CreateOrderRequest
coin string
// used for cancelling a non existent
cloid *string
// If doubleCancel is true, we attempt to cancel the same OID twice to exercise the error path.
doubleCancel bool
wantErr string
record bool
}

cases := []tc{
{
name: "cancel resting order by cloid",
cassetteName: "CancelByCloid",
placeFirst: true,
order: CreateOrderRequest{
Coin: "DOGE",
IsBuy: true,
Size: 50,
Price: 0.12330, // low so it stays resting
OrderType: OrderType{
Limit: &LimitOrderType{Tif: TifGtc},
},
ClientOrderID: stringPtr("0x285ad26a251f390c83d065af51e3f8d9"),
},
coin: "DOGE",
record: false,
},
{
name: "cancel non-existent cloid",
cassetteName: "CancelByCloid",
placeFirst: false,
coin: "BTC",
cloid: stringPtr("0x0000000000000000000000000000fe54"),
wantErr: "Order was never placed, already canceled, or filled.",
record: false,
},
// {
// name: "double cancel DOES NOT return error on second attempt",
// cassetteName: "CancelByCloid",
// placeFirst: true,
// order: CreateOrderRequest{
// Coin: "KAS",
// IsBuy: true,
// Size: 170,
// Price: 0.060793,
// OrderType: OrderType{
// Limit: &LimitOrderType{Tif: TifGtc},
// },
// ClientOrderID: stringPtr("0x185ad26a251f390c83d065af51e3f8d8"),
// },
// coin: "KAS",
// doubleCancel: true,
// // we would expect an error, but it actually passes
// // even with a timeout, we leave this in in case the API ever changes
// // wantErr: "already canceled",
// record: false,
// },
}

for _, tc := range cases {
t.Run(tc.name, func(tt *testing.T) {
log.Printf("name: %s", tc.name)
initRecorder(tt, tc.record, tc.cassetteName)

exchange, err := newExchange(
"0x38d55ff1195c57b9dbc8a72c93119500f1fcd47a33f98149faa18d2fc37932fa",
TestnetAPIURL)
require.NoError(t, err)

cloid := tc.cloid
if tc.placeFirst {
placed, err := exchange.Order(tc.order, nil)
require.NoError(tt, err)
require.NotNil(tt, placed.Resting, "expected resting order so it can be canceled")
cloid = placed.Resting.ClientID
}

// First cancel
resp, err := exchange.CancelByCloid(tc.coin, *cloid)
if tc.wantErr != "" && !tc.doubleCancel {
require.Error(tt, err)
require.Contains(tt, err.Error(), tc.wantErr)
return
}
require.NoError(tt, err)
tt.Logf("cancel response: %+v", resp)

// // Optional second cancel to test error path
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

// if tc.doubleCancel {
// resp2, err2 := exchange.CancelByCloid(tc.coin, *cloid)
// if tc.wantErr != "" {
// require.Error(tt, err2, "expected error on second cancel")
// require.Contains(tt, err2.Error(), tc.wantErr)
// } else {
// require.NoError(tt, err2)
// }
// tt.Logf("second cancel response: %+v, err: %v", resp2, err2)
// }
})
}
}

func TestCancel(t *testing.T) {
type tc struct {
Expand Down Expand Up @@ -38,7 +148,7 @@ func TestCancel(t *testing.T) {
},
},
coin: "DOGE",
record: recordForDebug,
record: false,
},
{
name: "double cancel returns error on second attempt",
Expand All @@ -56,7 +166,7 @@ func TestCancel(t *testing.T) {
coin: "DOGE",
doubleCancel: true,
wantErr: "already canceled",
record: recordForDebug,
record: false,
},
{
name: "cancel non-existent oid",
Expand All @@ -65,7 +175,7 @@ func TestCancel(t *testing.T) {
coin: "DOGE",
oid: 1,
wantErr: "Order was never placed, already canceled, or filled.",
record: recordForDebug,
record: false,
},
}

Expand Down
8 changes: 2 additions & 6 deletions exchange_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ func TestOrders(t *testing.T) {
Tif: TifGtc,
},
},
ClientOrderID: strAsPtr("0x06c60000000000000000000000003f5a"),
ClientOrderID: stringPtr("0x06c60000000000000000000000003f5a"),
},
result: OrderStatus{
Resting: &OrderStatusResting{
Oid: 37543130760,
ClientID: "0x06c60000000000000000000000003f5a",
ClientID: stringPtr("0x06c60000000000000000000000003f5a"),
},
},
record: false,
Expand Down Expand Up @@ -291,7 +291,3 @@ func TestOrders(t *testing.T) {
})
}
}

func strAsPtr(s string) *string {
return &s
}
Loading
Loading