simulator-api 0.24.0

Wire-protocol types for the Solana simulator backtest WebSocket API
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
//! Per-template accounting of direct-fill probes. Probes have their own table
//! (`direct_fill_probes`) and their own wire variant (`ReplacementNotification::DirectFill`), so
//! a population arrives pre-selected; nothing here checks that, and metis rows fed in still
//! render as a plausible-looking probe table.

use std::collections::BTreeMap;

use serde::Serialize;

use super::{LegRecord, Prices, SwapMode, fmt::commas_u};

/// The bucket vocabulary [`ProbeReport::from_records`] keys `outcomes` on, reconstructed from a
/// probe's split outcome/category pair: reading `category` straight through would rebucket
/// `unattributed`/`unsimulatable` — which carry none — as `"filled"`. Keyed on the wire strings,
/// which the row's typed `outcome` renders to via its `#[sea_orm(string_value = ...)]`.
pub fn bucket(outcome: &str, category: Option<&str>) -> Option<String> {
    match outcome {
        "filled" => None,
        "reverted" => Some(format!("reverted_{}", category.unwrap_or_default())),
        other => Some(other.to_string()),
    }
}

#[derive(Default, Clone, Serialize)]
pub struct TemplateProbes {
    /// `filled`, `reverted_*`, `unattributed`, `unsimulatable` — the keys the run metrics
    /// count, so report and log reconcile.
    pub outcomes: BTreeMap<String, u64>,
    /// Probes with a recorded template-vs-historical change.
    pub scored_vs_historical: u64,
    /// Sum of the recorded `output_bps`, which the writer clamps.
    pub bps_vs_historical_total: i64,
    /// Probes where both the template and the same-state original filled — the like-for-like.
    pub pairs_vs_same_state: u64,
    /// Computed here, unclamped: a ratio of two same-state simulations of one hop is
    /// structurally bounded where a market ratio would not be.
    pub bps_vs_same_state_total: f64,
    /// Dollars behind `scored_vs_historical`, and those probes' bps weighted by them. Zero when
    /// the range priced no mint the probes traded.
    pub notional_usd: f64,
    pub weighted_bps_usd: f64,
}

impl TemplateProbes {
    fn probes(&self) -> u64 {
        self.outcomes.values().sum()
    }

    fn filled(&self) -> u64 {
        self.outcomes.get("filled").copied().unwrap_or(0)
    }

    fn mean(total: f64, count: u64, unit: &str) -> String {
        match count {
            0 => "—".to_string(),
            n => format!("{:+.2} ({} {unit})", total / n as f64, commas_u(n)),
        }
    }

    fn row(&self, template: &str) -> String {
        let probes = self.probes();
        let filled = self.filled();
        let pct = filled as f64 / probes as f64 * 100.0;
        let vs_historical = Self::mean(
            self.bps_vs_historical_total as f64,
            self.scored_vs_historical,
            "scored",
        );
        let vs_same_state = Self::mean(
            self.bps_vs_same_state_total,
            self.pairs_vs_same_state,
            "pairs",
        );
        let per_dollar = match self.notional_usd {
            notional if notional > 0.0 => format!(
                "{:+.2} (${})",
                self.weighted_bps_usd / notional,
                commas_u(notional as u64)
            ),
            _ => "—".to_string(),
        };
        format!(
            "| {template} | {} | {} ({pct:.1}%) | {vs_historical} | {per_dollar} | {vs_same_state} |",
            commas_u(probes),
            commas_u(filled),
        )
    }

    fn outcomes_line(&self, template: &str) -> String {
        let parts: Vec<String> = self
            .outcomes
            .iter()
            .map(|(outcome, n)| format!("{outcome} {}", commas_u(*n)))
            .collect();
        format!("{template} outcomes: {}", parts.join(", "))
    }
}

#[derive(Default, Clone, Serialize)]
pub struct ProbeReport {
    pub by_template: BTreeMap<String, TemplateProbes>,
}

