fynd-core 0.99.13

Core solving logic for Fynd DEX router
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Computes the amount out a route delivers when its pAMM legs fall back to Uniswap V3.
//!
//! The encoder drops the quote when this number cannot clear `min_amount_out`.
//!
//! A pAMM quote only reaches the chain in the block the maker quotes for, so simulating a pAMM
//! route against a mined block reverts. Routing the leg through Titan's PropAMMRouter
//! (`propammfallback:` protocol family) falls back to Uniswap V3 instead of reverting. The fallback
//! pays less than the pAMM quote. `min_amount_out` keeps describing the pAMM quote and the slippage
//! the user accepted, so a fallback below that floor reverts the route anyway — and lowering the
//! floor to fit would pay the user less than they accepted. Such a route is not quoted.

pub mod fee_tier_fetcher;

use std::sync::{Arc, RwLock};

use num_bigint::BigUint;
use rustc_hash::{FxHashMap, FxHashSet};
use tycho_simulation::tycho_common::models::Address;

use crate::{
    feed::{events::MarketEvent, market_data::MarketDataView},
    replay::replay_route,
    types::{ComponentId, Route, Swap},
};

/// Whether `route` has a leg the PropAMMRouter executes (`propammfallback:` protocol family).
///
/// Gate `fallback_amount_out` behind this: it lets a route without a pAMM leg — every route until
/// a venue adopts the prefix — cost neither a market read lock nor a copy of the fee tiers.
pub fn has_pamm_leg(route: &Route) -> bool {
    route.swaps().iter().any(|swap| {
        swap.protocol()
            .starts_with(PROPAMM_FALLBACK_PREFIX)
    })
}

/// Replays `route` with every `propammfallback:` leg pointing at its Uniswap V3 fallback pool.
///
/// Uses `replay_route`, so split fractions and shared-pool depletion behave exactly as they do for
/// any other route: a substituted leg's smaller output feeds the next one, and two legs on one
/// pool see it deplete. The replay runs against `market`'s overlay-aware states, so a labeled
/// solve prices the fallback on the same state the route was solved on.
///
/// A route without a pAMM leg substitutes nothing, so the result is its plain replayed amount out.
/// Check `has_pamm_leg` first to skip that pointless replay.
pub fn fallback_amount_out(
    route: &Route,
    market: &MarketDataView<'_>,
    fee_tiers: &FeeTiers,
    index: &FallbackPoolIndex,
) -> FallbackAmountOut {
    let mut substituted = Vec::with_capacity(route.swaps().len());
    for swap in route.swaps() {
        if !swap
            .protocol()
            .starts_with(PROPAMM_FALLBACK_PREFIX)
        {
            substituted.push(swap.clone());
            continue;
        }

        let fee_tier = fee_tiers.resolved_tier(swap.token_in(), swap.token_out());
        let Some(pool) = index.pool_for(swap.token_in(), swap.token_out(), fee_tier) else {
            return FallbackAmountOut::NoFallbackPool {
                component_id: swap.component_id().to_string(),
                fee_tier,
            };
        };
        let (Some(component), Some(state)) =
            (market.get_component(pool), market.get_simulation_state(pool))
        else {
            // The index was built from this market, so a missing component or state means it was
            // removed between the two reads. Treat it as no fallback pool.
            return FallbackAmountOut::NoFallbackPool {
                component_id: swap.component_id().to_string(),
                fee_tier,
            };
        };
        substituted.push(
            Swap::new(
                pool.clone(),
                component.protocol_system.clone(),
                swap.token_in().clone(),
                swap.token_out().clone(),
                swap.amount_in().clone(),
                swap.amount_out().clone(),
                swap.gas_estimate().clone(),
                component.clone(),
                state.clone_box(),
            )
            .with_split(*swap.split()),
        );
    }

    // `replay_route` resolves every pool from the state passed to it, so the substituted route has
    // to be priced against the same overlay the algorithm solved against.
    let replayed_components: Vec<ComponentId> = substituted
        .iter()
        .map(|swap| swap.component_id().to_string())
        .collect();
    let market_state = market.extract_subset_with_overlay(
        &replayed_components
            .iter()
            .collect::<FxHashSet<_>>(),
    );

    let substituted = match Route::new(substituted, FxHashMap::default()) {
        Ok(route) => route,
        Err(e) => return FallbackAmountOut::NotPriceable { reason: e.to_string() },
    };
    match replay_route(&substituted, &market_state) {
        Ok(replay) => FallbackAmountOut::AmountOut(replay.amount_out),
        Err(e) => FallbackAmountOut::NotPriceable { reason: e.to_string() },
    }
}

