-
-
Notifications
You must be signed in to change notification settings - Fork 26
[Tests] Add and fix CancelByCloid #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sonirico
merged 1 commit into
sonirico:master
from
terwey:test/exchange-orders-cancelbycloid
Aug 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -38,7 +148,7 @@ func TestCancel(t *testing.T) { | |
}, | ||
}, | ||
coin: "DOGE", | ||
record: recordForDebug, | ||
record: false, | ||
}, | ||
{ | ||
name: "double cancel returns error on second attempt", | ||
|
@@ -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", | ||
|
@@ -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, | ||
}, | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.