hyperliquid-sdk-rs 0.1.2

High-performance Rust SDK for Hyperliquid Protocol
Documentation
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
//! WebSocket message types for Hyperliquid

use std::collections::HashMap;

use alloy::primitives::Address;
use serde::{Deserialize, Serialize};

// Subscription types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum Subscription {
    AllMids,
    Notification { user: Address },
    WebData2 { user: Address },
    Candle { coin: String, interval: String },
    L2Book { coin: String },
    Trades { coin: String },
    OrderUpdates { user: Address },
    UserEvents { user: Address },
    UserFills { user: Address },
    UserFundings { user: Address },
    UserNonFundingLedgerUpdates { user: Address },
    // Phase 1 new subscriptions
    Bbo { coin: String },
    OpenOrders { user: Address },
    ClearinghouseState { user: Address },
    // Phase 2 new subscriptions
    WebData3 { user: Address },
    TwapStates { user: Address },
    ActiveAssetCtx { coin: String },
    ActiveAssetData { user: Address, coin: String },
    UserTwapSliceFills { user: Address },
    UserTwapHistory { user: Address },
}

// Incoming message types
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "channel", rename_all = "camelCase")]
pub enum Message {
    AllMids(AllMids),
    Trades(Trades),
    L2Book(L2Book),
    Candle(Candle),
    OrderUpdates(OrderUpdates),
    UserFills(UserFills),
    UserFundings(UserFundings),
    UserNonFundingLedgerUpdates(UserNonFundingLedgerUpdates),
    Notification(Notification),
    WebData2(WebData2),
    User(User),
    SubscriptionResponse,
    Pong,
    // Phase 1 new message types
    Bbo(Bbo),
    OpenOrders(OpenOrdersWs),
    ClearinghouseState(ClearinghouseStateWs),
    // Phase 2 new message types
    WebData3(WebData3Ws),
    TwapStates(TwapStatesWs),
    ActiveAssetCtx(ActiveAssetCtxWs),
    ActiveAssetData(ActiveAssetDataWs),
    UserTwapSliceFills(UserTwapSliceFillsWs),
    UserTwapHistory(UserTwapHistoryWs),
}

// Market data structures
#[derive(Debug, Clone, Deserialize)]
pub struct AllMids {
    pub data: AllMidsData,
}

