use crate::client::request::{body, dex, number, text};
use crate::client::{ApiError, Transport, info};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Precision {
figures: Option<u32>,
step: Option<u32>,
}
impl Precision {
pub const FULL: Precision = Precision { figures: None, step: None };
#[must_use]
pub const fn figures(figures: u32) -> Option<Precision> {
match figures {
2..=5 => Some(Precision { figures: Some(figures), step: None }),
_ => None,
}
}
#[must_use]
pub const fn step(self, step: u32) -> Option<Precision> {
match (self.figures, step) {
(Some(5), 1 | 2 | 5) => Some(Precision { figures: Some(5), step: Some(step) }),
_ => None,
}
}
}
pub use crate::markets::book::Level;
pub type L2Book = crate::markets::book::Book;
pub use crate::markets::candle::{Candle, Interval, UnknownInterval};
pub type MarketTrade = crate::markets::trades::Trade;
pub use crate::markets::mids::Mids;
#[must_use]
pub fn l2_book_request(coin: &str, precision: Precision) -> String {
let figures = precision.figures.map(|figures| ("nSigFigs", number(u64::from(figures))));
let step = precision.step.map(|step| ("mantissa", number(u64::from(step))));
body("l2Book", [Some(("coin", text(coin))), figures, step].into_iter().flatten())
}
#[must_use]
pub fn candle_snapshot_request(coin: &str, interval: Interval, start: u64, end: u64) -> String {
let req = crate::utils::Json::Object(vec![
("coin".to_owned(), text(coin)),
("interval".to_owned(), text(interval.wire())),
("startTime".to_owned(), number(start)),
("endTime".to_owned(), number(end)),
]);
body("candleSnapshot", [("req", req)])
}
#[must_use]
pub fn recent_trades_request(coin: &str) -> String {
body("recentTrades", [("coin", text(coin))])
}
#[must_use]
pub fn all_mids_request(on: Option<&str>) -> String {
body("allMids", dex(on))
}
pub async fn l2_book(transport: &impl Transport, coin: &str, precision: Precision) -> Result<L2Book, ApiError> {
info(transport, l2_book_request(coin, precision)).await
}
pub async fn candle_snapshot(
transport: &impl Transport,
coin: &str,
interval: Interval,
start: u64,
end: u64,
) -> Result<Vec<Candle>, ApiError> {
let candles: Option<Vec<Candle>> = info(transport, candle_snapshot_request(coin, interval, start, end)).await?;
Ok(candles.unwrap_or_default())
}
pub async fn recent_trades(transport: &impl Transport, coin: &str) -> Result<Vec<MarketTrade>, ApiError> {
info(transport, recent_trades_request(coin)).await
}
pub async fn all_mids(transport: &impl Transport, on: Option<&str>) -> Result<Mids, ApiError> {
info(transport, all_mids_request(on)).await
}
#[cfg(test)]
#[path = "tests/reads.rs"]
mod tests;