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
use std::collections::HashMap;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InstanceServers {
    pub instance_servers: Vec<InstanceServer>,
    pub token: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InstanceServer {
    pub ping_interval: i32,
    pub endpoint: String,
    pub protocol: String,
    pub encrypt: bool,
    pub ping_timeout: i32,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum WSTopic {
    Ticker(Vec<String>),
    AllTicker,
    Snapshot(String),
    OrderBook(Vec<String>),
    Match(Vec<String>),
    Level3Public(Vec<String>),
    Level3Private(Vec<String>),
    IndexPrice(Vec<String>),
    MarketPrice(Vec<String>),
    OrderBookChange(Vec<String>),
    StopOrder(Vec<String>),
    Balances,
    DebtRatio,
    PositionChange,
    MarginTradeOrder(String),
}

pub enum WSType {
    Public,
    Private,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum KucoinWebsocketMsg {
    WelcomeMsg(DefaultMsg),
    SubscribeMsg(Subscribe),
    PingMsg(DefaultMsg),
    PongMsg(DefaultMsg),
    Ping,
    Pong,
    Binary(Vec<u8>),
    TickerMsg(WSResp<SymbolTicker>),
    AllTickerMsg(WSResp<SymbolTicker>),
    SnapshotMsg(WSResp<Snapshot>),
    OrderBookMsg(WSResp<Level2>),
    MatchMsg(WSResp<Match>),
    Level3ReceivedMsg(WSResp<Level3Received>),
    Level3OpenMsg(WSResp<Level3Open>),
    Level3MatchMsg(WSResp<Level3Match>),
    Level3DoneMsg(WSResp<Level3Done>),
    Level3ChangeMsg(WSResp<Level3Change>),
    IndexPriceMsg(WSResp<IndexPrice>),
    MarketPriceMsg(WSResp<MarketPrice>),
    OrderBookChangeMsg(WSResp<BookChange>),
    StopOrderMsg(WSResp<StopOrder>),
    BalancesMsg(WSResp<Balances>),
    DebtRatioMsg(WSResp<DebtRatio>),
    PositionChangeMsg(WSResp<PositionChange>),
    MarginTradeOpenMsg(WSResp<MarginTradeOpen>),
    MarginTradeUpdateMsg(WSResp<MarginTradeUpdate>),
    MarginTradeDoneMsg(WSResp<MarginTradeDone>),
    Error(String),
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WSResp<T> {
    pub r#type: String,
    pub topic: String,
    pub subject: String,
    pub data: T,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DefaultMsg {
    pub id: String,
    pub r#type: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Subscribe {
    pub id: String,
    pub r#type: String,
    pub topic: String,
    pub private_channel: bool,
    pub response: bool,
}


#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SymbolTicker {
    pub sequence: String,
    pub best_ask: String,
    pub size: String,
    pub best_bid_size: String,
    pub price: String,
    pub best_ask_size: String,
    pub best_bid: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Snapshot {
    pub sequence: i64,
    pub data: SnapshotData,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotData {
    pub trading: bool,
    pub symbol: String,
    pub buy: f32,
    pub sell: f32,
    pub sort: i32,
    pub vol_value: f32,
    pub base_currency: String,
    pub market: String,
    pub quote_currency: String,
    pub symbol_code: String,
    pub datetime: i64,
    pub high: Option<f32>,
    pub vol: f32,
    pub low: Option<f32>,
    pub change_price: Option<f32>,
    pub change_rate: f32,
    pub last_traded_price: f32,
    pub board: i32,
    pub mark: i32,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level2 {
    pub sequence_start: i64,
    pub sequence_end: i64,
    pub symbol: String,
    pub changes: Level2Changes,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level2Changes {
    pub asks: Vec<Vec<String>>,
    pub bids: Vec<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Match {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub size: String,
    pub price: String,
    pub taker_order_id: String,
    pub time: String,
    pub r#type: String,
    pub maker_order_id: String,
    pub trade_id: String
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level3Received {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub order_id: String,
    pub price: Option<String>,
    pub time: String,
    pub client_oid: Option<String>,
    pub r#type: String,
    pub order_type: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level3Open {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub size: String,
    pub order_id: String,
    pub price: String,
    pub time: String,
    pub r#type: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level3Done {
    pub sequence: String,
    pub symbol: String,
    pub reason: String,
    pub side: String,
    pub order_id: String,
    pub time: String,
    pub r#type: String,
    pub size: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level3Match {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub size: String,
    pub price: String,
    pub taker_order_id: String,
    pub time: String,
    pub r#type: String,
    pub maker_order_id: String,
    pub trade_id: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Level3Change {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub order_id: String,
    pub price: String,
    pub new_size: String,
    pub time: String,
    pub r#type: String,
    pub old_size: String,
}


#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexPrice {
    pub symbol: String,
    pub granularity: i32,
    pub timestamp: i64,
    pub value: f32,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MarketPrice {
    pub symbol: String,
    pub granularity: i32,
    pub timestamp: i64,
    pub value: f32,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BookChange {
    pub sequence: i32,
    pub currency: String,
    pub daily_int_rate: f32,
    pub annual_int_rate: f32,
    pub term: i32,
    pub size: f32,
    pub side: String,
    pub ts: i64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StopOrder {
    pub sequence: String,
    pub symbol: String,
    pub side: String,
    pub order_id: String,
    pub stop_entry: String,
    pub funds: String,
    pub time: String,
    pub r#type: String,
    pub reason: Option<String>
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Balances {
    pub total: String,
    pub available: String,
    pub available_change: String,
    pub currency: String,
    pub hold: String,
    pub hold_change: String,
    pub relation_event: String,
    pub relation_event_id: String,
    pub time: String,
    pub account_id: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DebtRatio {
    pub debt_ratio: f32,
    pub total_debt: String,
    pub debt_list: HashMap<String, String>,
    pub timestamp: i64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PositionChange {
    pub r#type: String,
    pub timestamp: i64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginTradeOpen {
    pub currency: String,
    pub order_id: String,
    pub daily_int_rate: f32,
    pub term: i32,
    pub size: i32,
    pub side: String,
    pub ts: i64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginTradeUpdate {
    pub currency: String,
    pub order_id: String,
    pub daily_int_rate: f32,
    pub term: i32,
    pub size: i32,
    pub lent_size: f32,
    pub side: String,
    pub ts: i64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginTradeDone {
    pub currency: String,
    pub order_id: String,
    pub reason: String,
    pub side: String,
    pub ts: i64,
}