binance-sdk 45.0.0

This is a lightweight library that works as a connector to the Binance public API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * Binance Margin Trading REST API
 *
 * OpenAPI Specification for the Binance Margin Trading REST API
 *
 * The version of the OpenAPI document: 1.0.0
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

#![allow(unused_imports)]
use async_trait::async_trait;
use derive_builder::Builder;
use reqwest;
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;

use crate::common::{
    config::ConfigurationRestApi,
    models::{ParamBuildError, RestApiResponse},
    utils::send_request,
};
use crate::margin_trading::rest_api::models;

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait TransferApi: Send + Sync {
    async fn get_cross_margin_transfer_history(
        &self,
        params: GetCrossMarginTransferHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>>;
    async fn query_max_transfer_out_amount(
        &self,
        params: QueryMaxTransferOutAmountParams,
    ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>>;
}

#[derive(Debug, Clone)]
pub struct TransferApiClient {
    configuration: ConfigurationRestApi,
}

impl TransferApiClient {
    pub fn new(configuration: ConfigurationRestApi) -> Self {
        Self { configuration }
    }
}

/// Request parameters for the [`get_cross_margin_transfer_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_cross_margin_transfer_history`](#method.get_cross_margin_transfer_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetCrossMarginTransferHistoryParams {
    ///
    /// The `asset` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub asset: Option<String>,
    /// Transfer Type: `ROLL_IN`, `ROLL_OUT`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub r#type: Option<String>,
    /// Only supports querying data from the past 90 days.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Currently querying page. Start from 1. Default:1
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub current: Option<i64>,
    /// Default:10 Max:100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub size: Option<i64>,
    /// isolated symbol
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub isolated_symbol: Option<String>,
    /// No more than 60000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetCrossMarginTransferHistoryParams {
    /// Create a builder for [`get_cross_margin_transfer_history`].
    ///
    #[must_use]
    pub fn builder() -> GetCrossMarginTransferHistoryParamsBuilder {
        GetCrossMarginTransferHistoryParamsBuilder::default()
    }
}
/// Request parameters for the [`query_max_transfer_out_amount`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`query_max_transfer_out_amount`](#method.query_max_transfer_out_amount).
#[derive(Clone, Debug, Builder)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct QueryMaxTransferOutAmountParams {
    ///
    /// The `asset` parameter.
    ///
    /// This field is **required.
    #[builder(setter(into))]
    pub asset: String,
    /// isolated symbol
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub isolated_symbol: Option<String>,
    /// No more than 60000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl QueryMaxTransferOutAmountParams {
    /// Create a builder for [`query_max_transfer_out_amount`].
    ///
    /// Required parameters:
    ///
    /// * `asset` — String
    ///
    #[must_use]
    pub fn builder(asset: String) -> QueryMaxTransferOutAmountParamsBuilder {
        QueryMaxTransferOutAmountParamsBuilder::default().asset(asset)
    }
}

