Skip to main content

rust_okx/api/finance/
api.rs

1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::model::RequestParams;
4use crate::transport::Transport;
5
6use super::endpoints::*;
7use super::internal::optional_ccy;
8use super::requests::*;
9use super::responses::*;
10/// Accessor for OKX finance endpoint groups.
11///
12/// Obtain one via [`OkxClient::finance`](crate::OkxClient::finance).
13pub struct Finance<'a, T> {
14    client: &'a OkxClient<T>,
15}
16
17impl<'a, T: Transport> Finance<'a, T> {
18    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
19        Self { client }
20    }
21
22    /// Access Savings endpoints.
23    pub fn savings(&self) -> Savings<'_, T> {
24        Savings {
25            client: self.client,
26        }
27    }
28
29    /// Access Staking/DeFi endpoints.
30    pub fn staking_defi(&self) -> StakingDefi<'_, T> {
31        StakingDefi {
32            client: self.client,
33        }
34    }
35
36    /// Access ETH staking endpoints.
37    pub fn eth_staking(&self) -> EthStaking<'_, T> {
38        EthStaking {
39            client: self.client,
40        }
41    }
42
43    /// Access SOL staking endpoints.
44    pub fn sol_staking(&self) -> SolStaking<'_, T> {
45        SolStaking {
46            client: self.client,
47        }
48    }
49
50    /// Access Flexible Loan endpoints.
51    pub fn flexible_loan(&self) -> FlexibleLoan<'_, T> {
52        FlexibleLoan {
53            client: self.client,
54        }
55    }
56}
57
58/// Accessor for Savings endpoints.
59pub struct Savings<'a, T> {
60    client: &'a OkxClient<T>,
61}
62
63impl<T: Transport> Savings<'_, T> {
64    /// Retrieve savings balances.
65    ///
66    /// # Errors
67    ///
68    /// Returns authentication, API, transport, or decode errors.
69    pub async fn get_saving_balance(&self, ccy: Option<&str>) -> Result<Vec<SavingBalance>, Error> {
70        let query = optional_ccy(ccy);
71        self.client.get(SAVINGS_BALANCE, &query, true).await
72    }
73
74    /// Purchase or redeem savings.
75    ///
76    /// # Errors
77    ///
78    /// Returns authentication, API, transport, or decode errors.
79    pub async fn purchase_redemption(
80        &self,
81        request: &SavingsPurchaseRedemptionRequest,
82    ) -> Result<Vec<SavingsPurchaseRedemptionResult>, Error> {
83        self.client
84            .post(SAVINGS_PURCHASE_REDEMPT, request, true)
85            .await
86    }
87
88    /// Set the savings lending rate.
89    ///
90    /// # Errors
91    ///
92    /// Returns authentication, API, transport, or decode errors.
93    pub async fn set_lending_rate(
94        &self,
95        ccy: &str,
96        rate: &str,
97    ) -> Result<Vec<SetLendingRateResult>, Error> {
98        let body = RequestParams::new().param("ccy", ccy).param("rate", rate);
99        self.client
100            .post(SAVINGS_SET_LENDING_RATE, &body, true)
101            .await
102    }
103
104    /// Retrieve lending history.
105    ///
106    /// # Errors
107    ///
108    /// Returns authentication, API, transport, or decode errors.
109    pub async fn get_lending_history(
110        &self,
111        request: &FinanceHistoryRequest,
112    ) -> Result<Vec<LendingHistory>, Error> {
113        self.client
114            .get(SAVINGS_LENDING_HISTORY, request, true)
115            .await
116    }
117
118    /// Retrieve public borrow history.
119    ///
120    /// # Errors
121    ///
122    /// Returns API, transport, or decode errors.
123    pub async fn get_public_borrow_history(
124        &self,
125        request: &FinanceHistoryRequest,
126    ) -> Result<Vec<PublicBorrowHistory>, Error> {
127        self.client
128            .get(SAVINGS_PUBLIC_BORROW_HISTORY, request, false)
129            .await
130    }
131
132    /// Retrieve public borrow info.
133    ///
134    /// # Errors
135    ///
136    /// Returns API, transport, or decode errors.
137    pub async fn get_public_borrow_info(
138        &self,
139        ccy: Option<&str>,
140    ) -> Result<Vec<PublicBorrowInfo>, Error> {
141        let query = optional_ccy(ccy);
142        self.client
143            .get(SAVINGS_PUBLIC_BORROW_INFO, &query, false)
144            .await
145    }
146}
147
148/// Accessor for Staking/DeFi endpoints.
149pub struct StakingDefi<'a, T> {
150    client: &'a OkxClient<T>,
151}
152
153impl<T: Transport> StakingDefi<'_, T> {
154    /// Retrieve Staking/DeFi offers.
155    ///
156    /// # Errors
157    ///
158    /// Returns API, transport, or decode errors.
159    pub async fn get_offers(
160        &self,
161        request: &StakingDefiOffersRequest,
162    ) -> Result<Vec<StakingDefiOffer>, Error> {
163        self.client.get(STAKING_DEFI_OFFERS, request, false).await
164    }
165
166    /// Purchase a Staking/DeFi product.
167    ///
168    /// # Errors
169    ///
170    /// Returns authentication, API, transport, or decode errors.
171    pub async fn purchase(
172        &self,
173        request: &StakingDefiPurchaseRequest,
174    ) -> Result<Vec<StakingDefiOrder>, Error> {
175        self.client.post(STAKING_DEFI_PURCHASE, request, true).await
176    }
177
178    /// Redeem a Staking/DeFi order.
179    ///
180    /// # Errors
181    ///
182    /// Returns authentication, API, transport, or decode errors.
183    pub async fn redeem(
184        &self,
185        request: &StakingDefiRedeemRequest,
186    ) -> Result<Vec<StakingDefiOrder>, Error> {
187        self.client.post(STAKING_DEFI_REDEEM, request, true).await
188    }
189
190    /// Cancel a Staking/DeFi order.
191    ///
192    /// # Errors
193    ///
194    /// Returns authentication, API, transport, or decode errors.
195    pub async fn cancel(
196        &self,
197        request: &StakingDefiCancelRequest,
198    ) -> Result<Vec<StakingDefiOrder>, Error> {
199        self.client.post(STAKING_DEFI_CANCEL, request, true).await
200    }
201
202    /// Retrieve active Staking/DeFi orders.
203    ///
204    /// # Errors
205    ///
206    /// Returns authentication, API, transport, or decode errors.
207    pub async fn get_active_orders(
208        &self,
209        request: &StakingDefiOrdersRequest,
210    ) -> Result<Vec<StakingDefiOrder>, Error> {
211        self.client
212            .get(STAKING_DEFI_ACTIVE_ORDERS, request, true)
213            .await
214    }
215
216    /// Retrieve Staking/DeFi order history.
217    ///
218    /// # Errors
219    ///
220    /// Returns authentication, API, transport, or decode errors.
221    pub async fn get_orders_history(
222        &self,
223        request: &StakingDefiOrdersRequest,
224    ) -> Result<Vec<StakingDefiOrder>, Error> {
225        self.client
226            .get(STAKING_DEFI_ORDERS_HISTORY, request, true)
227            .await
228    }
229}
230
231/// Accessor for ETH staking endpoints.
232pub struct EthStaking<'a, T> {
233    client: &'a OkxClient<T>,
234}
235
236impl<T: Transport> EthStaking<'_, T> {
237    /// Retrieve ETH staking product info.
238    ///
239    /// # Errors
240    ///
241    /// Returns API, transport, or decode errors.
242    pub async fn product_info(&self) -> Result<Vec<StakingProductInfo>, Error> {
243        self.client
244            .get(ETH_PRODUCT_INFO, &RequestParams::new(), false)
245            .await
246    }
247
248    /// Purchase ETH staking.
249    ///
250    /// # Errors
251    ///
252    /// Returns authentication, API, transport, or decode errors.
253    pub async fn purchase(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
254        let body = RequestParams::new().param("amt", amt);
255        self.client.post(ETH_PURCHASE, &body, true).await
256    }
257
258    /// Redeem ETH staking.
259    ///
260    /// # Errors
261    ///
262    /// Returns authentication, API, transport, or decode errors.
263    pub async fn redeem(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
264        let body = RequestParams::new().param("amt", amt);
265        self.client.post(ETH_REDEEM, &body, true).await
266    }
267
268    /// Retrieve ETH staking balance.
269    ///
270    /// # Errors
271    ///
272    /// Returns authentication, API, transport, or decode errors.
273    pub async fn balance(&self) -> Result<Vec<StakingBalance>, Error> {
274        self.client
275            .get(ETH_BALANCE, &RequestParams::new(), true)
276            .await
277    }
278
279    /// Retrieve ETH staking purchase/redeem history.
280    ///
281    /// # Errors
282    ///
283    /// Returns authentication, API, transport, or decode errors.
284    pub async fn purchase_redeem_history(
285        &self,
286        request: &FinanceHistoryRequest,
287    ) -> Result<Vec<StakingHistory>, Error> {
288        self.client.get(ETH_HISTORY, request, true).await
289    }
290
291    /// Retrieve ETH staking APY history.
292    ///
293    /// # Errors
294    ///
295    /// Returns API, transport, or decode errors.
296    pub async fn apy_history(&self, days: &str) -> Result<Vec<StakingApyHistory>, Error> {
297        let query = RequestParams::new().param("days", days);
298        self.client.get(ETH_APY_HISTORY, &query, false).await
299    }
300}
301
302/// Accessor for SOL staking endpoints.
303pub struct SolStaking<'a, T> {
304    client: &'a OkxClient<T>,
305}
306
307impl<T: Transport> SolStaking<'_, T> {
308    /// Retrieve SOL staking product info.
309    ///
310    /// # Errors
311    ///
312    /// Returns API, transport, or decode errors.
313    pub async fn product_info(&self) -> Result<Vec<StakingProductInfo>, Error> {
314        self.client
315            .get(SOL_PRODUCT_INFO, &RequestParams::new(), false)
316            .await
317    }
318
319    /// Purchase SOL staking.
320    ///
321    /// # Errors
322    ///
323    /// Returns authentication, API, transport, or decode errors.
324    pub async fn purchase(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
325        let body = RequestParams::new().param("amt", amt);
326        self.client.post(SOL_PURCHASE, &body, true).await
327    }
328
329    /// Redeem SOL staking.
330    ///
331    /// # Errors
332    ///
333    /// Returns authentication, API, transport, or decode errors.
334    pub async fn redeem(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
335        let body = RequestParams::new().param("amt", amt);
336        self.client.post(SOL_REDEEM, &body, true).await
337    }
338
339    /// Retrieve SOL staking balance.
340    ///
341    /// # Errors
342    ///
343    /// Returns authentication, API, transport, or decode errors.
344    pub async fn balance(&self) -> Result<Vec<StakingBalance>, Error> {
345        self.client
346            .get(SOL_BALANCE, &RequestParams::new(), true)
347            .await
348    }
349
350    /// Retrieve SOL staking purchase/redeem history.
351    ///
352    /// # Errors
353    ///
354    /// Returns authentication, API, transport, or decode errors.
355    pub async fn purchase_redeem_history(
356        &self,
357        request: &FinanceHistoryRequest,
358    ) -> Result<Vec<StakingHistory>, Error> {
359        self.client.get(SOL_HISTORY, request, true).await
360    }
361
362    /// Retrieve SOL staking APY history.
363    ///
364    /// # Errors
365    ///
366    /// Returns API, transport, or decode errors.
367    pub async fn apy_history(&self, days: &str) -> Result<Vec<StakingApyHistory>, Error> {
368        let query = RequestParams::new().param("days", days);
369        self.client.get(SOL_APY_HISTORY, &query, false).await
370    }
371}
372
373/// Accessor for Flexible Loan endpoints.
374pub struct FlexibleLoan<'a, T> {
375    client: &'a OkxClient<T>,
376}
377
378impl<T: Transport> FlexibleLoan<'_, T> {
379    /// Retrieve borrowable currencies.
380    ///
381    /// # Errors
382    ///
383    /// Returns API, transport, or decode errors.
384    pub async fn borrow_currencies(&self) -> Result<Vec<FlexibleLoanCurrency>, Error> {
385        self.client
386            .get(FLEX_BORROW_CURRENCIES, &RequestParams::new(), false)
387            .await
388    }
389
390    /// Retrieve collateral assets.
391    ///
392    /// # Errors
393    ///
394    /// Returns API, transport, or decode errors.
395    pub async fn collateral_assets(
396        &self,
397        ccy: Option<&str>,
398    ) -> Result<Vec<FlexibleLoanCollateralAsset>, Error> {
399        let query = optional_ccy(ccy);
400        self.client.get(FLEX_COLLATERAL_ASSETS, &query, false).await
401    }
402
403    /// Estimate maximum flexible-loan amount.
404    ///
405    /// # Errors
406    ///
407    /// Returns authentication, API, transport, or decode errors.
408    pub async fn max_loan(
409        &self,
410        request: &FlexibleLoanMaxLoanRequest,
411    ) -> Result<Vec<FlexibleLoanMaxLoan>, Error> {
412        self.client.post(FLEX_MAX_LOAN, request, true).await
413    }
414
415    /// Retrieve maximum collateral redeem amount.
416    ///
417    /// # Errors
418    ///
419    /// Returns authentication, API, transport, or decode errors.
420    pub async fn max_collateral_redeem_amount(
421        &self,
422        ccy: Option<&str>,
423    ) -> Result<Vec<FlexibleLoanMaxRedeem>, Error> {
424        let query = optional_ccy(ccy);
425        self.client.get(FLEX_MAX_REDEEM, &query, true).await
426    }
427
428    /// Adjust flexible-loan collateral.
429    ///
430    /// # Errors
431    ///
432    /// Returns authentication, API, transport, or decode errors.
433    pub async fn adjust_collateral(
434        &self,
435        request: &FlexibleLoanAdjustCollateralRequest,
436    ) -> Result<Vec<FlexibleLoanOrder>, Error> {
437        self.client
438            .post(FLEX_ADJUST_COLLATERAL, request, true)
439            .await
440    }
441
442    /// Retrieve flexible-loan info.
443    ///
444    /// # Errors
445    ///
446    /// Returns authentication, API, transport, or decode errors.
447    pub async fn loan_info(&self) -> Result<Vec<FlexibleLoanInfo>, Error> {
448        self.client
449            .get(FLEX_LOAN_INFO, &RequestParams::new(), true)
450            .await
451    }
452
453    /// Retrieve flexible-loan history.
454    ///
455    /// # Errors
456    ///
457    /// Returns authentication, API, transport, or decode errors.
458    pub async fn loan_history(
459        &self,
460        request: &FinanceHistoryRequest,
461    ) -> Result<Vec<FlexibleLoanHistory>, Error> {
462        self.client.get(FLEX_LOAN_HISTORY, request, true).await
463    }
464
465    /// Retrieve flexible-loan accrued interest.
466    ///
467    /// # Errors
468    ///
469    /// Returns authentication, API, transport, or decode errors.
470    pub async fn interest_accrued(
471        &self,
472        request: &FinanceHistoryRequest,
473    ) -> Result<Vec<FlexibleLoanInterest>, Error> {
474        self.client.get(FLEX_INTEREST_ACCRUED, request, true).await
475    }
476}