1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use super::super::message::GateioMessage;
use crate::{
    event::{MarketEvent, MarketIter},
    exchange::{ExchangeId, ExchangeSub},
    subscription::trade::PublicTrade,
    Identifier,
};
use barter_integration::model::{instrument::Instrument, Exchange, Side, SubscriptionId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Terse type alias for an [`GateioSpot`](super::GateioSpot) real-time trades WebSocket message.
pub type GateioSpotTrade = GateioMessage<GateioSpotTradeInner>;

/// [`GateioSpot`](super::GateioSpot) real-time trade WebSocket message.
///
/// ### Raw Payload Examples
/// See docs: <https://www.gate.io/docs/developers/apiv4/ws/en/#public-trades-channel>
/// ```json
/// {
///   "id": 309143071,
///   "create_time": 1606292218,
///   "create_time_ms": "1606292218213.4578",
///   "side": "sell",
///   "currency_pair": "GT_USDT",
///   "amount": "16.4700000000",
///   "price": "0.4705000000"
/// }
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct GateioSpotTradeInner {
    #[serde(rename = "currency_pair")]
    pub market: String,
    #[serde(
        rename = "create_time_ms",
        deserialize_with = "barter_integration::de::de_str_f64_epoch_ms_as_datetime_utc"
    )]
    pub time: DateTime<Utc>,
    pub id: u64,
    #[serde(deserialize_with = "barter_integration::de::de_str")]
    pub price: f64,

    #[serde(alias = "size", deserialize_with = "barter_integration::de::de_str")]
    pub amount: f64,

    /// Taker [`Side`] of the trade.
    pub side: Side,
}

impl Identifier<Option<SubscriptionId>> for GateioSpotTrade {
    fn id(&self) -> Option<SubscriptionId> {
        Some(ExchangeSub::from((&self.channel, &self.data.market)).id())
    }
}

impl From<(ExchangeId, Instrument, GateioSpotTrade)> for MarketIter<PublicTrade> {
    fn from((exchange_id, instrument, trade): (ExchangeId, Instrument, GateioSpotTrade)) -> Self {
        Self(vec![Ok(MarketEvent {
            exchange_time: trade.data.time,
            received_time: Utc::now(),
            exchange: Exchange::from(exchange_id),
            instrument,
            kind: PublicTrade {
                id: trade.data.id.to_string(),
                price: trade.data.price,
                amount: trade.data.amount,
                side: trade.data.side,
            },
        })])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod de {
        use super::*;

        #[test]
        fn test_gateio_message_futures_trade() {
            let input = r#"
            {
                "time": 1606292218,
                "time_ms": 1606292218231,
                "channel": "spot.trades",
                "event": "update",
                "result": {
                    "id": 309143071,
                    "create_time": 1606292218,
                    "create_time_ms": "1606292218213.4578",
                    "side": "sell",
                    "currency_pair": "GT_USDT",
                    "amount": "16.4700000000",
                    "price": "0.4705000000"
                }
            }
            "#;
            serde_json::from_str::<GateioSpotTrade>(input).unwrap();
        }
    }
}