Skip to main content

rust_okx/api/account/requests/
trading.rs

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