forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_msgpack_test.go
More file actions
86 lines (72 loc) · 2.03 KB
/
types_msgpack_test.go
File metadata and controls
86 lines (72 loc) · 2.03 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package hyperliquid
import (
"bytes"
"encoding/hex"
"fmt"
"log"
"testing"
"github.com/vmihailenco/msgpack/v5"
)
type OrderWireOld struct {
Asset int `json:"a"`
IsBuy bool `json:"b"`
LimitPx string `json:"p"`
Size string `json:"s"`
ReduceOnly bool `json:"r"`
OrderType map[string]any `json:"t"`
Cloid *string `json:"c,omitempty"`
}
type OrderWireNew struct {
Asset int `json:"a" msgpack:"a"`
IsBuy bool `json:"b" msgpack:"b"`
LimitPx string `json:"p" msgpack:"p"`
Size string `json:"s" msgpack:"s"`
ReduceOnly bool `json:"r" msgpack:"r"`
OrderType map[string]any `json:"t" msgpack:"t"`
Cloid *string `json:"c,omitempty" msgpack:"c,omitempty"`
}
func Test_Msgpack_Field_Ordering(t *testing.T) {
// Test data
orderType := map[string]any{
"limit": map[string]any{
"tif": "Gtc",
},
}
// Test with old struct (no msgpack tags)
oldOrder := OrderWireOld{
Asset: 0,
IsBuy: true,
LimitPx: "40000",
Size: "0.001",
ReduceOnly: false,
OrderType: orderType,
}
// Test with new struct (with msgpack tags)
newOrder := OrderWireNew{
Asset: 0,
IsBuy: true,
LimitPx: "40000",
Size: "0.001",
ReduceOnly: false,
OrderType: orderType,
}
// Serialize both with msgpack
var bufOld, bufNew bytes.Buffer
encOld := msgpack.NewEncoder(&bufOld)
encOld.SetSortMapKeys(true)
encNew := msgpack.NewEncoder(&bufNew)
encNew.SetSortMapKeys(true)
err := encOld.Encode(oldOrder)
if err != nil {
log.Fatal(err)
}
err = encNew.Encode(newOrder)
if err != nil {
log.Fatal(err)
}
oldBytes := bufOld.Bytes()
newBytes := bufNew.Bytes()
fmt.Printf("Old struct (no msgpack tags): %s\n", hex.EncodeToString(oldBytes))
fmt.Printf("New struct (with msgpack tags): %s\n", hex.EncodeToString(newBytes))
fmt.Printf("Are they identical? %v\n", bytes.Equal(oldBytes, newBytes))
}