-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathtypes_msgpack_test.go
More file actions
66 lines (54 loc) · 1.82 KB
/
types_msgpack_test.go
File metadata and controls
66 lines (54 loc) · 1.82 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package hyperliquid
import (
"bytes"
"encoding/hex"
"testing"
"github.com/vmihailenco/msgpack/v5"
)
func Test_Msgpack_Field_Ordering(t *testing.T) {
// CRITICAL: This test verifies that OrderWire struct serializes in the correct order
// to match Python SDK's msgpack output. Python preserves dict insertion order.
// Python order for order_wire is: a, b, p, s, r, t (and optionally c)
// See: hyperliquid-python-sdk/hyperliquid/utils/signing.py:order_request_to_order_wire
orderTypeNew := OrderWireType{
Limit: &OrderWireTypeLimit{
Tif: TifGtc,
},
}
// Test with current struct (must match Python order)
newOrder := OrderWire{
Asset: 0,
IsBuy: true,
LimitPx: "40000",
Size: "0.001",
ReduceOnly: false,
OrderType: orderTypeNew,
Cloid: nil, // No cloid for this test
}
// Serialize with msgpack
var bufNew bytes.Buffer
encNew := msgpack.NewEncoder(&bufNew)
// CRITICAL: Do NOT use SetSortMapKeys(true) - we need field order from struct
encNew.UseCompactInts(true)
err := encNew.Encode(newOrder)
if err != nil {
t.Fatalf("Failed to encode: %v", err)
}
newBytes := bufNew.Bytes()
goHex := hex.EncodeToString(newBytes)
// Expected output from Python SDK for equivalent order_wire
// Python code: {"a": 0, "b": True, "p": "40000", "s": "0.001", "r": False, "t": {"limit": {"tif": "Gtc"}}}
// Verified with: python3 test_order_wire.py
pythonExpectedHex := "86a16100a162c3a170a53430303030a173a5302e303031a172c2a17481a56c696d697481a3746966a3477463"
t.Logf("Go msgpack output: %s", goHex)
t.Logf("Python expected: %s", pythonExpectedHex)
if goHex != pythonExpectedHex {
t.Errorf(
"Msgpack output does NOT match Python SDK!\nGot: %s\nExpected: %s",
goHex,
pythonExpectedHex,
)
} else {
t.Logf("✓ Msgpack field ordering matches Python SDK!")
}
}