kraken_async_rs/wss/public/
orderbooks.rs

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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use crate::response_types::BidOrAsk;
use rust_decimal::Decimal;
use serde::de::{MapAccess, SeqAccess, Visitor};
use serde::{de, Deserialize, Deserializer};
use serde_tuple::Deserialize_tuple;
use std::fmt::Formatter;

/// A bid or ask, depending on context
#[derive(Debug, Deserialize_tuple, PartialEq)]
pub struct BidAsk {
    pub price: Decimal,
    pub volume: Decimal,
    pub time: String,
}

/// Message for an orderbook snapshot
#[derive(Debug, Deserialize, PartialEq)]
pub struct OrderbookMessage {
    #[serde(rename = "channelID")]
    pub channel_id: i64,
    pub orderbook: Orderbook,
    #[serde(rename = "channelName")]
    pub channel_name: String,
    pub pair: String,
}

/// Orderbook containing a `Vec<BidAsk>` for each of bids and asks
#[derive(Debug, Deserialize, PartialEq)]
pub struct Orderbook {
    #[serde(rename = "as")]
    pub asks: Vec<BidAsk>,
    #[serde(rename = "bs")]
    pub bids: Vec<BidAsk>,
}

/// Update to a price point for bids or asks
#[derive(Debug, PartialEq)]
pub struct BidAskUpdate {
    pub price: Decimal,
    pub volume: Decimal,
    pub timestamp: String,
    pub update_type: Option<String>,
}

/// Updates for a particular side of the book, including checksum of final book state
#[derive(Debug, PartialEq)]
struct BidAskUpdates {
    pub side: BidOrAsk,
    pub updates: Vec<BidAskUpdate>,
    pub checksum: Option<String>,
}

/// Orderbook update message containing all updates and the expected checksum of the final book
#[derive(Debug, PartialEq)]
pub struct OrderbookUpdateMessage {
    pub channel_id: i64,
    pub bids: Vec<BidAskUpdate>,
    pub asks: Vec<BidAskUpdate>,
    pub channel_name: String,
    pub pair: String,
    pub checksum: String,
}

impl<'de> Deserialize<'de> for OrderbookUpdateMessage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Debug, Deserialize, PartialEq)]
        #[serde(untagged)]
        enum MessageField {
            ChannelId(i64),
            BidAskUpdates(BidAskUpdates),
            String(String),
        }

        struct OrderbookUpdateMessageVisitor;

        impl<'de> Visitor<'de> for OrderbookUpdateMessageVisitor {
            type Value = OrderbookUpdateMessage;

            fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
                formatter.write_str("OrderbookUpdateMessage")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                if let Some(size) = seq.size_hint() {
                    if !(4..=5).contains(&size) {
                        return Err(de::Error::invalid_length(size, &self));
                    }
                }

                let channel_id;
                let mut bids = None;
                let mut asks = None;
                let mut channel_name = None;
                let mut pair = None;
                let mut checksum = None;

                let channel_id_field: MessageField = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                let first_update_field: MessageField = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(1, &self))?;
                let update_or_channel_name_field: MessageField = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(2, &self))?;

                if let MessageField::ChannelId(id) = channel_id_field {
                    channel_id = Some(id);
                } else {
                    return Err(de::Error::custom("first field was not channel id as i64"));
                }

                match update_or_channel_name_field {
                    MessageField::ChannelId(_) => {}
                    MessageField::String(name) => {
                        channel_name = Some(name);

                        let pair_field: MessageField = seq
                            .next_element()?
                            .ok_or_else(|| de::Error::invalid_length(3, &self))?;

                        if let MessageField::String(pair_value) = pair_field {
                            pair = Some(pair_value)
                        }

                        match first_update_field {
                            MessageField::BidAskUpdates(first_updates) => {
                                match first_updates.side {
                                    BidOrAsk::Bid => bids = Some(first_updates.updates),
                                    BidOrAsk::Ask => asks = Some(first_updates.updates),
                                }

                                checksum = first_updates.checksum;
                            }
                            _ => {
                                return Err(de::Error::custom(
                                    "second field could not be deserialized to BidAskUpdates",
                                ));
                            }
                        }
                    }
                    MessageField::BidAskUpdates(second_updates) => {
                        let name_field: MessageField = seq
                            .next_element()?
                            .ok_or_else(|| de::Error::invalid_length(4, &self))?;

                        let pair_field: MessageField = seq
                            .next_element()?
                            .ok_or_else(|| de::Error::invalid_length(5, &self))?;

                        if let MessageField::String(name) = name_field {
                            channel_name = Some(name)
                        }

                        if let MessageField::String(pair_value) = pair_field {
                            pair = Some(pair_value)
                        }

                        if let MessageField::BidAskUpdates(first_updates) = first_update_field {
                            match first_updates.side {
                                BidOrAsk::Bid => bids = Some(first_updates.updates),
                                BidOrAsk::Ask => asks = Some(first_updates.updates),
                            }
                        } else {
                            return Err(de::Error::custom(
                                "second field could not be deserialized to BidAskUpdates",
                            ));
                        }

                        match second_updates.side {
                            BidOrAsk::Bid => bids = Some(second_updates.updates),
                            BidOrAsk::Ask => asks = Some(second_updates.updates),
                        }

                        checksum = second_updates.checksum
                    }
                }

                if let Some(checksum) = checksum {
                    Ok(OrderbookUpdateMessage {
                        channel_id: channel_id.unwrap(),
                        bids: bids.unwrap_or(vec![]),
                        asks: asks.unwrap_or(vec![]),
                        channel_name: channel_name.unwrap(),
                        pair: pair.unwrap(),
                        checksum,
                    })
                } else {
                    Err(de::Error::custom("checksum not present on bids or asks"))
                }
            }
        }

        const FIELDS: &[&str] = &["channel_id", "bids", "asks", "channel_name", "pair"];
        deserializer.deserialize_struct(
            "OrderbookUpdateMessage",
            FIELDS,
            OrderbookUpdateMessageVisitor,
        )
    }
}

