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
use self::spot::SideEffect;

use super::{MarginOp, Rest, RestEndpoint, RestError};
use serde::Serialize;

/// Usd-Margin futures.
pub mod usd_margin_futures;

/// Spot.
pub mod spot;

/// Place order.
#[derive(Debug, Clone)]
pub struct PlaceOrder {
    pub(crate) inner: exc_core::types::PlaceOrder,
}

impl PlaceOrder {
    fn dispatch(&self, endpoint: &RestEndpoint) -> Result<PlaceOrderKind, RestError> {
        match endpoint {
            RestEndpoint::UsdMarginFutures => Ok(PlaceOrderKind::UsdMarginFutures(
                usd_margin_futures::PlaceOrder::try_from(&self.inner)?,
            )),
            RestEndpoint::Spot(options) => {
                let mut req = spot::PlaceOrder::try_from(&self.inner)?;
                if let Some(margin) = options.margin.as_ref() {
                    let margin = if self.inner.place.size.is_sign_positive() {
                        margin.buy.as_ref()
                    } else {
                        margin.sell.as_ref()
                    };
                    match margin {
                        Some(MarginOp::Loan) => {
                            req.side_effect_type = Some(SideEffect::MarginBuy);
                        }
                        Some(MarginOp::Repay) => {
                            req.side_effect_type = Some(SideEffect::AutoRepay);
                        }
                        None => {
                            req.side_effect_type = None;
                        }
                    }
                }
                Ok(PlaceOrderKind::Spot(req))
            }
        }
    }
}

/// Place order kind.
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum PlaceOrderKind {
    /// Usd-Margin futures.
    UsdMarginFutures(usd_margin_futures::PlaceOrder),
    /// Spot.
    Spot(spot::PlaceOrder),
}

impl Rest for PlaceOrder {
    fn method(&self, _endpoint: &RestEndpoint) -> Result<http::Method, RestError> {
        Ok(http::Method::POST)
    }

    fn to_path(&self, endpoint: &RestEndpoint) -> Result<String, RestError> {
        match endpoint {
            RestEndpoint::UsdMarginFutures => Ok("/fapi/v1/order".to_string()),
            RestEndpoint::Spot(options) => {
                if options.margin.is_some() {
                    Ok("/sapi/v1/margin/order".to_string())
                } else {
                    Ok("/api/v3/order".to_string())
                }
            }
        }
    }

    fn need_apikey(&self) -> bool {
        true
    }

    fn need_sign(&self) -> bool {
        true
    }

    fn serialize(&self, endpoint: &RestEndpoint) -> Result<serde_json::Value, RestError> {
        Ok(serde_json::to_value(self.dispatch(endpoint)?)?)
    }

    fn to_payload(&self) -> super::Payload {
        super::Payload::new(self.clone())
    }
}

/// Get order inner.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetOrderInner {
    /// Symbol.
    pub symbol: String,
    /// Order Id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_id: Option<i64>,
    /// Client Id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub orig_client_order_id: Option<String>,
}

/// Cancel order.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CancelOrder {
    /// Inner.
    #[serde(flatten)]
    pub inner: GetOrderInner,
}

impl Rest for CancelOrder {
    fn method(&self, _endpoint: &RestEndpoint) -> Result<http::Method, RestError> {
        Ok(http::Method::DELETE)
    }

    fn to_path(&self, endpoint: &RestEndpoint) -> Result<String, RestError> {
        match endpoint {
            RestEndpoint::UsdMarginFutures => Ok("/fapi/v1/order".to_string()),
            RestEndpoint::Spot(options) => {
                if options.margin.is_some() {
                    Ok("/sapi/v1/margin/order".to_string())
                } else {
                    Ok("/api/v3/order".to_string())
                }
            }
        }
    }

    fn need_apikey(&self) -> bool {
        true
    }

    fn need_sign(&self) -> bool {
        true
    }

    fn serialize(&self, _endpoint: &RestEndpoint) -> Result<serde_json::Value, RestError> {
        Ok(serde_json::to_value(self)?)
    }

    fn to_payload(&self) -> super::Payload {
        super::Payload::new(self.clone())
    }
}

/// Get order.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetOrder {
    /// Inner.
    #[serde(flatten)]
    pub inner: GetOrderInner,
}

impl Rest for GetOrder {
    fn method(&self, _endpoint: &RestEndpoint) -> Result<http::Method, RestError> {
        Ok(http::Method::GET)
    }

    fn to_path(&self, endpoint: &RestEndpoint) -> Result<String, RestError> {
        match endpoint {
            RestEndpoint::UsdMarginFutures => Ok("/fapi/v1/order".to_string()),
            RestEndpoint::Spot(options) => {
                if options.margin.is_some() {
                    Ok("/sapi/v1/margin/order".to_string())
                } else {
                    Ok("/api/v3/order".to_string())
                }
            }
        }
    }

    fn need_apikey(&self) -> bool {
        true
    }

    fn need_sign(&self) -> bool {
        true
    }

    fn serialize(&self, _endpoint: &RestEndpoint) -> Result<serde_json::Value, RestError> {
        Ok(serde_json::to_value(self)?)
    }

    fn to_payload(&self) -> super::Payload {
        super::Payload::new(self.clone())
    }
}