kraken-api-client 0.1.0

An async Rust client library for the Kraken exchange REST and WebSocket v2 APIs
Documentation
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Request and response types for Futures REST API.

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::futures::types::*;
use crate::types::common::BuySell;


// Response Wrappers


/// Response for tickers endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct TickersResponse {
    /// Result status
    pub result: String,
    /// List of tickers
    pub tickers: Vec<FuturesTicker>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for order book endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct OrderBookResponse {
    /// Result status
    pub result: String,
    /// Order book data
    #[serde(rename = "orderBook")]
    pub order_book: FuturesOrderBook,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for trade history endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct TradeHistoryResponse {
    /// Result status
    pub result: String,
    /// Trade history
    pub history: Vec<FuturesTrade>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for instruments endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct InstrumentsResponse {
    /// Result status
    pub result: String,
    /// List of instruments
    pub instruments: Vec<FuturesInstrument>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for accounts endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct AccountsResponse {
    /// Result status
    pub result: String,
    /// Account information by account type
    pub accounts: HashMap<String, FuturesAccount>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for open positions endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct OpenPositionsResponse {
    /// Result status
    pub result: String,
    /// List of open positions
    #[serde(rename = "openPositions")]
    pub open_positions: Vec<FuturesPosition>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Response for open orders endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct OpenOrdersResponse {
    /// Result status
    pub result: String,
    /// List of open orders
    #[serde(alias = "openOrders", alias = "orders")]
    pub open_orders: Vec<FuturesOrder>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Request for fills endpoint.
#[derive(Debug, Clone, Serialize)]
pub struct FillsRequest {
    /// Symbol filter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub symbol: Option<String>,
    /// Get fills after this time
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "lastFillTime")]
    pub last_fill_time: Option<String>,
}

/// Response for fills endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct FillsResponse {
    /// Result status
    pub result: String,
    /// List of fills
    pub fills: Vec<FuturesFill>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}


// Trading Request/Response Types


/// Request to send a new order.
#[derive(Debug, Clone, Serialize)]
pub struct SendOrderRequest {
    /// The order type (lmt, mkt, stp, take_profit, ioc)
    #[serde(rename = "orderType")]
    pub order_type: FuturesOrderType,
    /// The symbol (e.g., "PI_XBTUSD")
    pub symbol: String,
    /// Order side (buy or sell)
    pub side: BuySell,
    /// Order size (number of contracts)
    pub size: Decimal,
    /// Limit price (required for limit orders)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "limitPrice")]
    pub limit_price: Option<Decimal>,
    /// Stop price (required for stop orders)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "stopPrice")]
    pub stop_price: Option<Decimal>,
    /// Trigger signal for stop orders (mark or last)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "triggerSignal")]
    pub trigger_signal: Option<String>,
    /// Reduce-only order
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "reduceOnly")]
    pub reduce_only: Option<bool>,
    /// Client order ID
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
}

impl SendOrderRequest {
    /// Create a new limit order request.
    pub fn limit(symbol: impl Into<String>, side: BuySell, size: Decimal, price: Decimal) -> Self {
        Self {
            order_type: FuturesOrderType::Limit,
            symbol: symbol.into(),
            side,
            size,
            limit_price: Some(price),
            stop_price: None,
            trigger_signal: None,
            reduce_only: None,
            cli_ord_id: None,
        }
    }

    /// Create a new market order request.
    pub fn market(symbol: impl Into<String>, side: BuySell, size: Decimal) -> Self {
        Self {
            order_type: FuturesOrderType::Market,
            symbol: symbol.into(),
            side,
            size,
            limit_price: None,
            stop_price: None,
            trigger_signal: None,
            reduce_only: None,
            cli_ord_id: None,
        }
    }

    /// Create a new stop order request.
    pub fn stop(
        symbol: impl Into<String>,
        side: BuySell,
        size: Decimal,
        stop_price: Decimal,
    ) -> Self {
        Self {
            order_type: FuturesOrderType::Stop,
            symbol: symbol.into(),
            side,
            size,
            limit_price: None,
            stop_price: Some(stop_price),
            trigger_signal: None,
            reduce_only: None,
            cli_ord_id: None,
        }
    }

    /// Set the reduce-only flag.
    pub fn reduce_only(mut self, reduce_only: bool) -> Self {
        self.reduce_only = Some(reduce_only);
        self
    }

    /// Set a client order ID.
    pub fn cli_ord_id(mut self, id: impl Into<String>) -> Self {
        self.cli_ord_id = Some(id.into());
        self
    }

    /// Set the trigger signal for stop orders.
    pub fn trigger_signal(mut self, signal: impl Into<String>) -> Self {
        self.trigger_signal = Some(signal.into());
        self
    }
}

/// Response for send order endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct SendOrderResponse {
    /// Result status
    pub result: String,
    /// The status of the send (e.g., "placed")
    #[serde(rename = "sendStatus")]
    pub send_status: SendStatus,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Status of an order placement.
#[derive(Debug, Clone, Deserialize)]
pub struct SendStatus {
    /// Order ID
    #[serde(rename = "order_id")]
    pub order_id: String,
    /// Status message
    pub status: String,
    /// Received time
    #[serde(rename = "receivedTime")]
    pub received_time: Option<String>,
    /// Client order ID
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
}

/// Request to edit an existing order.
#[derive(Debug, Clone, Serialize)]
pub struct EditOrderRequest {
    /// Order ID to edit
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "orderId")]
    pub order_id: Option<String>,
    /// Client order ID to edit
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
    /// New size
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<Decimal>,
    /// New limit price
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "limitPrice")]
    pub limit_price: Option<Decimal>,
    /// New stop price
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "stopPrice")]
    pub stop_price: Option<Decimal>,
}

