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
62
63
64
65
66
use super::*;

#[derive(Deserialize, Debug)]
struct Response {
    transactions: Vec<Transaction>,
}

#[serde_as]
#[derive(Deserialize, Debug)]
pub struct Transaction {
    pub transaction_id: u64,
    #[serde_as(as = "DisplayFromStr")]
    pub side: Side,
    #[serde_as(as = "DisplayFromStr")]
    pub price: f64,
    #[serde_as(as = "DisplayFromStr")]
    pub amount: f64,
    #[serde_as(as = "TimestampMilliSeconds")]
    pub executed_at: NaiveDateTime,
}

#[derive(derive_more::Display)]
#[display(fmt = "{_0}{_1}{_2}")]
pub struct Date(u16, u8, u8);

#[derive(TypedBuilder)]
pub struct Params {
    pair: Pair,
    #[builder(default, setter(strip_option))]
    date: Option<Date>,
}

pub async fn get(params: Params) -> anyhow::Result<Vec<Transaction>> {
    let pair = params.pair;
    let date = params
        .date
        .map(|x| format!("/{x}"))
        .unwrap_or("".to_owned());

    let path = format!("/{pair}/transactions{date}");
    let resp: Response = do_get(path).await?;
    Ok(resp.transactions)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[tokio::test]
    async fn test_transactions_no_date() -> anyhow::Result<()> {
        let params = Params::builder().pair(Pair(XRP, JPY)).build();
        let resp = get(params).await?;
        assert!(resp.len() <= 60);
        dbg!(&resp);
        Ok(())
    }
    #[tokio::test]
    async fn test_transactions() -> anyhow::Result<()> {
        let params = Params::builder()
            .pair(Pair(XRP, JPY))
            .date(Date(2022, 12, 25))
            .build();
        let resp = get(params).await?;
        dbg!(&resp);
        Ok(())
    }
}