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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Futures-specific domain types.
//!
//! This module contains types specific to Futures trading that differ from
//! or extend the Spot API types.

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::types::common::BuySell;


// Contract Types


/// Type of futures contract.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ContractType {
    /// Perpetual contract (no expiry)
    #[default]
    Perpetual,
    /// Fixed maturity contract (has expiry date)
    #[serde(alias = "futures_inverse", alias = "futures_vanilla")]
    FixedMaturity,
    /// Index (not tradeable)
    #[serde(alias = "spot index")]
    Index,
}

/// Futures order type.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FuturesOrderType {
    /// Limit order
    #[serde(alias = "lmt")]
    #[default]
    Limit,
    /// Market order
    #[serde(alias = "mkt")]
    Market,
    /// Stop order (stop-loss)
    #[serde(alias = "stp")]
    Stop,
    /// Take profit order
    TakeProfit,
    /// Immediate or cancel
    #[serde(alias = "ioc")]
    ImmediateOrCancel,
    /// Post-only (maker only)
    PostOnly,
}

/// Order status for futures orders.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FuturesOrderStatus {
    /// Order is in the book
    #[serde(alias = "untouched")]
    #[default]
    Open,
    /// Order is partially filled
    #[serde(alias = "partiallyFilled")]
    PartiallyFilled,
    /// Order is fully filled
    Filled,
    /// Order was cancelled
    Cancelled,
}

/// Fill type classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FillType {
    /// Maker (provided liquidity)
    Maker,
    /// Taker (removed liquidity)
    Taker,
    /// Liquidation
    Liquidation,
    /// Assignment (options)
    Assignee,
    /// Assigned from (options)
    Assignor,
}


// Account Types


/// Futures account type.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AccountType {
    /// Cash account (no leverage)
    #[serde(alias = "cashAccount")]
    #[default]
    Cash,
    /// Margin account (single currency collateral)
    #[serde(alias = "marginAccount")]
    Margin,
    /// Multi-collateral margin account
    #[serde(alias = "multiCollateralMarginAccount")]
    MultiCollateral,
    /// Flex futures (cross-margined)
    FlexFutures,
}


// Position and Order Structs


/// A futures position.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesPosition {
    /// The futures symbol (e.g., "PI_XBTUSD")
    pub symbol: String,
    /// Position side
    pub side: BuySell,
    /// Position size (positive)
    pub size: Decimal,
    /// Average entry price
    #[serde(alias = "price")]
    pub entry_price: Decimal,
    /// Current mark price
    #[serde(default)]
    pub mark_price: Option<Decimal>,
    /// Liquidation price (None for cash accounts)
    #[serde(default)]
    pub liquidation_threshold: Option<Decimal>,
    /// Unrealized PnL
    #[serde(default)]
    pub unrealized_pnl: Option<Decimal>,
    /// Unrealized funding (perpetuals only)
    #[serde(default, alias = "unrealizedFunding")]
    pub unrealized_funding: Option<Decimal>,
    /// Initial margin requirement
    #[serde(default)]
    pub initial_margin: Option<Decimal>,
    /// Maintenance margin requirement
    #[serde(default)]
    pub maintenance_margin: Option<Decimal>,
    /// Effective leverage
    #[serde(default)]
    pub effective_leverage: Option<Decimal>,
    /// Return on equity percentage
    #[serde(default)]
    pub return_on_equity: Option<Decimal>,
    /// PnL currency for multi-collateral
    #[serde(default)]
    pub pnl_currency: Option<String>,
    /// Maximum fixed leverage for isolated positions
    #[serde(default, alias = "maxFixedLeverage")]
    pub max_fixed_leverage: Option<Decimal>,
    /// Fill time (deprecated but still returned)
    #[serde(default, alias = "fillTime")]
    pub fill_time: Option<String>,
}

