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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use indexmap::map::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

use crate::auth::KrakenAuth;
// Structs/Enums
use super::{EndpointInfo, KAssetPair, KrakenInput, MethodType, OrderFlags, OrderType, TradeType};

// Traits
use super::{Input, MutateInput, Output, UpdateInput};

/// Amount of leverage to be used when placing an order
pub enum Leverage {
    /// 2x leverage
    Two,
    /// 3x leverage
    Three,
    /// 4x leverage
    Four,
    /// 5x leverage
    Five
}

impl Display for Leverage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Leverage::Two => write!(f, "2"),
            Leverage::Three => write!(f, "3"),
            Leverage::Four => write!(f, "4"),
            Leverage::Five => write!(f, "5"),
        }
    }
}

/// Request builder for the Add Standard Order endpoint
pub struct KIAddOrder {
    params: IndexMap<String, String>,
}

impl KIAddOrder {
    /// Constructor returning a KrakenInput builder for the add standard order endpoint
    ///
    /// * `pair` - asset pair for order
    /// * `tradetype` - [TradeType]
    /// * `ordertype` - [OrderType]
    /// * `volume` - order volume in lots
    pub fn build(
        pair: KAssetPair,
        tradetype: TradeType,
        ordertype: OrderType,
        volume: f64,
    ) -> Self {
        let new = KIAddOrder {
            params: IndexMap::new(),
        };

        new.with_pair(pair)
            .with_transaction_type(tradetype)
            .with_order_type_ref(&ordertype)
            .with_price1(&ordertype)
            .with_price2(&ordertype)
            .with_volume(volume)
    }

    /// Update the asset pair for this order. Useful for templating
    pub fn with_pair(self, pair: KAssetPair) -> Self {
        self.update_input("pair", pair.to_string())
    }

    /// Update the transaction/trade type for this order. Useful for templating
    pub fn with_transaction_type(self, tradetype: TradeType) -> Self {
        self.update_input("type", tradetype.to_string())
    }

    /// Update the order type for this order. Useful for templating. Note that OrderType encodes
    /// the desired price (limit price, stop loss trigger price etc.)
    pub fn with_order_type(self, ordertype: OrderType) -> Self {
        self.update_input("ordertype", ordertype.to_string())
    }

    fn with_order_type_ref(self, ordertype: &OrderType) -> Self {
        self.update_input("ordertype", ordertype.to_string())
    }

    fn with_price1(self, ordertype: &OrderType) -> Self {
        match ordertype.price1() {
            Some(price) => self.update_input("price", price),
            None => self,
        }
    }

    fn with_price2(self, ordertype: &OrderType) -> Self {
        match ordertype.price2() {
            Some(price) => self.update_input("price2", price),
            None => self,
        }
    }

    /// Update the order volume in lots
    pub fn with_volume(self, volume: f64) -> Self {
        self.update_input("volume", volume.to_string())
    }

    /// Amount of leverage for this order. Subject to [margin trading
    /// restrictions](https://support.kraken.com/hc/en-us/articles/227876608)
    pub fn with_leverage(self, leverage: Leverage) -> Self {
        self.update_input("leverage", format!("{}:{}", leverage.to_string(), 1u8))
    }

    /// Order flags to set. Accepts any iterable collection of [OrderFlags]
    pub fn with_order_flags<T>(mut self, flags: T) -> Self
    where
        T: IntoIterator<Item = OrderFlags>,
    {
        let listname = String::from("oflags");
        match self.params.contains_key(&listname) {
            true => {
                flags.into_iter().for_each(|flag| self.format_flag(flag));
                self
            }
            false => {
                let mut iter = flags.into_iter();
                match iter.next() {
                    Some(val) => {
                        self.params.insert(listname, val.to_string());
                        self.with_order_flags(iter)
                    }
                    None => self,
                }
            }
        }
    }

    /// Scedule the order start time for `secs` seconds from now
    pub fn start_in(self, secs: u32) -> Self {
        self.update_input("starttm", String::from("%2B") + &secs.to_string())
    }

    /// Scedule the order start time for the Unix `timestamp` in seconds
    pub fn start_at(self, timestamp: u64) -> Self {
        self.update_input("starttm", timestamp.to_string())
    }

    /// Order to expire in `secs` seconds
    pub fn expire_in(self, secs: u32) -> Self {
        self.update_input("expiretm", secs.to_string())
    }

    /// Order to expire at the Unix `timestamp` in seconds
    pub fn expire_at(self, timestamp: u64) -> Self {
        self.update_input("expiretm", timestamp.to_string())
    }

    /// User supplied unsigned 32 bit integer which Kraken will use to demarcate this order for
    /// future reference
    pub fn with_userref(self, userref: u32) -> Self {
        self.update_input("userref", userref.to_string())
    }

    /// Validate inputs on Kraken's servers. Don't submit order
    pub fn validate(self, validate: bool) -> Self {
        self.update_input("validate", validate.to_string())
    }

    /// Closing order to add to the system when this order gets filled
    pub fn with_closing_order(self, ordertype: OrderType) -> Self {
        let price1 = ordertype.price1();
        let price2 = ordertype.price2();
        match (price1, price2) {
            (Some(price1), Some(price2)) => self
                .update_input("close%5Bordertype%5D", ordertype.to_string())
                .update_input("close%5Bprice%5D", price1)
                .update_input("close%5Bprice2%5D", price2),
            (Some(price1), None) => self
                .update_input("close%5Bordertype%5D", ordertype.to_string())
                .update_input("close%5Bprice%5D", price1),
            (None, Some(_)) => {
                unreachable!()
            }
            (None, None) => self,
        }
    }

    fn with_nonce(self) -> Self {
        self.update_input("nonce", KrakenAuth::nonce())
    }

    fn format_flag(&mut self, flag: OrderFlags) {
        let listname = String::from("oflags");
        match self.params.get_mut(&listname) {
            Some(list) => {
                // Silently disallow adding the same input to the list multiple times
                if list.contains(&flag.to_string()) {
                    return;
                }

                *list = format!("{},{}", list, flag.to_string());
            }
            None => {
                self.list_mut().insert(listname, flag.to_string());
            }
        }
    }
}
impl MutateInput for KIAddOrder {
    fn list_mut(&mut self) -> &mut IndexMap<String, String> {
        &mut self.params
    }
}

impl UpdateInput for KIAddOrder {}

impl Input for KIAddOrder {
    fn finish(self) -> KrakenInput {
        KrakenInput {
            info: EndpointInfo {
                methodtype: MethodType::Private,
                endpoint: String::from("AddOrder"),
            },
            params: Some(self.with_nonce().params),
        }
    }

    fn finish_clone(self) -> (KrakenInput, Self) {
        let newself = self.with_nonce();
        (
            KrakenInput {
                info: EndpointInfo {
                    methodtype: MethodType::Private,
                    endpoint: String::from("AddOrder"),
                },
                params: Some(newself.params.clone()),
            },
            newself,
        )
    }
}

/// Response from the Add Standard Order endpoint
#[derive(Deserialize, Serialize, Debug)]
pub struct KOAddOrder {
    /// Order description info
    pub descr: AddOrderDesc,
    /// Array of transaction ids for order (if order was added successfully)
    pub txid: Option<Vec<String>>,
}

impl Output for KOAddOrder {}

/// Textual description of placed order and optional close order
#[derive(Deserialize, Serialize, Debug)]
pub struct AddOrderDesc {
    /// Order description
    pub order: String,
    /// Conditional close order description (if order was added successfully)
    pub close: Option<String>,
}