Skip to main content

rust_okx/api/sub_account/
requests.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5/// Account type used in sub-account asset transfers.
6#[derive(Debug, Clone, Copy, Serialize)]
7pub enum SubAccountType {
8    /// Funding account (wire value `"6"`).
9    #[serde(rename = "6")]
10    Funding,
11    /// Trading account (wire value `"18"`).
12    #[serde(rename = "18")]
13    Trading,
14}
15
16/// Query parameters for [`SubAccount::get_subaccount_list`](crate::api::sub_account::SubAccount::get_subaccount_list).
17#[derive(Debug, Clone, Default, Serialize)]
18#[serde(rename_all = "camelCase")]
19pub struct SubAccountListRequest<'a> {
20    #[serde(skip_serializing_if = "Option::is_none")]
21    enable: Option<bool>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    sub_acct: Option<Cow<'a, str>>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    after: Option<Cow<'a, str>>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    before: Option<Cow<'a, str>>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    limit: Option<u32>,
30}
31
32impl<'a> SubAccountListRequest<'a> {
33    /// Start with no filters applied.
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Filter by enabled/disabled status.
39    pub fn enable(mut self, enable: bool) -> Self {
40        self.enable = Some(enable);
41        self
42    }
43
44    /// Filter to a specific sub-account by name.
45    pub fn sub_acct(mut self, sub_acct: impl Into<Cow<'a, str>>) -> Self {
46        self.sub_acct = Some(sub_acct.into());
47        self
48    }
49
50    /// Return results with UID older than this value.
51    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
52        self.after = Some(after.into());
53        self
54    }
55
56    /// Return results with UID newer than this value.
57    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
58        self.before = Some(before.into());
59        self
60    }
61
62    /// Maximum number of results (default 100, max 100).
63    pub fn limit(mut self, limit: u32) -> Self {
64        self.limit = Some(limit);
65        self
66    }
67}
68
69/// Query parameters for [`SubAccount::get_subaccount_apikeys`](crate::api::sub_account::SubAccount::get_subaccount_apikeys).
70#[derive(Debug, Clone, Serialize)]
71#[serde(rename_all = "camelCase")]
72pub struct SubAccountApiKeysRequest<'a> {
73    sub_acct: Cow<'a, str>,
74    #[serde(skip_serializing_if = "Option::is_none")]
75    api_key: Option<Cow<'a, str>>,
76}
77
78impl<'a> SubAccountApiKeysRequest<'a> {
79    /// List all API keys for `sub_acct`.
80    pub fn new(sub_acct: impl Into<Cow<'a, str>>) -> Self {
81        Self {
82            sub_acct: sub_acct.into(),
83            api_key: None,
84        }
85    }
86
87    /// Retrieve a specific API key.
88    pub fn api_key(mut self, api_key: impl Into<Cow<'a, str>>) -> Self {
89        self.api_key = Some(api_key.into());
90        self
91    }
92}
93
94/// Query parameters for [`SubAccount::get_subaccount_trading_balances`](crate::api::sub_account::SubAccount::get_subaccount_trading_balances).
95#[derive(Debug, Clone, Serialize)]
96#[serde(rename_all = "camelCase")]
97pub struct SubAccountTradingBalancesRequest<'a> {
98    sub_acct: Cow<'a, str>,
99}
100
101impl<'a> SubAccountTradingBalancesRequest<'a> {
102    /// Query trading-account balances for `sub_acct`.
103    pub fn new(sub_acct: impl Into<Cow<'a, str>>) -> Self {
104        Self {
105            sub_acct: sub_acct.into(),
106        }
107    }
108}
109
110/// Query parameters for [`SubAccount::get_subaccount_funding_balances`](crate::api::sub_account::SubAccount::get_subaccount_funding_balances).
111#[derive(Debug, Clone, Serialize)]
112#[serde(rename_all = "camelCase")]
113pub struct SubAccountFundingBalancesRequest<'a> {
114    sub_acct: Cow<'a, str>,
115    #[serde(skip_serializing_if = "Option::is_none")]
116    ccy: Option<Cow<'a, str>>,
117}
118
119impl<'a> SubAccountFundingBalancesRequest<'a> {
120    /// Query funding-account balances for `sub_acct`.
121    pub fn new(sub_acct: impl Into<Cow<'a, str>>) -> Self {
122        Self {
123            sub_acct: sub_acct.into(),
124            ccy: None,
125        }
126    }
127
128    /// Filter by currency.
129    pub fn ccy(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
130        self.ccy = Some(ccy.into());
131        self
132    }
133}
134
135/// Query parameters for [`SubAccount::get_subaccount_max_withdrawal`](crate::api::sub_account::SubAccount::get_subaccount_max_withdrawal).
136#[derive(Debug, Clone, Serialize)]
137#[serde(rename_all = "camelCase")]
138pub struct SubAccountMaxWithdrawalRequest<'a> {
139    sub_acct: Cow<'a, str>,
140    #[serde(skip_serializing_if = "Option::is_none")]
141    ccy: Option<Cow<'a, str>>,
142}
143
144impl<'a> SubAccountMaxWithdrawalRequest<'a> {
145    /// Query maximum withdrawal for `sub_acct`.
146    pub fn new(sub_acct: impl Into<Cow<'a, str>>) -> Self {
147        Self {
148            sub_acct: sub_acct.into(),
149            ccy: None,
150        }
151    }
152
153    /// Filter to a specific currency.
154    pub fn ccy(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
155        self.ccy = Some(ccy.into());
156        self
157    }
158}
159
160/// Request body for [`SubAccount::create_subaccount`](crate::api::sub_account::SubAccount::create_subaccount).
161#[derive(Debug, Clone, Serialize)]
162#[serde(rename_all = "camelCase")]
163pub struct CreateSubAccountRequest<'a> {
164    sub_acct: Cow<'a, str>,
165    r#type: Cow<'a, str>,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    label: Option<Cow<'a, str>>,
168    /// Password is security-sensitive; kept as owned `String`.
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pwd: Option<String>,
171}
172
173impl<'a> CreateSubAccountRequest<'a> {
174    /// Create a sub-account with the given name and type.
175    ///
176    /// `sub_type` is required by OKX: `"1"` = standard sub-account,
177    /// `"5"` = custody trading (Copper), `"12"` = custody trading (Komainu).
178    pub fn new(sub_acct: impl Into<Cow<'a, str>>, sub_type: impl Into<Cow<'a, str>>) -> Self {
179        Self {
180            sub_acct: sub_acct.into(),
181            r#type: sub_type.into(),
182            label: None,
183            pwd: None,
184        }
185    }
186
187    /// Set a display label for the sub-account.
188    pub fn label(mut self, label: impl Into<Cow<'a, str>>) -> Self {
189        self.label = Some(label.into());
190        self
191    }
192
193    /// Set the login password.
194    pub fn pwd(mut self, pwd: impl Into<String>) -> Self {
195        self.pwd = Some(pwd.into());
196        self
197    }
198}
199
200/// Request body for [`SubAccount::create_subaccount_apikey`](crate::api::sub_account::SubAccount::create_subaccount_apikey).
201#[derive(Debug, Clone, Serialize)]
202#[serde(rename_all = "camelCase")]
203pub struct CreateSubAccountApiKeyRequest<'a> {
204    sub_acct: Cow<'a, str>,
205    label: Cow<'a, str>,
206    /// Passphrase is security-sensitive; kept as owned `String`.
207    passphrase: String,
208    #[serde(skip_serializing_if = "Option::is_none")]
209    perm: Option<Cow<'a, str>>,
210    #[serde(skip_serializing_if = "Option::is_none")]
211    ip: Option<Cow<'a, str>>,
212}
213
214impl<'a> CreateSubAccountApiKeyRequest<'a> {
215    /// Create an API key for `sub_acct` with a label and passphrase.
216    pub fn new(
217        sub_acct: impl Into<Cow<'a, str>>,
218        label: impl Into<Cow<'a, str>>,
219        passphrase: impl Into<String>,
220    ) -> Self {
221        Self {
222            sub_acct: sub_acct.into(),
223            label: label.into(),
224            passphrase: passphrase.into(),
225            perm: None,
226            ip: None,
227        }
228    }
229
230    /// Set permissions (comma-separated, e.g. `"read_only,trade"`).
231    pub fn perm(mut self, perm: impl Into<Cow<'a, str>>) -> Self {
232        self.perm = Some(perm.into());
233        self
234    }
235
236    /// Restrict to specific IP addresses (comma-separated).
237    pub fn ip(mut self, ip: impl Into<Cow<'a, str>>) -> Self {
238        self.ip = Some(ip.into());
239        self
240    }
241}
242
243/// Request body for [`SubAccount::modify_subaccount_apikey`](crate::api::sub_account::SubAccount::modify_subaccount_apikey).
244#[derive(Debug, Clone, Serialize)]
245#[serde(rename_all = "camelCase")]
246pub struct ModifySubAccountApiKeyRequest<'a> {
247    sub_acct: Cow<'a, str>,
248    api_key: Cow<'a, str>,
249    #[serde(skip_serializing_if = "Option::is_none")]
250    label: Option<Cow<'a, str>>,
251    #[serde(skip_serializing_if = "Option::is_none")]
252    perm: Option<Cow<'a, str>>,
253    #[serde(skip_serializing_if = "Option::is_none")]
254    ip: Option<Cow<'a, str>>,
255}
256
257impl<'a> ModifySubAccountApiKeyRequest<'a> {
258    /// Modify the API key identified by `api_key` on `sub_acct`.
259    pub fn new(sub_acct: impl Into<Cow<'a, str>>, api_key: impl Into<Cow<'a, str>>) -> Self {
260        Self {
261            sub_acct: sub_acct.into(),
262            api_key: api_key.into(),
263            label: None,
264            perm: None,
265            ip: None,
266        }
267    }
268
269    /// Change the label.
270    pub fn label(mut self, label: impl Into<Cow<'a, str>>) -> Self {
271        self.label = Some(label.into());
272        self
273    }
274
275    /// Change permissions (comma-separated).
276    pub fn perm(mut self, perm: impl Into<Cow<'a, str>>) -> Self {
277        self.perm = Some(perm.into());
278        self
279    }
280
281    /// Change allowed IP addresses (comma-separated).
282    pub fn ip(mut self, ip: impl Into<Cow<'a, str>>) -> Self {
283        self.ip = Some(ip.into());
284        self
285    }
286}
287
288/// Request body for [`SubAccount::delete_subaccount_apikey`](crate::api::sub_account::SubAccount::delete_subaccount_apikey).
289#[derive(Debug, Clone, Serialize)]
290#[serde(rename_all = "camelCase")]
291pub struct DeleteSubAccountApiKeyRequest<'a> {
292    sub_acct: Cow<'a, str>,
293    api_key: Cow<'a, str>,
294}
295
296impl<'a> DeleteSubAccountApiKeyRequest<'a> {
297    /// Delete the API key identified by `api_key` from `sub_acct`.
298    pub fn new(sub_acct: impl Into<Cow<'a, str>>, api_key: impl Into<Cow<'a, str>>) -> Self {
299        Self {
300            sub_acct: sub_acct.into(),
301            api_key: api_key.into(),
302        }
303    }
304}
305
306/// Request body for [`SubAccount::transfer_between_subaccounts`](crate::api::sub_account::SubAccount::transfer_between_subaccounts).
307#[derive(Debug, Clone, Serialize)]
308#[serde(rename_all = "camelCase")]
309pub struct SubAccountTransferRequest<'a> {
310    ccy: Cow<'a, str>,
311    amt: Cow<'a, str>,
312    from: SubAccountType,
313    to: SubAccountType,
314    from_sub_account: Cow<'a, str>,
315    to_sub_account: Cow<'a, str>,
316    #[serde(skip_serializing_if = "Option::is_none")]
317    loan_trans: Option<bool>,
318    #[serde(skip_serializing_if = "Option::is_none")]
319    omit_pos_risk: Option<bool>,
320}
321
322impl<'a> SubAccountTransferRequest<'a> {
323    /// Transfer `amt` of `ccy` between sub-accounts.
324    ///
325    /// `from` / `to` select the account type on each side:
326    /// [`SubAccountType::Funding`] or [`SubAccountType::Trading`].
327    pub fn new(
328        ccy: impl Into<Cow<'a, str>>,
329        amt: impl Into<Cow<'a, str>>,
330        from: SubAccountType,
331        to: SubAccountType,
332        from_sub_account: impl Into<Cow<'a, str>>,
333        to_sub_account: impl Into<Cow<'a, str>>,
334    ) -> Self {
335        Self {
336            ccy: ccy.into(),
337            amt: amt.into(),
338            from,
339            to,
340            from_sub_account: from_sub_account.into(),
341            to_sub_account: to_sub_account.into(),
342            loan_trans: None,
343            omit_pos_risk: None,
344        }
345    }
346
347    /// Whether to transfer with borrowing.
348    pub fn loan_trans(mut self, loan_trans: bool) -> Self {
349        self.loan_trans = Some(loan_trans);
350        self
351    }
352
353    /// Whether to ignore position risk when transferring.
354    pub fn omit_pos_risk(mut self, omit_pos_risk: bool) -> Self {
355        self.omit_pos_risk = Some(omit_pos_risk);
356        self
357    }
358}
359
360/// Request body for [`SubAccount::set_subaccount_transfer_out`](crate::api::sub_account::SubAccount::set_subaccount_transfer_out).
361#[derive(Debug, Clone, Serialize)]
362#[serde(rename_all = "camelCase")]
363pub struct SetTransferOutRequest<'a> {
364    sub_acct: Cow<'a, str>,
365    // True is default value.
366    can_trans_out: bool,
367}
368
369impl<'a> SetTransferOutRequest<'a> {
370    /// Enable or disable transfers out for `sub_acct`.
371    pub fn new(sub_acct: impl Into<Cow<'a, str>>) -> Self {
372        Self {
373            sub_acct: sub_acct.into(),
374            can_trans_out: true,
375        }
376    }
377
378    ///Filter by can_trans_out
379    pub fn can_trans_out(mut self, can_trans_out: bool) -> Self {
380        self.can_trans_out = can_trans_out;
381        self
382    }
383}
384
385/// Query parameters for [`SubAccount::get_subaccount_bills`](crate::api::sub_account::SubAccount::get_subaccount_bills).
386#[derive(Debug, Clone, Default, Serialize)]
387#[serde(rename_all = "camelCase")]
388pub struct SubAccountBillsRequest<'a> {
389    #[serde(skip_serializing_if = "Option::is_none")]
390    ccy: Option<Cow<'a, str>>,
391    #[serde(skip_serializing_if = "Option::is_none")]
392    r#type: Option<Cow<'a, str>>,
393    #[serde(skip_serializing_if = "Option::is_none")]
394    sub_acct: Option<Cow<'a, str>>,
395    #[serde(skip_serializing_if = "Option::is_none")]
396    after: Option<Cow<'a, str>>,
397    #[serde(skip_serializing_if = "Option::is_none")]
398    before: Option<Cow<'a, str>>,
399    #[serde(skip_serializing_if = "Option::is_none")]
400    limit: Option<u32>,
401}
402
403impl<'a> SubAccountBillsRequest<'a> {
404    /// Start with no filters applied.
405    pub fn new() -> Self {
406        Self::default()
407    }
408
409    /// Filter by currency.
410    pub fn ccy(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
411        self.ccy = Some(ccy.into());
412        self
413    }
414
415    /// Filter by bill type.
416    pub fn bill_type(mut self, r#type: impl Into<Cow<'a, str>>) -> Self {
417        self.r#type = Some(r#type.into());
418        self
419    }
420
421    /// Filter by sub-account name.
422    pub fn sub_acct(mut self, sub_acct: impl Into<Cow<'a, str>>) -> Self {
423        self.sub_acct = Some(sub_acct.into());
424        self
425    }
426
427    /// Return results older than this bill ID.
428    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
429        self.after = Some(after.into());
430        self
431    }
432
433    /// Return results newer than this bill ID.
434    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
435        self.before = Some(before.into());
436        self
437    }
438
439    /// Maximum number of results (default 100, max 100).
440    pub fn limit(mut self, limit: u32) -> Self {
441        self.limit = Some(limit);
442        self
443    }
444}
445
446/// Query parameters for [`SubAccount::get_subaccount_managed_bills`](crate::api::sub_account::SubAccount::get_subaccount_managed_bills).
447#[derive(Debug, Clone, Default, Serialize)]
448#[serde(rename_all = "camelCase")]
449pub struct ManagedSubAccountBillsRequest<'a> {
450    #[serde(skip_serializing_if = "Option::is_none")]
451    ccy: Option<Cow<'a, str>>,
452    #[serde(skip_serializing_if = "Option::is_none")]
453    r#type: Option<Cow<'a, str>>,
454    #[serde(skip_serializing_if = "Option::is_none")]
455    sub_acct: Option<Cow<'a, str>>,
456    #[serde(skip_serializing_if = "Option::is_none")]
457    sub_uid: Option<Cow<'a, str>>,
458    #[serde(skip_serializing_if = "Option::is_none")]
459    after: Option<Cow<'a, str>>,
460    #[serde(skip_serializing_if = "Option::is_none")]
461    before: Option<Cow<'a, str>>,
462    #[serde(skip_serializing_if = "Option::is_none")]
463    limit: Option<u32>,
464}
465
466impl<'a> ManagedSubAccountBillsRequest<'a> {
467    /// Start with no filters applied.
468    pub fn new() -> Self {
469        Self::default()
470    }
471
472    /// Filter by currency.
473    pub fn ccy(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
474        self.ccy = Some(ccy.into());
475        self
476    }
477
478    /// Filter by bill type.
479    pub fn bill_type(mut self, r#type: impl Into<Cow<'a, str>>) -> Self {
480        self.r#type = Some(r#type.into());
481        self
482    }
483
484    /// Filter by sub-account name.
485    pub fn sub_acct(mut self, sub_acct: impl Into<Cow<'a, str>>) -> Self {
486        self.sub_acct = Some(sub_acct.into());
487        self
488    }
489
490    /// Filter by sub-account UID.
491    pub fn sub_uid(mut self, sub_uid: impl Into<Cow<'a, str>>) -> Self {
492        self.sub_uid = Some(sub_uid.into());
493        self
494    }
495
496    /// Return results older than this bill ID.
497    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
498        self.after = Some(after.into());
499        self
500    }
501
502    /// Return results newer than this bill ID.
503    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
504        self.before = Some(before.into());
505        self
506    }
507
508    /// Maximum number of results (default 100, max 100).
509    pub fn limit(mut self, limit: u32) -> Self {
510        self.limit = Some(limit);
511        self
512    }
513}
514
515/// Query parameters for [`SubAccount::get_entrust_subaccount_list`](crate::api::sub_account::SubAccount::get_entrust_subaccount_list).
516#[derive(Debug, Clone, Default, Serialize)]
517#[serde(rename_all = "camelCase")]
518pub struct EntrustSubAccountListRequest<'a> {
519    #[serde(skip_serializing_if = "Option::is_none")]
520    sub_acct: Option<Cow<'a, str>>,
521}