/// A futures order.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesOrder {
    /// Order ID
    #[serde(alias = "order_id")]
    pub order_id: String,
    /// Client order ID (if provided)
    #[serde(default, alias = "cliOrdId")]
    pub cli_ord_id: Option<String>,
    /// Futures symbol
    pub symbol: String,
    /// Order side
    pub side: BuySell,
    /// Order type
    #[serde(alias = "orderType")]
    pub order_type: FuturesOrderType,
    /// Order status
    pub status: FuturesOrderStatus,
    /// Total order size
    #[serde(alias = "quantity", alias = "qty")]
    pub size: Decimal,
    /// Filled size
    #[serde(default, alias = "filledSize")]
    pub filled_size: Decimal,
    /// Unfilled (remaining) size
    #[serde(default, alias = "unfilledSize")]
    pub unfilled_size: Decimal,
    /// Limit price (for limit orders)
    #[serde(default, alias = "limitPrice")]
    pub limit_price: Option<Decimal>,
    /// Stop price (for stop orders)
    #[serde(default, alias = "stopPrice")]
    pub stop_price: Option<Decimal>,
    /// Whether this is a reduce-only order
    #[serde(default, alias = "reduceOnly")]
    pub reduce_only: bool,
    /// Time the order was received
    #[serde(default, alias = "receivedTime")]
    pub received_time: Option<String>,
    /// Last update time
    #[serde(default, alias = "lastUpdateTime")]
    pub last_update_time: Option<String>,
}

/// A fill (trade execution).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesFill {
    /// Fill ID
    #[serde(alias = "fill_id")]
    pub fill_id: String,
    /// Order ID this fill belongs to
    #[serde(alias = "order_id")]
    pub order_id: String,
    /// Client order ID
    #[serde(default, alias = "cliOrdId")]
    pub cli_ord_id: Option<String>,
    /// Futures symbol
    pub symbol: String,
    /// Fill side
    pub side: BuySell,
    /// Fill size
    pub size: Decimal,
    /// Fill price
    pub price: Decimal,
    /// Fill type (maker/taker/liquidation)
    #[serde(alias = "fillType")]
    pub fill_type: FillType,
    /// Fill timestamp
    #[serde(alias = "fillTime")]
    pub fill_time: String,
}


// Account Information


/// Futures account summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesAccount {
    /// Account type
    #[serde(alias = "type")]
    pub account_type: AccountType,
    /// Base currency for this account
    #[serde(default)]
    pub currency: Option<String>,
    /// Balances by currency
    #[serde(default)]
    pub balances: Option<std::collections::HashMap<String, Decimal>>,
    /// Margin requirements
    #[serde(default)]
    pub margin_requirements: Option<MarginRequirements>,
    /// Trigger estimates (liquidation prices)
    #[serde(default)]
    pub trigger_estimates: Option<TriggerEstimates>,
    /// Auxiliary account info
    #[serde(default)]
    pub auxiliary: Option<AuxiliaryInfo>,
}

/// Margin requirements for an account.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarginRequirements {
    /// Initial margin
    pub im: Decimal,
    /// Maintenance margin
    pub mm: Decimal,
    /// Liquidation threshold
    pub lt: Decimal,
    /// Termination threshold
    pub tt: Decimal,
}

/// Trigger price estimates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TriggerEstimates {
    /// Initial margin trigger
    pub im: Decimal,
    /// Maintenance margin trigger
    pub mm: Decimal,
    /// Liquidation trigger
    pub lt: Decimal,
    /// Termination trigger
    pub tt: Decimal,
}

/// Auxiliary account information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuxiliaryInfo {
    /// Available funds
    pub af: Decimal,
    /// Funding component
    #[serde(default)]
    pub funding: Option<Decimal>,
    /// Profit/Loss
    pub pnl: Decimal,
    /// Portfolio value
    pub pv: Decimal,
    /// USD equivalent
    #[serde(default)]
    pub usd: Option<Decimal>,
}

/// Multi-collateral (Flex) account info.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FlexAccountInfo {
    /// Balances by currency
    pub currencies: std::collections::HashMap<String, FlexCurrencyBalance>,
    /// Total balance value in USD
    pub balance_value: Decimal,
    /// Total portfolio value
    pub portfolio_value: Decimal,
    /// Total collateral value
    pub collateral_value: Decimal,
    /// Initial margin requirement
    pub initial_margin: Decimal,
    /// Maintenance margin requirement
    pub maintenance_margin: Decimal,
    /// Total PnL
    pub pnl: Decimal,
    /// Unrealized funding
    #[serde(default)]
    pub unrealized_funding: Option<Decimal>,
    /// Available margin
    pub available_margin: Decimal,
    /// Margin equity
    pub margin_equity: Decimal,
}

