Skip to main content

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::serde::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::serde::de::de_str")]
42    pub price: f64,
43
44    #[serde(
45        alias = "size",
46        deserialize_with = "barter_integration::serde::de::de_str"
47    )]
48    pub amount: f64,
49
50    /// Taker [`Side`] of the trade.
51    pub side: Side,
52}
53
54impl Identifier<Option<SubscriptionId>> for GateioSpotTrade {
55    fn id(&self) -> Option<SubscriptionId> {
56        Some(ExchangeSub::from((&self.channel, &self.data.market)).id())
57    }
58}
59
60impl<InstrumentKey> From<(ExchangeId, InstrumentKey, GateioSpotTrade)>
61    for MarketIter<InstrumentKey, PublicTrade>
62{
63    fn from(
64        (exchange_id, instrument, trade): (ExchangeId, InstrumentKey, GateioSpotTrade),
65    ) -> Self {
66        Self(vec![Ok(MarketEvent {
67            time_exchange: trade.data.time,
68            time_received: Utc::now(),
69            exchange: exchange_id,
70            instrument,
71            kind: PublicTrade {
72                id: trade.data.id.to_string(),
73                price: trade.data.price,
74                amount: trade.data.amount,
75                side: trade.data.side,
76            },
77        })])
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    mod de {
86        use super::*;
87
88        #[test]
89        fn test_gateio_message_futures_trade() {
90            let input = r#"
91            {
92                "time": 1606292218,
93                "time_ms": 1606292218231,
94                "channel": "spot.trades",
95                "event": "update",
96                "result": {
97                    "id": 309143071,
98                    "create_time": 1606292218,
99                    "create_time_ms": "1606292218213.4578",
100                    "side": "sell",
101                    "currency_pair": "GT_USDT",
102                    "amount": "16.4700000000",
103                    "price": "0.4705000000"
104                }
105            }
106            "#;
107            serde_json::from_str::<GateioSpotTrade>(input).unwrap();
108        }
109    }
110}