/// The amount out a route delivers when its pAMM legs fall back to Uniswap V3.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FallbackAmountOut {
    /// The fallback fill. Quote the route only when this amount clears `min_amount_out`.
    AmountOut(BigUint),
    /// No Uniswap V3 pool at the router's fee tier, so the fallback reverts too. Drop the route.
    NoFallbackPool {
        /// The pAMM leg with no fallback pool.
        component_id: ComponentId,
        /// The fee tier the router would have used.
        fee_tier: u32,
    },
    /// The substituted route could not be simulated, so there is no amount to floor at. Drop the
    /// route.
    NotPriceable {
        /// What stopped the simulation.
        reason: String,
    },
}

/// The Uniswap V3 fee tier the PropAMMRouter falls back to, per token pair.
///
/// Mirrors the router's `resolvedFee`: the per-pair override if set, else `fallbackFee`. Both are
/// settable on chain without an upgrade, so this is read from chain rather than hardcoded.
#[derive(Debug, Clone)]
pub struct FeeTiers {
    default_tier: u32,
    per_pair: FxHashMap<(Address, Address), u32>,
}

impl FeeTiers {
    /// Creates the tiers with `default_tier` as the router's global `fallbackFee`.
    pub fn new(default_tier: u32) -> Self {
        Self { default_tier, per_pair: FxHashMap::default() }
    }

    /// Order-independent, like the router's `_pairKey`.
    pub fn resolved_tier(&self, token_a: &Address, token_b: &Address) -> u32 {
        self.per_pair
            .get(&sorted_pair(token_a, token_b))
            .copied()
            .unwrap_or(self.default_tier)
    }

    /// A tier of 0 clears the override, matching the router's `setPairFee`.
    pub fn set_pair_tier(&mut self, token_a: &Address, token_b: &Address, fee_tier: u32) {
        let key = sorted_pair(token_a, token_b);
        if fee_tier == 0 {
            self.per_pair.remove(&key);
        } else {
            self.per_pair.insert(key, fee_tier);
        }
    }

    /// How many pairs carry an override.
    pub fn pair_override_count(&self) -> usize {
        self.per_pair.len()
    }
}

/// Shared with the task that reads the tiers from the router.
///
/// Empty until the first successful read. A worker that finds it empty drops the pAMM route rather
/// than guess a tier: the wrong tier prices the wrong pool, and a floor derived from the wrong
/// pool is the revert this whole path exists to prevent.
#[derive(Debug, Clone, Default)]
pub struct SharedFeeTiers(Arc<RwLock<Option<FeeTiers>>>);

impl SharedFeeTiers {
    /// Returns a copy of the current fee tiers, or `None` before the first successful read.
    pub fn snapshot(&self) -> Option<FeeTiers> {
        self.0
            .read()
            .expect("fallback fee tier lock poisoned")
            .clone()
    }

    /// Replaces the fee tiers with freshly read on-chain values.
    pub fn set(&self, fee_tiers: FeeTiers) {
        *self
            .0
            .write()
            .expect("fallback fee tier lock poisoned") = Some(fee_tiers);
    }
}

/// Fallback pools by token pair and fee tier, so finding one is a lookup, not a market scan.
///
/// A pool qualifies on its component alone — protocol system, token pair and `fee` attribute — so
/// the index only changes when the market adds or removes a component, not when a pool's state
/// changes. `apply_event` keeps it current from `MarketEvent::MarketUpdated`, which costs one
/// lookup per added component instead of a full rebuild.
#[derive(Debug, Default, Clone)]
pub struct FallbackPoolIndex {
    pools: FxHashMap<(Address, Address, u32), ComponentId>,
    /// Reverse map, so a removed component can be found without scanning `pools`.
    keys: FxHashMap<ComponentId, (Address, Address, u32)>,
}

impl FallbackPoolIndex {
    /// Indexes every qualifying component in the market. Call once, then keep current with
    /// `apply_event`.
    pub fn build(market: &MarketDataView<'_>) -> Self {
        let mut index = Self::default();
        for component_id in market.component_topology().into_keys() {
            index.insert(market, component_id);
        }
        index
    }