#[derive(Debug, Clone, Deserialize)]
pub struct AllMidsData {
    pub mids: HashMap<String, String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Trades {
    pub data: Vec<Trade>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Trade {
    pub coin: String,
    pub side: String,
    pub px: String,
    pub sz: String,
    pub time: u64,
    pub hash: String,
    pub tid: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct L2Book {
    pub data: L2BookData,
}

#[derive(Debug, Clone, Deserialize)]
pub struct L2BookData {
    pub coin: String,
    pub time: u64,
    pub levels: Vec<Vec<BookLevel>>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct BookLevel {
    pub px: String,
    pub sz: String,
    pub n: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Candle {
    pub data: CandleData,
}

#[derive(Debug, Clone, Deserialize)]
pub struct CandleData {
    #[serde(rename = "T")]
    pub time_close: u64,
    #[serde(rename = "c")]
    pub close: String,
    #[serde(rename = "h")]
    pub high: String,
    #[serde(rename = "i")]
    pub interval: String,
    #[serde(rename = "l")]
    pub low: String,
    #[serde(rename = "n")]
    pub num_trades: u64,
    #[serde(rename = "o")]
    pub open: String,
    #[serde(rename = "s")]
    pub coin: String,
    #[serde(rename = "t")]
    pub time_open: u64,
    #[serde(rename = "v")]
    pub volume: String,
}

// User event structures
#[derive(Debug, Clone, Deserialize)]
pub struct OrderUpdates {
    pub data: Vec<OrderUpdate>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderUpdate {
    pub order: BasicOrder,
    pub status: String,
    pub status_timestamp: u64,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BasicOrder {
    pub coin: String,
    pub side: String,
    pub limit_px: String,
    pub sz: String,
    pub oid: u64,
    pub timestamp: u64,
    pub orig_sz: String,
    pub cloid: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct UserFills {
    pub data: UserFillsData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserFillsData {
    pub is_snapshot: Option<bool>,
    pub user: Address,
    pub fills: Vec<TradeInfo>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TradeInfo {
    pub coin: String,
    pub side: String,
    pub px: String,
    pub sz: String,
    pub time: u64,
    pub hash: String,
    pub start_position: String,
    pub dir: String,
    pub closed_pnl: String,
    pub oid: u64,
    pub cloid: Option<String>,
    pub crossed: bool,
    pub fee: String,
    pub fee_token: String,
    pub tid: u64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct UserFundings {
    pub data: UserFundingsData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserFundingsData {
    pub is_snapshot: Option<bool>,
    pub user: Address,
    pub fundings: Vec<UserFunding>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserFunding {
    pub time: u64,
    pub coin: String,
    pub usdc: String,
    pub szi: String,
    pub funding_rate: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct UserNonFundingLedgerUpdates {
    pub data: UserNonFundingLedgerUpdatesData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserNonFundingLedgerUpdatesData {
    pub is_snapshot: Option<bool>,
    pub user: Address,
    pub non_funding_ledger_updates: Vec<LedgerUpdateData>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct LedgerUpdateData {
    pub time: u64,
    pub hash: String,
    pub delta: LedgerUpdate,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
pub enum LedgerUpdate {
    Deposit {
        usdc: String,
    },
    Withdraw {
        usdc: String,
        nonce: u64,
        fee: String,
    },
    InternalTransfer {
        usdc: String,
        user: Address,
        destination: Address,
        fee: String,
    },
    SubAccountTransfer {
        usdc: String,
        user: Address,
        destination: Address,
    },
    SpotTransfer {
        token: String,
        amount: String,
        user: Address,
        destination: Address,
        fee: String,
    },
}

#[derive(Debug, Clone, Deserialize)]
pub struct Notification {
    pub data: NotificationData,
}

#[derive(Debug, Clone, Deserialize)]
pub struct NotificationData {
    pub notification: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct WebData2 {
    pub data: WebData2Data,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebData2Data {
    pub user: Address,
}

#[derive(Debug, Clone, Deserialize)]
pub struct User {
    pub data: UserData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum UserData {
    Fills(Vec<TradeInfo>),
    Funding(UserFunding),
}

// WebSocket protocol messages
#[derive(Debug, Serialize)]
pub struct WsRequest {
    pub method: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscription: Option<Subscription>,
}

impl WsRequest {
    pub fn subscribe(subscription: Subscription) -> Self {
        Self {
            method: "subscribe",
            subscription: Some(subscription),
        }
    }

    pub fn unsubscribe(subscription: Subscription) -> Self {
        Self {
            method: "unsubscribe",
            subscription: Some(subscription),
        }
    }

    pub fn ping() -> Self {
        Self {
            method: "ping",
            subscription: None,
        }
    }
}

// ==================== Phase 1 New Message Types ====================

/// Best bid/offer update
#[derive(Debug, Clone, Deserialize)]
pub struct Bbo {
    pub data: BboData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BboData {
    pub coin: String,
    pub time: u64,
    pub bbo: BboLevel,
}

#[derive(Debug, Clone, Deserialize)]
pub struct BboLevel {
    pub bid: PriceLevel,
    pub ask: PriceLevel,
}

#[derive(Debug, Clone, Deserialize)]
pub struct PriceLevel {
    pub px: String,
    pub sz: String,
}

/// Real-time open orders
#[derive(Debug, Clone, Deserialize)]
pub struct OpenOrdersWs {
    pub data: OpenOrdersWsData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenOrdersWsData {
    pub user: Address,
    pub is_snapshot: Option<bool>,
    pub orders: Vec<BasicOrder>,
}

/// Real-time clearinghouse state
#[derive(Debug, Clone, Deserialize)]
pub struct ClearinghouseStateWs {
    pub data: ClearinghouseStateWsData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClearinghouseStateWsData {
    pub user: Address,
    pub margin_summary: MarginSummaryWs,
    pub cross_margin_summary: MarginSummaryWs,
    pub withdrawable: String,
    pub asset_positions: Vec<AssetPositionWs>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginSummaryWs {
    pub account_value: String,
    pub total_margin_used: String,
    pub total_ntl_pos: String,
    pub total_raw_usd: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetPositionWs {
    pub position: PositionWs,
    #[serde(rename = "type")]
    pub type_string: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PositionWs {
    pub coin: String,
    pub entry_px: Option<String>,
    pub liquidation_px: Option<String>,
    pub margin_used: String,
    pub position_value: String,
    pub return_on_equity: String,
    pub szi: String,
    pub unrealized_pnl: String,
}

// ==================== Phase 2 New Message Types ====================

/// WebData3 - Aggregate user information (newer version)
#[derive(Debug, Clone, Deserialize)]
pub struct WebData3Ws {
    pub data: WebData3Data,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebData3Data {
    pub user: Address,
    #[serde(default)]
    pub is_snapshot: Option<bool>,
    #[serde(default)]
    pub clearinghouse_state: Option<ClearinghouseStateWsData>,
    #[serde(default)]
    pub open_orders: Option<Vec<BasicOrder>>,
    #[serde(default)]
    pub fills: Option<Vec<TradeInfo>>,
    #[serde(default)]
    pub fundings: Option<Vec<UserFunding>>,
}

/// TWAP order states
#[derive(Debug, Clone, Deserialize)]
pub struct TwapStatesWs {
    pub data: TwapStatesData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TwapStatesData {
    pub user: Address,
    #[serde(default)]
    pub is_snapshot: Option<bool>,
    pub twap_states: Vec<TwapState>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TwapState {
    pub twap_id: u64,
    pub coin: String,
    pub side: String,
    pub sz: String,
    pub sz_filled: String,
    pub duration_minutes: u32,
    pub start_time: u64,
    pub end_time: u64,
    pub status: String,
    #[serde(default)]
    pub randomize: Option<bool>,
}

/// Active asset context
#[derive(Debug, Clone, Deserialize)]
pub struct ActiveAssetCtxWs {
    pub data: ActiveAssetCtxData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActiveAssetCtxData {
    pub coin: String,
    pub ctx: AssetCtx,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetCtx {
    pub funding: String,
    pub open_interest: String,
    pub prev_day_px: String,
    pub day_ntl_vlm: String,
    pub premium: Option<String>,
    pub oracle_px: String,
    pub mark_px: String,
    pub mid_px: Option<String>,
    pub impact_pxs: Option<Vec<String>>,
}

/// Active asset data (perps only)
#[derive(Debug, Clone, Deserialize)]
pub struct ActiveAssetDataWs {
    pub data: ActiveAssetDataData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActiveAssetDataData {
    pub user: Address,
    pub coin: String,
    pub leverage: LeverageWs,
    #[serde(default)]
    pub max_trade_szs: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LeverageWs {
    #[serde(rename = "type")]
    pub leverage_type: String,
    pub value: u32,
    #[serde(default)]
    pub raw_usd: Option<String>,
}

/// User TWAP slice fills
#[derive(Debug, Clone, Deserialize)]
pub struct UserTwapSliceFillsWs {
    pub data: UserTwapSliceFillsData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserTwapSliceFillsData {
    pub user: Address,
    #[serde(default)]
    pub is_snapshot: Option<bool>,
    pub twap_slice_fills: Vec<TwapSliceFill>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TwapSliceFill {
    pub twap_id: u64,
    pub coin: String,
    pub side: String,
    pub px: String,
    pub sz: String,
    pub time: u64,
    pub fee: String,
    pub oid: u64,
    pub hash: String,
}

/// User TWAP history
#[derive(Debug, Clone, Deserialize)]
pub struct UserTwapHistoryWs {
    pub data: UserTwapHistoryData,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserTwapHistoryData {
    pub user: Address,
    #[serde(default)]
    pub is_snapshot: Option<bool>,
    pub twap_history: Vec<TwapHistoryEntry>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TwapHistoryEntry {
    pub twap_id: u64,
    pub coin: String,
    pub side: String,
    pub sz: String,
    pub sz_filled: String,
    pub avg_px: Option<String>,
    pub duration_minutes: u32,
    pub start_time: u64,
    pub end_time: u64,
    pub status: String,
    #[serde(default)]
    pub randomize: Option<bool>,
}