barter_data/exchange/bitfinex/
market.rs

1use super::Bitfinex;
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, ToSmolStr, format_smolstr};
8
9/// Type that defines how to translate a Barter [`Subscription`] into a
10/// [`Bitfinex`] market that can be subscribed to.
11///
12/// See docs: <https://docs.bitfinex.com/docs/ws-public>
13#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
14pub struct BitfinexMarket(pub SmolStr);
15
16impl<Kind> Identifier<BitfinexMarket> for Subscription<Bitfinex, MarketDataInstrument, Kind> {
17    fn id(&self) -> BitfinexMarket {
18        bitfinex_market(&self.instrument.base, &self.instrument.quote)
19    }
20}
21
22impl<InstrumentKey, Kind> Identifier<BitfinexMarket>
23    for Subscription<Bitfinex, Keyed<InstrumentKey, MarketDataInstrument>, Kind>
24{
25    fn id(&self) -> BitfinexMarket {
26        bitfinex_market(&self.instrument.value.base, &self.instrument.value.quote)
27    }
28}
29
30impl<InstrumentKey, Kind> Identifier<BitfinexMarket>
31    for Subscription<Bitfinex, MarketInstrumentData<InstrumentKey>, Kind>
32{
33    fn id(&self) -> BitfinexMarket {
34        BitfinexMarket(self.instrument.name_exchange.to_smolstr())
35    }
36}
37
38impl AsRef<str> for BitfinexMarket {
39    fn as_ref(&self) -> &str {
40        &self.0
41    }
42}
43
44fn bitfinex_market(base: &AssetNameInternal, quote: &AssetNameInternal) -> BitfinexMarket {
45    BitfinexMarket(format_smolstr!(
46        "t{}{}",
47        base.to_string().to_uppercase(),
48        quote.to_string().to_uppercase()
49    ))
50}