Skip to main content

rustrade_data/exchange/hyperliquid/
channel.rs

1use super::{Hyperliquid, HyperliquidSpot};
2use crate::{
3    Identifier,
4    subscription::{Subscription, book::OrderBooksL2, trade::PublicTrades},
5};
6use serde::Serialize;
7use serde_json::{Value, json};
8
9/// Hyperliquid WebSocket channel types.
10///
11/// Maps to the `"type"` field in the subscription payload:
12/// ```json
13/// {"method": "subscribe", "subscription": {"type": "trades", "coin": "BTC"}}
14/// ```
15#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize)]
16pub enum HyperliquidChannel {
17    /// Real-time trades stream.
18    Trades,
19    /// L2 order book snapshots.
20    L2Book,
21}
22
23impl HyperliquidChannel {
24    /// Build the subscription payload for this channel.
25    pub fn subscription_payload(&self, coin: &str) -> Value {
26        match self {
27            Self::Trades => json!({
28                "type": "trades",
29                "coin": coin
30            }),
31            Self::L2Book => json!({
32                "type": "l2Book",
33                "coin": coin
34            }),
35        }
36    }
37}
38
39impl<Instrument> Identifier<HyperliquidChannel>
40    for Subscription<Hyperliquid, Instrument, PublicTrades>
41{
42    fn id(&self) -> HyperliquidChannel {
43        HyperliquidChannel::Trades
44    }
45}
46
47impl<Instrument> Identifier<HyperliquidChannel>
48    for Subscription<Hyperliquid, Instrument, OrderBooksL2>
49{
50    fn id(&self) -> HyperliquidChannel {
51        HyperliquidChannel::L2Book
52    }
53}
54
55impl<Instrument> Identifier<HyperliquidChannel>
56    for Subscription<HyperliquidSpot, Instrument, PublicTrades>
57{
58    fn id(&self) -> HyperliquidChannel {
59        HyperliquidChannel::Trades
60    }
61}
62
63impl<Instrument> Identifier<HyperliquidChannel>
64    for Subscription<HyperliquidSpot, Instrument, OrderBooksL2>
65{
66    fn id(&self) -> HyperliquidChannel {
67        HyperliquidChannel::L2Book
68    }
69}
70
71impl AsRef<str> for HyperliquidChannel {
72    fn as_ref(&self) -> &str {
73        match self {
74            Self::Trades => "trades",
75            Self::L2Book => "l2Book",
76        }
77    }
78}