Skip to main content

simulator_api/reroute_report/
record.rs

1//! The source-agnostic input both reports consume — one row of a reroute population,
2//! whether it arrived as a websocket notification or a session-DB row. Adapters own the
3//! mapping; nothing here does I/O.
4
5use serde::{Deserialize, Serialize};
6use solana_address::Address;
7
8use crate::route_plan::RoutePlan;
9
10/// Whether the swap fixes the input amount (`ExactIn`) or the output amount (`ExactOut`).
11///
12/// Spelled `ExactIn`/`ExactOut` on the wire — the same rendering as [`Self::as_str`], the
13/// sqlite column and the stream's `swapMode`.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
15pub enum SwapMode {
16    /// The input amount is fixed; the output is measured.
17    ExactIn,
18    /// The output amount is fixed; the input charged is measured.
19    ExactOut,
20}
21
22impl SwapMode {
23    /// The wire spelling: `ExactIn` or `ExactOut`.
24    pub const fn as_str(self) -> &'static str {
25        match self {
26            Self::ExactIn => "ExactIn",
27            Self::ExactOut => "ExactOut",
28        }
29    }
30
31    /// Anything unrecognized reads as `ExactIn`, the overwhelming default in every source.
32    pub fn parse(raw: &str) -> Self {
33        match raw == Self::ExactOut.as_str() {
34            true => Self::ExactOut,
35            false => Self::ExactIn,
36        }
37    }
38}
39
40/// One rerouted swap. The three plans share one shape ([`RoutePlan`]); a side that was not
41/// recorded is `None`, never an empty plan.
42#[derive(Debug, Clone)]
43pub struct LegRecord {
44    /// The original swap's transaction signature, base58.
45    pub original_signature: String,
46    #[allow(missing_docs)]
47    pub input_mint: Address,
48    #[allow(missing_docs)]
49    pub output_mint: Address,
50    /// Raw base units of the input token for ExactIn, the output token for ExactOut.
51    pub amount: u64,
52    /// Which side `amount` fixes.
53    pub swap_mode: SwapMode,
54    /// The replacement never landed — a reverted simulation or a not-ok execution. Neither
55    /// report column counts it as flow, but pricing still reads the original's side.
56    pub failed: bool,
57    /// The original swap failed, so its route events are not flow any venue served.
58    pub original_failed: bool,
59    /// What the replacement actually produced; `None` when unattributable.
60    pub realized_output_amount: Option<u64>,
61    /// What the historical original produced. `None` when it cannot be attributed to this
62    /// record alone — a multi-swap transaction's total is nobody's single leg.
63    pub original_realized_output_amount: Option<u64>,
64    /// The route the replacement was quoted over.
65    pub route_plan: Option<RoutePlan>,
66    /// The route the original swap actually took.
67    pub original_route_plan: Option<RoutePlan>,
68    /// What the replacement's own simulation moved, hop by hop — the fill to compare
69    /// `original_route_plan` against.
70    pub realized_route_plan: Option<RoutePlan>,
71    /// The replacement's identity: the route summary for a router row, the template label for a
72    /// venue probe.
73    pub template: Option<String>,
74    /// The original side, in the measured mint (output for ExactIn, input charged for ExactOut):
75    /// as quoted on router and stream rows, or what the original filled against the same state as
76    /// the probe on probe rows.
77    pub quoted_output_amount: Option<u64>,
78    /// What the replacement itself produced at its own simulation, in the measured mint.
79    pub new_output_amount: Option<u64>,
80    /// Why an unfilled probe did not fill — `reverted_*`, `unattributed` (ran clean, but the
81    /// seeded account was gone or unreadable), `unsimulatable` (could not be compiled or run),
82    /// `unresolved_*` (the template could not be brought up to the pass) — or the reroute failure
83    /// bucket on router rows.
84    pub error_category: Option<String>,
85    /// The recorded `output_bps` column, clamped by the writer. The historical fill itself is
86    /// not persisted, so this cannot be recomputed downstream.
87    pub output_bps: Option<i64>,
88}
89
90#[cfg(test)]
91mod tests {
92    use rstest::rstest;
93
94    use crate::SwapMode;
95
96    #[rstest]
97    #[case::exact_in(SwapMode::ExactIn)]
98    #[case::exact_out(SwapMode::ExactOut)]
99    fn swap_mode_serializes_as_its_as_str(#[case] mode: SwapMode) {
100        assert_eq!(serde_json::to_value(mode).unwrap(), mode.as_str());
101    }
102}