xapi_binance/clients/spot/
market.rs

1use crate::{
2    clients::spot::BnSpot,
3    common::response::{BnRestRespType, BnWsApiRespType},
4    data::{
5        enums::ratelimit::BnRateLimitType,
6        order_book::{BnGetOrderBookParams, BnSpotOrderBook},
7    },
8};
9use http::Method;
10use nonzero_ext::nonzero;
11use xapi_shared::rest::SharedRestClientTrait;
12
13impl BnSpot {
14    /// Get order book
15    ///
16    /// <https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#order-book>
17    pub async fn get_order_book(
18        &self,
19        params: &BnGetOrderBookParams,
20    ) -> BnRestRespType<BnSpotOrderBook> {
21        let weight = match params.limit.unwrap_or(100) {
22            1..=100 => nonzero!(5u32),
23            101..=500 => nonzero!(25u32),
24            501..=1000 => nonzero!(50u32),
25            _ => nonzero!(250u32),
26        };
27
28        self.executor
29            .call(
30                &[
31                    (BnRateLimitType::RequestWeight, weight),
32                    (BnRateLimitType::RawRequests, nonzero!(1u32)),
33                ],
34                false,
35                Method::GET,
36                self.executor
37                    .get_endpoint()
38                    .build_rest_api_url("/api/v3/depth"),
39                Some(params),
40            )
41            .await
42    }
43
44    // /// Get current order book.
45    // ///
46    // /// <https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#order-book>
47    // pub async fn get_order_book_ws(
48    //     &self,
49    //     params: &BnGetOrderBookParams,
50    // ) -> BnWsApiRespType<BnSpotOrderBook> {
51    //     let weight = match params.limit.unwrap_or(100) {
52    //         1..=100 => nonzero!(5u32),
53    //         101..=500 => nonzero!(25u32),
54    //         501..=1000 => nonzero!(50u32),
55    //         _ => nonzero!(250u32),
56    //     };
57
58    //     self.executor
59    //         .send(
60    //             &[
61    //                 (BnRateLimitType::RequestWeight, weight),
62    //                 (BnRateLimitType::RawRequests, nonzero!(1u32)),
63    //             ],
64    //             false,
65    //             "depth",
66    //             Some(params),
67    //         )
68    //         .await
69    // }
70
71    /// Get current order book.
72    ///
73    /// <https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/market-data-requests#order-book>
74    pub async fn get_order_book_ws(
75        &self,
76        params: &BnGetOrderBookParams,
77    ) -> BnWsApiRespType<BnSpotOrderBook> {
78        let weight = match params.limit.unwrap_or(100) {
79            1..=100 => nonzero!(5u32),
80            101..=500 => nonzero!(25u32),
81            501..=1000 => nonzero!(50u32),
82            _ => nonzero!(250u32),
83        };
84
85        self.executor
86            .call_ws_api(
87                &[
88                    (BnRateLimitType::RequestWeight, weight),
89                    (BnRateLimitType::RawRequests, nonzero!(1u32)),
90                ],
91                false,
92                "depth",
93                Some(params),
94            )
95            .await
96    }
97}