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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::{
    model::subscription::{SubKind, SubscriptionIds, SubscriptionMeta},
    ExchangeId, ExchangeTransformer, MarketEvent, Subscriber, Subscription, Validator,
};
use barter_integration::{
    error::SocketError, model::SubscriptionId, protocol::websocket::WsMessage, Transformer,
};
use model::{CoinbaseMessage, CoinbaseSubResponse};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use tokio::sync::mpsc;

/// [`Coinbase`] specific data structures.
mod model;

/// [`Coinbase`] [`Subscriber`] & [`ExchangeTransformer`] implementor for the collection
/// of `Spot` & `Futures` data.
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
pub struct Coinbase {
    pub ids: SubscriptionIds,
}

impl Subscriber for Coinbase {
    type SubResponse = CoinbaseSubResponse;

    fn base_url() -> &'static str {
        "wss://ws-feed.exchange.coinbase.com"
    }

    fn build_subscription_meta(
        subscriptions: &[Subscription],
    ) -> Result<SubscriptionMeta, SocketError> {
        // Allocate SubscriptionIds HashMap to track identifiers for each actioned Subscription
        let mut ids = SubscriptionIds(HashMap::with_capacity(subscriptions.len()));

        // Map Barter Subscriptions to Coinbase channels
        let subscriptions = subscriptions
            .iter()
            .map(|subscription| {
                // Determine the Coinbase specific channel & market for this Barter Subscription
                let (channel, market) = Self::build_channel_meta(subscription)?;

                // Use "channel|market" as the SubscriptionId key in the SubscriptionIds HashMap
                // eg/ SubscriptionId("matches|ETH-USD")
                ids.insert(
                    Coinbase::subscription_id(channel, &market),
                    subscription.clone(),
                );

                // Construct Coinbase specific subscription message
                Ok(Self::subscription(channel, &market))
            })
            .collect::<Result<Vec<_>, SocketError>>()?;

        Ok(SubscriptionMeta {
            ids,
            expected_responses: subscriptions.len(),
            subscriptions,
        })
    }
}

impl ExchangeTransformer for Coinbase {
    const EXCHANGE: ExchangeId = ExchangeId::Coinbase;
    fn new(_: mpsc::UnboundedSender<WsMessage>, ids: SubscriptionIds) -> Self {
        Self { ids }
    }
}

impl Transformer<MarketEvent> for Coinbase {
    type Input = CoinbaseMessage;
    type OutputIter = Vec<Result<MarketEvent, SocketError>>;

    fn transform(&mut self, input: Self::Input) -> Self::OutputIter {
        match input {
            CoinbaseMessage::Trade(trade) => {
                match self.ids.find_instrument(&trade.subscription_id) {
                    Ok(instrument) => vec![Ok(MarketEvent::from((
                        Coinbase::EXCHANGE,
                        instrument,
                        trade,
                    )))],
                    Err(error) => vec![Err(error)],
                }
            }
        }
    }
}

impl Coinbase {
    /// [`Coinbase`] trades channel name.
    ///
    /// See docs: <https://docs.cloud.coinbase.com/exchange/docs/websocket-channels#match>
    pub const CHANNEL_TRADES: &'static str = "matches";

    /// Determine the [`Coinbase`] channel metadata associated with an input Barter [`Subscription`].
    /// This includes the [`Coinbase`] &str channel, and a `String` market identifier. Both are
    /// used to build an [`Coinbase`] subscription payload.
    ///
    /// Example Ok return: Ok("matches", "BTC-USD")
    /// where channel == "matches" & market == "BTC-USD".
    pub fn build_channel_meta(sub: &Subscription) -> Result<(&str, String), SocketError> {
        // Validate provided Subscription InstrumentKind is supported by Coinbase
        let sub = sub.validate()?;

        // Determine Coinbase channel using the Subscription SubKind
        let channel = match &sub.kind {
            SubKind::Trade => Self::CHANNEL_TRADES,
            other => {
                return Err(SocketError::Unsupported {
                    entity: Self::EXCHANGE.as_str(),
                    item: other.to_string(),
                })
            }
        };

        // Determine Coinbase market identifier using the Instrument (eg/ "BTC-USD")
        let market = format!("{}-{}", sub.instrument.base, sub.instrument.quote).to_uppercase();

        Ok((channel, market))
    }

    /// Build a [`Coinbase`] compatible subscription message using the channel & market provided.
    pub fn subscription(channel: &str, market: &str) -> WsMessage {
        WsMessage::Text(
            json!({
                "type": "subscribe",
                "product_ids": [market],
                "channels": [channel],
            })
            .to_string(),
        )
    }

