Skip to main content

df_hyperliquid_core/live/
request.rs

1//! The frames sent: subscribe, unsubscribe, ping, and a post, which carries an info read or a
2//! signed action over the socket instead of over HTTP. Each is one text frame, as
3//! `JSON.stringify` would write it, keys in the docs' order.
4
5use crate::live::Subscription;
6use crate::utils::Json;
7use crate::utils::json::BadJson;
8
9/// The heartbeat. The venue answers `{"channel":"pong"}`.
10pub const PING: &str = r#"{"method":"ping"}"#;
11
12/// `{"method":"subscribe","subscription":{...}}`.
13#[must_use]
14pub fn subscribe(subscription: &Subscription) -> String {
15    wrap("subscribe", subscription)
16}
17
18/// `{"method":"unsubscribe","subscription":{...}}`, with the same object the subscribe sent:
19/// the venue matches the options too.
20#[must_use]
21pub fn unsubscribe(subscription: &Subscription) -> String {
22    wrap("unsubscribe", subscription)
23}
24
25fn wrap(method: &str, subscription: &Subscription) -> String {
26    Json::Object(vec![
27        ("method".to_owned(), Json::String(method.to_owned())),
28        ("subscription".to_owned(), subscription.to_json()),
29    ])
30    .compact()
31}
32
33/// What a post carries: an info read, as it would be posted to `/info`, or a signed action, the
34/// whole `/exchange` body. Either is kept as written, key order and digits, since an action's
35/// signature is over exactly what was built.
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum Post {
38    /// An info read, the body `/info` would take.
39    Info(Json),
40    /// A signed action, the body `/exchange` would take.
41    Action(Json),
42}
43
44impl Post {
45    /// An info read, the body `/info` would take: `{"type":"l2Book","coin":"ETH"}`.
46    pub fn info(request: &str) -> Result<Post, BadJson> {
47        Json::parse(request).map(Post::Info)
48    }
49
50    /// A signed action, the body `/exchange` would take: `{"action":..,"nonce":..,
51    /// "signature":..,"vaultAddress":..}`. Like `/exchange`, never sent twice: a post that
52    /// goes unanswered may still have landed.
53    pub fn action(body: &str) -> Result<Post, BadJson> {
54        Json::parse(body).map(Post::Action)
55    }
56
57    /// `info` or `action`, as the request's `type` names it.
58    #[must_use]
59    pub const fn kind(&self) -> &'static str {
60        match self {
61            Post::Info(_) => "info",
62            Post::Action(_) => "action",
63        }
64    }
65
66    /// The body it carries, as written.
67    #[must_use]
68    pub const fn payload(&self) -> &Json {
69        match self {
70            Post::Info(payload) | Post::Action(payload) => payload,
71        }
72    }
73}
74
75/// `{"method":"post","id":..,"request":{"type":"info"|"action","payload":..}}`. The answer comes
76/// on the channel `post` with the same `id`.
77#[must_use]
78pub fn post(id: u64, request: &Post) -> String {
79    Json::Object(vec![
80        ("method".to_owned(), Json::String("post".to_owned())),
81        ("id".to_owned(), Json::Number(id.to_string())),
82        (
83            "request".to_owned(),
84            Json::Object(vec![
85                ("type".to_owned(), Json::String(request.kind().to_owned())),
86                ("payload".to_owned(), request.payload().clone()),
87            ]),
88        ),
89    ])
90    .compact()
91}
92
93#[cfg(test)]
94#[path = "tests/request.rs"]
95mod tests;