df_hyperliquid_core/live/
market.rs1use std::cmp::Ordering;
7
8use crate::markets::{Book, Level};
9use crate::utils::Dec;
10use serde::Deserialize;
11
12#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
15#[serde(from = "RawBbo")]
16#[non_exhaustive]
17pub struct Bbo {
18 pub coin: String,
20 pub time: u64,
22 pub bid: Option<Level>,
24 pub ask: Option<Level>,
26}
27
28#[derive(Deserialize)]
29struct RawBbo {
30 coin: String,
31 time: u64,
32 bbo: (Option<Level>, Option<Level>),
33}
34
35impl From<RawBbo> for Bbo {
36 fn from(raw: RawBbo) -> Self {
37 let (bid, ask) = raw.bbo;
38 Bbo { coin: raw.coin, time: raw.time, bid, ask }
39 }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
45#[non_exhaustive]
46pub struct Mids {
47 #[serde(default)]
49 pub dex: String,
50 pub mids: crate::markets::mids::Mids,
52}
53
54impl Mids {
55 #[must_use]
57 pub fn get(&self, coin: &str) -> Option<Dec> {
58 self.mids.get(coin)
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
66#[non_exhaustive]
67pub enum L2 {
68 #[serde(rename = "s")]
70 Snapshot(Book),
71 #[serde(rename = "u")]
73 Diff(L2Diff),
74 #[serde(rename = "c")]
76 Compressed(String),
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
82pub struct L2Level {
83 #[serde(rename = "p")]
85 pub px: Dec,
86 #[serde(rename = "s")]
88 pub sz: Dec,
89}
90
91#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
95#[serde(from = "RawL2Diff")]
96#[non_exhaustive]
97pub struct L2Diff {
98 pub coin: String,
100 pub time: u64,
102 pub bids: Vec<L2Level>,
104 pub asks: Vec<L2Level>,
106 pub removed_bids: Vec<usize>,
108 pub removed_asks: Vec<usize>,
110 pub spread: Option<Dec>,
112}
113
114#[derive(Deserialize)]
116struct RawL2Diff {
117 c: String,
118 t: u64,
119 l: (Vec<L2Level>, Vec<L2Level>),
120 r: (Vec<usize>, Vec<usize>),
121 #[serde(default)]
122 s: Option<Dec>,
123}
124
125impl From<RawL2Diff> for L2Diff {
126 fn from(raw: RawL2Diff) -> Self {
127 let ((bids, asks), (removed_bids, removed_asks)) = (raw.l, raw.r);
128 L2Diff { coin: raw.c, time: raw.t, bids, asks, removed_bids, removed_asks, spread: raw.s }
129 }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
134#[non_exhaustive]
135pub enum BadDiff {
136 #[error("a diff for {diff} applied to the book of {book}")]
138 Coin {
139 book: String,
141 diff: String,
143 },
144 #[error("a diff removes level {index} of a side that has {levels}: a frame was missed")]
146 Index {
147 index: usize,
149 levels: usize,
151 },
152}
153
154impl L2Diff {
155 pub fn apply(&self, book: &mut Book) -> Result<(), BadDiff> {
161 if book.coin != self.coin {
162 return Err(BadDiff::Coin { book: book.coin.clone(), diff: self.coin.clone() });
163 }
164 let bids = changed(&book.bids, &self.removed_bids, &self.bids, |a, b| b.cmp(a))?;
165 let asks = changed(&book.asks, &self.removed_asks, &self.asks, Ord::cmp)?;
166 book.bids = bids;
167 book.asks = asks;
168 book.time = self.time;
169 book.spread = self.spread;
170 Ok(())
171 }
172}
173
174fn changed(
176 side: &[Level],
177 removed: &[usize],
178 levels: &[L2Level],
179 best: fn(&Dec, &Dec) -> Ordering,
180) -> Result<Vec<Level>, BadDiff> {
181 if let Some(&index) = removed.iter().find(|&&index| index >= side.len()) {
182 return Err(BadDiff::Index { index, levels: side.len() });
183 }
184 let mut kept: Vec<Level> =
185 side.iter().enumerate().filter(|(index, _)| !removed.contains(index)).map(|(_, level)| *level).collect();
186 for level in levels {
187 let now = Level { px: level.px, sz: level.sz, n: 0 };
188 match kept.binary_search_by(|resting| best(&resting.px, &level.px)) {
189 Ok(at) => kept[at] = now,
190 Err(at) => kept.insert(at, now),
191 }
192 }
193 Ok(kept)
194}
195
196#[cfg(test)]
197#[path = "tests/market.rs"]
198mod tests;