    /// Build a [`Coinbase`] compatible [`SubscriptionId`] using the channel & market provided.
    /// This is used to associate [`Coinbase`] data structures received over the WebSocket with it's
    /// original Barter [`Subscription`].
    ///
    /// eg/ SubscriptionId("matches|ETH-USD")
    pub fn subscription_id(channel: &str, market: &str) -> SubscriptionId {
        SubscriptionId::from(format!("{channel}|{market}"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::exchange::coinbase::model::CoinbaseTrade;
    use crate::model::{subscription::Interval, DataKind, PublicTrade};
    use barter_integration::model::{Exchange, Instrument, InstrumentKind, Side};
    use chrono::Utc;

    fn coinbase(subscriptions: Vec<Subscription>) -> Coinbase {
        let ids = SubscriptionIds(
            subscriptions
                .into_iter()
                .map(|sub| {
                    let subscription_id = match (&sub.kind, &sub.instrument.kind) {
                        (SubKind::Trade, InstrumentKind::Spot) => {
                            let product_id =
                                format!("{}-{}", sub.instrument.base, sub.instrument.quote)
                                    .to_uppercase();
                            format!("matches|{}", product_id)
                        }
                        (_, _) => {
                            panic!("not supported")
                        }
                    };

                    (SubscriptionId::from(subscription_id), sub)
                })
                .collect(),
        );

        Coinbase { ids }
    }

    #[test]
    fn test_build_channel_meta() {
        struct TestCase {
            input: Subscription,
            expected: Result<(&'static str, String), SocketError>,
        }

        let cases = vec![
            TestCase {
                // TC0: Supported InstrumentKind::Spot trades subscription
                input: Subscription::new(
                    ExchangeId::Coinbase,
                    ("btc", "usd", InstrumentKind::Spot),
                    SubKind::Trade,
                ),
                expected: Ok(("matches", "BTC-USD".to_owned())),
            },
            TestCase {
                // TC1: Unsupported InstrumentKind::Spot candle subscription
                input: Subscription::new(
                    ExchangeId::Coinbase,
                    ("btc", "usd", InstrumentKind::Spot),
                    SubKind::Candle(Interval::Minute5),
                ),
                expected: Err(SocketError::Unsupported {
                    entity: "",
                    item: "".to_string(),
                }),
            },
            TestCase {
                // TC2: Unsupported InstrumentKind::FuturePerpetual trades subscription
                input: Subscription::new(
                    ExchangeId::Coinbase,
                    ("btc", "usd", InstrumentKind::FuturePerpetual),
                    SubKind::Trade,
                ),
                expected: Err(SocketError::Unsupported {
                    entity: "",
                    item: "".to_string(),
                }),
            },
        ];

        for (index, test) in cases.into_iter().enumerate() {
            let actual = Coinbase::build_channel_meta(&test.input);
            match (actual, test.expected) {
                (Ok(actual), Ok(expected)) => {
                    assert_eq!(actual, expected, "TC{} failed", index)
                }
                (Err(_), Err(_)) => {
                    // Test passed
                }
                (actual, expected) => {
                    // Test failed
                    panic!("TC{index} failed because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n");
                }
            }
        }
    }

    #[test]
    fn test_coinbase_transform() {
        let mut transformer = coinbase(vec![Subscription::from((
            ExchangeId::Coinbase,
            "btc",
            "usd",
            InstrumentKind::Spot,
            SubKind::Trade,
        ))]);

        let time = Utc::now();

        struct TestCase {
            input: CoinbaseMessage,
            expected: Vec<Result<MarketEvent, SocketError>>,
        }

        let cases = vec![
            TestCase {
                // TC0: CoinbaseMessage Spot trades w/ known SubscriptionId
                input: CoinbaseMessage::Trade(CoinbaseTrade {
                    subscription_id: SubscriptionId::from("matches|BTC-USD"),
                    id: 2,
                    price: 1.0,
                    quantity: 1.0,
                    side: Side::Buy,
                    time,
                }),
                expected: vec![Ok(MarketEvent {
                    exchange_time: time,
                    received_time: time,
                    exchange: Exchange::from(ExchangeId::Coinbase),
                    instrument: Instrument::from(("btc", "usd", InstrumentKind::Spot)),
                    kind: DataKind::Trade(PublicTrade {
                        id: "2".to_string(),
                        price: 1.0,
                        quantity: 1.0,
                        side: Side::Buy,
                    }),
                })],
            },
            TestCase {
                // TC1: CoinbaseMessage with unknown SubscriptionId
                input: CoinbaseMessage::Trade(CoinbaseTrade {
                    subscription_id: SubscriptionId::from("unknown"),
                    id: 1,
                    price: 1.0,
                    quantity: 1.0,
                    side: Side::Buy,
                    time,
                }),
                expected: vec![Err(SocketError::Unidentifiable(SubscriptionId::from(
                    "unknown",
                )))],
            },
        ];

        for (index, test) in cases.into_iter().enumerate() {
            let actual = transformer.transform(test.input);
            assert_eq!(
                actual.len(),
                test.expected.len(),
                "TestCase {} failed at vector length assert_eq with actual: {:?}",
                index,
                actual
            );

            for (vector_index, (actual, expected)) in actual
                .into_iter()
                .zip(test.expected.into_iter())
                .enumerate()
            {
                match (actual, expected) {
                    (Ok(actual), Ok(expected)) => {
                        // Scrub Utc::now() timestamps to allow comparison
                        let actual = MarketEvent {
                            received_time: time,
                            ..actual
                        };
                        assert_eq!(
                            actual, expected,
                            "TC{} failed at vector index {}",
                            index, vector_index
                        )
                    }
                    (Err(_), Err(_)) => {
                        // Test passed
                    }
                    (actual, expected) => {
                        // Test failed
                        panic!("TC{index} failed at vector index {vector_index} because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n");
                    }
                }
            }
        }
    }
}