/// Balance info for a single currency in flex account.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FlexCurrencyBalance {
    /// Quantity held
    pub quantity: Decimal,
    /// Value in USD
    pub value: Decimal,
    /// Collateral value (after haircut)
    #[serde(alias = "collateral_value")]
    pub collateral_value: Decimal,
    /// Available for withdrawal
    pub available: Decimal,
    /// Haircut percentage
    #[serde(default)]
    pub haircut: Option<Decimal>,
    /// Conversion spread
    #[serde(default)]
    pub conversion_spread: Option<Decimal>,
}


// Instrument Information


/// A futures instrument (contract).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesInstrument {
    /// Symbol (e.g., "PI_XBTUSD")
    pub symbol: String,
    /// Trading pair (e.g., "XBT:USD")
    #[serde(default)]
    pub pair: Option<String>,
    /// Contract type
    #[serde(default, alias = "type")]
    pub contract_type: Option<ContractType>,
    /// Whether the instrument is tradeable
    #[serde(default)]
    pub tradeable: Option<bool>,
    /// Tick size (minimum price increment)
    #[serde(default, alias = "tickSize")]
    pub tick_size: Option<Decimal>,
    /// Contract size (value per unit)
    #[serde(default, alias = "contractSize")]
    pub contract_size: Option<Decimal>,
    /// Maximum leverage
    #[serde(default)]
    pub leverage: Option<String>,
    /// Margin type
    #[serde(default, alias = "marginLevels")]
    pub margin_levels: Option<Vec<MarginLevel>>,
    /// Maturity time (for fixed maturity contracts)
    #[serde(default, alias = "lastTradingTime")]
    pub maturity_time: Option<String>,
    /// Opening time
    #[serde(default, alias = "openingDate")]
    pub opening_date: Option<String>,
    /// Category tag (perpetual, month, quarter)
    #[serde(default)]
    pub tag: Option<String>,
    /// Post-only mode
    #[serde(default, alias = "postOnly")]
    pub post_only: Option<bool>,
}

/// Margin level tier.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginLevel {
    /// Number of contracts
    pub contracts: Decimal,
    /// Initial margin percentage
    #[serde(alias = "initialMargin")]
    pub initial_margin: Decimal,
    /// Maintenance margin percentage
    #[serde(alias = "maintenanceMargin")]
    pub maintenance_margin: Decimal,
}


// Ticker Data


/// Futures ticker data.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesTicker {
    /// Symbol
    pub symbol: String,
    /// Trading pair
    #[serde(default)]
    pub pair: Option<String>,
    /// Last trade price
    pub last: Decimal,
    /// Best bid price
    #[serde(default)]
    pub bid: Option<Decimal>,
    /// Best bid size
    #[serde(default, alias = "bidSize")]
    pub bid_size: Option<Decimal>,
    /// Best ask price
    #[serde(default)]
    pub ask: Option<Decimal>,
    /// Best ask size
    #[serde(default, alias = "askSize")]
    pub ask_size: Option<Decimal>,
    /// 24h volume
    #[serde(default)]
    pub volume: Option<Decimal>,
    /// 24h volume in quote currency
    #[serde(default, alias = "volumeQuote")]
    pub volume_quote: Option<Decimal>,
    /// Open interest
    #[serde(default, alias = "openInterest")]
    pub open_interest: Option<Decimal>,
    /// 24h open price
    #[serde(default)]
    pub open: Option<Decimal>,
    /// 24h high price
    #[serde(default)]
    pub high: Option<Decimal>,
    /// 24h low price
    #[serde(default)]
    pub low: Option<Decimal>,
    /// 24h change percentage
    #[serde(default)]
    pub change: Option<Decimal>,
    /// Mark price
    #[serde(default, alias = "markPrice")]
    pub mark_price: Option<Decimal>,
    /// Index price
    #[serde(default, alias = "index")]
    pub index_price: Option<Decimal>,
    /// Current funding rate (perpetuals)
    #[serde(default, alias = "fundingRate")]
    pub funding_rate: Option<Decimal>,
    /// Predicted next funding rate
    #[serde(default, alias = "fundingRatePrediction")]
    pub funding_rate_prediction: Option<Decimal>,
    /// Time until next funding
    #[serde(default, alias = "nextFundingRateTime")]
    pub next_funding_rate_time: Option<i64>,
    /// Days to maturity
    #[serde(default)]
    pub dtm: Option<i32>,
    /// Maturity time
    #[serde(default, alias = "maturityTime")]
    pub maturity_time: Option<i64>,
    /// Contract tag
    #[serde(default)]
    pub tag: Option<String>,
    /// Market suspended
    #[serde(default)]
    pub suspended: Option<bool>,
    /// Post-only mode
    #[serde(default, alias = "postOnly")]
    pub post_only: Option<bool>,
    /// Timestamp
    #[serde(default)]
    pub time: Option<i64>,
}


