use std::cmp::Ordering;
use crate::markets::{Book, Level};
use crate::utils::Dec;
use serde::Deserialize;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(from = "RawBbo")]
pub struct Bbo {
pub coin: String,
pub time: u64,
pub bid: Option<Level>,
pub ask: Option<Level>,
}
#[derive(Deserialize)]
struct RawBbo {
coin: String,
time: u64,
bbo: (Option<Level>, Option<Level>),
}
impl From<RawBbo> for Bbo {
fn from(raw: RawBbo) -> Self {
let (bid, ask) = raw.bbo;
Bbo { coin: raw.coin, time: raw.time, bid, ask }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Mids {
#[serde(default)]
pub dex: String,
pub mids: crate::markets::mids::Mids,
}
impl Mids {
#[must_use]
pub fn get(&self, coin: &str) -> Option<Dec> {
self.mids.get(coin)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub enum L2 {
#[serde(rename = "s")]
Snapshot(Book),
#[serde(rename = "u")]
Diff(L2Diff),
#[serde(rename = "c")]
Compressed(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub struct L2Level {
#[serde(rename = "p")]
pub px: Dec,
#[serde(rename = "s")]
pub sz: Dec,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(from = "RawL2Diff")]
pub struct L2Diff {
pub coin: String,
pub time: u64,
pub bids: Vec<L2Level>,
pub asks: Vec<L2Level>,
pub removed_bids: Vec<usize>,
pub removed_asks: Vec<usize>,
pub spread: Option<Dec>,
}
#[derive(Deserialize)]
struct RawL2Diff {
c: String,
t: u64,
l: (Vec<L2Level>, Vec<L2Level>),
r: (Vec<usize>, Vec<usize>),
#[serde(default)]
s: Option<Dec>,
}
impl From<RawL2Diff> for L2Diff {
fn from(raw: RawL2Diff) -> Self {
let ((bids, asks), (removed_bids, removed_asks)) = (raw.l, raw.r);
L2Diff { coin: raw.c, time: raw.t, bids, asks, removed_bids, removed_asks, spread: raw.s }
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum BadDiff {
#[error("a diff for {diff} applied to the book of {book}")]
Coin {
book: String,
diff: String,
},
#[error("a diff removes level {index} of a side that has {levels}: a frame was missed")]
Index {
index: usize,
levels: usize,
},
}
impl L2Diff {
pub fn apply(&self, book: &mut Book) -> Result<(), BadDiff> {
if book.coin != self.coin {
return Err(BadDiff::Coin { book: book.coin.clone(), diff: self.coin.clone() });
}
let bids = changed(&book.bids, &self.removed_bids, &self.bids, |a, b| b.cmp(a))?;
let asks = changed(&book.asks, &self.removed_asks, &self.asks, Ord::cmp)?;
book.bids = bids;
book.asks = asks;
book.time = self.time;
book.spread = self.spread;
Ok(())
}
}
fn changed(
side: &[Level],
removed: &[usize],
levels: &[L2Level],
best: fn(&Dec, &Dec) -> Ordering,
) -> Result<Vec<Level>, BadDiff> {
if let Some(&index) = removed.iter().find(|&&index| index >= side.len()) {
return Err(BadDiff::Index { index, levels: side.len() });
}
let mut kept: Vec<Level> =
side.iter().enumerate().filter(|(index, _)| !removed.contains(index)).map(|(_, level)| *level).collect();
for level in levels {
let now = Level { px: level.px, sz: level.sz, n: 0 };
match kept.binary_search_by(|resting| best(&resting.px, &level.px)) {
Ok(at) => kept[at] = now,
Err(at) => kept.insert(at, now),
}
}
Ok(kept)
}
#[cfg(test)]
#[path = "tests/market.rs"]
mod tests;