rustrade_data/exchange/gateio/channel.rs
1use crate::{
2 Identifier,
3 instrument::InstrumentData,
4 subscription::{Subscription, trade::PublicTrades},
5};
6use rustrade_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 // Gateio lists no CFDs, so `Cfd` is unreachable here: `exchange_supports_instrument_kind`
42 // denies it before a `Subscription` can be validated. `Identifier` is infallible, so the
43 // arm exists to keep this match total, and shares `Spot`'s channel as the nearest
44 // (identically shaped) wire form.
45 MarketDataInstrumentKind::Spot | MarketDataInstrumentKind::Cfd => {
46 GateioChannel::SPOT_TRADES
47 }
48 MarketDataInstrumentKind::Future { .. } | MarketDataInstrumentKind::Perpetual => {
49 GateioChannel::FUTURE_TRADES
50 }
51 MarketDataInstrumentKind::Option { .. } => GateioChannel::OPTION_TRADES,
52 }
53 }
54}
55
56impl AsRef<str> for GateioChannel {
57 fn as_ref(&self) -> &str {
58 self.0
59 }
60}