barter_data/exchange/gateio/
channel.rs

1use crate::{
2    Identifier,
3    instrument::InstrumentData,
4    subscription::{Subscription, trade::PublicTrades},
5};
6use barter_instrument::instrument::market_data::kind::MarketDataInstrumentKind;
7use serde::Serialize;
8
9/// Type that defines how to translate a Barter [`Subscription`] into a
10/// [`Gateio`](super::Gateio) channel to be subscribed to.
11///
12/// See docs: <https://www.okx.com/docs-v5/en/#websocket-api-public-channel>
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize)]
14pub struct GateioChannel(pub &'static str);
15
16impl GateioChannel {
17    /// Gateio [`MarketDataInstrumentKind::Spot`] real-time trades channel.
18    ///
19    /// See docs: <https://www.gate.io/docs/developers/apiv4/ws/en/#public-trades-channel>
20    pub const SPOT_TRADES: Self = Self("spot.trades");
21
22    /// Gateio [`MarketDataInstrumentKind::Future`] & [`MarketDataInstrumentKind::Perpetual`] real-time trades channel.
23    ///
24    /// See docs: <https://www.gate.io/docs/developers/futures/ws/en/#trades-subscription>
25    /// See docs: <https://www.gate.io/docs/developers/delivery/ws/en/#trades-subscription>
26    pub const FUTURE_TRADES: Self = Self("futures.trades");
27
28    /// Gateio [`MarketDataInstrumentKind::Option`] real-time trades channel.
29    ///
30    /// See docs: <https://www.gate.io/docs/developers/options/ws/en/#public-contract-trades-channel>
31    pub const OPTION_TRADES: Self = Self("options.trades");
32}
33
34impl<GateioExchange, Instrument> Identifier<GateioChannel>
35    for Subscription<GateioExchange, Instrument, PublicTrades>
36where
37    Instrument: InstrumentData,
38{
39    fn id(&self) -> GateioChannel {
40        match self.instrument.kind() {
41            MarketDataInstrumentKind::Spot => GateioChannel::SPOT_TRADES,
42            MarketDataInstrumentKind::Future { .. } | MarketDataInstrumentKind::Perpetual => {
43                GateioChannel::FUTURE_TRADES
44            }
45            MarketDataInstrumentKind::Option { .. } => GateioChannel::OPTION_TRADES,
46        }
47    }
48}
49
50impl AsRef<str> for GateioChannel {
51    fn as_ref(&self) -> &str {
52        self.0
53    }
54}