forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloid.go
More file actions
43 lines (37 loc) · 1.19 KB
/
cloid.go
File metadata and controls
43 lines (37 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package hyperliquid
import (
"encoding/hex"
"fmt"
"strings"
)
// normalizeCloid normalizes a client order ID to match Python SDK format.
// Python SDK uses cloid.to_raw() which returns hex WITH 0x prefix.
// The cloid is serialized in msgpack and JSON with the 0x prefix.
// This function:
// - Adds 0x prefix if not present
// - Validates it's exactly 32 hex characters (16 bytes) excluding prefix
// - Returns error if format is invalid
func normalizeCloid(cloid *string) (*string, error) {
if cloid == nil || *cloid == "" {
return nil, nil
}
cloidValue := *cloid
// Add 0x prefix if not present
if !strings.HasPrefix(cloidValue, "0x") {
cloidValue = "0x" + cloidValue
}
// Validate it's exactly 34 characters (0x + 32 hex chars = 16 bytes)
if len(cloidValue) != 34 {
return nil, fmt.Errorf(
"cloid must be exactly 32 hex characters (got %d excluding 0x prefix): %s",
len(cloidValue)-2,
cloidValue,
)
}
// Verify the hex part (excluding 0x) is valid hex
if _, err := hex.DecodeString(cloidValue[2:]); err != nil {
return nil, fmt.Errorf("cloid must be valid hex string: %w", err)
}
// Return normalized value WITH 0x prefix to match Python SDK
return &cloidValue, nil
}