Skip to main content

gateio_rs/api/spot/
countdown_cancel_all.rs

1use crate::http::{Credentials, Method, request::Request};
2use serde_json::{Map, Value, json};
3
4/// # Countdown cancel all orders
5///
6/// Start a countdown timer to cancel all open spot orders.
7/// If the timeout is reached without being reset, all open orders will be cancelled automatically.
8/// This is useful as a safety mechanism to prevent orders from remaining open if connection is lost.
9///
10/// ## Important Notes:
11/// - The countdown can be reset by calling this endpoint again with a new timeout
12/// - Setting timeout to 0 will disable the countdown
13/// - Only affects orders for the specified currency pair (if provided)
14/// - Only affects spot orders, not futures or other types
15///
16/// [Gate API Documentation](https://www.gate.com/docs/developers/apiv4/#countdown-cancel-orders)
17pub struct CountdownCancelAll {
18    /// Countdown timeout in seconds (0 to disable)
19    pub timeout: i64,
20    /// Optional currency pair to limit cancellation to
21    pub currency_pair: Option<String>,
22    /// API credentials for authentication
23    pub credentials: Option<Credentials>,
24}
25
26impl CountdownCancelAll {
27    /// Create a new countdown cancel all request
28    pub fn new(timeout: i64) -> Self {
29        Self {
30            timeout,
31            currency_pair: None,
32            credentials: None,
33        }
34    }
35
36    /// Set the currency pair to limit cancellation to
37    pub fn currency_pair(mut self, currency_pair: &str) -> Self {
38        self.currency_pair = Some(currency_pair.to_string());
39        self
40    }
41
42    /// Set API credentials for authentication
43    pub fn credentials(mut self, creds: Credentials) -> Self {
44        self.credentials = Some(creds);
45        self
46    }
47}
48
49impl From<CountdownCancelAll> for Request {
50    fn from(request: CountdownCancelAll) -> Request {
51        let params = Vec::new();
52        let mut payload = Map::new();
53
54        payload.insert("timeout".to_string(), json!(request.timeout));
55
56        if let Some(currency_pair) = request.currency_pair {
57            payload.insert("currency_pair".to_string(), json!(currency_pair));
58        }
59
60        let payload_json = Value::Object(payload);
61
62        Request {
63            method: Method::Post,
64            path: "/api/v4/spot/countdown_cancel_all".into(),
65            params,
66            payload: payload_json.to_string(),
67            x_gate_exp_time: None,
68            credentials: request.credentials,
69            sign: true,
70        }
71    }
72}