Skip to main content

gateio_rs/api/spot/
get_market_trades.rs

1use crate::http::{Credentials, Method, request::Request};
2
3/// Request for retrieving recent public trades for a currency pair
4pub struct GetMarketTrades {
5    /// Currency pair to get trades for
6    pub currency_pair: String,
7    /// Maximum number of trades to return
8    pub limit: Option<i32>,
9    /// Last trade ID for pagination
10    pub last_id: Option<String>,
11    /// Whether to reverse the order of results
12    pub reverse: Option<bool>,
13    /// Start timestamp for trade history range
14    pub from: Option<i64>,
15    /// End timestamp for trade history range
16    pub to: Option<i64>,
17    /// Page number for pagination
18    pub page: Option<i32>,
19    /// API credentials for authentication (optional for public data)
20    pub credentials: Option<Credentials>,
21}
22
23impl GetMarketTrades {
24    /// Creates a new GetMarketTrades request for the specified currency pair
25    pub fn new(currency_pair: &str) -> Self {
26        Self {
27            currency_pair: currency_pair.to_owned(),
28            limit: None,
29            last_id: None,
30            reverse: None,
31            from: None,
32            to: None,
33            page: None,
34            credentials: None,
35        }
36    }
37
38    /// Sets the maximum number of trades to return
39    pub fn limit(mut self, limit: i32) -> Self {
40        self.limit = Some(limit.into());
41        self
42    }
43
44    /// Sets the last trade ID for pagination
45    pub fn last_id(mut self, last_id: &str) -> Self {
46        self.last_id = Some(last_id.into());
47        self
48    }
49
50    /// Sets whether to reverse the order of results
51    pub fn reverse(mut self, reverse: bool) -> Self {
52        self.reverse = Some(reverse.into());
53        self
54    }
55
56    /// Sets the start timestamp for trade history range
57    pub fn from(mut self, from: i64) -> Self {
58        self.from = Some(from.into());
59        self
60    }
61
62    /// Sets the end timestamp for trade history range
63    pub fn to(mut self, to: i64) -> Self {
64        self.to = Some(to.into());
65        self
66    }
67
68    /// Sets the page number for pagination
69    pub fn page(mut self, page: i32) -> Self {
70        self.page = Some(page.into());
71        self
72    }
73
74    /// Sets the API credentials for authentication
75    pub fn credentials(mut self, creds: Credentials) -> Self {
76        self.credentials = Some(creds);
77        self
78    }
79}
80
81impl From<GetMarketTrades> for Request {
82    fn from(request: GetMarketTrades) -> Request {
83        let mut params = vec![("currency_pair".to_owned(), request.currency_pair)];
84
85        if let Some(limit) = request.limit {
86            params.push(("limit".into(), limit.to_string()));
87        }
88
89        if let Some(last_id) = request.last_id {
90            params.push(("last_id".into(), last_id.to_string()));
91        }
92
93        if let Some(reverse) = request.reverse {
94            params.push(("reverse".into(), reverse.to_string()));
95        }
96
97        if let Some(from) = request.from {
98            params.push(("from".into(), from.to_string()));
99        }
100
101        if let Some(to) = request.to {
102            params.push(("to".into(), to.to_string()));
103        }
104
105        if let Some(page) = request.page {
106            params.push(("page".into(), page.to_string()));
107        }
108
109        Request {
110            method: Method::Get,
111            path: "/api/v4/spot/trades".into(),
112            params,
113            payload: "".to_string(),
114            x_gate_exp_time: None,
115            credentials: request.credentials,
116            sign: false,
117        }
118    }
119}