Skip to main content

rust_okx/api/funding/
api.rs

1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::transport::Transport;
4
5use super::endpoints::*;
6use super::requests::*;
7use super::responses::*;
8
9/// Accessor for authenticated funding-account and asset endpoints.
10///
11/// Obtain one via [`OkxClient::funding`](crate::OkxClient::funding). All methods
12/// require credentials. Mutating endpoints operate on the funding account, not
13/// the trading account.
14pub struct Funding<'a, T> {
15    client: &'a OkxClient<T>,
16}
17
18impl<'a, T: Transport> Funding<'a, T> {
19    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
20        Self { client }
21    }
22
23    /// Retrieve currency metadata and chain settings.
24    ///
25    /// `GET /api/v5/asset/currencies`. Authenticated.
26    ///
27    /// # Errors
28    ///
29    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) without credentials, [`RestError::Okx`](crate::RestError::Okx) on a
30    /// non-zero OKX code, or transport/decode errors.
31    pub async fn get_currencies(
32        &self,
33        request: &CurrencyRequest<'_>,
34    ) -> Result<Vec<Currency>, Error> {
35        self.client.get(CURRENCIES, request, true).await
36    }
37
38    /// Retrieve funding-account balances.
39    ///
40    /// `GET /api/v5/asset/balances`. Authenticated.
41    ///
42    /// # Errors
43    ///
44    /// See [`get_currencies`](Self::get_currencies).
45    pub async fn get_balances(
46        &self,
47        request: &CurrencyRequest<'_>,
48    ) -> Result<Vec<FundingBalance>, Error> {
49        self.client.get(BALANCES, request, true).await
50    }
51
52    /// Retrieve non-tradable assets.
53    ///
54    /// `GET /api/v5/asset/non-tradable-assets`. Authenticated.
55    ///
56    /// # Errors
57    ///
58    /// See [`get_currencies`](Self::get_currencies).
59    pub async fn get_non_tradable_assets(
60        &self,
61        request: &CurrencyRequest<'_>,
62    ) -> Result<Vec<NonTradableAsset>, Error> {
63        self.client.get(NON_TRADABLE_ASSETS, request, true).await
64    }
65
66    /// Retrieve deposit addresses for a currency.
67    ///
68    /// `GET /api/v5/asset/deposit-address`. Authenticated.
69    ///
70    /// # Errors
71    ///
72    /// See [`get_currencies`](Self::get_currencies).
73    pub async fn get_deposit_address(
74        &self,
75        request: &DepositAddressRequest<'_>,
76    ) -> Result<Vec<DepositAddress>, Error> {
77        self.client.get(DEPOSIT_ADDRESS, request, true).await
78    }
79
80    /// Transfer funds between OKX account types.
81    ///
82    /// `POST /api/v5/asset/transfer`. Authenticated.
83    ///
84    /// # Errors
85    ///
86    /// See [`get_currencies`](Self::get_currencies).
87    pub async fn funds_transfer(
88        &self,
89        request: &FundsTransferRequest<'_>,
90    ) -> Result<Vec<TransferResult>, Error> {
91        self.client.post(TRANSFER, request, true).await
92    }
93
94    /// Retrieve the state of a funds transfer.
95    ///
96    /// `GET /api/v5/asset/transfer-state`. Authenticated.
97    ///
98    /// # Errors
99    ///
100    /// See [`get_currencies`](Self::get_currencies).
101    pub async fn transfer_state(
102        &self,
103        request: &TransferStateRequest<'_>,
104    ) -> Result<Vec<TransferState>, Error> {
105        self.client.get(TRANSFER_STATE, request, true).await
106    }
107
108    /// Withdraw funds from OKX.
109    ///
110    /// `POST /api/v5/asset/withdrawal`. Authenticated. This is a real asset
111    /// movement endpoint; build requests deliberately with
112    /// [`WithdrawalRequest::new`].
113    ///
114    /// # Errors
115    ///
116    /// See [`get_currencies`](Self::get_currencies).
117    pub async fn withdrawal(
118        &self,
119        request: &WithdrawalRequest<'_>,
120    ) -> Result<Vec<WithdrawalResult>, Error> {
121        self.client.post(WITHDRAWAL, request, true).await
122    }
123
124    /// Retrieve deposit history.
125    ///
126    /// `GET /api/v5/asset/deposit-history`. Authenticated.
127    ///
128    /// # Errors
129    ///
130    /// See [`get_currencies`](Self::get_currencies).
131    pub async fn get_deposit_history(
132        &self,
133        request: &DepositHistoryRequest<'_>,
134    ) -> Result<Vec<DepositRecord>, Error> {
135        self.client.get(DEPOSIT_HISTORY, request, true).await
136    }
137
138    /// Retrieve funding-account bills.
139    ///
140    /// `GET /api/v5/asset/bills`. Authenticated.
141    ///
142    /// # Errors
143    ///
144    /// See [`get_currencies`](Self::get_currencies).
145    pub async fn get_bills(
146        &self,
147        request: &FundingBillsRequest<'_>,
148    ) -> Result<Vec<FundingBill>, Error> {
149        self.client.get(BILLS, request, true).await
150    }
151
152    /// Create or retrieve a Lightning Network deposit invoice.
153    ///
154    /// `GET /api/v5/asset/deposit-lightning`. Authenticated.
155    ///
156    /// # Errors
157    ///
158    /// See [`get_currencies`](Self::get_currencies).
159    pub async fn get_deposit_lightning(
160        &self,
161        request: &DepositLightningRequest<'_>,
162    ) -> Result<Vec<DepositLightning>, Error> {
163        self.client.get(DEPOSIT_LIGHTNING, request, true).await
164    }
165
166    /// Withdraw through the Lightning Network.
167    ///
168    /// `POST /api/v5/asset/withdrawal-lightning`. Authenticated.
169    ///
170    /// # Errors
171    ///
172    /// See [`get_currencies`](Self::get_currencies).
173    pub async fn withdrawal_lightning(
174        &self,
175        request: &WithdrawalLightningRequest<'_>,
176    ) -> Result<Vec<WithdrawalLightning>, Error> {
177        self.client.post(WITHDRAWAL_LIGHTNING, request, true).await
178    }
179
180    /// Cancel a withdrawal.
181    ///
182    /// `POST /api/v5/asset/cancel-withdrawal`. Authenticated.
183    ///
184    /// # Errors
185    ///
186    /// See [`get_currencies`](Self::get_currencies).
187    pub async fn cancel_withdrawal(
188        &self,
189        request: &CancelWithdrawalRequest<'_>,
190    ) -> Result<Vec<WithdrawalResult>, Error> {
191        self.client.post(CANCEL_WITHDRAWAL, request, true).await
192    }
193
194    /// Retrieve total asset valuation.
195    ///
196    /// `GET /api/v5/asset/asset-valuation`. Authenticated.
197    ///
198    /// # Errors
199    ///
200    /// See [`get_currencies`](Self::get_currencies).
201    pub async fn get_asset_valuation(
202        &self,
203        request: &CurrencyRequest<'_>,
204    ) -> Result<Vec<AssetValuation>, Error> {
205        self.client.get(ASSET_VALUATION, request, true).await
206    }
207
208    /// Retrieve deposit/withdrawal status.
209    ///
210    /// `GET /api/v5/asset/deposit-withdraw-status`. Authenticated.
211    ///
212    /// # Errors
213    ///
214    /// See [`get_currencies`](Self::get_currencies).
215    pub async fn get_deposit_withdraw_status(
216        &self,
217        request: &DepositWithdrawStatusRequest<'_>,
218    ) -> Result<Vec<DepositWithdrawStatus>, Error> {
219        self.client
220            .get(DEPOSIT_WITHDRAW_STATUS, request, true)
221            .await
222    }
223
224    /// Retrieve withdrawal history.
225    ///
226    /// `GET /api/v5/asset/withdrawal-history`. Authenticated.
227    ///
228    /// # Errors
229    ///
230    /// See [`get_currencies`](Self::get_currencies).
231    pub async fn get_withdrawal_history(
232        &self,
233        request: &WithdrawalHistoryRequest<'_>,
234    ) -> Result<Vec<WithdrawalRecord>, Error> {
235        self.client.get(WITHDRAWAL_HISTORY, request, true).await
236    }
237}