bitbank_api/public/
depth.rs

1use super::*;
2
3#[derive(Deserialize, Debug)]
4struct RawResponse {
5    asks: Vec<RawLimitOrder>,
6    bids: Vec<RawLimitOrder>,
7}
8
9#[serde_as]
10#[derive(Deserialize, Debug)]
11pub(crate) struct RawLimitOrder(
12    #[serde_as(as = "DisplayFromStr")] f64,
13    #[serde_as(as = "DisplayFromStr")] f64,
14);
15
16#[derive(Debug)]
17pub struct LimitOrder {
18    pub price: f64,
19    pub amount: f64,
20}
21impl LimitOrder {
22    pub(crate) fn new(x: RawLimitOrder) -> Self {
23        Self {
24            price: x.0,
25            amount: x.1,
26        }
27    }
28}
29
30#[derive(Debug)]
31pub struct Depth {
32    pub asks: Vec<LimitOrder>,
33    pub bids: Vec<LimitOrder>,
34}
35
36#[derive(TypedBuilder)]
37pub struct Params {
38    pair: Pair,
39}
40
41pub async fn get(params: Params) -> anyhow::Result<Depth> {
42    let path = format!("/{}/depth", params.pair);
43    let resp: RawResponse = do_get(path).await?;
44    Ok(Depth {
45        asks: resp.asks.into_iter().map(LimitOrder::new).collect(),
46        bids: resp.bids.into_iter().map(LimitOrder::new).collect(),
47    })
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[tokio::test]
55    async fn test_depth() -> anyhow::Result<()> {
56        let params = Params::builder().pair(Pair(XRP, JPY)).build();
57        let resp = get(params).await?;
58        dbg!(&resp);
59        Ok(())
60    }
61}