bitbank_api/public/
transactions.rs1use super::*;
2
3#[derive(Deserialize, Debug)]
4struct Response {
5 transactions: Vec<Transaction>,
6}
7
8#[serde_as]
9#[derive(Deserialize, Debug)]
10pub struct Transaction {
11 pub transaction_id: u64,
12 #[serde_as(as = "DisplayFromStr")]
13 pub side: Side,
14 #[serde_as(as = "DisplayFromStr")]
15 pub price: f64,
16 #[serde_as(as = "DisplayFromStr")]
17 pub amount: f64,
18 #[serde_as(as = "TimestampMilliSeconds")]
19 pub executed_at: NaiveDateTime,
20}
21
22#[derive(derive_more::Display)]
23#[display(fmt = "{_0}{_1}{_2}")]
24pub struct Date(u16, u8, u8);
25
26#[derive(TypedBuilder)]
27pub struct Params {
28 pair: Pair,
29 #[builder(default, setter(strip_option))]
30 date: Option<Date>,
31}
32
33pub async fn get(params: Params) -> anyhow::Result<Vec<Transaction>> {
34 let pair = params.pair;
35 let date = params
36 .date
37 .map(|x| format!("/{x}"))
38 .unwrap_or("".to_owned());
39
40 let path = format!("/{pair}/transactions{date}");
41 let resp: Response = do_get(path).await?;
42 Ok(resp.transactions)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 #[tokio::test]
49 async fn test_transactions_no_date() -> anyhow::Result<()> {
50 let params = Params::builder().pair(Pair(XRP, JPY)).build();
51 let resp = get(params).await?;
52 assert!(resp.len() <= 60);
53 dbg!(&resp);
54 Ok(())
55 }
56 #[tokio::test]
57 async fn test_transactions() -> anyhow::Result<()> {
58 let params = Params::builder()
59 .pair(Pair(XRP, JPY))
60 .date(Date(2022, 12, 25))
61 .build();
62 let resp = get(params).await?;
63 dbg!(&resp);
64 Ok(())
65 }
66}