Skip to main content

rust_okx/api/account/
api.rs

1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::transport::Transport;
4
5use super::endpoints::*;
6use super::requests::*;
7use super::responses::*;
8use crate::model::EmptyRequest;
9
10/// Accessor for the authenticated account endpoints.
11///
12/// Obtain one via [`OkxClient::account`](crate::OkxClient::account). All methods
13/// require credentials; calling them without credentials returns
14/// [`Error::Configuration`].
15pub struct Account<'a, T> {
16    client: &'a OkxClient<T>,
17}
18
19impl<'a, T: Transport> Account<'a, T> {
20    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
21        Self { client }
22    }
23
24    /// Retrieve the trading-account balance.
25    ///
26    /// `GET /api/v5/account/balance`. Authenticated.
27    ///
28    /// # Errors
29    ///
30    /// Returns [`Error::Configuration`] if no credentials are set,
31    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
32    pub async fn get_balance(
33        &self,
34        request: BalanceRequest<'_>,
35    ) -> Result<Vec<AccountBalance>, Error> {
36        self.client.get(BALANCE, &request, true).await
37    }
38
39    /// Retrieve open positions.
40    ///
41    /// `GET /api/v5/account/positions`. Authenticated.
42    ///
43    /// # Errors
44    ///
45    /// Returns [`Error::Configuration`] if no credentials are set,
46    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
47    pub async fn get_positions(
48        &self,
49        request: &PositionsRequest<'_>,
50    ) -> Result<Vec<Position>, Error> {
51        self.client.get(POSITIONS, request, true).await
52    }
53
54    /// Retrieve account position risk.
55    ///
56    /// `GET /api/v5/account/account-position-risk`. Authenticated.
57    ///
58    /// # Errors
59    ///
60    /// Returns [`Error::Configuration`] if no credentials are set,
61    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
62    pub async fn get_position_risk(
63        &self,
64        request: &PositionRiskRequest<'_>,
65    ) -> Result<Vec<PositionRisk>, Error> {
66        self.client.get(POSITION_RISK, request, true).await
67    }
68
69    /// Retrieve account configuration.
70    ///
71    /// `GET /api/v5/account/config`. Authenticated.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`Error::Configuration`] if no credentials are set,
76    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
77    pub async fn get_account_config(&self) -> Result<Vec<AccountConfig>, Error> {
78        self.client
79            .get(ACCOUNT_CONFIG, &EmptyRequest {}, true)
80            .await
81    }
82
83    /// Retrieve recent account bills.
84    ///
85    /// `GET /api/v5/account/bills`. Authenticated.
86    ///
87    /// # Errors
88    ///
89    /// Returns [`Error::InvalidRequest`] on validation failure,
90    /// [`Error::Configuration`] if no credentials are set,
91    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
92    pub async fn get_account_bills(
93        &self,
94        request: &BillsRequest,
95    ) -> Result<Vec<AccountBill>, Error> {
96        self.client.get(BILLS, request, true).await
97    }
98
99    /// Retrieve archived account bills.
100    ///
101    /// `GET /api/v5/account/bills-archive`. Authenticated.
102    ///
103    /// # Errors
104    ///
105    /// Returns [`Error::InvalidRequest`] on validation failure,
106    /// [`Error::Configuration`] if no credentials are set,
107    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
108    pub async fn get_account_bills_archive(
109        &self,
110        request: &BillsArchiveRequest,
111    ) -> Result<Vec<AccountBill>, Error> {
112        self.client.get(BILLS_ARCHIVE, request, true).await
113    }
114
115    /// Set the account position mode.
116    ///
117    /// `POST /api/v5/account/set-position-mode`. Authenticated.
118    ///
119    /// # Errors
120    ///
121    /// Returns [`Error::Configuration`] if no credentials are set,
122    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
123    pub async fn set_position_mode(
124        &self,
125        request: &SetPositionModeRequest<'_>,
126    ) -> Result<Vec<SetPositionModeResult>, Error> {
127        self.client.post(SET_POSITION_MODE, request, true).await
128    }
129
130    /// Set leverage for an instrument or currency.
131    ///
132    /// `POST /api/v5/account/set-leverage`. Authenticated.
133    ///
134    /// # Errors
135    ///
136    /// Returns [`Error::InvalidRequest`] on validation failure,
137    /// [`Error::Configuration`] if no credentials are set,
138    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
139    pub async fn set_leverage(
140        &self,
141        request: &SetLeverageRequest,
142    ) -> Result<Vec<LeverageInfo>, Error> {
143        self.client.post(SET_LEVERAGE, request, true).await
144    }
145
146    /// Retrieve leverage settings.
147    ///
148    /// `GET /api/v5/account/leverage-info`. Authenticated.
149    ///
150    /// # Errors
151    ///
152    /// Returns [`Error::InvalidRequest`] on validation failure,
153    /// [`Error::Configuration`] if no credentials are set,
154    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
155    pub async fn get_leverage(
156        &self,
157        request: &LeverageRequest,
158    ) -> Result<Vec<LeverageInfo>, Error> {
159        self.client.get(GET_LEVERAGE, request, true).await
160    }
161
162    /// Retrieve maximum tradable size for an instrument.
163    ///
164    /// `GET /api/v5/account/max-size`. Authenticated.
165    ///
166    /// # Errors
167    ///
168    /// Returns [`Error::InvalidRequest`] on validation failure,
169    /// [`Error::Configuration`] if no credentials are set,
170    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
171    pub async fn get_max_order_size(
172        &self,
173        request: &MaxOrderSizeRequest,
174    ) -> Result<Vec<MaxOrderSize>, Error> {
175        self.client.get(MAX_ORDER_SIZE, request, true).await
176    }
177
178    /// Retrieve maximum available size for an instrument.
179    ///
180    /// `GET /api/v5/account/max-avail-size`. Authenticated.
181    ///
182    /// # Errors
183    ///
184    /// Returns [`Error::InvalidRequest`] on validation failure,
185    /// [`Error::Configuration`] if no credentials are set,
186    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
187    pub async fn get_max_avail_size(
188        &self,
189        request: &MaxAvailableSizeRequest,
190    ) -> Result<Vec<MaxAvailableSize>, Error> {
191        self.client.get(MAX_AVAILABLE_SIZE, request, true).await
192    }
193
194    /// Increase or decrease margin for a position.
195    ///
196    /// `POST /api/v5/account/position/margin-balance`. Authenticated.
197    ///
198    /// # Errors
199    ///
200    /// Returns [`Error::InvalidRequest`] on validation failure,
201    /// [`Error::Configuration`] if no credentials are set,
202    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
203    pub async fn adjust_margin(
204        &self,
205        request: &AdjustMarginRequest,
206    ) -> Result<Vec<AdjustMarginResult>, Error> {
207        self.client.post(ADJUST_MARGIN, request, true).await
208    }
209
210    /// Retrieve trade fee rates.
211    ///
212    /// `GET /api/v5/account/trade-fee`. Authenticated.
213    ///
214    /// # Errors
215    ///
216    /// Returns [`Error::InvalidRequest`] on validation failure,
217    /// [`Error::Configuration`] if no credentials are set,
218    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
219    pub async fn get_fee_rates(&self, request: &FeeRatesRequest) -> Result<Vec<FeeRate>, Error> {
220        self.client.get(FEE_RATES, request, true).await
221    }
222
223    /// Retrieve account-available instruments.
224    ///
225    /// `GET /api/v5/account/instruments`. Authenticated.
226    ///
227    /// # Errors
228    ///
229    /// Returns [`Error::InvalidRequest`] on validation failure,
230    /// [`Error::Configuration`] if no credentials are set,
231    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
232    pub async fn get_account_instruments(
233        &self,
234        request: &AccountInstrumentsRequest,
235    ) -> Result<Vec<AccountInstrument>, Error> {
236        self.client.get(ACCOUNT_INSTRUMENTS, request, true).await
237    }
238
239    /// Retrieve the maximum loan amount.
240    ///
241    /// `GET /api/v5/account/max-loan`. Authenticated.
242    ///
243    /// # Errors
244    ///
245    /// Returns [`Error::InvalidRequest`] on validation failure,
246    /// [`Error::Configuration`] if no credentials are set,
247    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
248    pub async fn get_max_loan(&self, request: &MaxLoanRequest) -> Result<Vec<MaxLoan>, Error> {
249        self.client.get(MAX_LOAN, request, true).await
250    }
251
252    /// Retrieve interest-accrued records.
253    ///
254    /// `GET /api/v5/account/interest-accrued`. Authenticated.
255    ///
256    /// # Errors
257    ///
258    /// Returns [`Error::InvalidRequest`] on validation failure,
259    /// [`Error::Configuration`] if no credentials are set,
260    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
261    pub async fn get_interest_accrued(
262        &self,
263        request: &InterestAccruedRequest,
264    ) -> Result<Vec<InterestAccrued>, Error> {
265        self.client.get(INTEREST_ACCRUED, request, true).await
266    }
267
268    /// Retrieve interest rates.
269    ///
270    /// `GET /api/v5/account/interest-rate`. Authenticated.
271    ///
272    /// # Errors
273    ///
274    /// Returns [`Error::Configuration`] if no credentials are set,
275    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
276    pub async fn get_interest_rate(
277        &self,
278        request: BalanceRequest<'_>,
279    ) -> Result<Vec<InterestRate>, Error> {
280        self.client.get(INTEREST_RATE, &request, true).await
281    }
282
283    /// Set the greeks display type.
284    ///
285    /// `POST /api/v5/account/set-greeks`. Authenticated.
286    ///
287    /// # Errors
288    ///
289    /// Returns [`Error::Configuration`] if no credentials are set,
290    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
291    pub async fn set_greeks(
292        &self,
293        request: &SetGreeksRequest<'_>,
294    ) -> Result<Vec<SetGreeksResult>, Error> {
295        self.client.post(SET_GREEKS, request, true).await
296    }
297
298    /// Set isolated margin transfer mode.
299    ///
300    /// `POST /api/v5/account/set-isolated-mode`. Authenticated.
301    ///
302    /// # Errors
303    ///
304    /// Returns [`Error::Configuration`] if no credentials are set,
305    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
306    pub async fn set_isolated_mode(
307        &self,
308        request: &SetIsolatedModeRequest<'_>,
309    ) -> Result<Vec<SetIsolatedModeResult>, Error> {
310        self.client.post(SET_ISOLATED_MODE, request, true).await
311    }
312
313    /// Retrieve maximum withdrawal amounts.
314    ///
315    /// `GET /api/v5/account/max-withdrawal`. Authenticated.
316    ///
317    /// # Errors
318    ///
319    /// Returns [`Error::Configuration`] if no credentials are set,
320    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
321    pub async fn get_max_withdrawal(
322        &self,
323        request: BalanceRequest<'_>,
324    ) -> Result<Vec<MaxWithdrawal>, Error> {
325        self.client.get(MAX_WITHDRAWAL, &request, true).await
326    }
327
328    /// Retrieve borrowing rate and limit information.
329    ///
330    /// `GET /api/v5/account/interest-limits`. Authenticated.
331    ///
332    /// # Errors
333    ///
334    /// Returns [`Error::InvalidRequest`] on validation failure,
335    /// [`Error::Configuration`] if no credentials are set,
336    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
337    pub async fn get_interest_limits(
338        &self,
339        request: &InterestLimitsRequest,
340    ) -> Result<Vec<InterestLimit>, Error> {
341        self.client.get(INTEREST_LIMITS, request, true).await
342    }
343
344    /// Calculate simulated margin information.
345    ///
346    /// `POST /api/v5/account/simulated_margin`. Authenticated.
347    ///
348    /// # Errors
349    ///
350    /// Returns [`Error::InvalidRequest`] on validation failure,
351    /// [`Error::Configuration`] if no credentials are set,
352    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
353    pub async fn get_simulated_margin(
354        &self,
355        request: &SimulatedMarginRequest,
356    ) -> Result<Vec<SimulatedMargin>, Error> {
357        self.client.post(SIMULATED_MARGIN, request, true).await
358    }
359
360    /// Retrieve greeks.
361    ///
362    /// `GET /api/v5/account/greeks`. Authenticated.
363    ///
364    /// # Errors
365    ///
366    /// Returns [`Error::Configuration`] if no credentials are set,
367    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
368    pub async fn get_greeks(&self, request: BalanceRequest<'_>) -> Result<Vec<Greek>, Error> {
369        self.client.get(GREEKS, &request, true).await
370    }
371
372    /// Retrieve position history.
373    ///
374    /// `GET /api/v5/account/positions-history`. Authenticated.
375    ///
376    /// # Errors
377    ///
378    /// Returns [`Error::InvalidRequest`] on validation failure,
379    /// [`Error::Configuration`] if no credentials are set,
380    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
381    pub async fn get_positions_history(
382        &self,
383        request: &PositionsHistoryRequest,
384    ) -> Result<Vec<PositionHistory>, Error> {
385        self.client.get(POSITIONS_HISTORY, request, true).await
386    }
387
388    /// Retrieve account position tiers.
389    ///
390    /// `GET /api/v5/account/position-tiers`. Authenticated.
391    ///
392    /// # Errors
393    ///
394    /// Returns [`Error::InvalidRequest`] on validation failure,
395    /// [`Error::Configuration`] if no credentials are set,
396    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
397    pub async fn get_account_position_tiers(
398        &self,
399        request: &AccountPositionTiersRequest,
400    ) -> Result<Vec<AccountPositionTier>, Error> {
401        self.client.get(ACCOUNT_POSITION_TIERS, request, true).await
402    }
403
404    /// Retrieve the account risk state.
405    ///
406    /// `GET /api/v5/account/risk-state`. Authenticated.
407    ///
408    /// # Errors
409    ///
410    /// Returns [`Error::Configuration`] if no credentials are set,
411    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
412    pub async fn get_risk_state(&self) -> Result<Vec<RiskState>, Error> {
413        self.client.get(RISK_STATE, &EmptyRequest {}, true).await
414    }
415
416    /// Set account auto-loan mode.
417    ///
418    /// `POST /api/v5/account/set-auto-loan`. Authenticated.
419    ///
420    /// # Errors
421    ///
422    /// Returns [`Error::Configuration`] if no credentials are set,
423    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
424    pub async fn set_auto_loan(
425        &self,
426        request: &SetAutoLoanRequest,
427    ) -> Result<Vec<SetAutoLoanResult>, Error> {
428        self.client.post(SET_AUTO_LOAN, request, true).await
429    }
430
431    /// Set the account level.
432    ///
433    /// `POST /api/v5/account/set-account-level`. Authenticated.
434    ///
435    /// # Errors
436    ///
437    /// Returns [`Error::Configuration`] if no credentials are set,
438    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
439    pub async fn set_account_level(
440        &self,
441        request: &SetAccountLevelRequest<'_>,
442    ) -> Result<Vec<SetAccountLevelResult>, Error> {
443        self.client.post(SET_ACCOUNT_LEVEL, request, true).await
444    }
445
446    /// Activate option trading.
447    ///
448    /// `POST /api/v5/account/activate-option`. Authenticated.
449    ///
450    /// # Errors
451    ///
452    /// Returns [`Error::Configuration`] if no credentials are set,
453    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
454    pub async fn activate_option(&self) -> Result<Vec<ActivateOptionResult>, Error> {
455        self.client
456            .post(ACTIVATE_OPTION, &EmptyRequest {}, true)
457            .await
458    }
459
460    /// Build simulated positions and equity.
461    ///
462    /// `POST /api/v5/account/position-builder`. Authenticated.
463    ///
464    /// # Errors
465    ///
466    /// Returns [`Error::InvalidRequest`] on validation failure,
467    /// [`Error::Configuration`] if no credentials are set,
468    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
469    pub async fn position_builder(
470        &self,
471        request: &PositionBuilderRequest,
472    ) -> Result<Vec<PositionBuilderResult>, Error> {
473        self.client.post(POSITION_BUILDER, request, true).await
474    }
475
476    /// Manually borrow or repay spot liabilities.
477    ///
478    /// `POST /api/v5/account/spot-manual-borrow-repay`. Authenticated.
479    ///
480    /// # Errors
481    ///
482    /// Returns [`Error::InvalidRequest`] on validation failure,
483    /// [`Error::Configuration`] if no credentials are set,
484    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
485    pub async fn spot_manual_borrow_repay(
486        &self,
487        request: &SpotManualBorrowRepayRequest,
488    ) -> Result<Vec<SpotBorrowRepayResult>, Error> {
489        self.client
490            .post(SPOT_MANUAL_BORROW_REPAY, request, true)
491            .await
492    }
493
494    /// Set automatic repayment for spot borrow/repay.
495    ///
496    /// `POST /api/v5/account/set-auto-repay`. Authenticated.
497    ///
498    /// # Errors
499    ///
500    /// Returns [`Error::InvalidRequest`] on validation failure,
501    /// [`Error::Configuration`] if no credentials are set,
502    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
503    pub async fn set_auto_repay(
504        &self,
505        request: &SetAutoRepayRequest,
506    ) -> Result<Vec<SetAutoRepayResult>, Error> {
507        self.client.post(SET_AUTO_REPAY, request, true).await
508    }
509
510    /// Retrieve spot borrow/repay history.
511    ///
512    /// `GET /api/v5/account/spot-borrow-repay-history`. Authenticated.
513    ///
514    /// # Errors
515    ///
516    /// Returns [`Error::InvalidRequest`] on validation failure,
517    /// [`Error::Configuration`] if no credentials are set,
518    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
519    pub async fn get_spot_borrow_repay_history(
520        &self,
521        request: &SpotBorrowRepayHistoryRequest,
522    ) -> Result<Vec<SpotBorrowRepayHistory>, Error> {
523        self.client
524            .get(SPOT_BORROW_REPAY_HISTORY, request, true)
525            .await
526    }
527
528    /// Set automatic earn for the account.
529    ///
530    /// `POST /api/v5/account/set-auto-earn`. Authenticated.
531    ///
532    /// # Errors
533    ///
534    /// Returns [`Error::InvalidRequest`] on validation failure,
535    /// [`Error::Configuration`] if no credentials are set,
536    /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
537    pub async fn set_auto_earn(
538        &self,
539        request: &SetAutoEarnRequest,
540    ) -> Result<Vec<SetAutoEarnResult>, Error> {
541        self.client.post(SET_AUTO_EARN, request, true).await
542    }
543}