    /// Adds the components in `event` and drops the removed ones.
    ///
    /// `updated_components` are state changes, which cannot alter whether a component qualifies,
    /// so they are ignored.
    pub fn apply_event(&mut self, market: &MarketDataView<'_>, event: &MarketEvent) {
        let MarketEvent::MarketUpdated { added_components, removed_components, .. } = event;
        for component_id in removed_components {
            self.remove(component_id);
        }
        for component_id in added_components.keys() {
            self.insert(market, component_id.clone());
        }
    }

    /// Indexes `component_id` if it can serve the router's fallback.
    ///
    /// Skips components that are not `uniswap_v3`, do not have exactly two tokens, or carry no
    /// `fee` attribute. The router's fallback is a single-hop `exactInputSingle`, so nothing else
    /// can serve it.
    fn insert(&mut self, market: &MarketDataView<'_>, component_id: ComponentId) {
        let Some(component) = market.get_component(&component_id) else { return };
        if component.protocol_system != FALLBACK_PROTOCOL_SYSTEM {
            return;
        }
        let [token_a, token_b] = component.tokens.as_slice() else { return };
        let Some(fee_tier) = component
            .static_attributes
            .get(FEE_ATTRIBUTE)
            .and_then(parse_fee_tier)
        else {
            return;
        };
        let (low, high) = sorted_pair(token_a, token_b);
        self.keys
            .insert(component_id.clone(), (low.clone(), high.clone(), fee_tier));
        self.pools
            .insert((low, high, fee_tier), component_id);
    }

    /// Drops `component_id`, leaving any pool that took over its key in place.
    fn remove(&mut self, component_id: &ComponentId) {
        let Some(key) = self.keys.remove(component_id) else { return };
        if self.pools.get(&key) == Some(component_id) {
            self.pools.remove(&key);
        }
    }

    /// The component the router would fall back to.
    pub fn pool_for(
        &self,
        token_a: &Address,
        token_b: &Address,
        fee_tier: u32,
    ) -> Option<&ComponentId> {
        let (low, high) = sorted_pair(token_a, token_b);
        self.pools.get(&(low, high, fee_tier))
    }
}

/// Must match `tycho-execution`'s `PROPAMM_FALLBACK_PREFIX`.
pub const PROPAMM_FALLBACK_PREFIX: &str = "propammfallback:";

/// The PropAMMRouter deployment on Ethereum mainnet: the router serving Titan's pAMM ecosystem,
/// written by LambdaClass.
///
/// <https://github.com/lambdaclass/propamm-router-contracts>
pub const PROPAMM_ROUTER_ADDRESS: &str = "0x4DdF368080CD7946db5b459aD591c350158175e1";

/// pAMM venues on the router's whitelist that Fynd routes through.
///
/// Only whitelisted venues may use `PROPAMM_FALLBACK_PREFIX`. The router reverts `UnknownVenue` for
/// any other address, which fires the catch arm and executes every swap on Uniswap V3 at a worse
/// price than the pAMM gives. `FeeTierFetcher` reads each venue's pairs to learn which pairs need
/// a fee tier.
pub const PROPAMM_VENUES: &[&str] = &[
    // Fermi (FermiSwapper)
    "0x5979458912F80B96d30D4220af8E2e4925A33320",
    // Kipseli
    "0x71e790dd841c8A9061487cb3E78C288E75cE0B3d",
];

/// Protocol system whose pools the PropAMMRouter falls back to.
const FALLBACK_PROTOCOL_SYSTEM: &str = "uniswap_v3";

/// Static attribute carrying a Uniswap V3 pool's fee tier, in hundredths of a bip.
const FEE_ATTRIBUTE: &str = "fee";

/// Order-independent pair key, matching the router's `_pairKey`.
fn sorted_pair(token_a: &Address, token_b: &Address) -> (Address, Address) {
    if token_a <= token_b {
        (token_a.clone(), token_b.clone())
    } else {
        (token_b.clone(), token_a.clone())
    }
}

/// Tycho encodes the `fee` attribute big-endian in up to 4 bytes.
fn parse_fee_tier(raw: &tycho_simulation::tycho_common::Bytes) -> Option<u32> {
    let bytes = raw.as_ref();
    if bytes.is_empty() || bytes.len() > 4 {
        return None;
    }
    let mut padded = [0u8; 4];
    padded[4 - bytes.len()..].copy_from_slice(bytes);
    Some(u32::from_be_bytes(padded))
}

