Skip to main content

rustrade_data/exchange/binance/spot/
mod.rs

1use super::{Binance, ExchangeServer};
2use crate::{
3    exchange::{
4        StreamSelector,
5        binance::{
6            BinanceWsStream,
7            spot::l2::{
8                BinanceSpotOrderBooksL2SnapshotFetcher, BinanceSpotOrderBooksL2Transformer,
9            },
10        },
11    },
12    instrument::InstrumentData,
13    subscription::book::OrderBooksL2,
14};
15use rustrade_instrument::exchange::ExchangeId;
16use std::fmt::{Display, Formatter};
17
18/// Level 2 OrderBook types.
19pub mod l2;
20
21/// [`BinanceSpot`] WebSocket server base url.
22///
23/// See docs: <https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams>
24pub const WEBSOCKET_BASE_URL_BINANCE_SPOT: &str = "wss://stream.binance.com:9443/ws";
25
26/// [`Binance`] spot exchange.
27///
28/// # WebSocket connection limits
29///
30/// Existing Binance constraints a multi-symbol consumer must respect (not new — already handled by
31/// `ReconnectingStream` + tungstenite's automatic pong, but worth stating):
32/// - **1024 streams per connection.**
33/// - **Incoming-message cap: 5 messages/second** (counts ping/pong + control frames) — space out
34///   bulk `SUBSCRIBE`s.
35/// - **Forced disconnect at 24h** (transparently re-established by `ReconnectingStream`).
36/// - **Keepalive:** the server pings every **20s** with a **1-minute** pong deadline (tungstenite
37///   auto-pongs, so no action) — note this is far tighter than futures' 3-min/10-min cadence.
38/// - **300 connections per 5 minutes per IP.**
39pub type BinanceSpot = Binance<BinanceServerSpot>;
40
41/// [`Binance`] spot [`ExchangeServer`].
42#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
43pub struct BinanceServerSpot;
44
45impl ExchangeServer for BinanceServerSpot {
46    const ID: ExchangeId = ExchangeId::BinanceSpot;
47
48    fn websocket_url() -> &'static str {
49        WEBSOCKET_BASE_URL_BINANCE_SPOT
50    }
51}
52
53impl<Instrument> StreamSelector<Instrument, OrderBooksL2> for BinanceSpot
54where
55    Instrument: InstrumentData,
56{
57    type SnapFetcher = BinanceSpotOrderBooksL2SnapshotFetcher;
58    type Stream = BinanceWsStream<BinanceSpotOrderBooksL2Transformer<Instrument::Key>>;
59}
60
61impl Display for BinanceSpot {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        write!(f, "BinanceSpot")
64    }
65}