impl ProbeReport {
    pub fn from_records(records: &[LegRecord], prices: Option<&Prices>) -> Self {
        let mut report = Self::default();
        for record in records {
            let template = record
                .template
                .clone()
                .unwrap_or_else(|| "(unnamed)".to_string());
            let entry = report.by_template.entry(template).or_default();
            let outcome = record
                .error_category
                .clone()
                .unwrap_or_else(|| "filled".to_string());
            *entry.outcomes.entry(outcome).or_default() += 1;
            if let Some(bps) = record.output_bps {
                entry.scored_vs_historical += 1;
                entry.bps_vs_historical_total += bps;
                let priced_mint = match record.swap_mode {
                    SwapMode::ExactIn => &record.input_mint,
                    SwapMode::ExactOut => &record.output_mint,
                };
                if let Some(usd) = prices.and_then(|p| p.usd(priced_mint, record.amount as f64)) {
                    entry.notional_usd += usd;
                    entry.weighted_bps_usd += usd * bps as f64;
                }
            }
            if let (Some(new), Some(original)) =
                (record.new_output_amount, record.quoted_output_amount)
                && original > 0
            {
                entry.pairs_vs_same_state += 1;
                let change = (new as f64 / original as f64 - 1.0) * 1e4;
                entry.bps_vs_same_state_total += match record.swap_mode {
                    SwapMode::ExactIn => change,
                    SwapMode::ExactOut => -change,
                };
            }
        }
        report
    }

    pub fn merge(&mut self, other: &Self) {
        for (template, probes) in &other.by_template {
            let entry = self.by_template.entry(template.clone()).or_default();
            entry.notional_usd += probes.notional_usd;
            entry.weighted_bps_usd += probes.weighted_bps_usd;
            for (outcome, n) in &probes.outcomes {
                *entry.outcomes.entry(outcome.clone()).or_default() += n;
            }
            entry.scored_vs_historical += probes.scored_vs_historical;
            entry.bps_vs_historical_total += probes.bps_vs_historical_total;
            entry.pairs_vs_same_state += probes.pairs_vs_same_state;
            entry.bps_vs_same_state_total += probes.bps_vs_same_state_total;
        }
    }

    pub fn is_empty(&self) -> bool {
        self.by_template.is_empty()
    }