impl EditOrderRequest {
    /// Create an edit request by order ID.
    pub fn by_order_id(order_id: impl Into<String>) -> Self {
        Self {
            order_id: Some(order_id.into()),
            cli_ord_id: None,
            size: None,
            limit_price: None,
            stop_price: None,
        }
    }

    /// Create an edit request by client order ID.
    pub fn by_cli_ord_id(cli_ord_id: impl Into<String>) -> Self {
        Self {
            order_id: None,
            cli_ord_id: Some(cli_ord_id.into()),
            size: None,
            limit_price: None,
            stop_price: None,
        }
    }

    /// Set new size.
    pub fn size(mut self, size: Decimal) -> Self {
        self.size = Some(size);
        self
    }

    /// Set new limit price.
    pub fn limit_price(mut self, price: Decimal) -> Self {
        self.limit_price = Some(price);
        self
    }

    /// Set new stop price.
    pub fn stop_price(mut self, price: Decimal) -> Self {
        self.stop_price = Some(price);
        self
    }
}

/// Response for edit order endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct EditOrderResponse {
    /// Result status
    pub result: String,
    /// Edit status
    #[serde(rename = "editStatus")]
    pub edit_status: EditStatus,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Status of an order edit.
#[derive(Debug, Clone, Deserialize)]
pub struct EditStatus {
    /// Order ID
    #[serde(rename = "orderId")]
    pub order_id: String,
    /// Status message
    pub status: String,
    /// Received time
    #[serde(rename = "receivedTime")]
    pub received_time: Option<String>,
}

/// Response for cancel order endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct CancelOrderResponse {
    /// Result status
    pub result: String,
    /// Cancel status
    #[serde(rename = "cancelStatus")]
    pub cancel_status: CancelStatus,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Status of an order cancellation.
#[derive(Debug, Clone, Deserialize)]
pub struct CancelStatus {
    /// Order ID
    #[serde(rename = "order_id")]
    pub order_id: Option<String>,
    /// Client order ID
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
    /// Status message
    pub status: String,
    /// Received time
    #[serde(rename = "receivedTime")]
    pub received_time: Option<String>,
}

/// Response for cancel all orders endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct CancelAllOrdersResponse {
    /// Result status
    pub result: String,
    /// List of cancelled orders
    #[serde(rename = "cancelStatus")]
    pub cancel_status: CancelAllStatus,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Status of cancel all operation.
#[derive(Debug, Clone, Deserialize)]
pub struct CancelAllStatus {
    /// Number of orders cancelled
    #[serde(rename = "cancelledOrders")]
    pub cancelled_orders: Option<Vec<CancelledOrder>>,
    /// Status message
    pub status: Option<String>,
    /// Received time
    #[serde(rename = "receivedTime")]
    pub received_time: Option<String>,
}

/// Info about a cancelled order.
#[derive(Debug, Clone, Deserialize)]
pub struct CancelledOrder {
    /// Order ID
    #[serde(rename = "order_id")]
    pub order_id: String,
}

/// Response for cancel all orders after (dead man's switch).
#[derive(Debug, Clone, Deserialize)]
pub struct CancelAllOrdersAfterResponse {
    /// Result status
    pub result: String,
    /// The status of the request
    pub status: String,
    /// Current time
    #[serde(rename = "currentTime")]
    pub current_time: Option<String>,
    /// Trigger time (when orders will be cancelled)
    #[serde(rename = "triggerTime")]
    pub trigger_time: Option<String>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}


// Batch Order Types


/// Request for batch order operations.
#[derive(Debug, Clone, Serialize)]
pub struct BatchOrderRequest {
    /// The batch elements
    #[serde(rename = "batchOrder")]
    pub batch_order: Vec<BatchElement>,
}

