betex 0.35.0

Betfair / Prediction Market Exchange
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
//! Single-market, deterministic order book module.
//!
//! Design goals (Betfair parity):
//! - **Single market**: one `Book` instance == one `market_id` source of truth.
//! - **Monotonic event stream**: every mutation emits events wrapped in `BookEventEnvelope { seq, ... }`.
//! - **Deterministic**: given the same initial book + same ordered commands, the emitted events are identical.
//! - **Matchability gating**: no matching occurs unless market state is matchable.
//! - **Forward-only execution state**: books update active orders and emit events; outcome/void
//!   policy is delegated to downstream systems.
//! - **Betfair tick ladder**: prices must be on the 350-price tick ladder (1.01 to 1000).
//! - **Time-in-force**: GTC (default), Immediate-or-Cancel, and Fill-or-Kill with optional minFillSize.
//!
//! ## Book Variants
//!
//! - **TwoRunnerBook**: For 2-runner markets (e.g., tennis, head-to-head). Uses implied matching
//!   where BACK A @ odds `a` ≡ LAY B @ odds `a/(a-1)`. No external hedging needed.
//! - **MultiRunnerBook**: For 3+ runner markets. Direct matching only; cross-matching is handled
//!   by an external `CrossMatchEngine`.
//!
//! Non-goals (by design, to keep the book "pure"):
//! - No wallet/risk/auth; the gateway is assumed to pre-authorize commands.
//! - No in-play delay queueing (that lives in front of the book).

mod binary_yes;
pub mod common;
mod error;
mod multi_runner;
pub mod protocol;
mod two_runner;

// Re-export common protocol types at the module level.
use crate::types::*;
pub use binary_yes::BinaryYesBook;
pub use common::*;
pub use error::{BookError, BookErrorDetail, RequestedBookState};
pub use multi_runner::MultiRunnerBook;
use protocol::command::{Command, CommandKind};
pub use protocol::command::{
    Persistence, ReduceBinaryOrderCondition, ReduceBinaryOrderTarget, ReduceOrderCondition,
    ReduceOrderTarget, TimeInForce,
};
pub use two_runner::TwoRunnerBook;

// ============================================================================
// Book Enum
// ============================================================================

/// A single-market order book that automatically selects the optimal implementation.
///
/// - For 2-runner markets: uses `TwoRunnerBook` with implied matching.
/// - For 3+ runner markets: uses `MultiRunnerBook` with direct matching only.
///
/// This enum provides a unified interface while allowing each variant to have
/// specialized matching logic.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Book {
    TwoRunner(Box<TwoRunnerBook>),
    MultiRunner(Box<MultiRunnerBook>),
    BinaryYes(Box<BinaryYesBook>),
}

impl Book {
    const DEFAULT_ORDER_STORE_CAPACITY: usize = 20_000;

    fn default_phase_for_kind(market_kind: MarketKind) -> MarketPhase {
        if market_kind == MarketKind::LiveOnly {
            MarketPhase::Live
        } else {
            MarketPhase::Pre
        }
    }

    /// Create a new book for a market with the given runners.
    ///
    /// Automatically selects the appropriate implementation based on runner count:
    /// - 2 runners: `TwoRunnerBook` with implied matching
    /// - 3+ runners: `MultiRunnerBook` with direct matching only
    ///
    /// # Panics
    /// Panics if fewer than 2 runners are provided.
    pub fn new(market_id: MarketId, runner_ids: impl IntoIterator<Item = RunnerId>) -> Self {
        Self::new_with_kind(market_id, MarketKind::InPlayCapable, runner_ids)
    }

    pub fn new_with_kind(
        market_id: MarketId,
        market_kind: MarketKind,
        runner_ids: impl IntoIterator<Item = RunnerId>,
    ) -> Self {
        Self::new_with_kind_and_phase(
            market_id,
            market_kind,
            Self::default_phase_for_kind(market_kind),
            runner_ids,
        )
    }

