1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use super::*;

#[derive(Deserialize, Debug)]
struct RawResponse {
    asks: Vec<RawOrder>,
    bids: Vec<RawOrder>,
}

#[serde_as]
#[derive(Deserialize, Debug)]
pub(crate) struct RawOrder(
    #[serde_as(as = "DisplayFromStr")] f64,
    #[serde_as(as = "DisplayFromStr")] f64,
);

#[derive(Debug)]
pub struct Order {
    pub price: f64,
    pub amount: f64,
}
impl Order {
    pub(crate) fn new(x: RawOrder) -> Self {
        Self {
            price: x.0,
            amount: x.1,
        }
    }
}

#[derive(Debug)]
pub struct Depth {
    pub asks: Vec<Order>,
    pub bids: Vec<Order>,
}

#[derive(TypedBuilder)]
pub struct Params {
    pair: Pair,
}

pub async fn get(params: Params) -> anyhow::Result<Depth> {
    let path = format!("/{}/depth", params.pair);
    let resp: RawResponse = do_get(path).await?;
    Ok(Depth {
        asks: resp.asks.into_iter().map(Order::new).collect(),
        bids: resp.bids.into_iter().map(Order::new).collect(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_depth() -> anyhow::Result<()> {
        let params = Params::builder().pair(Pair(XRP, JPY)).build();
        let resp = get(params).await?;
        dbg!(&resp);
        Ok(())
    }
}