forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.go
More file actions
828 lines (718 loc) · 21.7 KB
/
info.go
File metadata and controls
828 lines (718 loc) · 21.7 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
package hyperliquid
import (
"context"
"encoding/json"
"fmt"
)
const (
// spotAssetIndexOffset is the offset added to spot asset indices
spotAssetIndexOffset = 10000
)
type Info struct {
debug bool
client *client
coinToAsset map[string]int
nameToCoin map[string]string
assetToDecimal map[int]int
clientOpts []ClientOpt
}
func NewInfo(
ctx context.Context,
baseURL string,
skipWS bool,
meta *Meta,
spotMeta *SpotMeta,
opts ...InfoOpt,
) *Info {
info := &Info{
coinToAsset: make(map[string]int),
nameToCoin: make(map[string]string),
assetToDecimal: make(map[int]int),
}
for _, opt := range opts {
opt.Apply(info)
}
if info.debug {
info.clientOpts = append(info.clientOpts, clientOptDebugMode())
}
info.client = newClient(baseURL, info.clientOpts...)
if meta == nil {
var err error
meta, err = info.Meta(ctx)
if err != nil {
panic(err)
}
}
if spotMeta == nil {
var err error
spotMeta, err = info.SpotMeta(ctx)
if err != nil {
panic(err)
}
}
// Map perp assets
for asset, assetInfo := range meta.Universe {
info.coinToAsset[assetInfo.Name] = asset
info.nameToCoin[assetInfo.Name] = assetInfo.Name
info.assetToDecimal[asset] = assetInfo.SzDecimals
}
// Map spot assets starting at 10000
for _, spotInfo := range spotMeta.Universe {
asset := spotInfo.Index + spotAssetIndexOffset
info.coinToAsset[spotInfo.Name] = asset
info.nameToCoin[spotInfo.Name] = spotInfo.Name
info.assetToDecimal[asset] = spotMeta.Tokens[spotInfo.Tokens[0]].SzDecimals
}
return info
}
// postTimeRangeRequest makes a POST request with time range parameters
func (i *Info) postTimeRangeRequest(
ctx context.Context,
requestType, user string,
startTime int64,
endTime *int64,
extraParams map[string]any,
) ([]byte, error) {
payload := map[string]any{
"type": requestType,
"startTime": startTime,
}
if user != "" {
payload["user"] = user
}
if endTime != nil {
payload["endTime"] = *endTime
}
for k, v := range extraParams {
payload[k] = v
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch %s: %w", requestType, err)
}
return resp, nil
}
func parseMetaResponse(resp []byte) (*Meta, error) {
var meta map[string]json.RawMessage
if err := json.Unmarshal(resp, &meta); err != nil {
return nil, fmt.Errorf("failed to unmarshal meta response: %w", err)
}
var universe []AssetInfo
if err := json.Unmarshal(meta["universe"], &universe); err != nil {
return nil, fmt.Errorf("failed to unmarshal universe: %w", err)
}
var marginTables [][]any
if err := json.Unmarshal(meta["marginTables"], &marginTables); err != nil {
return nil, fmt.Errorf("failed to unmarshal margin tables: %w", err)
}
marginTablesResult := make([]MarginTable, len(marginTables))
for i, marginTable := range marginTables {
id := marginTable[0].(float64)
tableBytes, err := json.Marshal(marginTable[1])
if err != nil {
return nil, fmt.Errorf("failed to marshal margin table data: %w", err)
}
var marginTableData map[string]any
if err := json.Unmarshal(tableBytes, &marginTableData); err != nil {
return nil, fmt.Errorf("failed to unmarshal margin table data: %w", err)
}
marginTiersBytes, err := json.Marshal(marginTableData["marginTiers"])
if err != nil {
return nil, fmt.Errorf("failed to marshal margin tiers: %w", err)
}
var marginTiers []MarginTier
if err := json.Unmarshal(marginTiersBytes, &marginTiers); err != nil {
return nil, fmt.Errorf("failed to unmarshal margin tiers: %w", err)
}
marginTablesResult[i] = MarginTable{
ID: int(id),
Description: marginTableData["description"].(string),
MarginTiers: marginTiers,
}
}
return &Meta{
Universe: universe,
MarginTables: marginTablesResult,
}, nil
}
// Meta retrieves perpetuals metadata
// If dex is empty string, returns metadata for the first perp dex (default)
func (i *Info) Meta(ctx context.Context, dex ...string) (*Meta, error) {
payload := map[string]any{
"type": "meta",
}
if len(dex) > 0 && dex[0] != "" {
payload["dex"] = dex[0]
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch meta: %w", err)
}
return parseMetaResponse(resp)
}
func (i *Info) SpotMeta(ctx context.Context) (*SpotMeta, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "spotMeta",
})
if err != nil {
return nil, fmt.Errorf("failed to fetch spot meta: %w", err)
}
var spotMeta SpotMeta
if err := json.Unmarshal(resp, &spotMeta); err != nil {
return nil, fmt.Errorf("failed to unmarshal spot meta response: %w", err)
}
return &spotMeta, nil
}
func (i *Info) NameToAsset(name string) int {
coin := i.nameToCoin[name]
return i.coinToAsset[coin]
}
// UserState retrieves user's perpetuals account summary
// If dex is empty string, returns state for the first perp dex (default)
func (i *Info) UserState(ctx context.Context, address string, dex ...string) (*UserState, error) {
payload := map[string]any{
"type": "clearinghouseState",
"user": address,
}
if len(dex) > 0 && dex[0] != "" {
payload["dex"] = dex[0]
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch user state: %w", err)
}
var result UserState
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user state: %w", err)
}
return &result, nil
}
func (i *Info) SpotUserState(ctx context.Context, address string) (*SpotUserState, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "spotClearinghouseState",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch spot user state: %w", err)
}
var result SpotUserState
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal spot user state: %w", err)
}
return &result, nil
}
// OpenOrders retrieves user's open orders
// If dex is empty string, returns orders for the first perp dex (default)
// Note: Spot open orders are only included with the first perp dex
func (i *Info) OpenOrders(ctx context.Context, address string, dex ...string) ([]OpenOrder, error) {
payload := map[string]any{
"type": "openOrders",
"user": address,
}
if len(dex) > 0 && dex[0] != "" {
payload["dex"] = dex[0]
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch open orders: %w", err)
}
var result []OpenOrder
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal open orders: %w", err)
}
return result, nil
}
// FrontendOpenOrders retrieves user's open orders with frontend info
// If dex is empty string, returns orders for the first perp dex (default)
// Note: Spot open orders are only included with the first perp dex
func (i *Info) FrontendOpenOrders(
ctx context.Context,
address string,
dex ...string,
) ([]FrontendOpenOrder, error) {
payload := map[string]any{
"type": "frontendOpenOrders",
"user": address,
}
if len(dex) > 0 && dex[0] != "" {
payload["dex"] = dex[0]
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch frontend open orders: %w", err)
}
var result []FrontendOpenOrder
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal frontend open orders: %w", err)
}
return result, nil
}
// AllMids retrieves mids for all coins
// If dex is empty string, returns mids for the first perp dex (default)
// Note: Spot mids are only included with the first perp dex
func (i *Info) AllMids(ctx context.Context, dex ...string) (map[string]string, error) {
payload := map[string]any{
"type": "allMids",
}
if len(dex) > 0 && dex[0] != "" {
payload["dex"] = dex[0]
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch all mids: %w", err)
}
var result map[string]string
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal all mids: %w", err)
}
return result, nil
}
func (i *Info) UserFills(ctx context.Context, address string) ([]Fill, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "userFills",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch user fills: %w", err)
}
var result []Fill
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user fills: %w", err)
}
return result, nil
}
func (i *Info) HistoricalOrders(ctx context.Context, address string) ([]OrderQueryResponse, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "historicalOrders",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch historical orders: %w", err)
}
var result []OrderQueryResponse
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal historical orders: %w", err)
}
return result, nil
}
func (i *Info) UserFillsByTime(
ctx context.Context,
address string,
startTime int64,
endTime *int64,
aggregateByTime *bool,
) ([]Fill, error) {
var extraParams = make(map[string]any, 0)
if aggregateByTime != nil {
extraParams["aggregateByTime"] = aggregateByTime
}
resp, err := i.postTimeRangeRequest(
ctx,
"userFillsByTime",
address,
startTime,
endTime,
extraParams,
)
if err != nil {
return nil, err
}
var result []Fill
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user fills by time: %w", err)
}
return result, nil
}
func (i *Info) MetaAndAssetCtxs(ctx context.Context) (*MetaAndAssetCtxs, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "metaAndAssetCtxs",
})
if err != nil {
return nil, fmt.Errorf("failed to fetch meta and asset contexts: %w", err)
}
var result []any
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal meta and asset contexts: %w", err)
}
if len(result) < 2 {
return nil, fmt.Errorf("expected at least 2 elements in response, got %d", len(result))
}
metaBytes, err := json.Marshal(result[0])
if err != nil {
return nil, fmt.Errorf("failed to marshal meta data: %w", err)
}
meta, err := parseMetaResponse(metaBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse meta: %w", err)
}
ctxsBytes, err := json.Marshal(result[1])
if err != nil {
return nil, fmt.Errorf("failed to marshal ctxs data: %w", err)
}
var ctxs []AssetCtx
if err := json.Unmarshal(ctxsBytes, &ctxs); err != nil {
return nil, fmt.Errorf("failed to unmarshal ctxs: %w", err)
}
metaAndAssetCtxs := &MetaAndAssetCtxs{
Meta: *meta,
Ctxs: ctxs,
}
return metaAndAssetCtxs, nil
}
func (i *Info) SpotMetaAndAssetCtxs(ctx context.Context) (*SpotMetaAndAssetCtxs, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "spotMetaAndAssetCtxs",
})
if err != nil {
return nil, fmt.Errorf("failed to fetch spot meta and asset contexts: %w", err)
}
var result []any
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal spot meta and asset contexts: %w", err)
}
if len(result) < 2 {
return nil, fmt.Errorf("expected at least 2 elements in response, got %d", len(result))
}
// Unmarshal the first element (SpotMeta)
metaBytes, err := json.Marshal(result[0])
if err != nil {
return nil, fmt.Errorf("failed to marshal meta data: %w", err)
}
var meta SpotMeta
if err := json.Unmarshal(metaBytes, &meta); err != nil {
return nil, fmt.Errorf("failed to unmarshal meta: %w", err)
}
// Unmarshal the second element ([]SpotAssetCtx)
ctxsBytes, err := json.Marshal(result[1])
if err != nil {
return nil, fmt.Errorf("failed to marshal ctxs data: %w", err)
}
var ctxs []SpotAssetCtx
if err := json.Unmarshal(ctxsBytes, &ctxs); err != nil {
return nil, fmt.Errorf("failed to unmarshal ctxs: %w", err)
}
return &SpotMetaAndAssetCtxs{
Meta: meta,
Ctxs: ctxs,
}, nil
}
func (i *Info) FundingHistory(
ctx context.Context,
name string,
startTime int64,
endTime *int64,
) ([]FundingHistory, error) {
coin := i.nameToCoin[name]
resp, err := i.postTimeRangeRequest(
ctx,
"fundingHistory",
"",
startTime,
endTime,
map[string]any{"coin": coin},
)
if err != nil {
return nil, err
}
var result []FundingHistory
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal funding history: %w", err)
}
return result, nil
}
func (i *Info) UserFundingHistory(
ctx context.Context,
user string,
startTime int64,
endTime *int64,
) ([]UserFundingHistory, error) {
resp, err := i.postTimeRangeRequest(ctx, "userFunding", user, startTime, endTime, nil)
if err != nil {
return nil, err
}
var result []UserFundingHistory
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user funding history: %w", err)
}
return result, nil
}
func (i *Info) L2Snapshot(ctx context.Context, name string) (*L2Book, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "l2Book",
"coin": i.nameToCoin[name],
})
if err != nil {
return nil, fmt.Errorf("failed to fetch L2 snapshot: %w", err)
}
var result L2Book
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal L2 snapshot: %w", err)
}
return &result, nil
}
func (i *Info) CandlesSnapshot(
ctx context.Context,
name, interval string,
startTime, endTime int64,
) ([]Candle, error) {
req := map[string]any{
"coin": i.nameToCoin[name],
"interval": interval,
"startTime": startTime,
"endTime": endTime,
}
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "candleSnapshot",
"req": req,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch candles snapshot: %w", err)
}
var result []Candle
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal candles snapshot: %w", err)
}
return result, nil
}
func (i *Info) UserFees(ctx context.Context, address string) (*UserFees, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "userFees",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch user fees: %w", err)
}
var result UserFees
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user fees: %w", err)
}
return &result, nil
}
func (i *Info) UserActiveAssetData(
ctx context.Context,
address string,
coin string,
) (*UserActiveAssetData, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "activeAssetData",
"user": address,
"coin": coin,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch user active asset data: %w", err)
}
var result UserActiveAssetData
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal user active asset data: %w", err)
}
return &result, nil
}
func (i *Info) UserStakingSummary(ctx context.Context, address string) (*StakingSummary, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "delegatorSummary",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch staking summary: %w", err)
}
var result StakingSummary
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal staking summary: %w", err)
}
return &result, nil
}
func (i *Info) UserStakingDelegations(
ctx context.Context,
address string,
) ([]StakingDelegation, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "delegations",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch staking delegations: %w", err)
}
var result []StakingDelegation
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal staking delegations: %w", err)
}
return result, nil
}
func (i *Info) UserStakingRewards(ctx context.Context, address string) ([]StakingReward, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "delegatorRewards",
"user": address,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch staking rewards: %w", err)
}
var result []StakingReward
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal staking rewards: %w", err)
}
return result, nil
}
func (i *Info) QueryOrderByOid(
ctx context.Context,
user string,
oid int64,
) (*OrderQueryResult, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "orderStatus",
"user": user,
"oid": oid,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch order status: %w", err)
}
var result OrderQueryResult
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal order status: %w", err)
}
return &result, nil
}
func (i *Info) QueryOrderByCloid(
ctx context.Context,
user, cloid string,
) (*OrderQueryResult, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "orderStatus",
"user": user,
"oid": cloid,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch order status by cloid: %w", err)
}
var result OrderQueryResult
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal order status: %w", err)
}
return &result, nil
}
func (i *Info) QueryReferralState(ctx context.Context, user string) (*ReferralState, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "referral",
"user": user,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch referral state: %w", err)
}
var result ReferralState
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal referral state: %w", err)
}
return &result, nil
}
func (i *Info) QuerySubAccounts(ctx context.Context, user string) ([]SubAccount, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "subAccounts",
"user": user,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch sub accounts: %w", err)
}
var result []SubAccount
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal sub accounts: %w", err)
}
return result, nil
}
func (i *Info) QueryUserToMultiSigSigners(
ctx context.Context,
multiSigUser string,
) ([]MultiSigSigner, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "userToMultiSigSigners",
"user": multiSigUser,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch multi-sig signers: %w", err)
}
var result []MultiSigSigner
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal multi-sig signers: %w", err)
}
return result, nil
}
// PerpDexs returns the list of available perpetual dexes
// Returns an array where each element can be nil (for the default dex) or a PerpDex object
// The first element is always null (representing the default dex)
func (i *Info) PerpDexs(ctx context.Context) (MixedArray, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "perpDexs",
})
if err != nil {
return nil, fmt.Errorf("failed to fetch perp dexs: %w", err)
}
var result MixedArray
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal perp dexs: %w", err)
}
return result, nil
}
func (i *Info) TokenDetails(ctx context.Context, tokenId string) (*TokenDetail, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "tokenDetails",
"tokenId": tokenId,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch token detail: %w", err)
}
var tokenDetail TokenDetail
if err := json.Unmarshal(resp, &tokenDetail); err != nil {
return nil, fmt.Errorf("failed to unmarshal token detail response: %w", err)
}
return &tokenDetail, nil
}
// PerpDexLimits retrieves builder-deployed perp market limits
// dex must be a non-empty string (the empty string is not allowed for this endpoint)
func (i *Info) PerpDexLimits(ctx context.Context, dex string) (*PerpDexLimits, error) {
if dex == "" {
return nil, fmt.Errorf("dex parameter is required for perpDexLimits")
}
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "perpDexLimits",
"dex": dex,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch perp dex limits: %w", err)
}
var result PerpDexLimits
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal perp dex limits: %w", err)
}
return &result, nil
}
// PerpDexStatus retrieves perp market status
// If dex is empty string, returns status for the first perp dex (default)
func (i *Info) PerpDexStatus(ctx context.Context, dex string) (*PerpDexStatus, error) {
payload := map[string]any{
"type": "perpDexStatus",
}
if dex != "" {
payload["dex"] = dex
}
resp, err := i.client.post(ctx, "/info", payload)
if err != nil {
return nil, fmt.Errorf("failed to fetch perp dex status: %w", err)
}
var result PerpDexStatus
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal perp dex status: %w", err)
}
return &result, nil
}
// PerpDeployAuctionStatus retrieves information about the Perp Deploy Auction
func (i *Info) PerpDeployAuctionStatus(ctx context.Context) (*PerpDeployAuctionStatus, error) {
resp, err := i.client.post(ctx, "/info", map[string]any{
"type": "perpDeployAuctionStatus",
})
if err != nil {
return nil, fmt.Errorf("failed to fetch perp deploy auction status: %w", err)
}
var result PerpDeployAuctionStatus
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal perp deploy auction status: %w", err)
}
return &result, nil
}