    pub fn new_with_kind_and_phase(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        runner_ids: impl IntoIterator<Item = RunnerId>,
    ) -> Self {
        let runners: Vec<RunnerId> = runner_ids.into_iter().collect();
        match runners.len() {
            0 | 1 => panic!("Book requires at least 2 runners"),
            2 => Book::TwoRunner(Box::new(TwoRunnerBook::new_with_capacity(
                market_id,
                market_kind,
                market_phase,
                runners[0],
                runners[1],
                Self::DEFAULT_ORDER_STORE_CAPACITY,
            ))),
            _ => Book::MultiRunner(Box::new(MultiRunnerBook::new_with_capacity(
                market_id,
                market_kind,
                market_phase,
                runners,
                Self::DEFAULT_ORDER_STORE_CAPACITY,
            ))),
        }
    }

    /// Create a new empty book suitable for engine integration.
    ///
    /// This always creates a `MultiRunnerBook` since the runner count may not be
    /// known at bootstrap time, but orders on unknown runners are rejected until
    /// runners are explicitly configured.
    pub fn new_engine(market_id: MarketId) -> Self {
        Self::new_engine_with_kind(market_id, MarketKind::InPlayCapable)
    }

    pub fn new_engine_with_kind(market_id: MarketId, market_kind: MarketKind) -> Self {
        Self::new_engine_with_kind_and_phase(
            market_id,
            market_kind,
            Self::default_phase_for_kind(market_kind),
        )
    }

    pub fn new_engine_with_kind_and_phase(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
    ) -> Self {
        Book::MultiRunner(Box::new(MultiRunnerBook::new_engine_with_capacity(
            market_id,
            market_kind,
            market_phase,
            Self::DEFAULT_ORDER_STORE_CAPACITY,
        )))
    }

    pub fn new_engine_with_capacity(market_id: MarketId, order_store_capacity: usize) -> Self {
        Self::new_engine_with_kind_and_capacity(
            market_id,
            MarketKind::InPlayCapable,
            MarketPhase::Pre,
            order_store_capacity,
        )
    }

    pub fn new_engine_with_kind_and_capacity(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        order_store_capacity: usize,
    ) -> Self {
        Book::MultiRunner(Box::new(MultiRunnerBook::new_engine_with_capacity(
            market_id,
            market_kind,
            market_phase,
            order_store_capacity,
        )))
    }

    /// Create a two-runner book explicitly.
    pub fn new_two_runner(market_id: MarketId, runner_a: RunnerId, runner_b: RunnerId) -> Self {
        Self::new_two_runner_with_kind(market_id, MarketKind::InPlayCapable, runner_a, runner_b)
    }

    pub fn new_two_runner_with_kind(
        market_id: MarketId,
        market_kind: MarketKind,
        runner_a: RunnerId,
        runner_b: RunnerId,
    ) -> Self {
        Self::new_two_runner_with_kind_and_phase(
            market_id,
            market_kind,
            Self::default_phase_for_kind(market_kind),
            runner_a,
            runner_b,
        )
    }

    pub fn new_two_runner_with_kind_and_phase(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        runner_a: RunnerId,
        runner_b: RunnerId,
    ) -> Self {
        Book::TwoRunner(Box::new(TwoRunnerBook::new_with_capacity(
            market_id,
            market_kind,
            market_phase,
            runner_a,
            runner_b,
            Self::DEFAULT_ORDER_STORE_CAPACITY,
        )))
    }

    pub fn new_two_runner_with_capacity(
        market_id: MarketId,
        runner_a: RunnerId,
        runner_b: RunnerId,
        order_store_capacity: usize,
    ) -> Self {
        Self::new_two_runner_with_kind_and_capacity(
            market_id,
            MarketKind::InPlayCapable,
            MarketPhase::Pre,
            runner_a,
            runner_b,
            order_store_capacity,
        )
    }

    pub fn new_two_runner_with_kind_and_capacity(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        runner_a: RunnerId,
        runner_b: RunnerId,
        order_store_capacity: usize,
    ) -> Self {
        Book::TwoRunner(Box::new(TwoRunnerBook::new_with_capacity(
            market_id,
            market_kind,
            market_phase,
            runner_a,
            runner_b,
            order_store_capacity,
        )))
    }