#[cfg(test)]
mod tests {
    use tycho_simulation::{
        tycho_common::Bytes, tycho_core::simulation::protocol_sim::ProtocolSim,
    };

    use super::*;
    use crate::algorithm::test_utils::{self as util, addr};

    /// The tier the router reports as its global `fallbackFee`, in the tests below.
    const DEFAULT_TIER: u32 = 3000;

    #[test]
    fn test_resolved_tier_defaults_and_overrides() {
        let mut fee_tiers = FeeTiers::new(DEFAULT_TIER);
        assert_eq!(fee_tiers.resolved_tier(&addr(1), &addr(2)), DEFAULT_TIER);

        fee_tiers.set_pair_tier(&addr(2), &addr(1), 500);
        // Order-independent, like the router's sorted pair key.
        assert_eq!(fee_tiers.resolved_tier(&addr(1), &addr(2)), 500);
        assert_eq!(fee_tiers.resolved_tier(&addr(2), &addr(1)), 500);

        // A zero tier clears the override, matching `setPairFee`.
        fee_tiers.set_pair_tier(&addr(1), &addr(2), 0);
        assert_eq!(fee_tiers.resolved_tier(&addr(1), &addr(2)), DEFAULT_TIER);
    }

    /// Nothing may price a pAMM route before the router's tiers are read.
    #[test]
    fn test_shared_fee_tiers_empty_until_set() {
        let shared = SharedFeeTiers::default();
        assert!(shared.snapshot().is_none());

        shared.set(FeeTiers::new(500));
        assert_eq!(
            shared
                .snapshot()
                .expect("tiers were set")
                .resolved_tier(&addr(1), &addr(2)),
            500
        );
    }

    #[test]
    fn test_parse_fee_tier() {
        assert_eq!(parse_fee_tier(&Bytes::from(3000_i32.to_be_bytes().to_vec())), Some(3000));
        // Tycho trims leading zero bytes on some attributes.
        assert_eq!(parse_fee_tier(&Bytes::from(vec![0x01, 0xf4])), Some(500));
        assert_eq!(parse_fee_tier(&Bytes::from(Vec::new())), None);
        assert_eq!(parse_fee_tier(&Bytes::from(vec![0u8; 5])), None);
    }

    #[test]
    fn test_pool_for_token_order() {
        let mut index = FallbackPoolIndex::default();
        let (low, high) = sorted_pair(&addr(9), &addr(3));
        index
            .pools
            .insert((low, high, 500), "pool".to_string());

        assert_eq!(
            index
                .pool_for(&addr(3), &addr(9), 500)
                .map(String::as_str),
            Some("pool")
        );
        assert_eq!(
            index
                .pool_for(&addr(9), &addr(3), 500)
                .map(String::as_str),
            Some("pool")
        );
        // Wrong tier is a different pool.
        assert!(index
            .pool_for(&addr(3), &addr(9), 3000)
            .is_none());
    }

    /// The index tracks the component set, so an added pool becomes usable and a removed one
    /// stops being usable without a rebuild.
    #[test]
    fn test_apply_event_added_and_removed_components() {
        let market = market_with_fallback_pool(500);
        let view = market
            .try_read_blocking()
            .expect("uncontended");
        let mut index = FallbackPoolIndex::build(&view);
        assert_eq!(index.pools.len(), 1);

        index.apply_event(
            &view,
            &MarketEvent::MarketUpdated {
                added_components: FxHashMap::default(),
                removed_components: vec![FALLBACK_POOL.to_string()],
                updated_components: Vec::new(),
            },
        );
        assert!(index.pools.is_empty());
        assert!(index
            .pool_for(&addr(1), &addr(2), 500)
            .is_none());

        index.apply_event(
            &view,
            &MarketEvent::MarketUpdated {
                added_components: FxHashMap::from_iter([(FALLBACK_POOL.to_string(), Vec::new())]),
                removed_components: Vec::new(),
                updated_components: Vec::new(),
            },
        );
        assert_eq!(
            index
                .pool_for(&addr(1), &addr(2), 500)
                .map(String::as_str),
            Some(FALLBACK_POOL)
        );
    }

    /// A state change cannot alter whether a component qualifies, so it must not touch the index.
    #[test]
    fn test_apply_event_state_updates() {
        let market = market_with_fallback_pool(500);
        let view = market
            .try_read_blocking()
            .expect("uncontended");
        let mut index = FallbackPoolIndex::build(&view);

        index.apply_event(
            &view,
            &MarketEvent::MarketUpdated {
                added_components: FxHashMap::default(),
                removed_components: Vec::new(),
                updated_components: vec![FALLBACK_POOL.to_string()],
            },
        );

        assert_eq!(index.pools.len(), 1);
    }