#[async_trait]
impl TransferApi for TransferApiClient {
    async fn get_cross_margin_transfer_history(
        &self,
        params: GetCrossMarginTransferHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>> {
        let GetCrossMarginTransferHistoryParams {
            asset,
            r#type,
            start_time,
            end_time,
            current,
            size,
            isolated_symbol,
            recv_window,
        } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = asset {
            query_params.insert("asset".to_string(), json!(rw));
        }

        if let Some(rw) = r#type {
            query_params.insert("type".to_string(), json!(rw));
        }

        if let Some(rw) = start_time {
            query_params.insert("startTime".to_string(), json!(rw));
        }

        if let Some(rw) = end_time {
            query_params.insert("endTime".to_string(), json!(rw));
        }

        if let Some(rw) = current {
            query_params.insert("current".to_string(), json!(rw));
        }

        if let Some(rw) = size {
            query_params.insert("size".to_string(), json!(rw));
        }

        if let Some(rw) = isolated_symbol {
            query_params.insert("isolatedSymbol".to_string(), json!(rw));
        }

        if let Some(rw) = recv_window {
            query_params.insert("recvWindow".to_string(), json!(rw));
        }

        send_request::<models::GetCrossMarginTransferHistoryResponse>(
            &self.configuration,
            "/sapi/v1/margin/transfer",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn query_max_transfer_out_amount(
        &self,
        params: QueryMaxTransferOutAmountParams,
    ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>> {
        let QueryMaxTransferOutAmountParams {
            asset,
            isolated_symbol,
            recv_window,
        } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        query_params.insert("asset".to_string(), json!(asset));

        if let Some(rw) = isolated_symbol {
            query_params.insert("isolatedSymbol".to_string(), json!(rw));
        }

        if let Some(rw) = recv_window {
            query_params.insert("recvWindow".to_string(), json!(rw));
        }

        send_request::<models::QueryMaxTransferOutAmountResponse>(
            &self.configuration,
            "/sapi/v1/margin/maxTransferable",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }
}

#[cfg(all(test, feature = "margin_trading"))]
mod tests {
    use super::*;
    use crate::TOKIO_SHARED_RT;
    use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
    use async_trait::async_trait;
    use std::collections::HashMap;

    struct DummyRestApiResponse<T> {
        inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
        status: u16,
        headers: HashMap<String, String>,
        rate_limits: Option<Vec<RestApiRateLimit>>,
    }

    impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
        fn from(dummy: DummyRestApiResponse<T>) -> Self {
            Self {
                data_fn: dummy.inner,
                status: dummy.status,
                headers: dummy.headers,
                rate_limits: dummy.rate_limits,
            }
        }
    }

    struct MockTransferApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl TransferApi for MockTransferApiClient {
        async fn get_cross_margin_transfer_history(
            &self,
            _params: GetCrossMarginTransferHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetCrossMarginTransferHistoryResponse>>
        {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let dummy_response: models::GetCrossMarginTransferHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }

        async fn query_max_transfer_out_amount(
            &self,
            _params: QueryMaxTransferOutAmountParams,
        ) -> anyhow::Result<RestApiResponse<models::QueryMaxTransferOutAmountResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let dummy_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }
    }

    #[test]
    fn get_cross_margin_transfer_history_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = GetCrossMarginTransferHistoryParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let expected_response : models::GetCrossMarginTransferHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            let resp = client.get_cross_margin_transfer_history(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_cross_margin_transfer_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = GetCrossMarginTransferHistoryParams::builder().asset("asset_example".to_string()).r#type("r#type_example".to_string()).start_time(1623319461670).end_time(1641782889000).current(1).size(10).isolated_symbol("isolated_symbol_example".to_string()).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"amount":"0.10000000","asset":"BNB","status":"CONFIRMED","timestamp":1566898617,"txId":5240372201,"type":"ROLL_IN","transFrom":"SPOT","transTo":"ISOLATED_MARGIN"},{"amount":"5.00000000","asset":"USDT","status":"CONFIRMED","timestamp":1566888436,"txId":5239810406,"type":"ROLL_OUT","transFrom":"ISOLATED_MARGIN","transTo":"ISOLATED_MARGIN","fromSymbol":"BNBUSDT","toSymbol":"BTCUSDT"},{"amount":"1.00000000","asset":"EOS","status":"CONFIRMED","timestamp":1566888403,"txId":5239808703,"type":"ROLL_IN"}],"total":3}"#).unwrap();
            let expected_response : models::GetCrossMarginTransferHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetCrossMarginTransferHistoryResponse");

            let resp = client.get_cross_margin_transfer_history(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_cross_margin_transfer_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: true };

            let params = GetCrossMarginTransferHistoryParams::builder()
                .build()
                .unwrap();

            match client.get_cross_margin_transfer_history(params).await {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }

    #[test]
    fn query_max_transfer_out_amount_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .build()
                .unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let expected_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            let resp = client
                .query_max_transfer_out_amount(params)
                .await
                .expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn query_max_transfer_out_amount_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: false };

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .isolated_symbol("isolated_symbol_example".to_string())
                .recv_window(5000)
                .build()
                .unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"amount":"3.59498107"}"#).unwrap();
            let expected_response: models::QueryMaxTransferOutAmountResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::QueryMaxTransferOutAmountResponse");

            let resp = client
                .query_max_transfer_out_amount(params)
                .await
                .expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn query_max_transfer_out_amount_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockTransferApiClient { force_error: true };

            let params = QueryMaxTransferOutAmountParams::builder("asset_example".to_string())
                .build()
                .unwrap();

            match client.query_max_transfer_out_amount(params).await {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }
}