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