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 maximum order size.
87#[derive(Debug, Clone, Serialize)]
88pub struct MaxOrderSizeRequest<'a> {
89    #[serde(rename = "instId")]
90    inst_id: Cow<'a, str>,
91    #[serde(rename = "tdMode")]
92    td_mode: TradeMode,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    ccy: Option<Cow<'a, str>>,
95    #[serde(skip_serializing_if = "Option::is_none")]
96    px: Option<Cow<'a, str>>,
97    #[serde(rename = "tradeQuoteCcy", skip_serializing_if = "Option::is_none")]
98    trade_quote_ccy: Option<Cow<'a, str>>,
99}
100
101impl<'a> MaxOrderSizeRequest<'a> {
102    /// Create a maximum-order-size query.
103    pub fn new(inst_id: impl Into<Cow<'a, str>>, td_mode: TradeMode) -> Self {
104        Self {
105            inst_id: inst_id.into(),
106            td_mode,
107            ccy: None,
108            px: None,
109            trade_quote_ccy: None,
110        }
111    }
112
113    /// Set the currency.
114    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
115        self.ccy = Some(ccy.into());
116        self
117    }
118
119    /// Set the price.
120    pub fn price(mut self, px: impl Into<Cow<'a, str>>) -> Self {
121        self.px = Some(px.into());
122        self
123    }
124
125    /// Set the trade quote currency.
126    pub fn trade_quote_currency(mut self, trade_quote_ccy: impl Into<Cow<'a, str>>) -> Self {
127        self.trade_quote_ccy = Some(trade_quote_ccy.into());
128        self
129    }
130}
131
132/// Query parameters for maximum available size.
133#[derive(Debug, Clone, Serialize)]
134pub struct MaxAvailableSizeRequest<'a> {
135    #[serde(rename = "instId")]
136    inst_id: Cow<'a, str>,
137    #[serde(rename = "tdMode")]
138    td_mode: TradeMode,
139    #[serde(skip_serializing_if = "Option::is_none")]
140    ccy: Option<Cow<'a, str>>,
141    #[serde(rename = "reduceOnly", skip_serializing_if = "Option::is_none")]
142    reduce_only: Option<bool>,
143    #[serde(rename = "unSpotOffset", skip_serializing_if = "Option::is_none")]
144    un_spot_offset: Option<bool>,
145    #[serde(rename = "quickMgnType", skip_serializing_if = "Option::is_none")]
146    quick_mgn_type: Option<Cow<'a, str>>,
147    #[serde(rename = "tradeQuoteCcy", skip_serializing_if = "Option::is_none")]
148    trade_quote_ccy: Option<Cow<'a, str>>,
149}
150
151impl<'a> MaxAvailableSizeRequest<'a> {
152    /// Create a maximum-available-size query.
153    pub fn new(inst_id: impl Into<Cow<'a, str>>, td_mode: TradeMode) -> Self {
154        Self {
155            inst_id: inst_id.into(),
156            td_mode,
157            ccy: None,
158            reduce_only: None,
159            un_spot_offset: None,
160            quick_mgn_type: None,
161            trade_quote_ccy: None,
162        }
163    }
164
165    /// Set the currency.
166    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
167        self.ccy = Some(ccy.into());
168        self
169    }
170
171    /// Set the reduce-only filter.
172    pub fn reduce_only(mut self, reduce_only: bool) -> Self {
173        self.reduce_only = Some(reduce_only);
174        self
175    }
176
177    /// Set the spot offset flag.
178    pub fn un_spot_offset(mut self, un_spot_offset: bool) -> Self {
179        self.un_spot_offset = Some(un_spot_offset);
180        self
181    }
182
183    /// Set the quick margin type.
184    pub fn quick_margin_type(mut self, quick_mgn_type: impl Into<Cow<'a, str>>) -> Self {
185        self.quick_mgn_type = Some(quick_mgn_type.into());
186        self
187    }
188
189    /// Set the trade quote currency.
190    pub fn trade_quote_currency(mut self, trade_quote_ccy: impl Into<Cow<'a, str>>) -> Self {
191        self.trade_quote_ccy = Some(trade_quote_ccy.into());
192        self
193    }
194}
195
196/// Request body for adjusting position margin.
197#[derive(Debug, Clone, Serialize)]
198pub struct AdjustMarginRequest<'a> {
199    #[serde(rename = "instId")]
200    inst_id: Cow<'a, str>,
201    #[serde(rename = "posSide")]
202    pos_side: PositionSide,
203    #[serde(rename = "type")]
204    action: Cow<'a, str>,
205    amt: Cow<'a, str>,
206    #[serde(rename = "loanTrans", skip_serializing_if = "Option::is_none")]
207    loan_trans: Option<bool>,
208}
209
210impl<'a> AdjustMarginRequest<'a> {
211    /// Create a margin-adjustment request.
212    pub fn new(
213        inst_id: impl Into<Cow<'a, str>>,
214        pos_side: PositionSide,
215        action: impl Into<Cow<'a, str>>,
216        amt: impl Into<Cow<'a, str>>,
217    ) -> Self {
218        Self {
219            inst_id: inst_id.into(),
220            pos_side,
221            action: action.into(),
222            amt: amt.into(),
223            loan_trans: None,
224        }
225    }
226
227    /// Set whether to allow loan transfer.
228    pub fn loan_transfer(mut self, loan_trans: bool) -> Self {
229        self.loan_trans = Some(loan_trans);
230        self
231    }
232}
233
234/// Query parameters for trade fee rates.
235#[derive(Debug, Clone, Serialize)]
236pub struct FeeRatesRequest<'a> {
237    #[serde(rename = "instType")]
238    inst_type: InstType,
239    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
240    inst_id: Option<Cow<'a, str>>,
241    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
242    underlying: Option<Cow<'a, str>>,
243    #[serde(skip_serializing_if = "Option::is_none")]
244    category: Option<Cow<'a, str>>,
245    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
246    inst_family: Option<Cow<'a, str>>,
247}
248
249impl<'a> FeeRatesRequest<'a> {
250    /// Create a fee-rates query.
251    pub fn new(inst_type: InstType) -> Self {
252        Self {
253            inst_type,
254            inst_id: None,
255            underlying: None,
256            category: None,
257            inst_family: None,
258        }
259    }
260
261    /// Set the instrument ID.
262    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
263        self.inst_id = Some(inst_id.into());
264        self
265    }
266
267    /// Set the underlying.
268    pub fn underlying(mut self, underlying: impl Into<Cow<'a, str>>) -> Self {
269        self.underlying = Some(underlying.into());
270        self
271    }
272
273    /// Set the fee category.
274    pub fn category(mut self, category: impl Into<Cow<'a, str>>) -> Self {
275        self.category = Some(category.into());
276        self
277    }
278
279    /// Set the instrument family.
280    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
281        self.inst_family = Some(inst_family.into());
282        self
283    }
284}
285
286/// Query parameters for account instruments.
287#[derive(Debug, Clone, Serialize)]
288pub struct AccountInstrumentsRequest<'a> {
289    #[serde(rename = "instType")]
290    inst_type: InstType,
291    #[serde(rename = "seriesId", skip_serializing_if = "Option::is_none")]
292    series_id: Option<Cow<'a, str>>,
293    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
294    inst_family: Option<Cow<'a, str>>,
295    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
296    inst_id: Option<Cow<'a, str>>,
297}
298
299impl<'a> AccountInstrumentsRequest<'a> {
300    /// Create an empty account-instruments query.
301    pub fn new(inst_type: InstType) -> Self {
302        Self {
303            inst_type,
304            series_id: None,
305            inst_family: None,
306            inst_id: None,
307        }
308    }
309
310    /// Set the series_id filter.
311    pub fn series_id(mut self, series_id: impl Into<Cow<'a, str>>) -> Self {
312        self.series_id = Some(series_id.into());
313        self
314    }
315
316    /// Set the instrument family filter.
317    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
318        self.inst_family = Some(inst_family.into());
319        self
320    }
321
322    /// Set the instrument ID filter.
323    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
324        self.inst_id = Some(inst_id.into());
325        self
326    }
327}