Skip to main content

rust_okx/api/finance/requests/
staking.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5/// Query parameters for `GET /api/v5/finance/staking-defi/offers`.
6#[derive(Debug, Clone, Default, Serialize)]
7pub struct StakingDefiOffersRequest<'a> {
8    #[serde(rename = "productId", skip_serializing_if = "Option::is_none")]
9    product_id: Option<Cow<'a, str>>,
10    #[serde(rename = "protocolType", skip_serializing_if = "Option::is_none")]
11    protocol_type: Option<Cow<'a, str>>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    ccy: Option<Cow<'a, str>>,
14}
15
16impl<'a> StakingDefiOffersRequest<'a> {
17    /// Create an unfiltered offers query.
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Restrict the response to one product ID.
23    pub fn product_id(mut self, value: impl Into<Cow<'a, str>>) -> Self {
24        self.product_id = Some(value.into());
25        self
26    }
27
28    /// Restrict the response to `staking` or `defi` products.
29    pub fn protocol_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
30        self.protocol_type = Some(value.into());
31        self
32    }
33
34    /// Restrict the response to one investment currency.
35    pub fn currency(mut self, value: impl Into<Cow<'a, str>>) -> Self {
36        self.ccy = Some(value.into());
37        self
38    }
39}
40
41/// Currency and amount invested into one On-chain Earn product.
42#[derive(Debug, Clone, Serialize)]
43pub struct StakingDefiInvestment<'a> {
44    ccy: Cow<'a, str>,
45    amt: Cow<'a, str>,
46}
47
48impl<'a> StakingDefiInvestment<'a> {
49    /// Create one investment item.
50    pub fn new(ccy: impl Into<Cow<'a, str>>, amt: impl Into<Cow<'a, str>>) -> Self {
51        Self {
52            ccy: ccy.into(),
53            amt: amt.into(),
54        }
55    }
56}
57
58/// Request body for `POST /api/v5/finance/staking-defi/purchase`.
59#[derive(Debug, Clone, Serialize)]
60pub struct StakingDefiPurchaseRequest<'a> {
61    #[serde(rename = "productId")]
62    product_id: Cow<'a, str>,
63    #[serde(rename = "investData")]
64    invest_data: Vec<StakingDefiInvestment<'a>>,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    term: Option<Cow<'a, str>>,
67    #[serde(skip_serializing_if = "Option::is_none")]
68    tag: Option<Cow<'a, str>>,
69}
70
71impl<'a> StakingDefiPurchaseRequest<'a> {
72    /// Create a purchase with at least one currency/amount item.
73    pub fn new(
74        product_id: impl Into<Cow<'a, str>>,
75        invest_data: Vec<StakingDefiInvestment<'a>>,
76    ) -> Self {
77        Self {
78            product_id: product_id.into(),
79            invest_data,
80            term: None,
81            tag: None,
82        }
83    }
84
85    /// Set the fixed product term when required by the selected product.
86    pub fn term(mut self, value: impl Into<Cow<'a, str>>) -> Self {
87        self.term = Some(value.into());
88        self
89    }
90
91    /// Set a case-sensitive ASCII alphanumeric tag of at most 16 characters.
92    pub fn tag(mut self, value: impl Into<Cow<'a, str>>) -> Self {
93        self.tag = Some(value.into());
94        self
95    }
96}
97
98/// Request body for `POST /api/v5/finance/staking-defi/redeem`.
99#[derive(Debug, Clone, Serialize)]
100pub struct StakingDefiRedeemRequest<'a> {
101    #[serde(rename = "ordId")]
102    ord_id: Cow<'a, str>,
103    #[serde(rename = "protocolType")]
104    protocol_type: Cow<'a, str>,
105    #[serde(rename = "allowEarlyRedeem", skip_serializing_if = "Option::is_none")]
106    allow_early_redeem: Option<bool>,
107}
108
109impl<'a> StakingDefiRedeemRequest<'a> {
110    /// Create a redemption for an existing order.
111    pub fn new(ord_id: impl Into<Cow<'a, str>>, protocol_type: impl Into<Cow<'a, str>>) -> Self {
112        Self {
113            ord_id: ord_id.into(),
114            protocol_type: protocol_type.into(),
115            allow_early_redeem: None,
116        }
117    }
118
119    /// Allow early redemption when the product supports it.
120    pub fn allow_early_redeem(mut self, value: bool) -> Self {
121        self.allow_early_redeem = Some(value);
122        self
123    }
124}
125
126/// Request body for `POST /api/v5/finance/staking-defi/cancel`.
127#[derive(Debug, Clone, Serialize)]
128pub struct StakingDefiCancelRequest<'a> {
129    #[serde(rename = "ordId")]
130    ord_id: Cow<'a, str>,
131    #[serde(rename = "protocolType")]
132    protocol_type: Cow<'a, str>,
133}
134
135impl<'a> StakingDefiCancelRequest<'a> {
136    /// Create a cancellation for a pending On-chain Earn order.
137    pub fn new(ord_id: impl Into<Cow<'a, str>>, protocol_type: impl Into<Cow<'a, str>>) -> Self {
138        Self {
139            ord_id: ord_id.into(),
140            protocol_type: protocol_type.into(),
141        }
142    }
143}
144
145/// Query parameters for `GET /api/v5/finance/staking-defi/orders-active`.
146#[derive(Debug, Clone, Default, Serialize)]
147pub struct StakingDefiActiveOrdersRequest<'a> {
148    #[serde(rename = "productId", skip_serializing_if = "Option::is_none")]
149    product_id: Option<Cow<'a, str>>,
150    #[serde(rename = "protocolType", skip_serializing_if = "Option::is_none")]
151    protocol_type: Option<Cow<'a, str>>,
152    #[serde(skip_serializing_if = "Option::is_none")]
153    ccy: Option<Cow<'a, str>>,
154    #[serde(skip_serializing_if = "Option::is_none")]
155    state: Option<Cow<'a, str>>,
156}
157
158impl<'a> StakingDefiActiveOrdersRequest<'a> {
159    /// Create an unfiltered active-orders query.
160    pub fn new() -> Self {
161        Self::default()
162    }
163
164    /// Filter by product ID.
165    pub fn product_id(mut self, value: impl Into<Cow<'a, str>>) -> Self {
166        self.product_id = Some(value.into());
167        self
168    }
169
170    /// Filter by `staking` or `defi` protocol type.
171    pub fn protocol_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
172        self.protocol_type = Some(value.into());
173        self
174    }
175
176    /// Filter by investment currency.
177    pub fn currency(mut self, value: impl Into<Cow<'a, str>>) -> Self {
178        self.ccy = Some(value.into());
179        self
180    }
181
182    /// Filter by OKX active-order state (`1`, `2`, `8`, `9`, or `13`).
183    pub fn state(mut self, value: impl Into<Cow<'a, str>>) -> Self {
184        self.state = Some(value.into());
185        self
186    }
187}
188
189/// Query parameters for `GET /api/v5/finance/staking-defi/orders-history`.
190#[derive(Debug, Clone, Default, Serialize)]
191pub struct StakingDefiOrderHistoryRequest<'a> {
192    #[serde(rename = "productId", skip_serializing_if = "Option::is_none")]
193    product_id: Option<Cow<'a, str>>,
194    #[serde(rename = "protocolType", skip_serializing_if = "Option::is_none")]
195    protocol_type: Option<Cow<'a, str>>,
196    #[serde(skip_serializing_if = "Option::is_none")]
197    ccy: Option<Cow<'a, str>>,
198    #[serde(skip_serializing_if = "Option::is_none")]
199    after: Option<Cow<'a, str>>,
200    #[serde(skip_serializing_if = "Option::is_none")]
201    before: Option<Cow<'a, str>>,
202    #[serde(skip_serializing_if = "Option::is_none")]
203    limit: Option<u32>,
204}
205
206impl<'a> StakingDefiOrderHistoryRequest<'a> {
207    /// Create an unfiltered order-history query.
208    pub fn new() -> Self {
209        Self::default()
210    }
211
212    /// Filter by product ID.
213    pub fn product_id(mut self, value: impl Into<Cow<'a, str>>) -> Self {
214        self.product_id = Some(value.into());
215        self
216    }
217
218    /// Filter by `staking` or `defi` protocol type.
219    pub fn protocol_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
220        self.protocol_type = Some(value.into());
221        self
222    }
223
224    /// Filter by investment currency.
225    pub fn currency(mut self, value: impl Into<Cow<'a, str>>) -> Self {
226        self.ccy = Some(value.into());
227        self
228    }
229
230    /// Return records before this order-ID cursor.
231    pub fn after(mut self, value: impl Into<Cow<'a, str>>) -> Self {
232        self.after = Some(value.into());
233        self
234    }
235
236    /// Return records after this order-ID cursor.
237    pub fn before(mut self, value: impl Into<Cow<'a, str>>) -> Self {
238        self.before = Some(value.into());
239        self
240    }
241
242    /// Set the result count from 1 through 100.
243    pub fn limit(mut self, value: u32) -> Self {
244        self.limit = Some(value);
245        self
246    }
247}