kraapi/api/private/
closed_orders.rs

1use indexmap::map::IndexMap;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::auth::KrakenAuth;
6// Structs/Enums
7use super::{EndpointInfo, KrakenInput, MethodType, OrderCloseTime};
8
9// Traits
10use super::{Input, MutateInput, Output, UpdateInput};
11
12pub use super::KOOrderDescription;
13pub use super::KOOrderInfo;
14pub use super::KOOrderStatus;
15
16/// Request builder for the Get Closed Orders endpoint
17pub struct KIClosedOrders {
18    params: IndexMap<String, String>,
19}
20
21impl KIClosedOrders {
22    /// Constructor returning a [KrakenInput] builder for the get closed orders endpoint.
23    pub fn build() -> Self {
24        KIClosedOrders {
25            params: IndexMap::new(),
26        }
27    }
28
29    /// Should trades be included in returned output?
30    pub fn with_trade_info(self, include_trades: bool) -> Self {
31        if include_trades {
32            self.update_input("trades", include_trades.to_string())
33        } else {
34            self.update_input("trades", String::from(""))
35        }
36    }
37
38    /// Filter results to the given user ref id. 
39    /// A custom userref can be passed into the add order endpoint
40    pub fn with_userref(self, userref: u32) -> Self {
41        self.update_input("userref", userref.to_string())
42    }
43
44    /// Starting Unix timestamp to filter output by. Exclusive
45    pub fn starting_timestamp(self, timestamp: u64) -> Self {
46        self.update_input("start", timestamp.to_string())
47    }
48
49    /// Ending Unix timestamp to filter output by. Inclusive
50    pub fn ending_timestamp(self, timestamp: u64) -> Self {
51        self.update_input("end", timestamp.to_string())
52    }
53
54    /// Starting transaction ID to filter output by. Exclusive
55    pub fn starting_txid(self, txid: String) -> Self {
56        self.update_input("start", txid)
57    }
58
59    /// Ending transaction ID to filter output by. Inclusive
60    pub fn ending_txid(self, txid: String) -> Self {
61        self.update_input("end", txid)
62    }
63
64    /// Result offset. Not clear what this does
65    pub fn with_offset(self, offset: u64) -> Self {
66        self.update_input("ofs", offset.to_string())
67    }
68
69    /// Query orders by open time, close time, or both
70    pub fn with_closetime(self, closetime: OrderCloseTime) -> Self {
71        self.update_input("closetime", closetime.to_string())
72    }
73
74    fn with_nonce(self) -> Self {
75        self.update_input("nonce", KrakenAuth::nonce())
76    }
77}
78
79impl Input for KIClosedOrders {
80    fn finish(self) -> KrakenInput {
81        KrakenInput {
82            info: EndpointInfo {
83                methodtype: MethodType::Private,
84                endpoint: String::from("ClosedOrders"),
85            },
86            params: Some(self.with_nonce().params),
87        }
88    }
89
90    fn finish_clone(self) -> (KrakenInput, Self) {
91        let newself = self.with_nonce();
92        (
93            KrakenInput {
94                info: EndpointInfo {
95                    methodtype: MethodType::Private,
96                    endpoint: String::from("ClosedOrders"),
97                },
98                params: Some(newself.params.clone()),
99            },
100            newself,
101        )
102    }
103}
104
105impl MutateInput for KIClosedOrders {
106    fn list_mut(&mut self) -> &mut IndexMap<String, String> {
107        &mut self.params
108    }
109}
110
111impl UpdateInput for KIClosedOrders {}
112
113/// Response from the Get Closed Orders endpoint
114#[derive(Deserialize, Serialize, Debug)]
115pub struct KOClosedOrders {
116    pub closed: HashMap<String, KOOrderInfo>,
117    pub count: u32,
118}
119
120impl Output for KOClosedOrders {}