Skip to main content

rust_okx/api/trade/requests/
advanced.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5/// Request body for `POST /api/v5/trade/easy-convert`.
6#[derive(Debug, Clone, Serialize)]
7pub struct EasyConvertRequest<'a> {
8    #[serde(rename = "fromCcy")]
9    from_ccy: Vec<Cow<'a, str>>,
10    #[serde(rename = "toCcy")]
11    to_ccy: Cow<'a, str>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    source: Option<Cow<'a, str>>,
14}
15
16impl<'a> EasyConvertRequest<'a> {
17    /// Create an easy-convert request with one to five source currencies.
18    pub fn new<I, S>(from_ccy: I, to_ccy: impl Into<Cow<'a, str>>) -> Self
19    where
20        I: IntoIterator<Item = S>,
21        S: Into<Cow<'a, str>>,
22    {
23        Self {
24            from_ccy: from_ccy.into_iter().map(Into::into).collect(),
25            to_ccy: to_ccy.into(),
26            source: None,
27        }
28    }
29
30    /// Set the funding source: `1` for trading or `2` for funding.
31    pub fn source(mut self, value: impl Into<Cow<'a, str>>) -> Self {
32        self.source = Some(value.into());
33        self
34    }
35}
36
37/// Query parameters for easy-convert history.
38#[derive(Debug, Clone, Default, Serialize)]
39pub struct EasyConvertHistoryRequest<'a> {
40    #[serde(skip_serializing_if = "Option::is_none")]
41    after: Option<Cow<'a, str>>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    before: Option<Cow<'a, str>>,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    limit: Option<u32>,
46}
47
48impl<'a> EasyConvertHistoryRequest<'a> {
49    /// Create an empty history query.
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Return records earlier than this millisecond timestamp.
55    pub fn after(mut self, value: impl Into<Cow<'a, str>>) -> Self {
56        self.after = Some(value.into());
57        self
58    }
59
60    /// Return records newer than this millisecond timestamp.
61    pub fn before(mut self, value: impl Into<Cow<'a, str>>) -> Self {
62        self.before = Some(value.into());
63        self
64    }
65
66    /// Set the number of results, from 1 through 100.
67    pub fn limit(mut self, value: u32) -> Self {
68        self.limit = Some(value);
69        self
70    }
71}
72
73/// Query parameters for one-click-repay currency-list endpoints.
74#[derive(Debug, Clone, Default, Serialize)]
75pub struct OneClickRepayCurrencyListRequest<'a> {
76    #[serde(rename = "debtType", skip_serializing_if = "Option::is_none")]
77    debt_type: Option<Cow<'a, str>>,
78}
79
80impl<'a> OneClickRepayCurrencyListRequest<'a> {
81    /// Create an unfiltered currency-list query.
82    pub fn new() -> Self {
83        Self::default()
84    }
85
86    /// Filter debt by `cross` or `isolated` type.
87    pub fn debt_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
88        self.debt_type = Some(value.into());
89        self
90    }
91}
92
93/// One or more debt currencies accepted by the legacy and v2 repay APIs.
94#[derive(Debug, Clone, Serialize)]
95#[serde(untagged)]
96enum DebtCurrencySelection<'a> {
97    One(Cow<'a, str>),
98    Many(Vec<Cow<'a, str>>),
99}
100
101/// Request body shared by one-click-repay v1 and v2 endpoints.
102#[derive(Debug, Clone, Serialize)]
103pub struct OneClickRepayRequest<'a> {
104    #[serde(rename = "debtCcy")]
105    debt_ccy: DebtCurrencySelection<'a>,
106    #[serde(rename = "repayCcy", skip_serializing_if = "Option::is_none")]
107    repay_ccy: Option<Cow<'a, str>>,
108    #[serde(rename = "repayCcyList", skip_serializing_if = "Option::is_none")]
109    repay_ccy_list: Option<Vec<Cow<'a, str>>>,
110}
111
112impl<'a> OneClickRepayRequest<'a> {
113    /// Build the legacy v1 request (`debtCcy` array + one `repayCcy`).
114    pub fn new<I, S>(debt_ccy: I, repay_ccy: impl Into<Cow<'a, str>>) -> Self
115    where
116        I: IntoIterator<Item = S>,
117        S: Into<Cow<'a, str>>,
118    {
119        Self {
120            debt_ccy: DebtCurrencySelection::Many(debt_ccy.into_iter().map(Into::into).collect()),
121            repay_ccy: Some(repay_ccy.into()),
122            repay_ccy_list: None,
123        }
124    }
125
126    /// Build the v2 request (`debtCcy` string + prioritized `repayCcyList`).
127    pub fn v2<I, S>(debt_ccy: impl Into<Cow<'a, str>>, repay_ccy_list: I) -> Self
128    where
129        I: IntoIterator<Item = S>,
130        S: Into<Cow<'a, str>>,
131    {
132        Self {
133            debt_ccy: DebtCurrencySelection::One(debt_ccy.into()),
134            repay_ccy: None,
135            repay_ccy_list: Some(repay_ccy_list.into_iter().map(Into::into).collect()),
136        }
137    }
138}
139
140/// Query parameters for one-click-repay history endpoints.
141#[derive(Debug, Clone, Default, Serialize)]
142pub struct OneClickRepayHistoryRequest<'a> {
143    #[serde(skip_serializing_if = "Option::is_none")]
144    after: Option<Cow<'a, str>>,
145    #[serde(skip_serializing_if = "Option::is_none")]
146    before: Option<Cow<'a, str>>,
147    #[serde(skip_serializing_if = "Option::is_none")]
148    limit: Option<u32>,
149}
150
151impl<'a> OneClickRepayHistoryRequest<'a> {
152    /// Create an empty history query.
153    pub fn new() -> Self {
154        Self::default()
155    }
156
157    /// Return records earlier than this millisecond timestamp.
158    pub fn after(mut self, value: impl Into<Cow<'a, str>>) -> Self {
159        self.after = Some(value.into());
160        self
161    }
162
163    /// Return records newer than this millisecond timestamp.
164    pub fn before(mut self, value: impl Into<Cow<'a, str>>) -> Self {
165        self.before = Some(value.into());
166        self
167    }
168
169    /// Set the number of results, from 1 through 100.
170    pub fn limit(mut self, value: u32) -> Self {
171        self.limit = Some(value);
172        self
173    }
174}