    pub fn set_close_batch_max_events(&mut self, batch_max_events: u16) {
        match self {
            Book::TwoRunner(b) => b.set_close_batch_max_events(batch_max_events),
            Book::MultiRunner(b) => b.set_close_batch_max_events(batch_max_events),
            Book::BinaryYes(b) => b.set_close_batch_max_events(batch_max_events),
        }
    }

    pub fn close_batch_max_events(&self) -> u16 {
        match self {
            Book::TwoRunner(b) => b.close_batch_max_events(),
            Book::MultiRunner(b) => b.close_batch_max_events(),
            Book::BinaryYes(b) => b.close_batch_max_events(),
        }
    }

    /// Create a multi-runner book explicitly.
    pub fn new_multi_runner(
        market_id: MarketId,
        runner_ids: impl IntoIterator<Item = RunnerId>,
    ) -> Self {
        Self::new_multi_runner_with_kind(market_id, MarketKind::InPlayCapable, runner_ids)
    }

    pub fn new_multi_runner_with_kind(
        market_id: MarketId,
        market_kind: MarketKind,
        runner_ids: impl IntoIterator<Item = RunnerId>,
    ) -> Self {
        Self::new_multi_runner_with_kind_and_phase(
            market_id,
            market_kind,
            Self::default_phase_for_kind(market_kind),
            runner_ids,
        )
    }

    pub fn new_multi_runner_with_kind_and_phase(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        runner_ids: impl IntoIterator<Item = RunnerId>,
    ) -> Self {
        Book::MultiRunner(Box::new(MultiRunnerBook::new_with_capacity(
            market_id,
            market_kind,
            market_phase,
            runner_ids,
            Self::DEFAULT_ORDER_STORE_CAPACITY,
        )))
    }

    pub fn new_multi_runner_with_capacity(
        market_id: MarketId,
        runner_ids: impl IntoIterator<Item = RunnerId>,
        order_store_capacity: usize,
    ) -> Self {
        Self::new_multi_runner_with_kind_and_capacity(
            market_id,
            MarketKind::InPlayCapable,
            MarketPhase::Pre,
            runner_ids,
            order_store_capacity,
        )
    }

    pub fn new_multi_runner_with_kind_and_capacity(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        runner_ids: impl IntoIterator<Item = RunnerId>,
        order_store_capacity: usize,
    ) -> Self {
        Book::MultiRunner(Box::new(MultiRunnerBook::new_with_capacity(
            market_id,
            market_kind,
            market_phase,
            runner_ids,
            order_store_capacity,
        )))
    }

    /// Create a canonical YES-only prediction market book.
    pub fn new_binary_yes_with_kind_and_capacity(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        yes_runner_id: RunnerId,
        no_runner_id: RunnerId,
        max_price_ticks: u16,
        order_store_capacity: usize,
    ) -> Self {
        Book::BinaryYes(Box::new(BinaryYesBook::new_with_capacity(
            market_id,
            market_kind,
            market_phase,
            yes_runner_id,
            no_runner_id,
            max_price_ticks,
            order_store_capacity,
        )))
    }

    pub fn new_binary_yes_with_kind(
        market_id: MarketId,
        market_kind: MarketKind,
        yes_runner_id: RunnerId,
        no_runner_id: RunnerId,
        max_price_ticks: u16,
    ) -> Self {
        Self::new_binary_yes_with_kind_and_phase(
            market_id,
            market_kind,
            Self::default_phase_for_kind(market_kind),
            yes_runner_id,
            no_runner_id,
            max_price_ticks,
        )
    }

    pub fn new_binary_yes_with_kind_and_phase(
        market_id: MarketId,
        market_kind: MarketKind,
        market_phase: MarketPhase,
        yes_runner_id: RunnerId,
        no_runner_id: RunnerId,
        max_price_ticks: u16,
    ) -> Self {
        Self::new_binary_yes_with_kind_and_capacity(
            market_id,
            market_kind,
            market_phase,
            yes_runner_id,
            no_runner_id,
            max_price_ticks,
            Self::DEFAULT_ORDER_STORE_CAPACITY,
        )
    }

