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, strum::IntoStaticStr)]
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 fn as_str(self) -> &'static str {
25 self.into()
26 }
27
28 /// Anything unrecognized reads as `ExactIn`, the overwhelming default in every source.
29 pub fn parse(raw: &str) -> Self {
30 if raw == Self::ExactOut.as_str() {
31 Self::ExactOut
32 } else {
33 Self::ExactIn
34 }
35 }
36}
37
38/// One rerouted swap. The three plans share one shape ([`RoutePlan`]); a side that was not
39/// recorded is `None`, never an empty plan.
40#[derive(Debug, Clone)]
41pub struct LegRecord {
42 /// The original swap's transaction signature, base58.
43 pub original_signature: String,
44 #[expect(missing_docs, reason = "self-explanatory")]
45 pub input_mint: Address,
46 #[expect(missing_docs, reason = "self-explanatory")]
47 pub output_mint: Address,
48 /// Raw base units of the input token for ExactIn, the output token for ExactOut.
49 pub amount: u64,
50 /// Which side `amount` fixes.
51 pub swap_mode: SwapMode,
52 /// The replacement never landed — a reverted simulation or a not-ok execution. Neither
53 /// report column counts it as flow, but pricing still reads the original's side.
54 pub failed: bool,
55 /// The original swap failed, so its route events are not flow any venue served.
56 pub original_failed: bool,
57 /// What the replacement actually produced; `None` when unattributable.
58 pub realized_output_amount: Option<u64>,
59 /// What the historical original produced. `None` when it cannot be attributed to this
60 /// record alone — a multi-swap transaction's total is nobody's single leg.
61 pub original_realized_output_amount: Option<u64>,
62 /// The route the replacement was quoted over.
63 pub route_plan: Option<RoutePlan>,
64 /// The route the original swap actually took.
65 pub original_route_plan: Option<RoutePlan>,
66 /// What the replacement's own simulation moved, hop by hop — the fill to compare
67 /// `original_route_plan` against.
68 pub realized_route_plan: Option<RoutePlan>,
69 /// The replacement's identity: the route summary for a router row, the template label for a
70 /// venue probe.
71 pub template: Option<String>,
72 /// The original side, in the measured mint (output for ExactIn, input charged for ExactOut):
73 /// as quoted on router and stream rows, or what the original filled against the same state as
74 /// the probe on probe rows.
75 pub quoted_output_amount: Option<u64>,
76 /// What the replacement itself produced at its own simulation, in the measured mint.
77 pub new_output_amount: Option<u64>,
78 /// Why an unfilled probe did not fill — `reverted_*`, `unattributed` (ran clean, but the
79 /// seeded account was gone or unreadable), `unsimulatable` (could not be compiled or run),
80 /// `unresolved_*` (the template could not be brought up to the pass) — or the reroute failure
81 /// bucket on router rows.
82 pub error_category: Option<String>,
83 /// The recorded `output_bps` column, clamped by the writer. The historical fill itself is
84 /// not persisted, so this cannot be recomputed downstream.
85 pub output_bps: Option<i64>,
86}
87
88#[cfg(test)]
89mod tests {
90 use rstest::rstest;
91
92 use crate::SwapMode;
93
94 #[rstest]
95 #[case::exact_in(SwapMode::ExactIn)]
96 #[case::exact_out(SwapMode::ExactOut)]
97 fn swap_mode_serializes_as_its_as_str(#[case] mode: SwapMode) {
98 assert_eq!(serde_json::to_value(mode).unwrap(), mode.as_str());
99 }
100}