barter_data/exchange/gateio/spot/
trade.rs

1use super::super::message::GateioMessage;
2use crate::{
3    Identifier,
4    event::{MarketEvent, MarketIter},
5    exchange::ExchangeSub,
6    subscription::trade::PublicTrade,
7};
8use barter_instrument::{Side, exchange::ExchangeId};
9use barter_integration::subscription::SubscriptionId;
10use chrono::{DateTime, Utc};
11use serde::{Deserialize, Serialize};
12
13/// Terse type alias for an [`GateioSpot`](super::GateioSpot) real-time trades WebSocket message.
14pub type GateioSpotTrade = GateioMessage<GateioSpotTradeInner>;
15
16/// [`GateioSpot`](super::GateioSpot) real-time trade WebSocket message.
17///
18/// ### Raw Payload Examples
19/// See docs: <https://www.gate.io/docs/developers/apiv4/ws/en/#public-trades-channel>
20/// ```json
21/// {
22///   "id": 309143071,
23///   "create_time": 1606292218,
24///   "create_time_ms": "1606292218213.4578",
25///   "side": "sell",
26///   "currency_pair": "GT_USDT",
27///   "amount": "16.4700000000",
28///   "price": "0.4705000000"
29/// }
30/// ```
31#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
32pub struct GateioSpotTradeInner {
33    #[serde(rename = "currency_pair")]
34    pub market: String,
35    #[serde(
36        rename = "create_time_ms",
37        deserialize_with = "barter_integration::de::de_str_f64_epoch_ms_as_datetime_utc"
38    )]
39    pub time: DateTime<Utc>,
40    pub id: u64,
41    #[serde(deserialize_with = "barter_integration::de::de_str")]
42    pub price: f64,
43
44    #[serde(alias = "size", deserialize_with = "barter_integration::de::de_str")]
45    pub amount: f64,
46
47    /// Taker [`Side`] of the trade.
48    pub side: Side,
49}
50
51impl Identifier<Option<SubscriptionId>> for GateioSpotTrade {
52    fn id(&self) -> Option<SubscriptionId> {
53        Some(ExchangeSub::from((&self.channel, &self.data.market)).id())
54    }
55}
56
57impl<InstrumentKey> From<(ExchangeId, InstrumentKey, GateioSpotTrade)>
58    for MarketIter<InstrumentKey, PublicTrade>
59{
60    fn from(
61        (exchange_id, instrument, trade): (ExchangeId, InstrumentKey, GateioSpotTrade),
62    ) -> Self {
63        Self(vec![Ok(MarketEvent {
64            time_exchange: trade.data.time,
65            time_received: Utc::now(),
66            exchange: exchange_id,
67            instrument,
68            kind: PublicTrade {
69                id: trade.data.id.to_string(),
70                price: trade.data.price,
71                amount: trade.data.amount,
72                side: trade.data.side,
73            },
74        })])
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    mod de {
83        use super::*;
84
85        #[test]
86        fn test_gateio_message_futures_trade() {
87            let input = r#"
88            {
89                "time": 1606292218,
90                "time_ms": 1606292218231,
91                "channel": "spot.trades",
92                "event": "update",
93                "result": {
94                    "id": 309143071,
95                    "create_time": 1606292218,
96                    "create_time_ms": "1606292218213.4578",
97                    "side": "sell",
98                    "currency_pair": "GT_USDT",
99                    "amount": "16.4700000000",
100                    "price": "0.4705000000"
101                }
102            }
103            "#;
104            serde_json::from_str::<GateioSpotTrade>(input).unwrap();
105        }
106    }
107}