    pub fn new_binary_yes(
        market_id: MarketId,
        yes_runner_id: RunnerId,
        no_runner_id: RunnerId,
        max_price_ticks: u16,
    ) -> Self {
        Self::new_binary_yes_with_kind(
            market_id,
            MarketKind::InPlayCapable,
            yes_runner_id,
            no_runner_id,
            max_price_ticks,
        )
    }

    // ========================================================================
    // Accessors
    // ========================================================================

    pub fn set_market_name(&mut self, name: &str) {
        match self {
            Book::TwoRunner(b) => b.market_name = name.to_owned(),
            Book::MultiRunner(b) => b.market_name = name.to_owned(),
            Book::BinaryYes(b) => b.market_name = name.to_owned(),
        }
    }

    pub(crate) fn set_market_state(&mut self, state: BookMarketState) {
        match self {
            Book::TwoRunner(b) => b.set_market_state(state),
            Book::MultiRunner(b) => b.set_market_state(state),
            Book::BinaryYes(b) => b.set_market_state(state),
        }
    }

    pub fn set_runner_labels(&mut self, runner_ids: &[RunnerId], runner_labels: &[String]) {
        match self {
            Book::TwoRunner(b) => b.set_runner_labels(runner_ids, runner_labels),
            Book::MultiRunner(b) => b.set_runner_labels(runner_ids, runner_labels),
            Book::BinaryYes(b) => b.set_runner_labels(runner_ids, runner_labels),
        }
    }

    pub fn market_id(&self) -> MarketId {
        match self {
            Book::TwoRunner(b) => b.market_id(),
            Book::MultiRunner(b) => b.market_id(),
            Book::BinaryYes(b) => b.market_id(),
        }
    }

    pub fn market_model(&self) -> MarketModel {
        match self {
            Book::TwoRunner(_) | Book::MultiRunner(_) => MarketModel::ExchangeOdds,
            Book::BinaryYes(b) => MarketModel::BinaryYes {
                max_price_ticks: b.max_price_ticks(),
            },
        }
    }

    pub fn binary_depth(&self, depth: usize) -> Option<BinaryDepth> {
        match self {
            Book::BinaryYes(b) => Some(b.depth(depth)),
            _ => None,
        }
    }

    pub fn market_state(&self) -> BookMarketState {
        match self {
            Book::TwoRunner(b) => b.market_state(),
            Book::MultiRunner(b) => b.market_state(),
            Book::BinaryYes(b) => b.market_state(),
        }
    }

    pub fn market_phase(&self) -> MarketPhase {
        match self {
            Book::TwoRunner(b) => b.market_phase(),
            Book::MultiRunner(b) => b.market_phase(),
            Book::BinaryYes(b) => b.market_phase(),
        }
    }

    pub fn is_halted(&self) -> bool {
        match self {
            Book::TwoRunner(b) => b.is_halted(),
            Book::MultiRunner(b) => b.is_halted(),
            Book::BinaryYes(b) => b.is_halted(),
        }
    }

    pub fn get_order(&self, order_id: OrderId) -> Option<&BookOrder> {
        match self {
            Book::TwoRunner(b) => b.get_order(order_id),
            Book::MultiRunner(b) => b.get_order(order_id),
            Book::BinaryYes(_) => None,
        }
    }

    pub fn is_resting(&self, order_id: OrderId) -> bool {
        match self {
            Book::TwoRunner(b) => b.is_resting(order_id),
            Book::MultiRunner(b) => b.is_resting(order_id),
            Book::BinaryYes(b) => b.is_resting(order_id),
        }
    }

    pub fn active_order_count(&self) -> usize {
        // Note: this is O(n) over all orders and intended for diagnostics/UI.
        match self {
            Book::TwoRunner(b) => b.active_order_count(),
            Book::MultiRunner(b) => b.active_order_count(),
            Book::BinaryYes(b) => b.active_order_count(),
        }
    }

    pub fn batch_process_state(&self) -> Option<&BatchProcessState> {
        match self {
            Book::TwoRunner(b) => b.batch_process_state(),
            Book::MultiRunner(b) => b.batch_process_state(),
            Book::BinaryYes(b) => b.batch_process_state(),
        }
    }

