simulator-api 0.24.0

Wire-protocol types for the Solana simulator backtest WebSocket API
Documentation
//! The source-agnostic input both reports consume — one row of a reroute population,
//! whether it arrived as a websocket notification or a session-DB row. Adapters own the
//! mapping; nothing here does I/O.

use serde::{Deserialize, Serialize};
use solana_address::Address;

use crate::route_plan::RoutePlan;

/// Whether the swap fixes the input amount (`ExactIn`) or the output amount (`ExactOut`).
///
/// Spelled `ExactIn`/`ExactOut` on the wire — the same rendering as [`Self::as_str`], the
/// sqlite column and the stream's `swapMode`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SwapMode {
    ExactIn,
    ExactOut,
}

impl SwapMode {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ExactIn => "ExactIn",
            Self::ExactOut => "ExactOut",
        }
    }

    /// Anything unrecognized reads as `ExactIn`, the overwhelming default in every source.
    pub fn parse(raw: &str) -> Self {
        match raw == Self::ExactOut.as_str() {
            true => Self::ExactOut,
            false => Self::ExactIn,
        }
    }
}

/// One re-quoted swap. The three plans share one shape ([`RoutePlan`]); a side that was not
/// recorded is `None`, never an empty plan.
#[derive(Debug, Clone)]
pub struct LegRecord {
    pub original_signature: String,
    pub input_mint: Address,
    pub output_mint: Address,
    /// In the input token for ExactIn, the output token for ExactOut.
    pub amount: u64,
    pub swap_mode: SwapMode,
    /// The replacement never landed — a reverted simulation or a not-ok execution. Neither
    /// report column counts it as flow, but pricing still reads the original's side.
    pub failed: bool,
    /// The original failed on L1, so its route events are not flow any venue served.
    pub original_failed: bool,
    /// What the replacement actually produced; `None` when unattributable.
    pub realized_output_amount: Option<u64>,
    /// What the historical original produced. `None` when it cannot be attributed to this
    /// record alone — a multi-swap transaction's total is nobody's single leg.
    pub original_realized_output_amount: Option<u64>,
    /// The route the replacement was quoted over.
    pub route_plan: Option<RoutePlan>,
    /// The route the original actually took on L1.
    pub original_route_plan: Option<RoutePlan>,
    /// What the replacement's own simulation moved, hop by hop — the fill to compare
    /// `original_route_plan` against.
    pub realized_route_plan: Option<RoutePlan>,
    /// The replacement's identity: `new_route` for a metis row, the template label for a probe.
    pub template: Option<String>,
    /// The original side, in the measured mint (output for ExactIn, input charged for ExactOut):
    /// as quoted on metis and stream rows, or what the original filled against the same state as
    /// the probe on probe rows.
    pub quoted_output_amount: Option<u64>,
    /// What the replacement itself produced at its own simulation, in the measured mint.
    pub new_output_amount: Option<u64>,
    /// Why an unfilled probe did not fill — `reverted_*`, `unattributed` (ran clean, but the
    /// seeded account was gone or unreadable), `unsimulatable` (could not be compiled or run) —
    /// or the requote failure bucket on metis rows.
    pub error_category: Option<String>,
    /// The recorded `output_bps` column, clamped by the writer. The historical fill itself is
    /// not persisted, so this cannot be recomputed downstream.
    pub output_bps: Option<i64>,
}

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

    use crate::SwapMode;

    #[rstest]
    #[case::exact_in(SwapMode::ExactIn)]
    #[case::exact_out(SwapMode::ExactOut)]
    fn swap_mode_serializes_as_its_as_str(#[case] mode: SwapMode) {
        assert_eq!(serde_json::to_value(mode).unwrap(), mode.as_str());
    }
}