    pub fn render(&self, name: &str) -> String {
        let mut lines = vec![
            format!("### {name}: direct-fill probes"),
            "| Template | probes | filled | per hop (bps) | per dollar (bps) | vs same-state (bps) |"
                .to_string(),
            "|---|---|---|---|---|---|".to_string(),
        ];
        for (template, probes) in &self.by_template {
            lines.push(probes.row(template));
        }
        for (template, probes) in &self.by_template {
            lines.push(probes.outcomes_line(template));
        }
        lines.push(String::new());
        lines.join("\n")
    }
}

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

    use super::*;
    use crate::{
        mints,
        reroute_report::{Samples, testing::record_of},
    };

    /// Fixes SOL at $100 from the stream's own swaps, as the benchmark tests do.
    fn prices() -> Prices {
        let mut samples = Samples::default();
        for n in 1..=3u64 {
            samples.push(&LegRecord {
                original_realized_output_amount: Some(n * 100_000_000),
                ..record_of(mints::SOL, mints::USDC, n * 1_000_000_000, None, None, None)
            });
        }
        samples.prices().expect("anchored")
    }

    /// A hop counts for the money through it, so one large good fill outweighs many tiny bad
    /// ones — the per-hop mean says the template lost, the per-dollar mean says it won.
    #[test]
    fn the_per_dollar_mean_weighs_hops_by_notional() {
        let hop = |lamports: u64, bps: i64| LegRecord {
            template: Some("Tempest".to_string()),
            output_bps: Some(bps),
            ..record_of(mints::SOL, mints::USDC, lamports, None, None, None)
        };
        // Three hundredths of a SOL at -100 bps against one whole SOL at +10.
        let records = [
            hop(10_000_000, -100),
            hop(10_000_000, -100),
            hop(1_000_000_000, 10),
        ];
        let template = |prices: Option<&Prices>| {
            ProbeReport::from_records(&records, prices).by_template["Tempest"].clone()
        };

        let weighted = template(Some(&prices()));
        let per_hop: i64 = (-100 - 100 + 10) / 3;
        let per_dollar = (0.01 * -100.0 + 0.01 * -100.0 + 1.0 * 10.0) / 1.02;
        assert_eq!(weighted.bps_vs_historical_total / 3, per_hop, "per hop");
        assert!(
            (weighted.weighted_bps_usd / weighted.notional_usd - per_dollar).abs() < 0.01,
            "per dollar follows the large hop, not the count"
        );
        // Without prices the column is simply absent rather than silently unweighted.
        assert_eq!(template(None).notional_usd, 0.0);
    }

    fn probe(template: &str) -> LegRecord {
        LegRecord {
            template: Some(template.to_string()),
            ..record_of(mints::SOL, mints::USDC, 1_000_000, None, None, None)
        }
    }

    #[rstest]
    #[case::success_falls_back_to_filled(None, "filled")]
    #[case::reverted_keeps_its_category(Some("reverted_slippage".to_string()), "reverted_slippage")]
    #[case::unattributed_keeps_its_category(Some("unattributed".to_string()), "unattributed")]
    fn from_records_buckets_outcomes_by_error_category_else_filled(
        #[case] error_category: Option<String>,
        #[case] expected: &str,
    ) {
        let record = LegRecord {
            error_category,
            ..probe("Tempest")
        };
        let report = ProbeReport::from_records(&[record], None);
        assert_eq!(
            report.by_template["Tempest"].outcomes.get(expected),
            Some(&1)
        );
    }

    #[test]
    fn scored_vs_historical_counts_only_rows_carrying_output_bps() {
        let scored = LegRecord {
            output_bps: Some(12),
            ..probe("Tempest")
        };
        let unscored = probe("Tempest");

        let report = ProbeReport::from_records(&[scored, unscored], None);
        let template = &report.by_template["Tempest"];
        assert_eq!(template.scored_vs_historical, 1);
        assert_eq!(template.bps_vs_historical_total, 12);
    }

    #[rstest]
    #[case::both_present_and_positive(Some(1_010_000), Some(1_000_000), 1)]
    #[case::new_amount_missing(None, Some(1_000_000), 0)]
    #[case::original_amount_missing(Some(1_010_000), None, 0)]
    #[case::original_amount_zero(Some(1_010_000), Some(0), 0)]
    fn same_state_pairs_require_both_amounts_and_a_positive_original(
        #[case] new_output_amount: Option<u64>,
        #[case] quoted_output_amount: Option<u64>,
        #[case] expected_pairs: u64,
    ) {
        let record = LegRecord {
            new_output_amount,
            quoted_output_amount,
            ..probe("Tempest")
        };
        let report = ProbeReport::from_records(&[record], None);
        assert_eq!(
            report.by_template["Tempest"].pairs_vs_same_state,
            expected_pairs
        );
    }

    #[test]
    fn same_state_bps_is_the_relative_change_from_quoted_to_new() {
        let record = LegRecord {
            new_output_amount: Some(1_010_000),
            quoted_output_amount: Some(1_000_000),
            ..probe("Tempest")
        };
        let report = ProbeReport::from_records(&[record], None);
        let template = &report.by_template["Tempest"];
        assert_eq!(template.pairs_vs_same_state, 1);
        assert!((template.bps_vs_same_state_total - 100.0).abs() < 1e-9);
    }

    #[test]
    fn merge_carries_every_field() {
        let a = LegRecord {
            output_bps: Some(10),
            new_output_amount: Some(1_010_000),
            quoted_output_amount: Some(1_000_000),
            ..probe("Tempest")
        };
        let b = LegRecord {
            error_category: Some("reverted_slippage".to_string()),
            output_bps: Some(20),
            new_output_amount: Some(1_020_000),
            quoted_output_amount: Some(1_000_000),
            ..probe("Tempest")
        };

        let mut report = ProbeReport::from_records(&[a], None);
        report.merge(&ProbeReport::from_records(&[b], None));

        let template = &report.by_template["Tempest"];
        assert_eq!(template.outcomes.get("filled"), Some(&1));
        assert_eq!(template.outcomes.get("reverted_slippage"), Some(&1));
        assert_eq!(template.scored_vs_historical, 2);
        assert_eq!(template.bps_vs_historical_total, 30);
        assert_eq!(template.pairs_vs_same_state, 2);
        assert!((template.bps_vs_same_state_total - 300.0).abs() < 1e-9);
    }

    #[test]
    fn a_record_with_no_template_lands_under_unnamed() {
        let record = record_of(mints::SOL, mints::USDC, 1_000_000, None, None, None);
        let report = ProbeReport::from_records(&[record], None);
        assert!(report.by_template.contains_key("(unnamed)"));
    }

    #[test]
    fn render_contains_the_template_row_and_outcomes_line() {
        let filled_a = LegRecord {
            output_bps: Some(100),
            new_output_amount: Some(1_010_000),
            quoted_output_amount: Some(1_000_000),
            ..probe("Tempest")
        };
        let filled_b = LegRecord {
            output_bps: Some(200),
            new_output_amount: Some(1_020_000),
            quoted_output_amount: Some(1_000_000),
            ..probe("Tempest")
        };
        let reverted = LegRecord {
            error_category: Some("reverted_slippage".to_string()),
            ..probe("Tempest")
        };

        let report = ProbeReport::from_records(&[filled_a, filled_b, reverted], None);
        let rendered = report.render("R1");

        assert!(rendered.contains("### R1: direct-fill probes"));
        assert!(rendered.contains("| Template | probes | filled |"));
        assert!(
            rendered.contains(
                "| Tempest | 3 | 2 (66.7%) | +150.00 (2 scored) | — | +150.00 (2 pairs) |"
            )
        );
        assert!(rendered.contains("Tempest outcomes: filled 2, reverted_slippage 1"));
    }

    #[test]
    fn render_shows_an_em_dash_when_a_mean_has_no_observations() {
        let reverted = LegRecord {
            error_category: Some("reverted_slippage".to_string()),
            ..probe("Vertigo")
        };

        let report = ProbeReport::from_records(&[reverted], None);
        let rendered = report.render("R2");

        assert!(rendered.contains("| Vertigo | 1 | 0 (0.0%) | — | — |"));
        assert!(rendered.contains("Vertigo outcomes: reverted_slippage 1"));
    }

    #[test]
    fn is_empty_reflects_an_empty_template_map() {
        assert!(ProbeReport::default().is_empty());
        let report = ProbeReport::from_records(&[probe("Tempest")], None);
        assert!(!report.is_empty());
    }

    /// An ExactOut probe's `amount` is the output it was sized at, so its notional is the output
    /// mint's, not the input's: 1_000_000 base units is $1 of USDC, not 0.001 SOL at $100.
    #[test]
    fn an_exact_out_notional_is_priced_in_the_output_mint() {
        let record = LegRecord {
            swap_mode: SwapMode::ExactOut,
            output_bps: Some(10),
            ..probe("Tempest")
        };
        let report = ProbeReport::from_records(&[record], Some(&prices()));
        let template = &report.by_template["Tempest"];
        assert!(
            (template.notional_usd - 1.0).abs() < 1e-9,
            "{}",
            template.notional_usd
        );
    }

    #[rstest]
    #[case::cheaper(900, 1_000.0)]
    #[case::dearer(1_100, -1_000.0)]
    fn an_exact_out_comparison_scores_input_saved(#[case] charged: u64, #[case] expected: f64) {
        let record = LegRecord {
            swap_mode: SwapMode::ExactOut,
            new_output_amount: Some(charged),
            quoted_output_amount: Some(1_000),
            ..probe("t")
        };
        let report = ProbeReport::from_records(&[record], None);
        assert!((report.by_template["t"].bps_vs_same_state_total - expected).abs() < 1e-9);
    }
}