Skip to main content

gateio_rs/api/spot/
order.rs

1use serde::Serialize;
2
3/// Order data structure for creating and managing spot orders
4#[derive(Debug, Clone, Serialize)]
5pub struct Order {
6    /// User-defined text information for the order
7    #[serde(skip_serializing_if = "Option::is_none")]
8    pub text: Option<String>,
9    /// Currency pair for the order (e.g., "BTC_USDT")
10    pub currency_pair: String,
11    /// Order type ("limit", "market", "immediate_or_cancel", "fill_or_kill")
12    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
13    pub order_type: Option<String>,
14    /// Account type ("spot", "margin", "cross_margin")
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub account: Option<String>,
17    /// Order side ("buy" or "sell")
18    pub side: String,
19    /// Order amount in base currency
20    pub amount: String,
21    /// Order price (required for limit orders)
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub price: Option<String>,
24    /// Time in force policy ("gtc", "ioc", "fok")
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub time_in_force: Option<String>,
27    /// Iceberg order visible amount
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub iceberg: Option<String>,
30    /// Whether to automatically borrow for margin trading
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub auto_borrow: Option<bool>,
33    /// Whether to automatically repay for margin trading
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub auto_repay: Option<bool>,
36    /// Self-trade prevention action ("cn", "co", "cb")
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub stp_act: Option<String>,
39    /// Action mode for the order
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub action_mode: Option<String>,
42}
43
44impl Order {
45    /// Creates a new Order with required parameters
46    pub fn new(currency_pair: &str, side: &str, amount: &str) -> Self {
47        Self {
48            text: None,
49            currency_pair: currency_pair.to_owned(),
50            order_type: None,
51            account: None,
52            side: side.to_owned(),
53            amount: amount.to_owned(),
54            price: None,
55            time_in_force: None,
56            iceberg: None,
57            auto_borrow: None,
58            auto_repay: None,
59            stp_act: None,
60            action_mode: None,
61        }
62    }
63
64    /// Sets user-defined text information for the order
65    pub fn text(mut self, text: &str) -> Self {
66        self.text = Some(text.into());
67        self
68    }
69
70    /// Sets the order type
71    pub fn order_type(mut self, order_type: &str) -> Self {
72        self.order_type = Some(order_type.into());
73        self
74    }
75
76    /// Sets the account type for the order
77    pub fn account(mut self, account: &str) -> Self {
78        self.account = Some(account.into());
79        self
80    }
81
82    /// Sets the order price (required for limit orders)
83    pub fn price(mut self, price: &str) -> Self {
84        self.price = Some(price.into());
85        self
86    }
87
88    /// Sets the time in force policy
89    pub fn time_in_force(mut self, time_in_force: &str) -> Self {
90        self.time_in_force = Some(time_in_force.into());
91        self
92    }
93
94    /// Sets the iceberg order visible amount
95    pub fn iceberg(mut self, iceberg: &str) -> Self {
96        self.iceberg = Some(iceberg.into());
97        self
98    }
99
100    /// Sets whether to automatically borrow for margin trading
101    pub fn auto_borrow(mut self, auto_borrow: bool) -> Self {
102        self.auto_borrow = Some(auto_borrow);
103        self
104    }
105
106    /// Sets whether to automatically repay for margin trading
107    pub fn auto_repay(mut self, auto_repay: bool) -> Self {
108        self.auto_repay = Some(auto_repay);
109        self
110    }
111
112    /// Sets the self-trade prevention action
113    pub fn stp_act(mut self, stp_act: &str) -> Self {
114        self.stp_act = Some(stp_act.into());
115        self
116    }
117
118    /// Sets the action mode for the order
119    pub fn action_mode(mut self, action_mode: &str) -> Self {
120        self.action_mode = Some(action_mode.into());
121        self
122    }
123}