barter_data/exchange/coinbase/
market.rs

1use super::Coinbase;
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
10/// [`Coinbase`] market that can be subscribed to.
11///
12/// See docs: <https://docs.cloud.coinbase.com/exchange/docs/websocket-overview#subscribe>
13#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
14pub struct CoinbaseMarket(pub SmolStr);
15
16impl<Kind> Identifier<CoinbaseMarket> for Subscription<Coinbase, MarketDataInstrument, Kind> {
17    fn id(&self) -> CoinbaseMarket {
18        coinbase_market(&self.instrument.base, &self.instrument.quote)
19    }
20}
21
22impl<InstrumentKey, Kind> Identifier<CoinbaseMarket>
23    for Subscription<Coinbase, Keyed<InstrumentKey, MarketDataInstrument>, Kind>
24{
25    fn id(&self) -> CoinbaseMarket {
26        coinbase_market(&self.instrument.value.base, &self.instrument.value.quote)
27    }
28}
29
30impl<InstrumentKey, Kind> Identifier<CoinbaseMarket>
31    for Subscription<Coinbase, MarketInstrumentData<InstrumentKey>, Kind>
32{
33    fn id(&self) -> CoinbaseMarket {
34        CoinbaseMarket(self.instrument.name_exchange.name().clone())
35    }
36}
37
38impl AsRef<str> for CoinbaseMarket {
39    fn as_ref(&self) -> &str {
40        &self.0
41    }
42}
43
44fn coinbase_market(base: &AssetNameInternal, quote: &AssetNameInternal) -> CoinbaseMarket {
45    CoinbaseMarket(format_smolstr!("{base}-{quote}").to_uppercase_smolstr())
46}