Skip to main content

simulator_api/reroute_report/
prices.rs

1//! Pricing a reroute stream in dollars using nothing but the stream.
2//!
3//! No oracle and no external feed. SOL/USD comes from the range's own SOL<->USDC reroutes, the
4//! other majors from their pairings against SOL, USDC and USDT, and everything else from its
5//! major<->exotic legs. A derived price needs [`MIN_SAMPLES`] observations before it is trusted;
6//! the SOL anchor is exempt, since every other price hangs from it.
7
8use std::collections::HashMap;
9
10use solana_address::Address;
11
12use super::{
13    fmt::median,
14    record::{LegRecord, SwapMode},
15};
16use crate::mints;
17
18const LAMPORTS_PER_SOL: f64 = 1e9;
19/// Sub-SOL probe swaps realize off-market and drag the median, so the anchor ignores them.
20const ONE_SOL_LAMPORTS: u128 = 1_000_000_000;
21const USDC_UNITS_PER_USD: f64 = 1e6;
22/// A derived price needs at least this many samples to be trusted.
23const MIN_SAMPLES: usize = 3;
24
25/// One ExactIn reroute. ExactOut is excluded: its `amount` is in the output token and would
26/// poison every ratio it entered.
27struct Sample {
28    input_mint: Address,
29    output_mint: Address,
30    amount: u128,
31    original_output: u128,
32}
33
34/// Retained until the range ends, because a median cannot be folded. Two mints and two amounts
35/// against a notification's multi-KB route plans, which is what lets a report skip the stream.
36#[derive(Default)]
37pub struct Samples(Vec<Sample>);
38
39impl Samples {
40    /// Keeps the leg as a pricing sample only if it can price something: ExactIn, a realized and
41    /// nonzero original output, a nonzero input amount, the original didn't fail, and at least
42    /// one side is a major to anchor on. Anything else is silently dropped.
43    pub fn push(&mut self, record: &LegRecord) {
44        let Some(original_output) = record.original_realized_output_amount else {
45            return;
46        };
47        if record.swap_mode != SwapMode::ExactIn
48            || record.amount == 0
49            || original_output == 0
50            || record.original_failed
51        {
52            return;
53        }
54        // Every derivation anchors on a major, so a leg between two exotics is unreadable.
55        if !mints::is_major(&record.input_mint) && !mints::is_major(&record.output_mint) {
56            return;
57        }
58        self.0.push(Sample {
59            input_mint: record.input_mint,
60            output_mint: record.output_mint,
61            amount: u128::from(record.amount),
62            original_output: u128::from(original_output),
63        });
64    }
65
66    /// `None` when the range carries no SOL<->USDC ExactIn reroutes to anchor on.
67    pub fn prices(&self) -> Option<Prices> {
68        let sol_usd = sol_price(&self.0)?;
69        let mut prices = Prices {
70            sol_usd,
71            priced: priced_major_prices(&self.0, sol_usd),
72            exotic: HashMap::new(),
73        };
74        prices.exotic = exotic_unit_prices(&self.0, &prices);
75        Some(prices)
76    }
77}
78
79/// Per-range prices, derived from the range's own reroutes.
80pub struct Prices {
81    sol_usd: f64,
82    /// USD per whole unit for the priced majors.
83    priced: HashMap<Address, f64>,
84    /// USD per *raw* unit for exotics, inferred from their major<->exotic legs.
85    exotic: HashMap<Address, f64>,
86}
87
88impl Prices {
89    pub const fn sol_usd(&self) -> f64 {
90        self.sol_usd
91    }
92
93    /// `None` when the range could not price the mint, which the report counts rather than
94    /// folding in as zero.
95    ///
96    /// Linear in `raw` on every path, so a caller may sum one mint's raw amounts and convert the
97    /// total once, or pass an amount a weight has already scaled out of whole units.
98    pub fn usd(&self, mint: &Address, raw: f64) -> Option<f64> {
99        self.scaled_major(mint, raw)
100            .or_else(|| self.exotic.get(mint).map(|unit| raw * unit))
101    }
102
103    /// [`Self::usd`] for a mint still in its wire form. Unparseable is unpriceable.
104    pub fn usd_str(&self, mint: &str, raw: f64) -> Option<f64> {
105        self.usd(&mint.parse().ok()?, raw)
106    }
107
108    /// The majors alone, which is what [`exotic_unit_prices`] reads while building the exotics.
109    fn scaled_major(&self, mint: &Address, raw: f64) -> Option<f64> {
110        if *mint == mints::SOL {
111            return Some(raw / LAMPORTS_PER_SOL * self.sol_usd);
112        }
113        if let Some(decimals) = mints::stable_decimals(mint) {
114            return Some(raw / 10f64.powi(decimals as i32));
115        }
116        let decimals = mints::priced_decimals(mint)?;
117        let unit = self.priced.get(mint)?;
118        Some(raw / 10f64.powi(decimals as i32) * unit)
119    }
120}
121
122fn median_of(mut values: Vec<f64>) -> Option<f64> {
123    (values.len() >= MIN_SAMPLES).then(|| median(&mut values))
124}
125
126fn sol_price(samples: &[Sample]) -> Option<f64> {
127    let mut ratios: Vec<f64> = samples
128        .iter()
129        .filter_map(|sample| {
130            let (input, output) = (sample.input_mint, sample.output_mint);
131            if input == mints::SOL && output == mints::USDC && sample.amount >= ONE_SOL_LAMPORTS {
132                Some(
133                    (sample.original_output as f64 / USDC_UNITS_PER_USD)
134                        / (sample.amount as f64 / LAMPORTS_PER_SOL),
135                )
136            } else if input == mints::USDC
137                && output == mints::SOL
138                && sample.original_output >= ONE_SOL_LAMPORTS
139            {
140                Some(
141                    (sample.amount as f64 / USDC_UNITS_PER_USD)
142                        / (sample.original_output as f64 / LAMPORTS_PER_SOL),
143                )
144            } else {
145                None
146            }
147        })
148        .collect();
149    (!ratios.is_empty()).then(|| median(&mut ratios))
150}
151
152/// Median USD per priced major, from its pairings with USDC/USDT ($1) and SOL, both directions.
153fn priced_major_prices(samples: &[Sample], sol_usd: f64) -> HashMap<Address, f64> {
154    let mut per_mint: HashMap<Address, Vec<f64>> = HashMap::new();
155    for sample in samples {
156        let oriented = [
157            (
158                sample.input_mint,
159                sample.output_mint,
160                sample.amount,
161                sample.original_output,
162            ),
163            (
164                sample.output_mint,
165                sample.input_mint,
166                sample.original_output,
167                sample.amount,
168            ),
169        ];
170        for (mint, other, mint_raw, other_raw) in oriented {
171            let Some(decimals) = mints::priced_decimals(&mint) else {
172                continue;
173            };
174            let other_usd = if other == mints::USDC || other == mints::USDT {
175                other_raw as f64 / USDC_UNITS_PER_USD
176            } else if other == mints::SOL {
177                other_raw as f64 / LAMPORTS_PER_SOL * sol_usd
178            } else {
179                continue;
180            };
181            per_mint
182                .entry(mint)
183                .or_default()
184                .push(other_usd / (mint_raw as f64 / 10f64.powi(decimals as i32)));
185        }
186    }
187    per_mint
188        .into_iter()
189        .filter_map(|(mint, ratios)| Some((mint, median_of(ratios)?)))
190        .collect()
191}
192
193/// Median USD per raw unit for each exotic, from its major<->exotic legs.
194fn exotic_unit_prices(samples: &[Sample], prices: &Prices) -> HashMap<Address, f64> {
195    let mut per_mint: HashMap<Address, Vec<f64>> = HashMap::new();
196    for sample in samples {
197        let (input, output) = (sample.input_mint, sample.output_mint);
198        match (mints::is_major(&input), mints::is_major(&output)) {
199            (true, false) => {
200                if let Some(usd) = prices.scaled_major(&input, sample.amount as f64) {
201                    per_mint
202                        .entry(output)
203                        .or_default()
204                        .push(usd / sample.original_output as f64);
205                }
206            }
207            (false, true) => {
208                if let Some(usd) = prices.scaled_major(&output, sample.original_output as f64) {
209                    per_mint
210                        .entry(input)
211                        .or_default()
212                        .push(usd / sample.amount as f64);
213                }
214            }
215            _ => {}
216        }
217    }
218    per_mint
219        .into_iter()
220        .filter_map(|(mint, ratios)| Some((mint, median_of(ratios)?)))
221        .collect()
222}
223
224#[cfg(test)]
225mod tests {
226    use solana_address::address;
227
228    use super::*;
229
230    /// A longtail mint no table names, so it can only be priced through a major leg.
231    const BONK: Address = address!("DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263");
232    /// A governance blue-chip: parseable, deliberately not a major, and quoted by nobody below.
233    const RAY: Address = address!("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R");
234    /// What [`anchored`] derives, and what every price hanging off it is stated in.
235    const SOL_USD: f64 = 100.0;
236
237    fn sample_record(
238        input: Address,
239        output: Address,
240        amount: u64,
241        original_output: u64,
242    ) -> LegRecord {
243        LegRecord {
244            original_signature: "sig".to_string(),
245            input_mint: input,
246            output_mint: output,
247            amount,
248            swap_mode: SwapMode::ExactIn,
249            failed: false,
250            original_failed: false,
251            realized_output_amount: None,
252            original_realized_output_amount: Some(original_output),
253            route_plan: None,
254            original_route_plan: None,
255            realized_route_plan: None,
256            template: None,
257            quoted_output_amount: None,
258            new_output_amount: None,
259            error_category: None,
260            output_bps: None,
261        }
262    }
263
264    /// Three SOL->USDC reroutes at [`SOL_USD`], which is the only thing a range needs before any
265    /// other price can be derived.
266    fn anchored() -> Samples {
267        let mut samples = Samples::default();
268        for (amount, output) in [
269            (1_000_000_000, 100_000_000),
270            (2_000_000_000, 200_000_000),
271            (3_000_000_000, 300_000_000),
272        ] {
273            samples.push(&sample_record(mints::SOL, mints::USDC, amount, output));
274        }
275        samples
276    }
277
278    /// Sub-SOL swaps realize off-market and would drag the anchor. Two of each, so dropping the
279    /// gate moves the median off `SOL_USD` instead of leaving the real legs a majority.
280    #[test]
281    fn the_sol_anchor_ignores_swaps_under_one_sol() {
282        let mut samples = Samples::default();
283        // Two dust swaps priced at $1000/SOL and two real ones at $100: ungated the median would
284        // be 550.0.
285        for (amount, output) in [
286            (1_000, 1_000),
287            (2_000, 2_000),
288            (1_000_000_000, 100_000_000),
289            (2_000_000_000, 200_000_000),
290        ] {
291            samples.push(&sample_record(mints::SOL, mints::USDC, amount, output));
292        }
293        let prices = samples.prices().expect("anchored");
294        assert_eq!(prices.sol_usd(), SOL_USD);
295    }
296
297    /// The oriented-pair loop reads a priced major from either side of a leg, and the ratio has
298    /// to invert with it — transposed, one cbBTC would be worth 1/60,000th of a dollar.
299    #[test]
300    fn a_priced_major_medians_both_orientations_of_its_legs() {
301        let cbbtc = mints::PRICED_MAJORS[1].address;
302        assert_eq!(mints::priced_decimals(&cbbtc), Some(8));
303        let one_cbbtc: u64 = 1_00000000;
304
305        let mut samples = anchored();
306        // $59,000, $60,000 and — quoted the other way round — $61,000 per cbBTC.
307        for (input, output, amount, original_output) in [
308            (cbbtc, mints::USDC, one_cbbtc, 59_000_000_000),
309            (cbbtc, mints::USDC, 2 * one_cbbtc, 120_000_000_000),
310            (mints::USDC, cbbtc, 61_000_000_000, one_cbbtc),
311        ] {
312            samples.push(&sample_record(input, output, amount, original_output));
313        }
314
315        let prices = samples.prices().expect("anchored");
316        assert_eq!(prices.usd(&cbbtc, one_cbbtc as f64), Some(60_000.0));
317        // Linear in the raw amount, and read at cbBTC's eight decimals rather than USDC's six.
318        assert_eq!(prices.usd(&cbbtc, one_cbbtc as f64 / 2.0), Some(30_000.0));
319    }
320
321    /// USD per raw exotic unit, folded exactly as the derivation folds it: the major side's
322    /// dollars over the raw units they bought.
323    fn unit_price(lamports: f64, exotic_raw: f64) -> f64 {
324        lamports / LAMPORTS_PER_SOL * SOL_USD / exotic_raw
325    }
326
327    /// An exotic has no decimals to read, so it is priced per *raw* unit off its major leg.
328    /// Transposing that division would price the whole longtail at ~1e10 dollars a unit.
329    #[test]
330    fn an_exotic_is_priced_per_raw_unit_off_its_major_leg() {
331        let mut samples = anchored();
332        // 1e12 BONK per SOL, then 2e12 (half the price per unit), then 1e12 again — so the
333        // median unit is the one the first leg quotes, not the cheap outlier in the middle.
334        for (lamports, bonk) in [
335            (1_000_000_000, 1_000_000_000_000),
336            (2_000_000_000, 4_000_000_000_000),
337            (3_000_000_000, 3_000_000_000_000),
338        ] {
339            samples.push(&sample_record(mints::SOL, BONK, lamports, bonk));
340        }
341
342        let prices = samples.prices().expect("anchored");
343        let unit = unit_price(1_000_000_000.0, 1_000_000_000_000.0);
344        assert_eq!(prices.usd(&BONK, 1e12), Some(1e12 * unit));
345        assert_eq!(prices.usd(&BONK, 5e11), Some(5e11 * unit));
346    }
347
348    /// USDe is 9 decimals; treating every stable as 1e6 values its legs 1000x over. Stable
349    /// pricing reads decimals alone, so the USDe<->USDC legs below anchor nothing — the range
350    /// still needs its own SOL<->USDC reroutes to produce a `Prices` at all.
351    #[test]
352    fn a_stable_is_valued_at_its_own_decimals() {
353        let usde_mint = mints::STABLES[4].address;
354        let mut samples = anchored();
355        for _ in 0..3 {
356            samples.push(&sample_record(
357                usde_mint,
358                mints::USDC,
359                1_000_000_000,
360                1_000_000,
361            ));
362        }
363        let prices = samples.prices().expect("anchored");
364        assert_eq!(prices.usd(&usde_mint, 1e6), Some(1e6 / 1e9));
365        assert_eq!(prices.usd(&mints::USDC, 1e6), Some(1.0));
366        // Linear in the raw amount on every path, which is what lets a caller sum first.
367        assert_eq!(prices.usd(&usde_mint, 1e9), Some(1.0));
368        assert_eq!(prices.usd(&mints::SOL, 1e9), Some(SOL_USD));
369    }
370
371    #[test]
372    fn a_price_needs_enough_samples() {
373        assert_eq!(median_of(vec![1.0, 2.0]), None);
374        assert_eq!(median_of(vec![1.0, 2.0, 3.0]), Some(2.0));
375        assert_eq!(median_of(vec![4.0, 1.0, 2.0, 3.0]), Some(2.5));
376    }
377
378    #[test]
379    fn an_unpriceable_mint_is_none_rather_than_zero() {
380        let prices = anchored().prices().expect("anchored");
381        assert_eq!(prices.usd_str("SomeLongtailMintNobodyQuoted", 1e6), None);
382        // A mint the range simply never traded is as unpriceable as an unparseable one.
383        assert_eq!(prices.usd(&RAY, 1e6), None);
384    }
385}