Skip to main content

gateio_rs/api/spot/
create_cross_liquidate_orders.rs

1use crate::http::{Credentials, Method, request::Request};
2use serde::Serialize;
3
4/// # Cross liquidate order
5///
6/// Represents a single cross-liquidation order for margin trading.
7/// Cross liquidation allows closing positions across different currency pairs
8/// to meet margin requirements.
9#[derive(Debug, Clone, Serialize)]
10pub struct CrossLiquidateOrder {
11    /// Currency pair for the order
12    pub currency_pair: String,
13    /// Order amount
14    pub amount: String,
15    /// Order price
16    pub price: String,
17    /// Custom order text/label
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub text: Option<String>,
20    /// Processing mode for the response
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub action_mode: Option<String>,
23}
24
25impl CrossLiquidateOrder {
26    /// Create a new cross liquidate order
27    pub fn new(currency_pair: &str, amount: &str, price: &str) -> Self {
28        Self {
29            currency_pair: currency_pair.to_owned(),
30            amount: amount.to_owned(),
31            price: price.to_owned(),
32            text: None,
33            action_mode: None,
34        }
35    }
36
37    /// Set custom order text/label
38    pub fn text(mut self, text: &str) -> Self {
39        self.text = Some(text.to_owned());
40        self
41    }
42
43    /// Set the processing mode for the response
44    pub fn action_mode(mut self, action_mode: &str) -> Self {
45        self.action_mode = Some(action_mode.to_owned());
46        self
47    }
48}
49
50/// # Create cross liquidate orders
51///
52/// Create multiple cross-liquidation orders for margin trading.
53/// Cross liquidation helps close positions across different currency pairs
54/// to meet margin requirements automatically.
55///
56/// ## Important Notes:
57/// - Only available for margin trading accounts
58/// - Orders are processed to reduce overall margin risk
59/// - All orders must be valid for the request to succeed
60///
61/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#place-cross-liquidation-order)
62pub struct CreateCrossLiquidateOrders {
63    /// List of cross liquidate orders to create
64    pub orders: Vec<CrossLiquidateOrder>,
65    /// Request expiration time in milliseconds
66    pub x_gate_exp_time: Option<u128>,
67    /// API credentials for authentication
68    pub credentials: Option<Credentials>,
69}
70
71impl CreateCrossLiquidateOrders {
72    /// Create a new cross liquidate orders request
73    pub fn new(orders: Vec<CrossLiquidateOrder>) -> Self {
74        Self {
75            orders,
76            x_gate_exp_time: None,
77            credentials: None,
78        }
79    }
80
81    /// Set the request expiration time in milliseconds
82    pub fn x_gate_exp_time(mut self, x_gate_exp_time: u128) -> Self {
83        self.x_gate_exp_time = Some(x_gate_exp_time);
84        self
85    }
86
87    /// Set API credentials for authentication
88    pub fn credentials(mut self, creds: Credentials) -> Self {
89        self.credentials = Some(creds);
90        self
91    }
92}
93
94impl From<CreateCrossLiquidateOrders> for Request {
95    fn from(request: CreateCrossLiquidateOrders) -> Request {
96        let params = Vec::new();
97        let payload = serde_json::to_string(&request.orders).unwrap();
98
99        Request {
100            method: Method::Post,
101            path: "/api/v4/spot/cross_liquidate_orders".into(),
102            params,
103            payload,
104            x_gate_exp_time: request.x_gate_exp_time,
105            credentials: request.credentials,
106            sign: true,
107        }
108    }
109}