forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
190 lines (159 loc) · 3.81 KB
/
api.go
File metadata and controls
190 lines (159 loc) · 3.81 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package hyperliquid
import (
"encoding/json"
"errors"
"fmt"
"sync"
"github.com/valyala/fastjson"
)
// Pool of parsers to avoid allocations
var parserPool = sync.Pool{
New: func() any {
return &fastjson.Parser{}
},
}
type APIResponse[T any] struct {
Status string
Data T
Type string
Err string
Ok bool
}
func (r *APIResponse[T]) UnmarshalJSON(data []byte) error {
// Get parser from pool
parser := parserPool.Get().(*fastjson.Parser)
defer parserPool.Put(parser)
parsed, err := parser.ParseBytes(data)
if err != nil {
return fmt.Errorf("failed to parse JSON response: %w", err)
}
// Get status
r.Status = string(parsed.GetStringBytes("status"))
r.Ok = r.Status == "ok"
if !r.Ok {
// When status is not "ok", "response" is usually a string error message
r.Err = string(parsed.GetStringBytes("response"))
return nil
}
// When status is "ok", "response" contains "type" and "data"
r.Type = string(parsed.GetStringBytes("response", "type"))
// GetStringBytes() only works on string, we should do a Get instead
responseData := parsed.Get("response", "data")
if responseData == nil {
return fmt.Errorf("missing response.data field in successful response")
}
b := responseData.MarshalTo(nil)
// Use fastjson's built-in unmarshaling if possible, fallback to json.Unmarshal
if err := json.Unmarshal(b, &r.Data); err != nil {
return fmt.Errorf("failed to unmarshal response data: %w", err)
}
return nil
}
type Tuple2[E1 any, E2 any] struct {
First E1
Second E2
}
func (t *Tuple2[E1, E2]) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if len(raw) != 2 {
return fmt.Errorf("expected array of length 2, got %d", len(raw))
}
if err := json.Unmarshal(raw[0], &t.First); err != nil {
return err
}
if err := json.Unmarshal(raw[1], &t.Second); err != nil {
return err
}
return nil
}
func (t Tuple2[E1, E2]) MarshalJSON() ([]byte, error) {
return json.Marshal([2]any{t.First, t.Second})
}
type MixedValue json.RawMessage
func (mv *MixedValue) UnmarshalJSON(data []byte) error {
*mv = data
return nil
}
func (mv MixedValue) MarshalJSON() ([]byte, error) {
return mv, nil
}
func (mv *MixedValue) String() (string, bool) {
var s string
if err := json.Unmarshal(*mv, &s); err != nil {
return "", false
}
return s, true
}
func (mv *MixedValue) Object() (map[string]any, bool) {
var obj map[string]any
if err := json.Unmarshal(*mv, &obj); err != nil {
return nil, false
}
return obj, true
}
func (mv *MixedValue) Array() ([]json.RawMessage, bool) {
var arr []json.RawMessage
if err := json.Unmarshal(*mv, &arr); err != nil {
return nil, false
}
return arr, true
}
func (mv *MixedValue) Parse(v any) error {
return json.Unmarshal(*mv, v)
}
func (mv *MixedValue) Type() string {
if mv == nil || len(*mv) == 0 {
return "null"
}
first := (*mv)[0]
switch first {
case '"':
return "string"
case '{':
return "object"
case '[':
return "array"
case 't', 'f':
return "boolean"
case 'n':
return "null"
default:
return "number"
}
}
type MixedArray []MixedValue
func (ma *MixedArray) UnmarshalJSON(data []byte) error {
var rawArr []MixedValue
if err := json.Unmarshal(data, &rawArr); err != nil {
return err
}
*ma = rawArr
return nil
}
func (ma MixedArray) FirstError() error {
for _, mv := range ma {
if s, ok := mv.String(); ok {
if s == "success" {
continue
}
// any other string? treat as error text
return errors.New(s)
}
if obj, ok := mv.Object(); ok {
if v, ok := obj["error"]; ok {
if msg, ok := v.(string); ok && msg != "" {
return errors.New(msg)
}
// stringify unknown error shapes
b, _ := json.Marshal(v)
return errors.New(string(b))
}
}
// Unknown shape -> generic failure
return errors.New("cancel failed")
}
return nil
}