impl<'de> Deserialize<'de> for BidAskUpdates {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct BidAskUpdatesVisitor;

        impl<'de> Visitor<'de> for BidAskUpdatesVisitor {
            type Value = BidAskUpdates;

            fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
                formatter.write_str("object containing 'a' or 'b' key")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                #[derive(Debug, Deserialize, PartialEq)]
                #[serde(untagged)]
                enum BidAskUpdatesOrChecksum {
                    BidAskUpdates(Vec<BidAskUpdate>),
                    Checksum(String),
                }

                let mut side = BidOrAsk::Bid;
                let mut updates = None;
                let mut checksum = None;

                if let Some(size) = map.size_hint() {
                    if !(1..=2).contains(&size) {
                        return Err(de::Error::invalid_length(size, &self));
                    }
                }

                while let Some((key, value)) =
                    map.next_entry::<String, BidAskUpdatesOrChecksum>()?
                {
                    match value {
                        BidAskUpdatesOrChecksum::BidAskUpdates(value) => {
                            updates = Some(value);
                            match key.as_str() {
                                "a" => side = BidOrAsk::Ask,
                                "b" => side = BidOrAsk::Bid,
                                field => {
                                    return Err(de::Error::unknown_field(field, &["a", "b", "c"]));
                                }
                            }
                        }
                        BidAskUpdatesOrChecksum::Checksum(value) => checksum = Some(value),
                    }
                }

                let updates = updates.ok_or_else(|| de::Error::missing_field("a | b"))?;

                Ok(BidAskUpdates {
                    side,
                    updates,
                    checksum,
                })
            }
        }

        const FIELDS: &[&str] = &["updates", "checksum"];
        deserializer.deserialize_struct("Bid", FIELDS, BidAskUpdatesVisitor)
    }
}

