simulator-client 0.22.0

Async WebSocket client for the Solana simulator backtest API
Documentation
//! The notification is deserialized from its own encoding, so a serde rename fails the test.

use simulator_api::route_plan::{RouteHop, RouteHopSwapInfo, RoutePlan};

use crate::{RerouteLegNotification, RerouteNotification};

/// A program makes it a fill; without one, a quote naming a pool.
pub(super) fn hop(
    program: Option<&str>,
    label: &str,
    input_mint: &str,
    output_mint: &str,
    in_amount: u64,
) -> RouteHop {
    RouteHop {
        program: program.map(str::to_string),
        swap_info: RouteHopSwapInfo {
            label: Some(label.to_string()),
            amm_key: program.is_none().then(|| "pool".to_string()),
            input_mint: Some(input_mint.to_string()),
            in_amount: Some(in_amount.to_string()),
            output_mint: Some(output_mint.to_string()),
            out_amount: None,
        },
        ..RouteHop::default()
    }
}

pub(super) fn plan(hops: Vec<RouteHop>) -> Option<RoutePlan> {
    RoutePlan::new(hops)
}

/// The mints and amount decide which direction the leg lands in.
pub(super) fn leg_of(
    input_mint: &str,
    output_mint: &str,
    amount: u64,
    quoted: Option<RoutePlan>,
    original: Option<RoutePlan>,
    realized: Option<RoutePlan>,
) -> RerouteLegNotification {
    serde_json::from_value(serde_json::json!({
        "inputMint": input_mint,
        "outputMint": output_mint,
        "amount": amount,
        "swapMode": "ExactIn",
        "originalQuotedOut": 0,
        "metisQuotedOut": 0,
        "routeSummary": "",
        "routePlan": quoted,
        "originalRoutePlan": original,
        "realizedRoutePlan": realized,
    }))
    .expect("a leg fixture decodes")
}

/// A notification wrapping `legs`, landed on both sides unless told otherwise.
pub(super) struct Notification {
    legs: Vec<RerouteLegNotification>,
    err: Option<String>,
    original_failed: bool,
    original_realized_output_amount: Option<u64>,
}

impl Notification {
    pub(super) fn new(legs: Vec<RerouteLegNotification>) -> Self {
        Self {
            legs,
            err: None,
            original_failed: false,
            original_realized_output_amount: None,
        }
    }

    /// A routeless swap, the shape pricing reads.
    pub(super) fn swap(input_mint: &str, output_mint: &str, amount: u64, output: u64) -> Self {
        let leg = leg_of(input_mint, output_mint, amount, None, None, None);
        Self {
            original_realized_output_amount: Some(output),
            ..Self::new(vec![leg])
        }
    }

    /// The simulation reverted.
    pub(super) fn reverted(mut self) -> Self {
        self.err = Some("reverted".to_string());
        self
    }

    /// The original failed on L1, so its route events are not flow the venue served.
    pub(super) fn original_failed(mut self) -> Self {
        self.original_failed = true;
        self
    }

    pub(super) fn build(self) -> RerouteNotification {
        serde_json::from_value(serde_json::json!({
            "context": {"slot": 1},
            "slot": 1,
            "batchIndex": 0,
            "originalSignature": "sig",
            "legs": self.legs,
            "routedTransaction": {"data": "AA==", "encoding": "base64"},
            "err": self.err,
            "logs": [],
            "computeUnitsConsumed": 0,
            "fee": 5_000,
            "realizedOutputAmount": serde_json::Value::Null,
            "originalRealizedOutputAmount": self.original_realized_output_amount,
            "originalFailed": self.original_failed,
        }))
        .expect("a notification fixture decodes")
    }
}