kraken_async_rs/wss/
subscribe_messages.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
//! Subscribe and unsubscribe messages for websocket channels
use crate::crypto::secrets::Token;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::fmt::{Display, Formatter};

/// All available channels for subscription
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum SubscriptionName {
    Book,
    Ohlc,
    OpenOrders,
    OwnTrades,
    Spread,
    Ticker,
    Trade,
    #[serde(rename = "*")]
    All,
}

impl Display for SubscriptionName {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SubscriptionName::Book => write!(f, "book"),
            SubscriptionName::Ohlc => write!(f, "ohlc"),
            SubscriptionName::OpenOrders => write!(f, "openOrders"),
            SubscriptionName::OwnTrades => write!(f, "ownTrades"),
            SubscriptionName::Spread => write!(f, "spread"),
            SubscriptionName::Ticker => write!(f, "ticker"),
            SubscriptionName::Trade => write!(f, "trade"),
            SubscriptionName::All => write!(f, "*"),
        }
    }
}

/// General struct for subscribing to any channel.
///
/// Not all fields apply to each subscription, see the individual `new_*` methods for constructing
/// well-formed subscriptions for each channel.
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Subscription {
    pub depth: Option<i64>,
    pub interval: Option<i64>,
    pub name: Option<SubscriptionName>,
    #[serde(rename = "ratecounter")]
    pub rate_counter: Option<bool>,
    pub snapshot: Option<bool>,
    pub token: Option<Token>,
    pub consolidate_taker: Option<bool>,
}