    /// A pAMM quoting 2x and a fallback pool quoting 1x, so the result is visibly the fallback.
    const PAMM_PRICE: f64 = 2.0;
    const FALLBACK_PRICE: f64 = 1.0;
    const FALLBACK_POOL: &str = "0xretry";
    const PAMM_COMPONENT: &str = "0xpamm";
    const PAMM_PROTOCOL: &str = "propammfallback:fermiswap";

    /// Market holding one `uniswap_v3` fallback pool for the (1, 2) pair at `fee_tier`.
    fn market_with_fallback_pool(fee_tier: u32) -> crate::feed::market_data::MarketData {
        let (token_in, token_out) = (util::token(1, "WETH"), util::token(2, "USDC"));
        let mut component = util::component_with_protocol(
            FALLBACK_POOL,
            FALLBACK_PROTOCOL_SYSTEM,
            &[token_in.clone(), token_out.clone()],
        );
        component
            .static_attributes
            .insert(FEE_ATTRIBUTE.to_string(), Bytes::from(fee_tier.to_be_bytes().to_vec()));

        let market = crate::feed::market_data::MarketData::new_shared();
        {
            let mut state = market.try_write().expect("uncontended");
            state.upsert_tokens([token_in, token_out]);
            state.upsert_components([component]);
            state.update_states([(
                FALLBACK_POOL.to_string(),
                Box::new(util::MockProtocolSim::new(FALLBACK_PRICE)) as Box<dyn ProtocolSim>,
            )]);
        }
        market
    }

    fn pamm_swap() -> Swap {
        let (token_in, token_out) = (util::token(1, "WETH"), util::token(2, "USDC"));
        Swap::new(
            PAMM_COMPONENT.to_string(),
            PAMM_PROTOCOL.to_string(),
            token_in.address.clone(),
            token_out.address.clone(),
            BigUint::from(1_000u32),
            BigUint::from(2_000u32),
            BigUint::from(100_000u32),
            util::component_with_protocol(PAMM_COMPONENT, PAMM_PROTOCOL, &[token_in, token_out]),
            Box::new(util::MockProtocolSim::new(PAMM_PRICE)),
        )
    }

    /// A leg on the fallback pool itself, so the route holds no pAMM.
    fn uniswap_swap() -> Swap {
        let (token_in, token_out) = (util::token(1, "WETH"), util::token(2, "USDC"));
        Swap::new(
            FALLBACK_POOL.to_string(),
            FALLBACK_PROTOCOL_SYSTEM.to_string(),
            token_in.address.clone(),
            token_out.address.clone(),
            BigUint::from(1_000u32),
            BigUint::from(1_000u32),
            BigUint::from(100_000u32),
            util::component_with_protocol(
                FALLBACK_POOL,
                FALLBACK_PROTOCOL_SYSTEM,
                &[token_in, token_out],
            ),
            Box::new(util::MockProtocolSim::new(FALLBACK_PRICE)),
        )
    }

    /// Only a route with a `propammfallback:` leg pays for fallback pricing.
    #[test]
    fn test_has_pamm_leg() {
        let non_pamm =
            Route::new(vec![uniswap_swap()], FxHashMap::default()).expect("non-empty route");
        assert!(!has_pamm_leg(&non_pamm));

        let pamm = Route::new(vec![pamm_swap()], FxHashMap::default()).expect("non-empty route");
        assert!(has_pamm_leg(&pamm));
    }

    /// The result is the fallback pool's output, not the pAMM's.
    #[test]
    fn test_fallback_amount_out_with_fallback_pool() {
        let market = market_with_fallback_pool(500);
        let view = market
            .try_read_blocking()
            .expect("uncontended");
        let index = FallbackPoolIndex::build(&view);
        let mut fee_tiers = FeeTiers::new(DEFAULT_TIER);
        fee_tiers.set_pair_tier(&addr(1), &addr(2), 500);

        let swap = pamm_swap();
        let route = Route::new(vec![swap.clone()], FxHashMap::default()).expect("non-empty route");
        let amount_out = fallback_amount_out(&route, &view, &fee_tiers, &index);

        // MockProtocolSim multiplies by spot_price for ascending token addresses: 1000 * 1.0,
        // against the pAMM leg's recorded 2000.
        assert_eq!(amount_out, FallbackAmountOut::AmountOut(BigUint::from(1_000u32)));
        assert_eq!(*swap.amount_out(), BigUint::from(2_000u32));
    }

