Repository navigation
Expand file tree
/
Copy pathhyperliquid.go
More file actions
346 lines (322 loc) · 9.73 KB
/
Copy pathhyperliquid.go
File metadata and controls
346 lines (322 loc) · 9.73 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"time"
)
type HyperliquidClient struct {
baseURL string
client *http.Client
maxAttempts int
retryDelay time.Duration
}
func NewHyperliquidClient(proxyEnabled bool, proxyURL string) *HyperliquidClient {
return &HyperliquidClient{
baseURL: "https://api.hyperliquid.xyz",
client: newMarketHTTPClient(proxyEnabled, proxyURL),
maxAttempts: 3,
retryDelay: 2 * time.Second,
}
}
type hyperMeta struct {
Universe []struct {
Name string `json:"name"`
} `json:"universe"`
}
type hyperAssetContext struct {
MarkPrice json.RawMessage `json:"markPx"`
OraclePrice json.RawMessage `json:"oraclePx"`
PreviousPrice json.RawMessage `json:"prevDayPx"`
Funding json.RawMessage `json:"funding"`
OpenInterest json.RawMessage `json:"openInterest"`
DayTurnover json.RawMessage `json:"dayNtlVlm"`
}
type hyperCandle struct {
Start int64 `json:"t"`
End int64 `json:"T"`
Symbol string `json:"s"`
Interval string `json:"i"`
Open json.RawMessage `json:"o"`
High json.RawMessage `json:"h"`
Low json.RawMessage `json:"l"`
Close json.RawMessage `json:"c"`
Volume json.RawMessage `json:"v"`
}
func (c *HyperliquidClient) GetPerpetualPrices(symbols []string, now time.Time) ([]PerpetualPrice, error) {
if len(symbols) == 0 {
return nil, nil
}
contextsByDEX := make(map[string]map[string]json.RawMessage)
var issues []error
for _, symbol := range symbols {
dex, ok := perpetualDEX(symbol)
if !ok {
issues = append(issues, fmt.Errorf("%s has no DEX prefix", symbol))
continue
}
if _, loaded := contextsByDEX[dex]; loaded {
continue
}
contexts, err := c.getAssetContexts(dex)
if err != nil {
issues = append(issues, fmt.Errorf("DEX %s metadata: %w", dex, err))
contextsByDEX[dex] = nil
continue
}
contextsByDEX[dex] = contexts
}
prices := make([]PerpetualPrice, 0, len(symbols))
seen := make(map[string]bool, len(symbols))
for _, configuredSymbol := range symbols {
symbol := strings.TrimSpace(configuredSymbol)
if symbol == "" || seen[symbol] {
continue
}
seen[symbol] = true
dex, ok := perpetualDEX(symbol)
if !ok {
continue
}
contexts := contextsByDEX[dex]
if contexts == nil {
continue
}
raw, found := contexts[symbol]
if !found {
issues = append(issues, fmt.Errorf("%s is absent from Hyperliquid universe", symbol))
continue
}
item, err := parsePerpetualContext(symbol, raw, now)
if err != nil {
issues = append(issues, err)
continue
}
candle, err := c.getLatestCompletedCandle(symbol, now)
if err != nil {
issues = append(issues, fmt.Errorf("%s 1h candle: %w", symbol, err))
} else if candle != nil {
item.HourlyCandleStart = timePtr(time.UnixMilli(candle.Start))
item.HourlyVolume = floatPtr(candle.Volume)
item.HourlyTurnover = floatPtr(candle.Volume * candle.RepresentativePrice)
} else {
issues = append(issues, fmt.Errorf("%s has no completed 1h candle", symbol))
}
prices = append(prices, item)
}
return prices, errors.Join(issues...)
}
func (c *HyperliquidClient) getAssetContexts(dex string) (map[string]json.RawMessage, error) {
body, err := c.post(map[string]any{"type": "metaAndAssetCtxs", "dex": dex})
if err != nil {
return nil, err
}
var response []json.RawMessage
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("decode metaAndAssetCtxs: %w", err)
}
if len(response) != 2 {
return nil, fmt.Errorf("metaAndAssetCtxs returned %d elements, want 2", len(response))
}
var meta hyperMeta
if err := json.Unmarshal(response[0], &meta); err != nil {
return nil, fmt.Errorf("decode perpetual metadata: %w", err)
}
var contexts []json.RawMessage
if err := json.Unmarshal(response[1], &contexts); err != nil {
return nil, fmt.Errorf("decode asset contexts: %w", err)
}
if len(contexts) < len(meta.Universe) {
return nil, fmt.Errorf("asset contexts length %d is shorter than universe length %d", len(contexts), len(meta.Universe))
}
result := make(map[string]json.RawMessage, len(meta.Universe))
for index, asset := range meta.Universe {
result[asset.Name] = contexts[index]
}
return result, nil
}
type completedHyperCandle struct {
Start int64
Volume float64
RepresentativePrice float64
}
func (c *HyperliquidClient) getLatestCompletedCandle(symbol string, now time.Time) (*completedHyperCandle, error) {
body, err := c.post(map[string]any{
"type": "candleSnapshot",
"req": map[string]any{
"coin": symbol,
"interval": "1h",
"startTime": now.Add(-8 * time.Hour).UnixMilli(),
"endTime": now.UnixMilli(),
},
})
if err != nil {
return nil, err
}
var candles []hyperCandle
if err := json.Unmarshal(body, &candles); err != nil {
return nil, fmt.Errorf("decode candleSnapshot: %w", err)
}
cutoff := now.Truncate(time.Hour).UnixMilli()
var selected *hyperCandle
for index := range candles {
candle := &candles[index]
if candle.Symbol != symbol || candle.Interval != "1h" || candle.End >= cutoff {
continue
}
if selected == nil || candle.Start > selected.Start {
selected = candle
}
}
if selected == nil {
return nil, nil
}
open, err := requiredJSONNumber(selected.Open, "candle open")
if err != nil {
return nil, err
}
high, err := requiredJSONNumber(selected.High, "candle high")
if err != nil {
return nil, err
}
low, err := requiredJSONNumber(selected.Low, "candle low")
if err != nil {
return nil, err
}
closePrice, err := requiredJSONNumber(selected.Close, "candle close")
if err != nil {
return nil, err
}
volume, err := requiredJSONNumber(selected.Volume, "candle volume")
if err != nil {
return nil, err
}
if volume < 0 {
return nil, fmt.Errorf("candle volume is negative")
}
return &completedHyperCandle{
Start: selected.Start,
Volume: volume,
RepresentativePrice: (open + high + low + closePrice) / 4,
}, nil
}
func (c *HyperliquidClient) post(payload any) ([]byte, error) {
requestBody, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("encode request: %w", err)
}
var lastErr error
for attempt := 1; attempt <= c.maxAttempts; attempt++ {
req, err := http.NewRequest(http.MethodPost, c.baseURL+"/info", bytes.NewReader(requestBody))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
lastErr = fmt.Errorf("request failed: %w", err)
} else {
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
resp.Body.Close()
if readErr != nil {
lastErr = fmt.Errorf("read response: %w", readErr)
} else if resp.StatusCode != http.StatusOK {
lastErr = fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
} else {
return body, nil
}
}
if attempt < c.maxAttempts {
time.Sleep(c.retryDelay)
}
}
return nil, lastErr
}
func parsePerpetualContext(symbol string, raw json.RawMessage, now time.Time) (PerpetualPrice, error) {
var context hyperAssetContext
if err := json.Unmarshal(raw, &context); err != nil {
return PerpetualPrice{}, fmt.Errorf("%s context JSON: %w", symbol, err)
}
mark, err := requiredJSONNumber(context.MarkPrice, "markPx")
if err != nil || mark <= 0 {
if err == nil {
err = fmt.Errorf("must be positive")
}
return PerpetualPrice{}, fmt.Errorf("%s markPx: %w", symbol, err)
}
item := PerpetualPrice{Symbol: symbol, MarkPrice: mark, ObservedAt: now}
oracle, err := optionalJSONNumber(context.OraclePrice, "oraclePx")
if err != nil {
return PerpetualPrice{}, fmt.Errorf("%s: %w", symbol, err)
}
item.OraclePrice = oracle
if oracle != nil && *oracle != 0 {
spread := mark - *oracle
item.MarkOracleSpread = floatPtr(spread)
item.MarkOracleSpreadPct = floatPtr(spread / *oracle * 100)
}
previous, err := optionalJSONNumber(context.PreviousPrice, "prevDayPx")
if err != nil {
return PerpetualPrice{}, fmt.Errorf("%s: %w", symbol, err)
}
if previous != nil && *previous != 0 {
item.PriceChange24hPct = floatPtr((mark - *previous) / *previous * 100)
}
item.FundingRate, err = optionalJSONNumber(context.Funding, "funding")
if err != nil {
return PerpetualPrice{}, fmt.Errorf("%s: %w", symbol, err)
}
item.DayTurnover, err = optionalJSONNumber(context.DayTurnover, "dayNtlVlm")
if err != nil {
return PerpetualPrice{}, fmt.Errorf("%s: %w", symbol, err)
}
openInterest, err := optionalJSONNumber(context.OpenInterest, "openInterest")
if err != nil {
return PerpetualPrice{}, fmt.Errorf("%s: %w", symbol, err)
}
if openInterest != nil && *openInterest >= 0 {
notional := *openInterest * mark
if isFinite(notional) {
item.NotionalOpenInterest = floatPtr(notional)
}
}
return item, nil
}
func requiredJSONNumber(raw json.RawMessage, name string) (float64, error) {
value, err := optionalJSONNumber(raw, name)
if err != nil {
return 0, err
}
if value == nil {
return 0, fmt.Errorf("%s is unavailable", name)
}
return *value, nil
}
func optionalJSONNumber(raw json.RawMessage, name string) (*float64, error) {
trimmed := strings.TrimSpace(string(raw))
if trimmed == "" || trimmed == "null" || trimmed == `""` {
return nil, nil
}
if strings.HasPrefix(trimmed, `"`) {
var text string
if err := json.Unmarshal(raw, &text); err != nil {
return nil, fmt.Errorf("%s is malformed: %w", name, err)
}
trimmed = text
}
value, err := strconv.ParseFloat(trimmed, 64)
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) {
return nil, fmt.Errorf("%s is not a finite number", name)
}
return floatPtr(value), nil
}
func perpetualDEX(symbol string) (string, bool) {
dex, _, ok := strings.Cut(strings.TrimSpace(symbol), ":")
return dex, ok && dex != ""
}