use crate::orderbooks::delta::NormalizedDelta;
use serde::Deserialize;
#[derive(Deserialize, Debug, Clone)]
pub struct KrakenBookResponse {
pub channel: String,
#[serde(rename = "type")]
pub ty: String,
pub data: Vec<KrakenBookData>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct KrakenBookData {
pub symbol: String,
pub bids: Vec<KrakenPriceLevel>,
pub asks: Vec<KrakenPriceLevel>,
#[serde(default)]
pub checksum: u64,
pub timestamp: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct KrakenPriceLevel {
pub price: f64,
pub qty: f64,
}
impl KrakenBookResponse {
pub fn to_normalized(&self) -> Option<NormalizedDelta> {
let data = self.data.first()?;
let bids: Vec<(String, String)> = data
.bids
.iter()
.map(|l| (l.price.to_string(), l.qty.to_string()))
.collect();
let asks: Vec<(String, String)> = data
.asks
.iter()
.map(|l| (l.price.to_string(), l.qty.to_string()))
.collect();
Some(NormalizedDelta {
symbol: data.symbol.clone(),
bids,
asks,
update_id: data.checksum,
sequence: data.checksum,
is_snapshot: self.ty == "snapshot",
})
}
}