Skip to main content

df_hyperliquid_core/live/subscription/
wire.rs

1//! Writing a subscription as the object a subscribe or unsubscribe frame carries
2//! ([`Subscription::to_json`]), and as one line of JSON.
3
4use std::fmt;
5
6use crate::live::Subscription;
7use crate::utils::Json;
8
9impl Subscription {
10    /// The object a subscribe or unsubscribe frame carries: keys in the docs' order, optional ones
11    /// last and only when set, as the Python SDK sends them. The venue echoes left out options
12    /// filled in (`fast: false`, `dex: ""`), and [`Subscription::parse`] reads that echo back to
13    /// the same subscription.
14    #[must_use]
15    pub fn to_json(&self) -> Json {
16        let mut fields = vec![("type", text(self.kind()))];
17        match self {
18            Subscription::AllMids { dex } if !dex.is_empty() => fields.push(("dex", text(dex))),
19            Subscription::TwapStates { user, dex }
20            | Subscription::ClearinghouseState { user, dex }
21            | Subscription::OpenOrders { user, dex } => {
22                fields.push(("user", text(&user.to_hex())));
23                fields.push(("dex", text(dex)));
24            }
25            Subscription::Candle { coin, interval } => {
26                fields.push(("coin", text(coin)));
27                fields.push(("interval", text(interval.wire())));
28            }
29            Subscription::L2Book { coin, n_sig_figs, mantissa, fast } => {
30                fields.push(("coin", text(coin)));
31                if let Some(figures) = n_sig_figs {
32                    fields.push(("nSigFigs", Json::Number(figures.to_string())));
33                }
34                if let Some(mantissa) = mantissa {
35                    fields.push(("mantissa", Json::Number(mantissa.to_string())));
36                }
37                if *fast {
38                    fields.push(("fast", Json::Bool(true)));
39                }
40            }
41            Subscription::L2 { coin, n_sig_figs, mantissa } => {
42                fields.push(("c", text(coin)));
43                if let Some(figures) = n_sig_figs {
44                    fields.push(("s", Json::Number(figures.to_string())));
45                }
46                if let Some(mantissa) = mantissa {
47                    fields.push(("m", Json::Number(mantissa.to_string())));
48                }
49            }
50            Subscription::Trades { coin } | Subscription::ActiveAssetCtx { coin } | Subscription::Bbo { coin } => {
51                fields.push(("coin", text(coin)));
52            }
53            Subscription::UserFills { user, aggregate_by_time } => {
54                fields.push(("user", text(&user.to_hex())));
55                if *aggregate_by_time {
56                    fields.push(("aggregateByTime", Json::Bool(true)));
57                }
58            }
59            Subscription::ActiveAssetData { user, coin } => {
60                fields.push(("user", text(&user.to_hex())));
61                fields.push(("coin", text(coin)));
62            }
63            Subscription::SpotState { user, ignore_portfolio_margin } => {
64                fields.push(("user", text(&user.to_hex())));
65                if *ignore_portfolio_margin {
66                    fields.push(("ignorePortfolioMargin", Json::Bool(true)));
67                }
68            }
69            Subscription::AssetCtxs { dex } => fields.push(("dex", text(dex))),
70            Subscription::Notification { user }
71            | Subscription::WebData3 { user }
72            | Subscription::OrderUpdates { user }
73            | Subscription::UserEvents { user }
74            | Subscription::UserFundings { user }
75            | Subscription::UserNonFundingLedgerUpdates { user }
76            | Subscription::UserTwapSliceFills { user }
77            | Subscription::UserTwapHistory { user }
78            | Subscription::AllDexsClearinghouseState { user }
79            | Subscription::UserHistoricalOrders { user } => fields.push(("user", text(&user.to_hex()))),
80            Subscription::AllMids { .. }
81            | Subscription::AllDexsAssetCtxs
82            | Subscription::OutcomeMetaUpdates
83            | Subscription::FastAssetCtxs
84            | Subscription::SpotAssetCtxs
85            | Subscription::Pac
86            | Subscription::Sac => {}
87        }
88        Json::Object(fields.into_iter().map(|(key, value)| (key.to_owned(), value)).collect())
89    }
90}
91
92impl fmt::Display for Subscription {
93    /// The object as one line of JSON.
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        f.write_str(&self.to_json().compact())
96    }
97}
98
99fn text(value: &str) -> Json {
100    Json::String(value.to_owned())
101}
102
103#[cfg(test)]
104#[path = "tests/wire.rs"]
105mod tests;