kraapi/api/private/
open_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};
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 Open Orders endpoint
17pub struct KIOpenOrders {
18    params: IndexMap<String, String>,
19}
20
21impl KIOpenOrders {
22    /// Constructor returning a [KrakenInput] builder for the get open orders endpoint.
23    pub fn build() -> Self {
24        KIOpenOrders {
25            params: IndexMap::new(),
26        }
27    }
28
29    // FIXME: AFter testing, trades=false still causes trade data to be returned. So the entire key
30    // value pair needs to be removed on false input
31    /// Should trades be included in returned output?
32    pub fn with_trade_info(self, include_trades: bool) -> Self {
33        if include_trades {
34            self.update_input("trades", include_trades.to_string())
35        } else {
36            self.update_input("trades", String::from(""))
37        }
38    }
39
40    /// Filter results to the given user ref id. 
41    /// A custom userref can be passed into the add order endpoint
42    pub fn with_userref(self, userref: u32) -> Self {
43        self.update_input("userref", userref.to_string())
44    }
45
46    fn with_nonce(self) -> Self {
47        self.update_input("nonce", KrakenAuth::nonce())
48    }
49}
50
51impl Input for KIOpenOrders {
52    fn finish(self) -> KrakenInput {
53        KrakenInput {
54            info: EndpointInfo {
55                methodtype: MethodType::Private,
56                endpoint: String::from("OpenOrders"),
57            },
58            params: Some(self.with_nonce().params),
59        }
60    }
61
62    fn finish_clone(self) -> (KrakenInput, Self) {
63        let newself = self.with_nonce();
64        (
65            KrakenInput {
66                info: EndpointInfo {
67                    methodtype: MethodType::Private,
68                    endpoint: String::from("OpenOrders"),
69                },
70                params: Some(newself.params.clone()),
71            },
72            newself,
73        )
74    }
75}
76
77impl MutateInput for KIOpenOrders {
78    fn list_mut(&mut self) -> &mut IndexMap<String, String> {
79        &mut self.params
80    }
81}
82
83impl UpdateInput for KIOpenOrders {}
84
85/// Response from the Get Open Orders endpoint
86#[derive(Deserialize, Serialize, Debug)]
87pub struct KOOpenOrders {
88    pub orders: HashMap<String, KOOrderInfo>,
89}
90
91impl Output for KOOpenOrders {}