impl<'de> Deserialize<'de> for BidAskUpdate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct BidAskUpdatesVisitor;

        impl<'de> Visitor<'de> for BidAskUpdatesVisitor {
            type Value = BidAskUpdate;

            fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
                formatter.write_str("sequence BidAskUpdate")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let price = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                let volume = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(1, &self))?;
                let timestamp = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(2, &self))?;
                let update_type = seq.next_element()?.unwrap_or(None);

                Ok(BidAskUpdate {
                    price,
                    volume,
                    timestamp,
                    update_type,
                })
            }
        }

        const FIELDS: &[&str] = &["price", "volume", "timestamp", "update_type"];

        deserializer.deserialize_struct("BidAskUpdates", FIELDS, BidAskUpdatesVisitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::response_types::BidOrAsk::{Ask, Bid};
    use rust_decimal_macros::dec;

    const BID_ASK_UPDATE: &str = "[\"34236.30000\",\"0.20206679\",\"1698231193.410190\"]";
    const BID_ASK_UPDATE_REPUBLISH: &str =
        "[\"34210.00000\",\"2.23575055\",\"1698230978.434643\",\"r\"]";
    const BIDS_ONLY: &str =
        "{\"b\":[[\"34211.00000\",\"2.92303645\",\"1698230979.415494\"]],\"c\":\"976603157\"}";
    const ASKS_ONLY: &str =
        "{\"a\":[[\"34240.10000\",\"0.36250000\",\"1698230979.497975\"]],\"c\":\"2409299850\"}";
    const BIDS_WITHOUT_CHECKSUM: &str =
        "{\"b\":[[\"34211.00000\",\"2.92303645\",\"1698230979.415494\"]]}";

    const ASK_ONLY_MESSAGE: &str = "[336,{\"a\":[[\"34240.10000\",\"0.36250000\",\"1698230979.497975\"]],\"c\":\"2409299850\"},\"book-10\",\"XBT/USD\"]";
    const BID_ONLY_MESSAGE: &str = "[336,{\"b\":[[\"34211.00000\",\"2.92303645\",\"1698230979.415494\"]],\"c\":\"976603157\"},\"book-10\",\"XBT/USD\"]";
    const BID_ASK_MESSAGE: &str = "[336,{\
    \"a\":[[\"34236.30000\",\"0.20206679\",\"1698231193.410190\"]]},\
    {\"b\":[[\"34239.90000\",\"0.24117192\",\"1698231193.409849\"],[\"34208.40000\",\"0.36250000\",\"1698231193.221649\",\"r\"]],\"c\":\"4258113849\"},\
    \"book-10\",\"XBT/USD\"]";

    const BID_ASK_MESSAGE_MISSING_CHECKSUM: &str = "[336,{\
    \"a\":[[\"34236.30000\",\"0.20206679\",\"1698231193.410190\"]]},\
    {\"b\":[[\"34239.90000\",\"0.24117192\",\"1698231193.409849\"],[\"34208.40000\",\"0.36250000\",\"1698231193.221649\",\"r\"]]},\
    \"book-10\",\"XBT/USD\"]";

    const BID_ASK_MESSAGE_INCORRECT_FIRST_FIELD: &str =
        "[336,\"thisShouldn'tBeAString\",\"book-10\",\"XBT/USD\"]";

    #[test]
    fn test_deserializing_update() {
        let expected_bid_ask_update: BidAskUpdate = BidAskUpdate {
            price: dec!(34236.30000),
            volume: dec!(0.20206679),
            timestamp: "1698231193.410190".to_string(),
            update_type: None,
        };

        let bid_ask_update: BidAskUpdate =
            serde_json::from_str(BID_ASK_UPDATE).expect("Deserialize should not fail");

        assert_eq!(expected_bid_ask_update, bid_ask_update);
    }

    #[test]
    fn test_deserializing_update_republish() {
        let expected_bid_ask_update: BidAskUpdate = BidAskUpdate {
            price: dec!(34210.00000),
            volume: dec!(2.23575055),
            timestamp: "1698230978.434643".to_string(),
            update_type: Some("r".to_string()),
        };

        let bid_ask_update: BidAskUpdate = serde_json::from_str(BID_ASK_UPDATE_REPUBLISH).unwrap();

        assert_eq!(expected_bid_ask_update, bid_ask_update);
    }

    #[test]
    fn test_deserializing_bids_only() {
        let expected_bids_update = BidAskUpdates {
            side: Bid,
            updates: vec![BidAskUpdate {
                price: dec!(34211.00000),
                volume: dec!(2.92303645),
                timestamp: "1698230979.415494".to_string(),
                update_type: None,
            }],
            checksum: Some("976603157".into()),
        };

        let bids_update: BidAskUpdates = serde_json::from_str(BIDS_ONLY).unwrap();

        assert_eq!(expected_bids_update, bids_update);
    }

    #[test]
    fn test_deserializing_asks_only() {
        let expected_asks_update = BidAskUpdates {
            side: Ask,
            updates: vec![BidAskUpdate {
                price: dec!(34240.10000),
                volume: dec!(0.36250000),
                timestamp: "1698230979.497975".to_string(),
                update_type: None,
            }],
            checksum: Some("2409299850".into()),
        };

        let bids_update: BidAskUpdates = serde_json::from_str(ASKS_ONLY).unwrap();

        assert_eq!(expected_asks_update, bids_update);
    }

    #[test]
    fn test_deserializing_bids_without_checksum() {
        let expected_asks_update = BidAskUpdates {
            side: Bid,
            updates: vec![BidAskUpdate {
                price: dec!(34211.00000),
                volume: dec!(2.92303645),
                timestamp: "1698230979.415494".to_string(),
                update_type: None,
            }],
            checksum: None,
        };

        let bids_update: BidAskUpdates = serde_json::from_str(BIDS_WITHOUT_CHECKSUM).unwrap();

        assert_eq!(expected_asks_update, bids_update);
    }

    #[test]
    fn test_deserializing_orderbook_message_asks_only() {
        let expected_message = OrderbookUpdateMessage {
            channel_id: 336,
            bids: vec![],
            asks: vec![BidAskUpdate {
                price: dec!(34240.10000),
                volume: dec!(0.36250000),
                timestamp: "1698230979.497975".to_string(),
                update_type: None,
            }],
            channel_name: "book-10".to_string(),
            pair: "XBT/USD".to_string(),
            checksum: "2409299850".into(),
        };

        let message: OrderbookUpdateMessage = serde_json::from_str(ASK_ONLY_MESSAGE).unwrap();

        assert_eq!(expected_message, message);
    }

    #[test]
    fn test_deserializing_orderbook_message_bids_only() {
        let expected_message = OrderbookUpdateMessage {
            channel_id: 336,
            bids: vec![BidAskUpdate {
                price: dec!(34211.00000),
                volume: dec!(2.92303645),
                timestamp: "1698230979.415494".to_string(),
                update_type: None,
            }],
            asks: vec![],
            channel_name: "book-10".to_string(),
            pair: "XBT/USD".to_string(),
            checksum: "976603157".to_string(),
        };

        let message: OrderbookUpdateMessage = serde_json::from_str(BID_ONLY_MESSAGE).unwrap();

        assert_eq!(expected_message, message);
    }

    #[test]
    fn test_deserializing_orderbook_message_bids_and_asks() {
        let expected_message = OrderbookUpdateMessage {
            channel_id: 336,
            bids: vec![
                BidAskUpdate {
                    price: dec!(34239.90000),
                    volume: dec!(0.24117192),
                    timestamp: "1698231193.409849".to_string(),
                    update_type: None,
                },
                BidAskUpdate {
                    price: dec!(34208.40000),
                    volume: dec!(0.36250000),
                    timestamp: "1698231193.221649".to_string(),
                    update_type: Some("r".into()),
                },
            ],
            asks: vec![BidAskUpdate {
                price: dec!(34236.30000),
                volume: dec!(0.20206679),
                timestamp: "1698231193.410190".to_string(),
                update_type: None,
            }],
            channel_name: "book-10".to_string(),
            pair: "XBT/USD".to_string(),
            checksum: "4258113849".to_string(),
        };

        let message: OrderbookUpdateMessage = serde_json::from_str(BID_ASK_MESSAGE).unwrap();

        assert_eq!(expected_message, message);
    }

    #[test]
    #[should_panic(expected = "checksum not present on bids or asks")]
    fn test_deserializing_orderbook_message_missing_checksum() {
        let _message: OrderbookUpdateMessage =
            serde_json::from_str(BID_ASK_MESSAGE_MISSING_CHECKSUM).unwrap();
    }

    #[test]
    #[should_panic(expected = "second field could not be deserialized to BidAskUpdates")]
    fn test_deserializing_orderbook_message_invalid_first_field() {
        let _message: OrderbookUpdateMessage =
            serde_json::from_str(BID_ASK_MESSAGE_INCORRECT_FIRST_FIELD).unwrap();
    }
}