tokio_binance/client/
withdraw.rs

1use reqwest::{Url, Client};
2use crate::param::{
3    Parameters, 
4};
5use crate::builder::ParamBuilder;
6use crate::types::*;
7
8/// Client for dealing with withdrawals and sub accounts.
9#[derive(Clone)]
10pub struct WithdrawalClient {
11    pub(super) api_key: String,
12    pub(super) secret_key: String,
13    pub(super) url: Url,
14    pub(super) client: Client
15}
16
17impl WithdrawalClient {
18    /// Creates new client instance.
19    /// # Example
20    ///
21    /// ```no_run
22    /// use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
23    /// 
24    /// #[tokio::main]
25    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
26    ///     let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
27    ///     Ok(())
28    /// }
29    /// ```
30    pub fn connect<A, S, U>(api_key: A, secret_key: S, url: U) -> crate::error::Result<Self> 
31    where
32        A: Into<String>,
33        S: Into<String>,
34        U: Into<String>
35    {
36        Ok(Self {
37            api_key: api_key.into(), 
38            secret_key: secret_key.into(),
39            url: url.into().parse::<Url>()?,
40            client: Client::new()
41        })
42    }
43    /// Submit a withdraw request.
44    /// # Example
45    ///
46    /// ```no_run
47    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
48    /// use serde_json::Value;
49    /// 
50    /// # #[tokio::main]
51    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
52    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
53    /// let response = client
54    ///     .withdraw("BNB", "<public-address>", 5.00)
55    ///     //optional: Secondary address identifier for coins like XRP,XMR etc.
56    ///     .with_address_tag("<tag>")
57    ///     // optional: Description of the address.
58    ///     .with_name("<description>")
59    ///     // optional: processing time for request; default is 5000, can't be above 60000.
60    ///     .with_recv_window(8000)
61    ///     //
62    ///     .json::<Value>()
63    ///     .await?;
64    /// # Ok(())
65    /// # }
66    /// ```
67    pub fn withdraw<'a>(&self, asset: &'a str, address: &'a str, amount: f64) -> ParamBuilder<'a, '_, WithdrawParams>{
68        let Self { api_key, secret_key, url, client } = self;
69        let url = url.join("/wapi/v3/withdraw.html").unwrap();
70
71        ParamBuilder::new(
72            Parameters { 
73                asset: Some(asset), 
74                address: Some(address),
75                amount: Some(amount),
76                ..Parameters::default() 
77            },
78            client.post(url),
79            Some(api_key),
80            Some(secret_key)
81        )
82    }
83    /// Fetch deposit history.
84    /// # Example
85    ///
86    /// ```no_run
87    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
88    /// use chrono::{Utc, Duration};
89    /// use serde_json::Value;
90    /// 
91    /// # #[tokio::main]
92    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
93    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
94    /// let end = Utc::now();
95    /// let start = end - Duration::hours(23);
96    /// 
97    /// let response = client
98    ///     .get_deposit_history()
99    ///     // optional: filter by asset; gets all assets by default.
100    ///     .with_asset("BNB")
101    ///     // optional: 0(0:pending,6: credited but cannot withdraw, 1:success)
102    ///     .with_status(1)
103    ///     // optional: get deposits from; gets the most recent deposits by default.
104    ///     .with_start_time(start)
105    ///     // optional: get deposits until; default is now.
106    ///     .with_end_time(end)
107    ///     // optional: processing time for request; default is 5000, can't be above 60000.
108    ///     .with_recv_window(8000)
109    ///     //
110    ///     .json::<Value>()
111    ///     .await?;
112    /// # Ok(())
113    /// # }
114    /// ```
115    pub fn get_deposit_history(&self) -> ParamBuilder<'_, '_, DepositHistoryParams>{
116        let Self { api_key, secret_key, url, client } = self;
117        let url = url.join("/wapi/v3/depositHistory.html").unwrap();
118
119        ParamBuilder::new(
120            Parameters::default(),
121            client.get(url),
122            Some(api_key),
123            Some(secret_key)
124        )
125    }
126    /// Fetch withdraw history.
127    /// # Example
128    ///
129    /// ```no_run
130    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
131    /// use chrono::{Utc, Duration};
132    /// use serde_json::Value;
133    /// 
134    /// # #[tokio::main]
135    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
136    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
137    /// let end = Utc::now();
138    /// let start = end - Duration::hours(23);
139    /// 
140    /// let response = client
141    ///     .get_withdraw_history()
142    ///     // optional: filter by asset; gets all assets by default.
143    ///     .with_asset("BNB")
144    ///     // optional: 0(0:Email Sent,1:Cancelled 2:Awaiting Approval 3:Rejected 4:Processing 5:Failure 6Completed)
145    ///     .with_status(6)
146    ///     // optional: get deposits from; gets the most recent deposits by default.
147    ///     .with_start_time(start)
148    ///     // optional: get deposits until; default is now.
149    ///     .with_end_time(end)
150    ///     // optional: processing time for request; default is 5000, can't be above 60000.
151    ///     .with_recv_window(8000)
152    ///     //
153    ///     .json::<Value>()
154    ///     .await?;
155    /// # Ok(())
156    /// # }
157    /// ```
158    pub fn get_withdraw_history(&self) -> ParamBuilder<'_, '_, WithdrawHistoryParams>{
159        let Self { api_key, secret_key, url, client } = self;
160        let url = url.join("/wapi/v3/withdrawHistory.html").unwrap();
161
162        ParamBuilder::new(
163            Parameters::default(),
164            client.get(url),
165            Some(api_key),
166            Some(secret_key)
167        )
168    }
169    /// Fetch deposit address.
170    /// # Example
171    ///
172    /// ```no_run
173    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
174    /// use serde_json::Value;
175    /// 
176    /// # #[tokio::main]
177    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
178    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
179    /// let response = client
180    ///     .get_deposit_address("BNB")
181    ///     // optional: Boolean.
182    ///     .with_status(true)
183    ///     // optional: processing time for request; default is 5000, can't be above 60000.
184    ///     .with_recv_window(8000)
185    ///     //
186    ///     .json::<Value>()
187    ///     .await?;
188    /// # Ok(())
189    /// # }
190    /// ```
191    pub fn get_deposit_address<'a>(&self, asset: &'a str) -> ParamBuilder<'a, '_, DepositAddressParams>{
192        let Self { api_key, secret_key, url, client } = self;
193        let url = url.join("/wapi/v3/depositAddress.html").unwrap();
194
195        ParamBuilder::new(
196            Parameters { asset: Some(asset), ..Parameters::default() },
197            client.get(url),
198            Some(api_key),
199            Some(secret_key)
200        )
201    }
202    /// Fetch account status detail.
203    /// # Example
204    ///
205    /// ```no_run
206    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
207    /// use serde_json::Value;
208    /// 
209    /// # #[tokio::main]
210    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
211    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
212    /// let response = client
213    ///     .get_account_status()
214    ///     // optional: processing time for request; default is 5000, can't be above 60000.
215    ///     .with_recv_window(8000)
216    ///     //
217    ///     .json::<Value>()
218    ///     .await?;
219    /// # Ok(())
220    /// # }
221    /// ```
222    pub fn get_account_status(&self) -> ParamBuilder<'_, '_, AccountStatusParams>{
223        let Self { api_key, secret_key, url, client } = self;
224        let url = url.join("/wapi/v3/accountStatus.html").unwrap();
225
226        ParamBuilder::new(
227            Parameters::default(),
228            client.get(url),
229            Some(api_key),
230            Some(secret_key)
231        )
232    }
233    /// Fetch system status.
234    /// # Example
235    ///
236    /// ```no_run
237    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
238    /// use serde_json::Value;
239    /// 
240    /// # #[tokio::main]
241    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
242    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
243    /// let response = client
244    ///     .get_system_status()
245    ///     .json::<Value>()
246    ///     .await?;
247    /// # Ok(())
248    /// # }
249    /// ```
250    pub fn get_system_status(&self) -> ParamBuilder<'_, '_, SystemStatusParams>{
251        let Self { api_key, secret_key, url, client } = self;
252        let url = url.join("/wapi/v3/systemStatus.html").unwrap();
253
254        ParamBuilder::new(
255            Parameters::default(),
256            client.get(url),
257            Some(api_key),
258            Some(secret_key)
259        )
260    }
261    /// Fetch account api trading status detail.
262    /// # Example
263    ///
264    /// ```no_run
265    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
266    /// use serde_json::Value;
267    /// 
268    /// # #[tokio::main]
269    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
270    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
271    /// let response = client
272    ///     .get_api_status()
273    ///     // optional: processing time for request; default is 5000, can't be above 60000.
274    ///     .with_recv_window(8000)
275    ///     //
276    ///     .json::<Value>()
277    ///     .await?;
278    /// # Ok(())
279    /// # }
280    /// ```
281    pub fn get_api_status(&self) -> ParamBuilder<'_, '_, ApiStatusParams>{
282        let Self { api_key, secret_key, url, client } = self;
283        let url = url.join("/wapi/v3/apiTradingStatus.html").unwrap();
284
285        ParamBuilder::new(
286            Parameters::default(),
287            client.get(url),
288            Some(api_key),
289            Some(secret_key)
290        )
291    }
292    /// Fetch small amounts of assets exchanged BNB records.
293    /// # Example
294    ///
295    /// ```no_run
296    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
297    /// use serde_json::Value;
298    /// 
299    /// # #[tokio::main]
300    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
301    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
302    /// let response = client
303    ///     .get_dustlog()
304    ///     // optional: processing time for request; default is 5000, can't be above 60000.
305    ///     .with_recv_window(8000)
306    ///     //
307    ///     .json::<Value>()
308    ///     .await?;
309    /// # Ok(())
310    /// # }
311    /// ```
312    pub fn get_dustlog(&self) -> ParamBuilder<'_, '_, DustlogParams>{
313        let Self { api_key, secret_key, url, client } = self;
314        let url = url.join("/wapi/v3/userAssetDribbletLog.html").unwrap();
315
316        ParamBuilder::new(
317            Parameters::default(),
318            client.get(url),
319            Some(api_key),
320            Some(secret_key)
321        )
322    }
323    /// Fetch trade fee.
324    /// # Example
325    ///
326    /// ```no_run
327    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
328    /// use serde_json::Value;
329    /// 
330    /// # #[tokio::main]
331    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
332    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
333    /// let response = client
334    ///     .get_trade_fee()
335    ///     // optional: filter by symbol; gets all symbols by default.
336    ///     .with_symbol("BNBUSDT")
337    ///     // optional: processing time for request; default is 5000, can't be above 60000.
338    ///     .with_recv_window(8000)
339    ///     //
340    ///     .json::<Value>()
341    ///     .await?;
342    /// # Ok(())
343    /// # }
344    /// ```
345    pub fn get_trade_fee(&self) -> ParamBuilder<'_, '_, TradeFeeParams>{
346        let Self { api_key, secret_key, url, client } = self;
347        let url = url.join("/wapi/v3/tradeFee.html").unwrap();
348
349        ParamBuilder::new(
350            Parameters::default(),
351            client.get(url),
352            Some(api_key),
353            Some(secret_key)
354        )
355    }
356    /// Fetch asset detail.
357    /// # Example
358    ///
359    /// ```no_run
360    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
361    /// use serde_json::Value;
362    /// 
363    /// # #[tokio::main]
364    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
365    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
366    /// let response = client
367    ///     .get_asset_detail()
368    ///     // optional: processing time for request; default is 5000, can't be above 60000.
369    ///     .with_recv_window(8000)
370    ///     //
371    ///     .json::<Value>()
372    ///     .await?;
373    /// # Ok(())
374    /// # }
375    /// ```
376    pub fn get_asset_detail(&self) -> ParamBuilder<'_, '_, AssetDetailParams>{
377        let Self { api_key, secret_key, url, client } = self;
378        let url = url.join("/wapi/v3/assetDetail.html").unwrap();
379
380        ParamBuilder::new(
381            Parameters::default(),
382            client.get(url),
383            Some(api_key),
384            Some(secret_key)
385        )
386    }
387    /// Fetch sub account list.
388    /// # Example
389    ///
390    /// ```no_run
391    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
392    /// use serde_json::Value;
393    /// 
394    /// # #[tokio::main]
395    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
396    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
397    /// let response = client
398    ///     .get_sub_accounts()
399    ///     // optional: Sub-account email.
400    ///     .with_email("<email>")
401    ///     // optional: Sub-account status: enabled or disabled.
402    ///     .with_status("enabled")
403    ///     // optional: default value: 1.
404    ///     .with_page(2)
405    ///     // optional: limit the amount of sub accounts; default 500.
406    ///     .with_limit(100)
407    ///     // optional: processing time for request; default is 5000, can't be above 60000.
408    ///     .with_recv_window(8000)
409    ///     //
410    ///     .json::<Value>()
411    ///     .await?;
412    /// # Ok(())
413    /// # }
414    /// ```
415    pub fn get_sub_accounts(&self) -> ParamBuilder<'_, '_, SubAccountParams>{
416        let Self { api_key, secret_key, url, client } = self;
417        let url = url.join("/wapi/v3/sub-account/list.html").unwrap();
418
419        ParamBuilder::new(
420            Parameters::default(),
421            client.get(url),
422            Some(api_key),
423            Some(secret_key)
424        )
425    }
426    /// Fetch transfer history list
427    /// # Example
428    ///
429    /// ```no_run
430    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
431    /// use chrono::{Utc, Duration};
432    /// use serde_json::Value;
433    /// 
434    /// # #[tokio::main]
435    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
436    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
437    /// let end = Utc::now();
438    /// let start = end - Duration::days(99);
439    /// 
440    /// let response = client
441    ///     .get_transfer_history("<email>")
442    ///     // optional: get history from; default return the history with in 100 days
443    ///     .with_start_time(start)
444    ///     // optional: get history until; default is now.
445    ///     .with_end_time(end)
446    ///     // optional: default value: 1.
447    ///     .with_page(2)
448    ///     // optional: limit the amount of sub accounts; default 500.
449    ///     .with_limit(100)
450    ///     // optional: processing time for request; default is 5000, can't be above 60000.
451    ///     .with_recv_window(8000)
452    ///     //
453    ///     .json::<Value>()
454    ///     .await?;
455    /// # Ok(())
456    /// # }
457    /// ```
458    pub fn get_transfer_history<'a>(&self, email: &'a str) -> ParamBuilder<'a, '_, SubAccountTranferParams>{
459        let Self { api_key, secret_key, url, client } = self;
460        let url = url.join("/wapi/v3/sub-account/transfer/history.html").unwrap();
461
462        ParamBuilder::new(
463            Parameters { email: Some(email), ..Parameters::default() },
464            client.get(url),
465            Some(api_key),
466            Some(secret_key)
467        )
468    }
469    /// Execute sub-account transfer.
470    /// # Example
471    ///
472    /// ```no_run
473    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
474    /// use serde_json::Value;
475    /// 
476    /// # #[tokio::main]
477    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
478    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
479    /// let response = client
480    ///     .tranfer_sub_account("<from_email>", "<to_email>", "BNB", 5.00)
481    ///     // optional: processing time for request; default is 5000, can't be above 60000.
482    ///     .with_recv_window(8000)
483    ///     //
484    ///     .json::<Value>()
485    ///     .await?;
486    /// # Ok(())
487    /// # }
488    /// ```
489    pub fn tranfer_sub_account<'a>(&self, 
490        from_email: &'a str, 
491        to_email: &'a str, 
492        asset: &'a str, 
493        amount: f64
494    ) -> ParamBuilder<'a, '_, TransferSubAccountParams>{
495        let Self { api_key, secret_key, url, client } = self;
496        let url = url.join("/wapi/v3/sub-account/transfer.html").unwrap();
497
498        ParamBuilder::new(
499            Parameters { 
500                from_email: Some(from_email), 
501                to_email: Some(to_email), 
502                asset: Some(asset), 
503                amount: Some(amount), 
504                ..Parameters::default() 
505            },
506            client.post(url),
507            Some(api_key),
508            Some(secret_key)
509        )
510    }
511    /// Fetch sub-account assets.
512    /// # Example
513    ///
514    /// ```no_run
515    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
516    /// use serde_json::Value;
517    /// 
518    /// # #[tokio::main]
519    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
520    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
521    /// let response = client
522    ///     .get_sub_account_assets("<email>")
523    ///     // optional: filter by symbol; gets all symbols by default.
524    ///     .with_symbol("BNBUSDT")
525    ///     // optional: processing time for request; default is 5000, can't be above 60000.
526    ///     .with_recv_window(8000)
527    ///     //
528    ///     .json::<Value>()
529    ///     .await?;
530    /// # Ok(())
531    /// # }
532    /// ```
533    pub fn get_sub_account_assets<'a>(&self, email: &'a str) -> ParamBuilder<'a, '_, SubAccountAssetParams>{
534        let Self { api_key, secret_key, url, client } = self;
535        let url = url.join("/wapi/v3/sub-account/assets.html").unwrap();
536
537        ParamBuilder::new(
538            Parameters { email: Some(email), ..Parameters::default() },
539            client.get(url),
540            Some(api_key),
541            Some(secret_key)
542        )
543    }
544    /// Convert dust assets to BNB.
545    /// # Example
546    ///
547    /// ```no_run
548    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
549    /// use serde_json::Value;
550    /// 
551    /// # #[tokio::main]
552    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
553    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
554    /// let response = client
555    ///     // restricted to one asset at a time.
556    ///     .dust_transfer("ETH")
557    ///     // optional: processing time for request; default is 5000, can't be above 60000.
558    ///     .with_recv_window(8000)
559    ///     //
560    ///     .json::<Value>()
561    ///     .await?;
562    /// # Ok(())
563    /// # }
564    /// ```
565    pub fn dust_transfer<'a>(&self, asset: &'a str) -> ParamBuilder<'a, '_, DustTransferParams>{
566        let Self { api_key, secret_key, url, client } = self;
567        let url = url.join("/sapi/v1/asset/dust").unwrap();
568
569        ParamBuilder::new(
570            Parameters { asset: Some(asset), ..Parameters::default() },
571            client.post(url),
572            Some(api_key),
573            Some(secret_key)
574        )
575    }
576    /// Query asset dividend record.
577    /// # Example
578    ///
579    /// ```no_run
580    /// # use tokio_binance::{WithdrawalClient, BINANCE_US_URL};
581    /// use chrono::{Utc, Duration};
582    /// use serde_json::Value;
583    /// 
584    /// # #[tokio::main]
585    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
586    /// # let client = WithdrawalClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?;
587    /// let end = Utc::now();
588    /// let start = end - Duration::days(99);
589    /// 
590    /// let response = client
591    ///     .get_asset_dividends()
592    ///     // optional: filter by asset; gets all assets by default.
593    ///     .with_asset("BNB")
594    ///     // optional: get records from; gets recent records by default.
595    ///     .with_start_time(start)
596    ///     // optional: get records until; default is now.
597    ///     .with_end_time(end)
598    ///     // optional: processing time for request; default is 5000, can't be above 60000.
599    ///     .with_recv_window(8000)
600    ///     //
601    ///     .json::<Value>()
602    ///     .await?;
603    /// # Ok(())
604    /// # }
605    /// ```
606    pub fn get_asset_dividends(&self) -> ParamBuilder<'_, '_, AssetDividendParams>{
607        let Self { api_key, secret_key, url, client } = self;
608        let url = url.join("/sapi/v1/asset/assetDividend").unwrap();
609
610        ParamBuilder::new(
611            Parameters::default(),
612            client.get(url),
613            Some(api_key),
614            Some(secret_key)
615        )
616    }
617}