    /// Returns an iterator over all runner IDs in this market.
    pub fn runners(&self) -> Box<dyn Iterator<Item = RunnerId> + '_> {
        match self {
            Book::TwoRunner(b) => Box::new(b.runners()),
            Book::MultiRunner(b) => Box::new(b.runners()),
            Book::BinaryYes(b) => Box::new(b.runners()),
        }
    }

    // ========================================================================
    // Market Depth Queries
    // ========================================================================

    pub fn runner_prices(&self, runner_id: RunnerId, depth: usize) -> RunnerPrices {
        match self {
            Book::TwoRunner(b) => b.runner_prices(runner_id, depth),
            Book::MultiRunner(b) => b.runner_prices(runner_id, depth),
            Book::BinaryYes(b) => b.runner_prices(runner_id, depth),
        }
    }

    pub fn best_back_price(&self, runner_id: RunnerId) -> Option<PriceSize> {
        match self {
            Book::TwoRunner(b) => b.best_back_price(runner_id),
            Book::MultiRunner(b) => b.best_back_price(runner_id),
            Book::BinaryYes(b) => b.best_back_price(runner_id),
        }
    }

    pub fn best_lay_price(&self, runner_id: RunnerId) -> Option<PriceSize> {
        match self {
            Book::TwoRunner(b) => b.best_lay_price(runner_id),
            Book::MultiRunner(b) => b.best_lay_price(runner_id),
            Book::BinaryYes(b) => b.best_lay_price(runner_id),
        }
    }

    pub fn runner_matched_volume(&self, runner_id: RunnerId) -> Money {
        match self {
            Book::TwoRunner(b) => b.runner_matched_volume(runner_id),
            Book::MultiRunner(b) => b.runner_matched_volume(runner_id),
            Book::BinaryYes(b) => b.runner_matched_volume(runner_id),
        }
    }

    pub fn runner_label(&self, runner_id: RunnerId) -> &str {
        match self {
            Book::TwoRunner(b) => b.runner_label(runner_id),
            Book::MultiRunner(b) => b.runner_label(runner_id),
            Book::BinaryYes(b) => b.runner_label(runner_id),
        }
    }

    pub fn total_matched(&self) -> Money {
        match self {
            Book::TwoRunner(b) => b.total_matched(),
            Book::MultiRunner(b) => b.total_matched(),
            Book::BinaryYes(b) => b.total_matched(),
        }
    }

    // ========================================================================
    // Command Processing
    // ========================================================================

    pub fn handle(
        &mut self,
        cmd: &Command,
    ) -> Result<(Vec<BookEventEnvelope>, CommandResponse), BookError> {
        let (mut events, resp) = match self {
            Book::TwoRunner(b) => {
                let (events, resp) = b.handle_command(cmd)?;
                (events.into_vec(), resp)
            }
            Book::MultiRunner(b) => b.handle_command(cmd)?,
            Book::BinaryYes(b) => b.handle_command(cmd)?,
        };
        Self::attach_metadata_to_events(cmd, &mut events, &cmd.metadata);
        Ok((events, resp))
    }

    fn attach_metadata_to_events(
        cmd: &Command,
        events: &mut [BookEventEnvelope],
        metadata: &Option<serde_json::Value>,
    ) {
        if metadata.is_none() || matches!(&cmd.kind, CommandKind::ContinueBatchProcess) {
            return;
        }

        if matches!(&cmd.kind, CommandKind::BatchCancelOrders { .. }) {
            if let Some(first) = events.first_mut() {
                first.metadata = metadata.clone();
            }
            return;
        }

        for event in events {
            event.metadata = metadata.clone();
        }
    }

    pub fn apply_event(&mut self, env: &BookEventEnvelope) {
        debug_assert_eq!(
            env.market_id,
            self.market_id(),
            "event market_id mismatch: expected {:?}, got {:?}",
            self.market_id(),
            env.market_id
        );
        match self {
            Book::TwoRunner(b) => b.apply_event(env),
            Book::MultiRunner(b) => b.apply_event(env),
            Book::BinaryYes(b) => b.apply_event(env),
        }
    }

    pub fn apply_all_events(&mut self, envs: &[BookEventEnvelope]) {
        for env in envs {
            self.apply_event(env);
        }
    }
}