impl BatchOrderRequest {
    /// Create a new batch request.
    pub fn new() -> Self {
        Self {
            batch_order: Vec::new(),
        }
    }

    /// Add a place order element.
    pub fn place(mut self, order: SendOrderRequest) -> Self {
        self.batch_order.push(BatchElement::Place(PlaceBatchElement {
            order_type: order.order_type,
            symbol: order.symbol,
            side: order.side,
            size: order.size,
            limit_price: order.limit_price,
            stop_price: order.stop_price,
            reduce_only: order.reduce_only,
            cli_ord_id: order.cli_ord_id,
        }));
        self
    }

    /// Add a cancel order element.
    pub fn cancel(mut self, order_id: impl Into<String>) -> Self {
        self.batch_order.push(BatchElement::Cancel(CancelBatchElement {
            order_id: Some(order_id.into()),
            cli_ord_id: None,
        }));
        self
    }

    /// Add a cancel by client order ID element.
    pub fn cancel_by_cli_ord_id(mut self, cli_ord_id: impl Into<String>) -> Self {
        self.batch_order.push(BatchElement::Cancel(CancelBatchElement {
            order_id: None,
            cli_ord_id: Some(cli_ord_id.into()),
        }));
        self
    }
}

impl Default for BatchOrderRequest {
    fn default() -> Self {
        Self::new()
    }
}

/// A single element in a batch order request.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "order", rename_all = "lowercase")]
pub enum BatchElement {
    /// Place a new order
    Place(PlaceBatchElement),
    /// Cancel an existing order
    Cancel(CancelBatchElement),
}

/// Element for placing an order in a batch.
#[derive(Debug, Clone, Serialize)]
pub struct PlaceBatchElement {
    #[serde(rename = "orderType")]
    pub order_type: FuturesOrderType,
    pub symbol: String,
    pub side: BuySell,
    pub size: Decimal,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "limitPrice")]
    pub limit_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "stopPrice")]
    pub stop_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "reduceOnly")]
    pub reduce_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
}

/// Element for cancelling an order in a batch.
#[derive(Debug, Clone, Serialize)]
pub struct CancelBatchElement {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "order_id")]
    pub order_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "cliOrdId")]
    pub cli_ord_id: Option<String>,
}

/// Response for batch order endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct BatchOrderResponse {
    /// Result status
    pub result: String,
    /// Batch status
    #[serde(rename = "batchStatus")]
    pub batch_status: Vec<BatchElementStatus>,
    /// Server time
    #[serde(rename = "serverTime")]
    pub server_time: Option<String>,
}

/// Status of a single element in a batch.
#[derive(Debug, Clone, Deserialize)]
pub struct BatchElementStatus {
    /// Order ID (for place operations)
    #[serde(rename = "order_id")]
    pub order_id: Option<String>,
    /// Status message
    pub status: String,
    /// Error message (if failed)
    #[serde(rename = "errorMessage")]
    pub error_message: Option<String>,
}

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

    #[test]
    fn test_send_order_request_limit() {
        let request = SendOrderRequest::limit("PI_XBTUSD", BuySell::Buy, Decimal::from(100), Decimal::from(50000))
            .reduce_only(true)
            .cli_ord_id("my-order-1");

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("limitPrice"));
        assert!(json.contains("reduceOnly"));
        assert!(json.contains("cliOrdId"));
    }

    #[test]
    fn test_send_order_request_market() {
        let request = SendOrderRequest::market("PI_ETHUSD", BuySell::Sell, Decimal::from(50));

        let json = serde_json::to_string(&request).unwrap();
        assert!(!json.contains("limitPrice"));
        assert!(json.contains("PI_ETHUSD"));
    }

    #[test]
    fn test_edit_order_request() {
        let request = EditOrderRequest::by_order_id("abc123")
            .size(Decimal::from(200))
            .limit_price(Decimal::from(51000));

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("orderId"));
        assert!(json.contains("size"));
        assert!(json.contains("limitPrice"));
    }

    #[test]
    fn test_batch_order_request() {
        let batch = BatchOrderRequest::new()
            .place(SendOrderRequest::limit("PI_XBTUSD", BuySell::Buy, Decimal::from(100), Decimal::from(50000)))
            .cancel("order-to-cancel");

        assert_eq!(batch.batch_order.len(), 2);
    }

    #[test]
    fn test_deserialize_send_order_response() {
        let json = r#"{
            "result": "success",
            "sendStatus": {
                "order_id": "abc123",
                "status": "placed",
                "receivedTime": "2024-01-15T10:00:00Z"
            },
            "serverTime": "2024-01-15T10:00:00Z"
        }"#;

        let response: SendOrderResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.result, "success");
        assert_eq!(response.send_status.order_id, "abc123");
    }
}