// Order Book


/// Futures order book.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesOrderBook {
    /// Symbol
    pub symbol: String,
    /// Bids (price, size)
    pub bids: Vec<BookLevel>,
    /// Asks (price, size)
    pub asks: Vec<BookLevel>,
    /// Server time
    #[serde(default, alias = "serverTime")]
    pub server_time: Option<String>,
}

/// A single level in the order book.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookLevel {
    /// Price level
    pub price: Decimal,
    /// Size at this price
    #[serde(alias = "qty", alias = "quantity")]
    pub size: Decimal,
}


// Trade History


/// A public trade.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FuturesTrade {
    /// Trade ID
    #[serde(alias = "uid")]
    pub trade_id: String,
    /// Trade price
    pub price: Decimal,
    /// Trade size
    #[serde(alias = "qty", alias = "quantity")]
    pub size: Decimal,
    /// Trade side
    pub side: BuySell,
    /// Trade timestamp
    #[serde(alias = "time")]
    pub timestamp: String,
}

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

    #[test]
    fn test_deserialize_position() {
        let json = r#"{
            "symbol": "PI_XBTUSD",
            "side": "buy",
            "size": "1000",
            "price": "50000.0",
            "unrealizedFunding": "0.001"
        }"#;

        let pos: FuturesPosition = serde_json::from_str(json).unwrap();
        assert_eq!(pos.symbol, "PI_XBTUSD");
        assert_eq!(pos.side, BuySell::Buy);
        assert_eq!(pos.size, Decimal::from(1000));
    }

    #[test]
    fn test_deserialize_order() {
        let json = r#"{
            "order_id": "abc123",
            "symbol": "PI_XBTUSD",
            "side": "sell",
            "orderType": "lmt",
            "status": "open",
            "quantity": "500",
            "filledSize": "0",
            "unfilledSize": "500",
            "limitPrice": "55000.0",
            "reduceOnly": true
        }"#;

        let order: FuturesOrder = serde_json::from_str(json).unwrap();
        assert_eq!(order.order_id, "abc123");
        assert!(order.reduce_only);
        assert_eq!(order.limit_price, Some(Decimal::from(55000)));
    }

    #[test]
    fn test_deserialize_fill() {
        let json = r#"{
            "fill_id": "fill123",
            "order_id": "order456",
            "symbol": "PI_ETHUSD",
            "side": "buy",
            "size": "10",
            "price": "3500.5",
            "fillType": "taker",
            "fillTime": "2024-01-15T10:30:00Z"
        }"#;

        let fill: FuturesFill = serde_json::from_str(json).unwrap();
        assert_eq!(fill.fill_type, FillType::Taker);
    }

    #[test]
    fn test_deserialize_ticker() {
        let json = r#"{
            "symbol": "PI_XBTUSD",
            "last": "50000.0",
            "bid": "49999.5",
            "ask": "50000.5",
            "fundingRate": "0.0001",
            "openInterest": "1000000"
        }"#;

        let ticker: FuturesTicker = serde_json::from_str(json).unwrap();
        assert_eq!(ticker.symbol, "PI_XBTUSD");
        assert!(ticker.funding_rate.is_some());
    }

    #[test]
    fn test_contract_type_serde() {
        assert_eq!(
            serde_json::from_str::<ContractType>(r#""perpetual""#).unwrap(),
            ContractType::Perpetual
        );
    }

    #[test]
    fn test_order_type_serde() {
        // Test alias
        assert_eq!(
            serde_json::from_str::<FuturesOrderType>(r#""lmt""#).unwrap(),
            FuturesOrderType::Limit
        );
        // Test snake_case
        assert_eq!(
            serde_json::from_str::<FuturesOrderType>(r#""take_profit""#).unwrap(),
            FuturesOrderType::TakeProfit
        );
    }
}