impl Subscription {
    /// Create a new subscription to the orderbook at a given depth.
    pub fn new_book_subscription(depth: Option<i64>) -> Subscription {
        Subscription {
            depth,
            interval: None,
            name: Some(SubscriptionName::Book),
            rate_counter: None,
            snapshot: None,
            token: None,
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to the OHLC channel at the given interval.
    pub fn new_ohlc_subscription(interval: Option<i64>) -> Subscription {
        Subscription {
            depth: None,
            interval,
            name: Some(SubscriptionName::Ohlc),
            rate_counter: None,
            snapshot: None,
            token: None,
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to the OwnTrades channel for a specific token.
    ///
    /// Optionally receive a snapshot of recent orders.
    pub fn new_own_trades_subscription(token: Token, snapshot: Option<bool>) -> Subscription {
        Subscription {
            depth: None,
            interval: None,
            name: Some(SubscriptionName::OwnTrades),
            rate_counter: None,
            snapshot,
            token: Some(token),
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to the public trades channel.
    pub fn new_trades_subscription() -> Subscription {
        Subscription {
            depth: None,
            interval: None,
            name: Some(SubscriptionName::Trade),
            rate_counter: None,
            snapshot: None,
            token: None,
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to the Ticker channel.
    pub fn new_ticker_subscription() -> Subscription {
        Subscription {
            depth: None,
            interval: None,
            name: Some(SubscriptionName::Ticker),
            rate_counter: None,
            snapshot: None,
            token: None,
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to the Spread channel for best bids/asks.
    pub fn new_spread_subscription() -> Subscription {
        Subscription {
            depth: None,
            interval: None,
            name: Some(SubscriptionName::Spread),
            rate_counter: None,
            snapshot: None,
            token: None,
            consolidate_taker: None,
        }
    }

    /// Create a new subscription to all open orders for the user.
    ///
    /// Optionally get rate limiter updates from the server.
    pub fn new_open_orders_subscription(token: Token, rate_counter: Option<bool>) -> Subscription {
        Subscription {
            depth: None,
            interval: None,
            name: Some(SubscriptionName::OpenOrders),
            rate_counter,
            snapshot: None,
            token: Some(token),
            consolidate_taker: None,
        }
    }
}

/// Message for unsubscribing from a given channel.
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Unsubscription {
    pub depth: Option<i64>,
    pub interval: Option<i64>,
    pub name: Option<SubscriptionName>,
    pub token: Option<Token>,
}

/// Using a given [Subscription] message, generate the corresponding [Unsubscription] message.
impl From<Subscription> for Unsubscription {
    fn from(value: Subscription) -> Self {
        Unsubscription {
            depth: value.depth,
            interval: value.interval,
            name: value.name,
            token: value.token,
        }
    }
}

/// Struct for subscribing to any websocket channel.
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SubscribeMessage {
    pub event: String,
    #[serde(rename = "reqid")]
    pub req_id: i64,
    pub pair: Option<Vec<String>>,
    pub subscription: Subscription,
}

impl SubscribeMessage {
    /// Return a new [SubscribeMessage], potentially for a specific set of pairs.
    pub fn new(
        req_id: i64,
        pair: Option<Vec<String>>,
        subscription: Subscription,
    ) -> SubscribeMessage {
        SubscribeMessage {
            event: "subscribe".into(),
            req_id,
            pair,
            subscription,
        }
    }
}

/// A message to unsubscribe from the given channel and optionally for a specific pair.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UnsubscribeMessage {
    pub event: String,
    #[serde(rename = "reqid")]
    pub req_id: i64,
    pub pair: Option<Vec<String>>,
    pub subscription: Unsubscription,
}

impl UnsubscribeMessage {
    pub fn new(
        req_id: i64,
        pair: Option<Vec<String>>,
        unsubscription: Unsubscription,
    ) -> UnsubscribeMessage {
        UnsubscribeMessage {
            event: "unsubscribe".into(),
            req_id,
            pair,
            subscription: unsubscription,
        }
    }
}

impl From<SubscribeMessage> for UnsubscribeMessage {
    /// Create the corresponding [UnsubscribeMessage] for this [SubscribeMessage]
    fn from(value: SubscribeMessage) -> Self {
        UnsubscribeMessage {
            event: "unsubscribe".to_string(),
            req_id: value.req_id,
            pair: value.pair,
            subscription: value.subscription.into(),
        }
    }
}

/// Message to unsubscribe from a given channel or pair
#[derive(Debug, Deserialize)]
pub struct Unsubscribe {
    pub event: String,
    #[serde(rename = "reqid")]
    pub req_id: i64,
    pub pair: Vec<String>,
    pub subscription: Unsubscription,
}

/// Generic response for any subscription
///
/// This optionally contains fields relevant to each type of subscription.
#[derive(Debug, PartialEq, Deserialize)]
pub struct SubscriptionResponse {
    pub depth: Option<i64>,
    pub interval: Option<i64>,
    #[serde(rename = "maxratecount")]
    pub max_rate_count: Option<i64>,
    pub name: Option<SubscriptionName>,
    pub token: Option<Token>,
}

/// Status message returned after a subscription
#[derive(Debug, PartialEq, Deserialize)]
pub struct SubscriptionStatus {
    #[serde(rename = "channelID")]
    pub channel_id: Option<i64>,
    #[serde(rename = "channelName")]
    pub channel_name: String,
    pub event: String,
    pub pair: Option<String>,
    #[serde(rename = "reqid")]
    pub req_id: i64,
    pub status: String,
    pub subscription: SubscriptionResponse,
    #[serde(rename = "OneOf")]
    pub one_of: Option<OneOf>,
}

/// Wrapper type for an error message or the successfully subscribed channel id
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct OneOf {
    #[serde(rename = "errorMessage")]
    pub error_message: String,
    #[serde(rename = "channelID")]
    pub channel_id: i64,
}

#[cfg(test)]
mod tests {
    use crate::crypto::secrets::Token;
    use crate::wss::subscribe_messages::{Subscription, SubscriptionName};

    #[test]
    fn test_new_book_subscription() {
        let book_subscription = Subscription::new_book_subscription(Some(10));
        assert_eq!(Some(10), book_subscription.depth);
        assert_eq!(Some(SubscriptionName::Book), book_subscription.name);
    }

    #[test]
    fn test_new_ohlc_subscription() {
        let ohlc_subscription = Subscription::new_ohlc_subscription(Some(60));
        assert_eq!(Some(60), ohlc_subscription.interval);
        assert_eq!(Some(SubscriptionName::Ohlc), ohlc_subscription.name);
    }

    #[test]
    fn test_new_own_trades_subscription() {
        let ohlc_subscription = Subscription::new_own_trades_subscription(
            Token::new("someToken".to_string()),
            Some(true),
        );
        assert_eq!(
            Some(Token::new("someToken".to_string())),
            ohlc_subscription.token
        );
        assert_eq!(Some(true), ohlc_subscription.snapshot);
        assert_eq!(Some(SubscriptionName::OwnTrades), ohlc_subscription.name);
    }

    #[test]
    fn test_new_ticker_subscription() {
        let ohlc_subscription = Subscription::new_ticker_subscription();
        assert_eq!(Some(SubscriptionName::Ticker), ohlc_subscription.name);
    }

    #[test]
    fn test_new_spread_subscription() {
        let ohlc_subscription = Subscription::new_spread_subscription();
        assert_eq!(Some(SubscriptionName::Spread), ohlc_subscription.name);
    }

    #[test]
    fn test_new_open_orders_subscription() {
        let ohlc_subscription = Subscription::new_open_orders_subscription(
            Token::new("someToken".to_string()),
            Some(false),
        );
        assert_eq!(
            Some(Token::new("someToken".to_string())),
            ohlc_subscription.token
        );
        assert_eq!(Some(false), ohlc_subscription.rate_counter);
        assert_eq!(Some(SubscriptionName::OpenOrders), ohlc_subscription.name);
    }
}