bpx-api-types 0.20.2

Backpack Exchange API types
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
use std::{fmt, str::FromStr};

use rust_decimal::{Decimal, prelude::FromPrimitive};
use serde::{Deserialize, Deserializer, Serialize, de::Visitor};
use strum::{Display, EnumString};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TriggerBy {
    LastPrice,
    MarkPrice,
    IndexPrice,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TriggerQuantity {
    Percent(Decimal),
    Amount(Decimal),
}

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

        impl Visitor<'_> for QtyVisitor {
            type Value = TriggerQuantity;

            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str(r#"a string like "12.5%" or "0.01", or a number"#)
            }

            // ---------- JSON string ----------
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                parse_str(v).map_err(serde::de::Error::custom)
            }

            // ---------- JSON numbers ----------
            fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Decimal::from_f64(v)
                    .ok_or_else(|| serde::de::Error::custom("not a finite number"))
                    .map(TriggerQuantity::Amount)
            }

            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TriggerQuantity::Amount(Decimal::from(v)))
            }

            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TriggerQuantity::Amount(Decimal::from(v)))
            }
        }

        deserializer.deserialize_any(QtyVisitor)
    }
}

impl Serialize for TriggerQuantity {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(
            match self {
                Self::Percent(percent) => format!("{percent}%"),
                Self::Amount(amount) => format!("{amount}"),
            }
            .as_str(),
        )
    }
}

