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 /// Retrieve funding-account bills.
140 ///
141 /// `GET /api/v5/asset/bills`. Authenticated.
142 ///
143 /// # Errors
144 ///
145 /// See [`get_currencies`](Self::get_currencies).
146 pub async fn get_bills(
147 &self,
148 request: &FundingBillsRequest,
149 ) -> Result<Vec<FundingBill>, Error> {
150 self.client.get(BILLS, request, true).await
151 }
152
153 /// Create or retrieve a Lightning Network deposit invoice.
154 ///
155 /// `GET /api/v5/asset/deposit-lightning`. Authenticated.
156 ///
157 /// # Errors
158 ///
159 /// See [`get_currencies`](Self::get_currencies).
160 pub async fn get_deposit_lightning(
161 &self,
162 request: &DepositLightningRequest,
163 ) -> Result<Vec<DepositLightning>, Error> {
164 self.client.get(DEPOSIT_LIGHTNING, request, true).await
165 }
166
167 /// Withdraw through the Lightning Network.
168 ///
169 /// `POST /api/v5/asset/withdrawal-lightning`. Authenticated.
170 ///
171 /// # Errors
172 ///
173 /// See [`get_currencies`](Self::get_currencies).
174 pub async fn withdrawal_lightning(
175 &self,
176 request: &WithdrawalLightningRequest,
177 ) -> Result<Vec<WithdrawalLightning>, Error> {
178 self.client.post(WITHDRAWAL_LIGHTNING, request, true).await
179 }
180
181 /// Cancel a withdrawal.
182 ///
183 /// `POST /api/v5/asset/cancel-withdrawal`. Authenticated.
184 ///
185 /// # Errors
186 ///
187 /// See [`get_currencies`](Self::get_currencies).
188 pub async fn cancel_withdrawal(&self, wd_id: &str) -> Result<Vec<WithdrawalResult>, Error> {
189 let body = WithdrawalIdBody { wd_id };
190 self.client.post(CANCEL_WITHDRAWAL, &body, true).await
191 }
192
193 /// Retrieve total asset valuation.
194 ///
195 /// `GET /api/v5/asset/asset-valuation`. Authenticated.
196 ///
197 /// # Errors
198 ///
199 /// See [`get_currencies`](Self::get_currencies).
200 pub async fn get_asset_valuation(
201 &self,
202 ccy: Option<&str>,
203 ) -> Result<Vec<AssetValuation>, Error> {
204 let query = CcyQuery { ccy };
205 self.client.get(ASSET_VALUATION, &query, 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}