barter_data/exchange/binance/
mod.rs

1use self::{
2    book::l1::BinanceOrderBookL1, channel::BinanceChannel, market::BinanceMarket,
3    subscription::BinanceSubResponse, trade::BinanceTrade,
4};
5use crate::{
6    ExchangeWsStream, NoInitialSnapshots,
7    exchange::{Connector, ExchangeServer, ExchangeSub, StreamSelector},
8    instrument::InstrumentData,
9    subscriber::{WebSocketSubscriber, validator::WebSocketSubValidator},
10    subscription::{Map, book::OrderBooksL1, trade::PublicTrades},
11    transformer::stateless::StatelessTransformer,
12};
13use barter_instrument::exchange::ExchangeId;
14use barter_integration::{error::SocketError, protocol::websocket::WsMessage};
15use std::{fmt::Debug, marker::PhantomData};
16use url::Url;
17
18/// OrderBook types common to both [`BinanceSpot`](spot::BinanceSpot) and
19/// [`BinanceFuturesUsd`](futures::BinanceFuturesUsd).
20pub mod book;
21
22/// Defines the type that translates a Barter [`Subscription`](crate::subscription::Subscription)
23/// into an exchange [`Connector`] specific channel used for generating [`Connector::requests`].
24pub mod channel;
25
26/// [`ExchangeServer`] and [`StreamSelector`] implementations for
27/// [`BinanceFuturesUsd`](futures::BinanceFuturesUsd).
28pub mod futures;
29
30/// Defines the type that translates a Barter [`Subscription`](crate::subscription::Subscription)
31/// into an exchange [`Connector`] specific market used for generating [`Connector::requests`].
32pub mod market;
33
34/// [`ExchangeServer`] and [`StreamSelector`] implementations for
35/// [`BinanceSpot`](spot::BinanceSpot).
36pub mod spot;
37
38/// [`Subscription`](crate::subscription::Subscription) response type and response
39/// [`Validator`](barter_integration::Validator) common to both [`BinanceSpot`](spot::BinanceSpot)
40/// and [`BinanceFuturesUsd`](futures::BinanceFuturesUsd).
41pub mod subscription;
42
43/// Public trade types common to both [`BinanceSpot`](spot::BinanceSpot) and
44/// [`BinanceFuturesUsd`](futures::BinanceFuturesUsd).
45pub mod trade;
46
47/// Generic [`Binance<Server>`](Binance) exchange.
48///
49/// ### Notes
50/// A `Server` [`ExchangeServer`] implementations exists for
51/// [`BinanceSpot`](spot::BinanceSpot) and [`BinanceFuturesUsd`](futures::BinanceFuturesUsd).
52#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
53pub struct Binance<Server> {
54    server: PhantomData<Server>,
55}
56
57impl<Server> Connector for Binance<Server>
58where
59    Server: ExchangeServer,
60{
61    const ID: ExchangeId = Server::ID;
62    type Channel = BinanceChannel;
63    type Market = BinanceMarket;
64    type Subscriber = WebSocketSubscriber;
65    type SubValidator = WebSocketSubValidator;
66    type SubResponse = BinanceSubResponse;
67
68    fn url() -> Result<Url, SocketError> {
69        Url::parse(Server::websocket_url()).map_err(SocketError::UrlParse)
70    }
71
72    fn requests(exchange_subs: Vec<ExchangeSub<Self::Channel, Self::Market>>) -> Vec<WsMessage> {
73        let stream_names = exchange_subs
74            .into_iter()
75            .map(|sub| {
76                // Note:
77                // Market must be lowercase when subscribing, but lowercase in general since
78                // Binance sends message with uppercase MARKET (eg/ BTCUSDT).
79                format!(
80                    "{}{}",
81                    sub.market.as_ref().to_lowercase(),
82                    sub.channel.as_ref()
83                )
84            })
85            .collect::<Vec<String>>();
86
87        vec![WsMessage::text(
88            serde_json::json!({
89                "method": "SUBSCRIBE",
90                "params": stream_names,
91                "id": 1
92            })
93            .to_string(),
94        )]
95    }
96
97    fn expected_responses<InstrumentKey>(_: &Map<InstrumentKey>) -> usize {
98        1
99    }
100}
101
102impl<Instrument, Server> StreamSelector<Instrument, PublicTrades> for Binance<Server>
103where
104    Instrument: InstrumentData,
105    Server: ExchangeServer + Debug + Send + Sync,
106{
107    type SnapFetcher = NoInitialSnapshots;
108    type Stream =
109        ExchangeWsStream<StatelessTransformer<Self, Instrument::Key, PublicTrades, BinanceTrade>>;
110}
111
112impl<Instrument, Server> StreamSelector<Instrument, OrderBooksL1> for Binance<Server>
113where
114    Instrument: InstrumentData,
115    Server: ExchangeServer + Debug + Send + Sync,
116{
117    type SnapFetcher = NoInitialSnapshots;
118    type Stream = ExchangeWsStream<
119        StatelessTransformer<Self, Instrument::Key, OrderBooksL1, BinanceOrderBookL1>,
120    >;
121}
122
123impl<'de, Server> serde::Deserialize<'de> for Binance<Server>
124where
125    Server: ExchangeServer,
126{
127    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
128    where
129        D: serde::de::Deserializer<'de>,
130    {
131        let input = <String as serde::Deserialize>::deserialize(deserializer)?;
132
133        if input.as_str() == Self::ID.as_str() {
134            Ok(Self::default())
135        } else {
136            Err(serde::de::Error::invalid_value(
137                serde::de::Unexpected::Str(input.as_str()),
138                &Self::ID.as_str(),
139            ))
140        }
141    }
142}
143
144impl<Server> serde::Serialize for Binance<Server>
145where
146    Server: ExchangeServer,
147{
148    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
149    where
150        S: serde::ser::Serializer,
151    {
152        serializer.serialize_str(Self::ID.as_str())
153    }
154}