barter_data/exchange/bybit/
market.rs

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