barter_data/exchange/binance/
market.rs

1use super::Binance;
2use crate::{Identifier, instrument::MarketInstrumentData, subscription::Subscription};
3use barter_instrument::{
4    Keyed, asset::name::AssetNameInternal, instrument::market_data::MarketDataInstrument,
5};
6use serde::{Deserialize, Serialize};
7use smol_str::{SmolStr, StrExt, format_smolstr};
8
9/// Type that defines how to translate a Barter [`Subscription`] into a [`Binance`]
10/// market that can be subscribed to.
11///
12/// See docs: <https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams>
13/// See docs: <https://binance-docs.github.io/apidocs/futures/en/#websocket-market-streams>
14#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
15pub struct BinanceMarket(pub SmolStr);
16
17impl<Server, Kind> Identifier<BinanceMarket>
18    for Subscription<Binance<Server>, MarketDataInstrument, Kind>
19{
20    fn id(&self) -> BinanceMarket {
21        binance_market(&self.instrument.base, &self.instrument.quote)
22    }
23}
24
25impl<Server, InstrumentKey, Kind> Identifier<BinanceMarket>
26    for Subscription<Binance<Server>, Keyed<InstrumentKey, MarketDataInstrument>, Kind>
27{
28    fn id(&self) -> BinanceMarket {
29        binance_market(
30            &self.instrument.as_ref().base,
31            &self.instrument.as_ref().quote,
32        )
33    }
34}
35
36impl<Server, InstrumentKey, Kind> Identifier<BinanceMarket>
37    for Subscription<Binance<Server>, MarketInstrumentData<InstrumentKey>, Kind>
38{
39    fn id(&self) -> BinanceMarket {
40        BinanceMarket(self.instrument.name_exchange.name().clone())
41    }
42}
43
44impl AsRef<str> for BinanceMarket {
45    fn as_ref(&self) -> &str {
46        &self.0
47    }
48}
49
50pub(in crate::exchange::binance) fn binance_market(
51    base: &AssetNameInternal,
52    quote: &AssetNameInternal,
53) -> BinanceMarket {
54    // Notes:
55    // - Must be lowercase when subscribing (transformed to lowercase by Binance fn requests).
56    // - Must be uppercase since Binance sends message with uppercase MARKET (eg/ BTCUSDT).
57    BinanceMarket(format_smolstr!("{base}{quote}").to_uppercase_smolstr())
58}