Skip to main content

df_hyperliquid_core/markets/
context.rs

1//! Asset contexts: what a market is doing now. [`PerpCtx`] for a perp on any dex, [`SpotCtx`] for a
2//! spot pair or an outcome side, each the same shape from `/info` and from the socket.
3
4use serde::Deserialize;
5
6use crate::utils::dec::{Dec, Round};
7
8/// What a perp is doing now. The venue writes `midPx`, `premium` and `impactPxs` as null on a
9/// market with no book.
10#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct PerpCtx {
13    /// The mark price (`markPx`).
14    pub mark_px: Dec,
15    /// The mid price (`midPx`); `None` on a market with no book.
16    #[serde(default)]
17    pub mid_px: Option<Dec>,
18    /// The oracle price (`oraclePx`).
19    pub oracle_px: Dec,
20    /// The mark price a day ago (`prevDayPx`).
21    pub prev_day_px: Dec,
22    /// Hourly, as a fraction of the position's value.
23    pub funding: Dec,
24    /// In the base asset.
25    pub open_interest: Dec,
26    /// Quote volume over the last day.
27    pub day_ntl_vlm: Dec,
28    /// Base volume over the last day.
29    #[serde(default)]
30    pub day_base_vlm: Option<Dec>,
31    /// The premium over the oracle price that funding is computed from, as a fraction; `None` on a
32    /// market with no book.
33    #[serde(default)]
34    pub premium: Option<Dec>,
35    /// The impact bid and ask prices (`impactPxs`); `None` on a market with no book.
36    #[serde(default)]
37    pub impact_pxs: Option<Vec<Dec>>,
38}
39
40impl PerpCtx {
41    /// The price a market order is bounded from: the mid, else the mark.
42    #[must_use]
43    pub fn reference(&self) -> Dec {
44        self.mid_px.unwrap_or(self.mark_px)
45    }
46
47    /// The mark's change over the day in percent, to two places. `None` without a previous day's
48    /// price.
49    #[must_use]
50    pub fn day_change(&self) -> Option<Dec> {
51        let then = Some(self.prev_day_px).filter(|then| then.is_positive())?;
52        self.mark_px.checked_sub(then)?.checked_mul(Dec::from_int(100))?.checked_div(then, 2, Round::Nearest)
53    }
54
55    /// The open interest at the mark, in the quote.
56    #[must_use]
57    pub fn open_interest_value(&self) -> Option<Dec> {
58        self.open_interest.checked_mul(self.mark_px)
59    }
60}
61
62/// What a spot pair or an outcome side (`#<encoding>`) is doing now. An outcome side's open
63/// interest is its circulating supply.
64#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
65#[serde(rename_all = "camelCase")]
66pub struct SpotCtx {
67    /// The book name: `PURR/USDC`, `@107` or `#14730`.
68    pub coin: String,
69    /// The mark price (`markPx`).
70    pub mark_px: Dec,
71    /// The mid price (`midPx`); `None` on a market with no book.
72    #[serde(default)]
73    pub mid_px: Option<Dec>,
74    /// The mark price a day ago (`prevDayPx`).
75    pub prev_day_px: Dec,
76    /// Quote volume over the last day.
77    pub day_ntl_vlm: Dec,
78    /// Base volume over the last day.
79    #[serde(default)]
80    pub day_base_vlm: Option<Dec>,
81    /// The circulating supply (`circulatingSupply`), in the base token; an outcome side's open interest.
82    pub circulating_supply: Dec,
83    /// The total supply (`totalSupply`), in the base token.
84    #[serde(default)]
85    pub total_supply: Option<Dec>,
86}
87
88impl SpotCtx {
89    /// The price a market order is bounded from: the mid, else the mark.
90    #[must_use]
91    pub fn reference(&self) -> Dec {
92        self.mid_px.unwrap_or(self.mark_px)
93    }
94}
95
96#[cfg(test)]
97#[path = "tests/context.rs"]
98mod tests;