Skip to main content

rust_okx/api/account/requests/
trading.rs

1use serde::Serialize;
2
3use crate::model::{InstType, PositionSide, TradeMode};
4
5/// Request body for setting leverage.
6#[derive(Debug, Clone, Serialize)]
7pub struct SetLeverageRequest {
8    lever: String,
9    #[serde(rename = "mgnMode")]
10    mgn_mode: TradeMode,
11    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
12    inst_id: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    ccy: Option<String>,
15    #[serde(rename = "posSide", skip_serializing_if = "Option::is_none")]
16    pos_side: Option<PositionSide>,
17}
18
19impl SetLeverageRequest {
20    /// Create a leverage-setting request.
21    pub fn new(lever: impl Into<String>, mgn_mode: TradeMode) -> Self {
22        Self {
23            lever: lever.into(),
24            mgn_mode,
25            inst_id: None,
26            ccy: None,
27            pos_side: None,
28        }
29    }
30
31    /// Set the instrument ID.
32    pub fn inst_id(mut self, inst_id: impl Into<String>) -> Self {
33        self.inst_id = Some(inst_id.into());
34        self
35    }
36
37    /// Set the currency.
38    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
39        self.ccy = Some(ccy.into());
40        self
41    }
42
43    /// Set the position side.
44    pub fn position_side(mut self, pos_side: PositionSide) -> Self {
45        self.pos_side = Some(pos_side);
46        self
47    }
48}
49
50/// Query parameters for retrieving leverage.
51#[derive(Debug, Clone, Serialize)]
52pub struct LeverageRequest {
53    #[serde(rename = "mgnMode")]
54    mgn_mode: TradeMode,
55    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
56    inst_id: Option<String>,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    ccy: Option<String>,
59}
60
61impl LeverageRequest {
62    /// Create a leverage-info query.
63    pub fn new(mgn_mode: TradeMode) -> Self {
64        Self {
65            mgn_mode,
66            inst_id: None,
67            ccy: None,
68        }
69    }
70
71    /// Set the instrument ID.
72    pub fn inst_id(mut self, inst_id: impl Into<String>) -> Self {
73        self.inst_id = Some(inst_id.into());
74        self
75    }
76
77    /// Set the currency.
78    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
79        self.ccy = Some(ccy.into());
80        self
81    }
82}
83
84/// Query parameters for maximum order size.
85#[derive(Debug, Clone, Serialize)]
86pub struct MaxOrderSizeRequest {
87    #[serde(rename = "instId")]
88    inst_id: String,
89    #[serde(rename = "tdMode")]
90    td_mode: TradeMode,
91    #[serde(skip_serializing_if = "Option::is_none")]
92    ccy: Option<String>,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    px: Option<String>,
95    #[serde(rename = "tradeQuoteCcy", skip_serializing_if = "Option::is_none")]
96    trade_quote_ccy: Option<String>,
97}
98
99impl MaxOrderSizeRequest {
100    /// Create a maximum-order-size query.
101    pub fn new(inst_id: impl Into<String>, td_mode: TradeMode) -> Self {
102        Self {
103            inst_id: inst_id.into(),
104            td_mode,
105            ccy: None,
106            px: None,
107            trade_quote_ccy: None,
108        }
109    }
110
111    /// Set the currency.
112    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
113        self.ccy = Some(ccy.into());
114        self
115    }
116
117    /// Set the price.
118    pub fn price(mut self, px: impl Into<String>) -> Self {
119        self.px = Some(px.into());
120        self
121    }
122
123    /// Set the trade quote currency.
124    pub fn trade_quote_currency(mut self, trade_quote_ccy: impl Into<String>) -> Self {
125        self.trade_quote_ccy = Some(trade_quote_ccy.into());
126        self
127    }
128}
129
130/// Query parameters for maximum available size.
131#[derive(Debug, Clone, Serialize)]
132pub struct MaxAvailableSizeRequest {
133    #[serde(rename = "instId")]
134    inst_id: String,
135    #[serde(rename = "tdMode")]
136    td_mode: TradeMode,
137    #[serde(skip_serializing_if = "Option::is_none")]
138    ccy: Option<String>,
139    #[serde(rename = "reduceOnly", skip_serializing_if = "Option::is_none")]
140    reduce_only: Option<bool>,
141    #[serde(rename = "unSpotOffset", skip_serializing_if = "Option::is_none")]
142    un_spot_offset: Option<bool>,
143    #[serde(rename = "quickMgnType", skip_serializing_if = "Option::is_none")]
144    quick_mgn_type: Option<String>,
145    #[serde(rename = "tradeQuoteCcy", skip_serializing_if = "Option::is_none")]
146    trade_quote_ccy: Option<String>,
147}
148
149impl MaxAvailableSizeRequest {
150    /// Create a maximum-available-size query.
151    pub fn new(inst_id: impl Into<String>, td_mode: TradeMode) -> Self {
152        Self {
153            inst_id: inst_id.into(),
154            td_mode,
155            ccy: None,
156            reduce_only: None,
157            un_spot_offset: None,
158            quick_mgn_type: None,
159            trade_quote_ccy: None,
160        }
161    }
162
163    /// Set the currency.
164    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
165        self.ccy = Some(ccy.into());
166        self
167    }
168
169    /// Set the reduce-only filter.
170    pub fn reduce_only(mut self, reduce_only: bool) -> Self {
171        self.reduce_only = Some(reduce_only);
172        self
173    }
174
175    /// Set the spot offset flag.
176    pub fn un_spot_offset(mut self, un_spot_offset: bool) -> Self {
177        self.un_spot_offset = Some(un_spot_offset);
178        self
179    }
180
181    /// Set the quick margin type.
182    pub fn quick_margin_type(mut self, quick_mgn_type: impl Into<String>) -> Self {
183        self.quick_mgn_type = Some(quick_mgn_type.into());
184        self
185    }
186
187    /// Set the trade quote currency.
188    pub fn trade_quote_currency(mut self, trade_quote_ccy: impl Into<String>) -> Self {
189        self.trade_quote_ccy = Some(trade_quote_ccy.into());
190        self
191    }
192}
193
194/// Request body for adjusting position margin.
195#[derive(Debug, Clone, Serialize)]
196pub struct AdjustMarginRequest {
197    #[serde(rename = "instId")]
198    inst_id: String,
199    #[serde(rename = "posSide")]
200    pos_side: PositionSide,
201    #[serde(rename = "type")]
202    action: String,
203    amt: String,
204    #[serde(rename = "loanTrans", skip_serializing_if = "Option::is_none")]
205    loan_trans: Option<bool>,
206}
207
208impl AdjustMarginRequest {
209    /// Create a margin-adjustment request.
210    pub fn new(
211        inst_id: impl Into<String>,
212        pos_side: PositionSide,
213        action: impl Into<String>,
214        amt: impl Into<String>,
215    ) -> Self {
216        Self {
217            inst_id: inst_id.into(),
218            pos_side,
219            action: action.into(),
220            amt: amt.into(),
221            loan_trans: None,
222        }
223    }
224
225    /// Set whether to allow loan transfer.
226    pub fn loan_transfer(mut self, loan_trans: bool) -> Self {
227        self.loan_trans = Some(loan_trans);
228        self
229    }
230}
231
232/// Query parameters for trade fee rates.
233#[derive(Debug, Clone, Serialize)]
234pub struct FeeRatesRequest {
235    #[serde(rename = "instType")]
236    inst_type: InstType,
237    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
238    inst_id: Option<String>,
239    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
240    underlying: Option<String>,
241    #[serde(skip_serializing_if = "Option::is_none")]
242    category: Option<String>,
243    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
244    inst_family: Option<String>,
245}
246
247impl FeeRatesRequest {
248    /// Create a fee-rates query.
249    pub fn new(inst_type: InstType) -> Self {
250        Self {
251            inst_type,
252            inst_id: None,
253            underlying: None,
254            category: None,
255            inst_family: None,
256        }
257    }
258
259    /// Set the instrument ID.
260    pub fn inst_id(mut self, inst_id: impl Into<String>) -> Self {
261        self.inst_id = Some(inst_id.into());
262        self
263    }
264
265    /// Set the underlying.
266    pub fn underlying(mut self, underlying: impl Into<String>) -> Self {
267        self.underlying = Some(underlying.into());
268        self
269    }
270
271    /// Set the fee category.
272    pub fn category(mut self, category: impl Into<String>) -> Self {
273        self.category = Some(category.into());
274        self
275    }
276
277    /// Set the instrument family.
278    pub fn inst_family(mut self, inst_family: impl Into<String>) -> Self {
279        self.inst_family = Some(inst_family.into());
280        self
281    }
282}
283
284/// Query parameters for account instruments.
285#[derive(Debug, Clone, Serialize)]
286pub struct AccountInstrumentsRequest {
287    #[serde(rename = "instType")]
288    inst_type: InstType,
289    #[serde(rename = "seriesId", skip_serializing_if = "Option::is_none")]
290    series_id: Option<String>,
291    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
292    inst_family: Option<String>,
293    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
294    inst_id: Option<String>,
295}
296
297impl AccountInstrumentsRequest {
298    /// Create an empty account-instruments query.
299    pub fn new(inst_type: InstType) -> Self {
300        Self {
301            inst_type,
302            series_id: None,
303            inst_family: None,
304            inst_id: None,
305        }
306    }
307
308    /// Set the series_id filter.
309    pub fn series_id(mut self, series_id: impl Into<String>) -> Self {
310        self.series_id = Some(series_id.into());
311        self
312    }
313
314    /// Set the instrument family filter.
315    pub fn inst_family(mut self, inst_family: impl Into<String>) -> Self {
316        self.inst_family = Some(inst_family.into());
317        self
318    }
319
320    /// Set the instrument ID filter.
321    pub fn inst_id(mut self, inst_id: impl Into<String>) -> Self {
322        self.inst_id = Some(inst_id.into());
323        self
324    }
325}