fn parse_str(s: &str) -> Result<TriggerQuantity, &'static str> {
    if let Some(num) = s.strip_suffix('%') {
        let d = Decimal::from_str(num.trim()).map_err(|_| "invalid percent value")?;
        Ok(TriggerQuantity::Percent(d))
    } else {
        let d = Decimal::from_str(s.trim()).map_err(|_| "invalid decimal value")?;
        Ok(TriggerQuantity::Amount(d))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarketOrder {
    pub id: String,
    pub client_id: Option<u32>,
    pub symbol: String,
    pub side: Side,
    pub quantity: Option<Decimal>,
    pub executed_quantity: Decimal,
    pub quote_quantity: Option<Decimal>,
    pub executed_quote_quantity: Decimal,
    pub stop_loss_trigger_price: Option<Decimal>,
    pub stop_loss_limit_price: Option<Decimal>,
    pub stop_loss_trigger_by: Option<TriggerBy>,
    pub take_profit_trigger_price: Option<Decimal>,
    pub take_profit_limit_price: Option<Decimal>,
    pub take_profit_trigger_by: Option<TriggerBy>,
    pub trigger_by: Option<TriggerBy>,
    pub trigger_price: Option<Decimal>,
    pub trigger_quantity: Option<TriggerQuantity>,
    pub triggered_at: Option<i64>,
    pub time_in_force: TimeInForce,
    pub related_order_id: Option<String>,
    pub self_trade_prevention: SelfTradePrevention,
    pub reduce_only: Option<bool>,
    pub status: OrderStatus,
    pub created_at: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LimitOrder {
    pub id: String,
    pub client_id: Option<u32>,
    pub symbol: String,
    pub side: Side,
    pub quantity: Decimal,
    pub executed_quantity: Decimal,
    pub executed_quote_quantity: Decimal,
    pub stop_loss_trigger_price: Option<Decimal>,
    pub stop_loss_limit_price: Option<Decimal>,
    pub stop_loss_trigger_by: Option<TriggerBy>,
    pub take_profit_trigger_price: Option<Decimal>,
    pub take_profit_limit_price: Option<Decimal>,
    pub take_profit_trigger_by: Option<TriggerBy>,
    pub price: Decimal,
    pub trigger_by: Option<TriggerBy>,
    pub trigger_price: Option<Decimal>,
    pub trigger_quantity: Option<TriggerQuantity>,
    pub triggered_at: Option<i64>,
    pub time_in_force: TimeInForce,
    pub related_order_id: Option<String>,
    pub self_trade_prevention: SelfTradePrevention,
    pub post_only: bool,
    pub reduce_only: Option<bool>,
    pub status: OrderStatus,
    pub created_at: i64,
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum OrderType {
    #[default]
    #[serde(rename(deserialize = "LIMIT"))]
    Limit,
    #[serde(rename(deserialize = "MARKET"))]
    Market,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "orderType")]
pub enum Order {
    Market(MarketOrder),
    Limit(LimitOrder),
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "UPPERCASE")]
#[serde(rename_all = "UPPERCASE")]
pub enum TimeInForce {
    #[default]
    GTC,
    IOC,
    FOK,
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum SelfTradePrevention {
    #[default]
    RejectTaker,
    RejectMaker,
    RejectBoth,
    Allow,
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum OrderStatus {
    Cancelled,
    Expired,
    Filled,
    #[default]
    New,
    PartiallyFilled,
    Triggered,
    TriggerPending,
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum SystemOrderType {
    #[default]
    LiquidatePositionOnBook,
    LiquidatePositionOnBackstop,
    LiquidatePositionOnAdl,
    CollateralConversion,
    FutureExpiry,
    OrderBookClosed,
}

#[derive(
    Debug, Display, Clone, Copy, Serialize, Deserialize, Default, EnumString, PartialEq, Eq, Hash,
)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum Side {
    #[default]
    Bid,
    Ask,
}

#[derive(Debug, Display, Clone, Copy, Serialize, Deserialize, EnumString, PartialEq, Eq, Hash)]
#[strum(serialize_all = "PascalCase")]
#[serde(rename_all = "PascalCase")]
pub enum SlippageToleranceType {
    TickSize,
    Percent,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExecuteOrderPayload {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_lend: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_lend_redeem: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_borrow: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_borrow_repay: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<u32>,
    pub order_type: OrderType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quote_quantity: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reduce_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub self_trade_prevention: Option<SelfTradePrevention>,
    pub side: Side,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_loss_limit_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_loss_trigger_by: Option<TriggerBy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_loss_trigger_price: Option<Decimal>,
    pub symbol: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub take_profit_limit_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub take_profit_trigger_by: Option<TriggerBy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub take_profit_trigger_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_in_force: Option<TimeInForce>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_by: Option<TriggerBy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_price: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_quantity: Option<TriggerQuantity>,
    /// Slippage tolerance allowed for the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slippage_tolerance: Option<Decimal>,
    /// Slippage tolerance type.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slippage_tolerance_type: Option<SlippageToleranceType>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CancelOrderPayload {
    pub symbol: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CancelOpenOrdersPayload {
    pub symbol: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OrderUpdateType {
    OrderAccepted,
    OrderCancelled,
    OrderExpired,
    OrderFill,
    OrderModified,
    TriggerPlaced,
    TriggerFailed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderUpdate {
    /// Event type
    #[serde(rename = "e")]
    pub event_type: OrderUpdateType,

    /// Event timestamp in microseconds
    #[serde(rename = "E")]
    pub event_time: i64,

    /// Symbol
    #[serde(rename = "s")]
    pub symbol: String,

    /// Client order id
    #[serde(rename = "c")]
    pub client_order_id: Option<u64>,

    /// Side
    #[serde(rename = "S")]
    pub side: Side,

    /// Order type
    #[serde(rename = "o")]
    pub order_type: OrderType,

    /// Time in force
    #[serde(rename = "f")]
    pub time_in_force: TimeInForce,

    /// Quantity
    #[serde(rename = "q")]
    pub quantity: Decimal,

    /// Quantity in quote
    #[serde(rename = "Q")]
    pub quantity_in_quote: Option<Decimal>,

    /// price
    #[serde(rename = "p")]
    pub price: Option<Decimal>,

    /// trigger price
    #[serde(rename = "P")]
    pub trigger_price: Option<Decimal>,

    /// trigger by
    #[serde(rename = "B")]
    pub trigger_by: Option<TriggerBy>,

    /// Take profit trigger price
    #[serde(rename = "a")]
    pub take_profit_trigger_price: Option<Decimal>,

    /// Stop loss trigger price
    #[serde(rename = "b")]
    pub stop_loss_trigger_price: Option<Decimal>,

    /// Take profit trigger by
    #[serde(rename = "d")]
    pub take_profit_trigger_by: Option<TriggerBy>,

    /// Stop loss trigger by
    #[serde(rename = "g")]
    pub stop_loss_trigger_by: Option<TriggerBy>,

    /// Trigger quantity
    #[serde(rename = "Y")]
    pub trigger_quantity: Option<TriggerQuantity>,

    /// Order State
    #[serde(rename = "X")]
    pub order_status: OrderStatus,

    /// Order expiry reason
    #[serde(rename = "R")]
    pub order_expiry_reason: Option<String>,

    /// Order ID
    #[serde(rename = "i")]
    pub order_id: String,

    /// Trade ID
    #[serde(rename = "t")]
    pub trade_id: Option<u64>,

    /// Fill quantity
    #[serde(rename = "l")]
    pub fill_quantity: Option<Decimal>,

    /// Executed quantity
    #[serde(rename = "z")]
    pub executed_quantity: Decimal,

    /// Executed quantity in quote
    #[serde(rename = "Z")]
    pub executed_quantity_in_quote: Decimal,

    /// Fill price
    #[serde(rename = "L")]
    pub fill_price: Option<Decimal>,

    /// Fill price
    #[serde(rename = "m")]
    pub was_maker: Option<bool>,

    /// Fee
    #[serde(rename = "n")]
    pub fee: Option<Decimal>,

    /// Fee symbol
    #[serde(rename = "N")]
    pub fee_symbol: Option<String>,

    /// Self trade prevention
    #[serde(rename = "V")]
    pub self_trade_prevention: SelfTradePrevention,

    /// Engine timestamp in microseconds
    #[serde(rename = "T")]
    pub timestamp: i64,

    /// Origin of the update
    #[serde(rename = "O")]
    pub origin_of_the_update: String,

    /// Related order ID
    #[serde(rename = "I")]
    pub related_order_id: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderError {
    pub code: String,
    pub message: String,
    pub operation: String,
}

/// An item in the response for a batch order execution
/// which can be either a successful order or an error.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum BatchOrderResponse {
    Order(Order),
    Error(OrderError),
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;
    use serde_json::json;

    #[test]
    fn both_forms_round_trip() {
        let q: TriggerQuantity = serde_json::from_value(json!("12.5%")).unwrap();
        assert_eq!(q, TriggerQuantity::Percent(dec!(12.5)));

        let q: TriggerQuantity = serde_json::from_value(json!("0.01")).unwrap();
        assert_eq!(q, TriggerQuantity::Amount(dec!(0.01)));
    }

    #[test]
    fn test_trigger_quantity_serialize() {
        let trigger_quantity = TriggerQuantity::Percent(dec!(100));
        let trigger_quantity_str = serde_json::to_string(&trigger_quantity).unwrap();
        assert_eq!(trigger_quantity_str, "\"100%\"");

        let trigger_quantity = TriggerQuantity::Percent(dec!(75.50));
        let trigger_quantity_str = serde_json::to_string(&trigger_quantity).unwrap();
        assert_eq!(trigger_quantity_str, "\"75.50%\"");

        let trigger_quantity = TriggerQuantity::Amount(dec!(100));
        let trigger_quantity_str = serde_json::to_string(&trigger_quantity).unwrap();
        assert_eq!(trigger_quantity_str, "\"100\"");

        let trigger_quantity = TriggerQuantity::Amount(dec!(75.50));
        let trigger_quantity_str = serde_json::to_string(&trigger_quantity).unwrap();
        assert_eq!(trigger_quantity_str, "\"75.50\"");
    }

    #[test]
    fn test_trigger_by_serialize() {
        let trigger_by_last = TriggerBy::LastPrice;
        let trigger_by_last_str = serde_json::to_string(&trigger_by_last).unwrap();
        assert_eq!(trigger_by_last_str, "\"LastPrice\"");

        let trigger_by_mark = TriggerBy::MarkPrice;
        let trigger_by_mark_str = serde_json::to_string(&trigger_by_mark).unwrap();
        assert_eq!(trigger_by_mark_str, "\"MarkPrice\"");

        let trigger_by_index = TriggerBy::IndexPrice;
        let trigger_by_index_str = serde_json::to_string(&trigger_by_index).unwrap();
        assert_eq!(trigger_by_index_str, "\"IndexPrice\"");
    }

    #[test]
    fn test_order_update() {
        let data = r#"
        {"E":1748288167010366,"O":"USER","P":"178.05","Q":"0","S":"Ask","T":1748288167009460,"V":"RejectTaker","X":"TriggerPending","Y":"20.03","Z":"0","e":"triggerPlaced","f":"GTC","i":"114575813313101824","o":"LIMIT","p":"178.15","q":"0","r":false,"s":"SOL_USDC","t":null,"z":"0"}
        "#;

        let order_update: OrderUpdate = serde_json::from_str(data).unwrap();
        assert_eq!(order_update.price.unwrap(), dec!(178.15));
        assert_eq!(order_update.trigger_price.unwrap(), dec!(178.05));
        assert_eq!(
            order_update.trigger_quantity.unwrap(),
            TriggerQuantity::Amount(dec!(20.03))
        );
        assert_eq!(order_update.quantity_in_quote.unwrap(), dec!(0));

        let data = r#"
        {"E":1748288615134547,"O":"USER","Q":"3568.3445","S":"Ask","T":1748288615133255,"V":"RejectTaker","X":"New","Z":"0","e":"orderAccepted","f":"GTC","i":"114575842681290753","o":"LIMIT","p":"178.15","q":"20.03","r":false,"s":"SOL_USDC","t":null,"z":"0"}
        "#;

        let order_update: OrderUpdate = serde_json::from_str(data).unwrap();
        assert_eq!(order_update.price.unwrap(), dec!(178.15));
        assert_eq!(order_update.trigger_price, None);
        assert_eq!(order_update.quantity_in_quote.unwrap(), dec!(3568.3445));
        assert_eq!(order_update.quantity, dec!(20.03));

        let data = r#"
        {"B":"LastPrice","E":1748289564405220,"O":"USER","P":"178.55","S":"Ask","T":1748289564404373,"V":"RejectTaker","X":"Cancelled","Y":"80%","Z":"0","e":"orderCancelled","f":"GTC","i":"114575904705282048","o":"MARKET","q":"0","r":false,"s":"SOL_USDC","t":null,"z":"0"}
        "#;
        let order_update: OrderUpdate = serde_json::from_str(data).unwrap();
        assert_eq!(order_update.trigger_price.unwrap(), dec!(178.55));
        assert_eq!(
            order_update.trigger_quantity.unwrap(),
            TriggerQuantity::Percent(dec!(80))
        );
    }
}