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