Skip to main content

df_hyperliquid_core/live/account/
events.rs

1//! `userEvents`, on the channel `user`: one kind of event a frame, a fill, a funding payment, a
2//! liquidation, the venue's cancels or a TWAP's progress. A kind this build does not read is kept
3//! as written.
4
5use std::fmt;
6
7use crate::account::TwapSliceFill;
8use crate::account::history::Fill;
9use crate::live::{Funding, TwapEvent};
10use crate::utils::{Address, Dec};
11use serde::{Deserialize, Deserializer, de};
12use serde_json::value::RawValue;
13
14/// `userEvents`, on the channel `user`: one kind of event a frame.
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum UserEvent {
18    /// `fills`: fills of the account's orders.
19    Fills(Vec<Fill>),
20    /// `funding`: a funding payment, on the hour.
21    Funding(Funding),
22    /// `liquidation`: a liquidation the account took part in.
23    Liquidation(Liquidation),
24    /// Orders the venue canceled, not the account.
25    NonUserCancel(Vec<Cancel>),
26    /// `twapHistory`: TWAPs starting, finishing, being stopped or failing.
27    TwapHistory(Vec<TwapEvent>),
28    /// `twapSliceFills`: fills of TWAP slices.
29    TwapSliceFills(Vec<TwapSliceFill>),
30    /// A kind this build does not read: its key, and its value as written.
31    Other {
32        /// The event's key as sent.
33        key: String,
34        /// The event's value, the JSON as written.
35        data: String,
36    },
37}
38
39impl<'de> Deserialize<'de> for UserEvent {
40    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
41        struct Event;
42
43        impl<'de> de::Visitor<'de> for Event {
44            type Value = UserEvent;
45
46            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47                f.write_str("an object with one event in it")
48            }
49
50            fn visit_map<A: de::MapAccess<'de>>(self, mut map: A) -> Result<UserEvent, A::Error> {
51                let key: String = map.next_key()?.ok_or_else(|| de::Error::custom("an event with nothing in it"))?;
52                let event = match key.as_str() {
53                    "fills" => UserEvent::Fills(map.next_value()?),
54                    "funding" => UserEvent::Funding(map.next_value()?),
55                    "liquidation" => UserEvent::Liquidation(map.next_value()?),
56                    "nonUserCancel" => UserEvent::NonUserCancel(map.next_value()?),
57                    "twapHistory" => UserEvent::TwapHistory(map.next_value()?),
58                    "twapSliceFills" => UserEvent::TwapSliceFills(map.next_value()?),
59                    _ => {
60                        let data: Box<RawValue> = map.next_value()?;
61                        UserEvent::Other { key, data: data.get().to_owned() }
62                    }
63                };
64                if map.next_key::<de::IgnoredAny>()?.is_some() {
65                    return Err(de::Error::custom("more than one event in a frame"));
66                }
67                Ok(event)
68            }
69        }
70
71        deserializer.deserialize_map(Event)
72    }
73}
74
75/// A liquidation the account took part in. The venue writes these keys in snake case.
76#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
77#[non_exhaustive]
78pub struct Liquidation {
79    /// The liquidation's id.
80    pub lid: u64,
81    /// The account that took over the liquidated position.
82    pub liquidator: Address,
83    /// The account that was liquidated.
84    pub liquidated_user: Address,
85    /// The notional of the liquidated position, USDC.
86    pub liquidated_ntl_pos: Dec,
87    /// The liquidated account's value at the liquidation, USDC.
88    pub liquidated_account_value: Dec,
89}
90
91/// An order the venue canceled.
92#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
93#[non_exhaustive]
94pub struct Cancel {
95    /// The coin the order was on.
96    pub coin: String,
97    /// The order's id.
98    pub oid: u64,
99}
100
101#[cfg(test)]
102#[path = "tests/events.rs"]
103mod tests;