    /// A labeled solve prices the fallback on the overlay it was solved against, not on the base
    /// state.
    #[tokio::test]
    async fn test_fallback_amount_out_uses_the_overlay_state() {
        let market = market_with_fallback_pool(500);
        let label = "test_overlay".to_string();
        let mut overlay: rustc_hash::FxHashMap<ComponentId, Box<dyn ProtocolSim>> =
            FxHashMap::default();
        overlay.insert(
            FALLBACK_POOL.to_string(),
            Box::new(util::MockProtocolSim::new(FALLBACK_PRICE / 2.0)),
        );
        market
            .register_labeled_state(label.clone(), overlay, u64::MAX)
            .await;

        let view = market
            .read_labeled(&label)
            .await
            .expect("registered overlay");
        let index = FallbackPoolIndex::build(&view);
        let mut fee_tiers = FeeTiers::new(DEFAULT_TIER);
        fee_tiers.set_pair_tier(&addr(1), &addr(2), 500);

        let route = Route::new(vec![pamm_swap()], FxHashMap::default()).expect("non-empty route");
        let amount_out = fallback_amount_out(&route, &view, &fee_tiers, &index);

        // The base pool would pay 1000 * 1.0; the overlay halves the price.
        assert_eq!(amount_out, FallbackAmountOut::AmountOut(BigUint::from(500u32)));
    }

    /// No pool at the router's fee tier means the fallback reverts too, so the route is no better
    /// than the direct path and must be dropped.
    #[test]
    fn test_fallback_amount_out_without_pool_at_resolved_tier() {
        // Pool exists at 500, but the router resolves this pair to the 3000 default.
        let market = market_with_fallback_pool(500);
        let view = market
            .try_read_blocking()
            .expect("uncontended");
        let index = FallbackPoolIndex::build(&view);

        let route = Route::new(vec![pamm_swap()], FxHashMap::default()).expect("non-empty route");
        let amount_out = fallback_amount_out(&route, &view, &FeeTiers::new(DEFAULT_TIER), &index);

        assert_eq!(
            amount_out,
            FallbackAmountOut::NoFallbackPool {
                component_id: PAMM_COMPONENT.to_string(),
                fee_tier: DEFAULT_TIER,
            }
        );
    }

    /// A split route prices through `replay_route`, so both legs of the split are counted.
    #[test]
    fn test_fallback_amount_out_split_route() {
        let market = market_with_fallback_pool(500);
        let view = market
            .try_read_blocking()
            .expect("uncontended");
        let index = FallbackPoolIndex::build(&view);
        let mut fee_tiers = FeeTiers::new(DEFAULT_TIER);
        fee_tiers.set_pair_tier(&addr(1), &addr(2), 500);

        // 60% through the pAMM, the remainder through the same pool. Both legs substitute to the
        // one fallback pool, which then sees the second leg deplete it.
        let route =
            Route::new(vec![pamm_swap().with_split(0.6), pamm_swap()], FxHashMap::default())
                .expect("non-empty route");
        let amount_out = fallback_amount_out(&route, &view, &fee_tiers, &index);

        // The whole 1000 of input is routed either way, so the split must not lose or invent
        // amount relative to the single-leg case.
        assert!(
            matches!(&amount_out, FallbackAmountOut::AmountOut(amount) if *amount > BigUint::ZERO),
            "expected a priced split route, got {amount_out:?}"
        );
    }

    /// Only `uniswap_v3` components with a fee attribute can serve the fallback.
    #[test]
    fn test_pool_index_non_uniswap_v3_components() {
        let market = market_with_fallback_pool(500);
        {
            let mut state = market.try_write().expect("uncontended");
            state.upsert_components([util::component_with_protocol(
                "0xv2",
                "uniswap_v2",
                &[util::token(1, "WETH"), util::token(2, "USDC")],
            )]);
        }
        let view = market
            .try_read_blocking()
            .expect("uncontended");

        let index = FallbackPoolIndex::build(&view);
        assert_eq!(index.pools.len(), 1);
        assert_eq!(
            index
                .pool_for(&addr(1), &addr(2), 500)
                .map(String::as_str),
            Some(FALLBACK_POOL)
        );
    }
}