|
| 1 | +package hyperliquid |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +var recordForDebug = false |
| 10 | + |
| 11 | +func TestCancel(t *testing.T) { |
| 12 | + type tc struct { |
| 13 | + name string |
| 14 | + cassetteName string |
| 15 | + // If placeFirst is true, we first place a resting order and use its OID. |
| 16 | + placeFirst bool |
| 17 | + order CreateOrderRequest |
| 18 | + coin string |
| 19 | + oid int64 // used only when placeFirst == false |
| 20 | + // If doubleCancel is true, we attempt to cancel the same OID twice to exercise the error path. |
| 21 | + doubleCancel bool |
| 22 | + wantErr string |
| 23 | + record bool |
| 24 | + } |
| 25 | + |
| 26 | + cases := []tc{ |
| 27 | + { |
| 28 | + name: "cancel resting order by oid", |
| 29 | + cassetteName: "Cancel", |
| 30 | + placeFirst: true, |
| 31 | + order: CreateOrderRequest{ |
| 32 | + Coin: "DOGE", |
| 33 | + IsBuy: true, |
| 34 | + Size: 45, |
| 35 | + Price: 0.12330, // low so it stays resting |
| 36 | + OrderType: OrderType{ |
| 37 | + Limit: &LimitOrderType{Tif: TifGtc}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + coin: "DOGE", |
| 41 | + record: recordForDebug, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "double cancel returns error on second attempt", |
| 45 | + cassetteName: "Cancel", |
| 46 | + placeFirst: true, |
| 47 | + order: CreateOrderRequest{ |
| 48 | + Coin: "DOGE", |
| 49 | + IsBuy: true, |
| 50 | + Size: 45, |
| 51 | + Price: 0.12330, |
| 52 | + OrderType: OrderType{ |
| 53 | + Limit: &LimitOrderType{Tif: TifGtc}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + coin: "DOGE", |
| 57 | + doubleCancel: true, |
| 58 | + wantErr: "already canceled", |
| 59 | + record: recordForDebug, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "cancel non-existent oid", |
| 63 | + cassetteName: "Cancel", |
| 64 | + placeFirst: false, |
| 65 | + coin: "DOGE", |
| 66 | + oid: 1, |
| 67 | + wantErr: "Order was never placed, already canceled, or filled.", |
| 68 | + record: recordForDebug, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tc := range cases { |
| 73 | + t.Run(tc.name, func(tt *testing.T) { |
| 74 | + initRecorder(tt, tc.record, tc.cassetteName) |
| 75 | + |
| 76 | + exchange, err := newExchange( |
| 77 | + "0x38d55ff1195c57b9dbc8a72c93119500f1fcd47a33f98149faa18d2fc37932fa", |
| 78 | + TestnetAPIURL) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + oid := tc.oid |
| 82 | + if tc.placeFirst { |
| 83 | + placed, err := exchange.Order(tc.order, nil) |
| 84 | + require.NoError(tt, err) |
| 85 | + require.NotNil(tt, placed.Resting, "expected resting order so it can be canceled") |
| 86 | + oid = placed.Resting.Oid |
| 87 | + } |
| 88 | + |
| 89 | + // First cancel |
| 90 | + resp, err := exchange.Cancel(tc.coin, oid) |
| 91 | + if tc.wantErr != "" && !tc.doubleCancel { |
| 92 | + require.Error(tt, err) |
| 93 | + require.Contains(tt, err.Error(), tc.wantErr) |
| 94 | + return |
| 95 | + } |
| 96 | + require.NoError(tt, err) |
| 97 | + tt.Logf("cancel response: %+v", resp) |
| 98 | + |
| 99 | + // Optional second cancel to test error path |
| 100 | + if tc.doubleCancel { |
| 101 | + resp2, err2 := exchange.Cancel(tc.coin, oid) |
| 102 | + require.Error(tt, err2, "expected error on second cancel") |
| 103 | + if tc.wantErr != "" { |
| 104 | + require.Contains(tt, err2.Error(), tc.wantErr) |
| 105 | + } |
| 106 | + tt.Logf("second cancel response: %+v, err: %v", resp2, err2) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments