tycho-simulation 0.520.0

Provides tools for interacting with protocol states, calculating spot prices, and quoting token swaps.
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
use std::{
    any::Any,
    collections::{BTreeMap, HashMap},
};

use alloy::primitives::U256;
use itertools::Either;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use tycho_common::{
    dto::ProtocolStateDelta,
    models::token::Token,
    simulation::{
        errors::{SimulationError, TransitionError},
        protocol_sim::{Balances, GetAmountOutResult, ProtocolSim},
    },
    Bytes,
};

use crate::{
    evm::protocol::{
        pancakeswap_infinity_bin::{
            attributes::{
                apply_reserve, decode_bin_id_attribute, decode_bool_attribute,
                decode_u16_attribute, parse_bin_key,
            },
            math::{
                bin::{
                    get_amounts_out, get_drain_amounts, get_liquidity, get_price_from_id,
                    BinAmounts,
                },
                constants::{BIN_ID_COUNT, MAX_LIQUIDITY_PER_BIN},
                fee::{calculate_swap_fee, protocol_fee_amount},
            },
        },
        u256_num::u256_to_f64,
        utils::{add_fee_markup, uniswap::FEE_PIPS_DENOMINATOR as PIPS_DENOMINATOR},
    },
    utils::decimals_exponent,
};

// Measured through the TychoRouter on a Base fork (`PancakeswapInfinityBinGas.t.sol`, which
// pins them): `singleSwap` paid by `transferFrom` on the USDC/USDT pool in both directions,
// crossing 0 to 21 bins, fit by least squares to 204.5k + 12.1k per bin crossed, residuals within
// 6k. The router's work and the user's transfer are in scope on purpose: the executor is a
// callback protocol, so the gas estimator adds nothing for the transfer and the quote has to carry
// it. A quote's gas becomes `Swap::estimated_gas`, which the encoder sums into
// `EncodedSolution::estimated_gas`, so these size the solution's gas as well as ranking routes.
const SWAP_BASE_GAS: u64 = 205_000;
const GAS_PER_BIN_CROSSED: u64 = 12_100;

/// The pool takes the input and returns the output as `int128`, so an amount past this reverts.
const MAX_POOL_AMOUNT: u128 = i128::MAX as u128;

/// A bin's reserves after a step: the input credited less the protocol's share, the output taken.
///
/// Errors where the pool reverts, so `swap` propagates it and `get_limits` stops before the bin:
/// the input reserve overflowing `u128`, or the bin's liquidity passing `MAX_LIQUIDITY_PER_BIN`.
fn credit_bin(
    id: u32,
    (x, y): (u128, u128),
    swap_for_y: bool,
    price: U256,
    step: &BinAmounts,
    protocol_fee: u16,
    swap_fee: u32,
) -> Result<(u128, u128), SimulationError> {
    let (reserve_in, reserve_out) = if swap_for_y { (x, y) } else { (y, x) };
    // The protocol's cut leaves the pool: off the input, never off the output.
    let credited =
        step.amount_in_with_fee - protocol_fee_amount(step.fee_amount, protocol_fee, swap_fee)?;
    let new_reserve_in = reserve_in
        .checked_add(credited)
        .ok_or_else(|| {
            SimulationError::InvalidInput(
                format!("bin {id} reserve overflows crediting {credited}"),
                None,
            )
        })?;
    let new_reserve_out = reserve_out - step.amount_out;
    let (new_x, new_y) = if swap_for_y {
        (new_reserve_in, new_reserve_out)
    } else {
        (new_reserve_out, new_reserve_in)
    };
    if get_liquidity(new_x, new_y, price)? > MAX_LIQUIDITY_PER_BIN {
        return Err(SimulationError::InvalidInput(
            format!("bin {id} exceeds MAX_LIQUIDITY_PER_BIN after the swap"),
            None,
        ));
    }
    Ok((new_x, new_y))
}

/// A PancakeSwap Infinity Bin pool.
///
/// `x` is `currency0`, `y` is `currency1`, so `swap_for_y` means `token_in < token_out` by address.
///
/// Only bins with liquidity are kept, and the `BTreeMap` order replaces the on-chain bitmap tree:
/// the next non-empty bin is a range query. The one bin that can hold liquidity outside the tree
/// is the active one, after a burn down to `MINIMUM_SHARE`; `active_in_tree` marks it.
///
/// Built through [`Self::new`], which holds the invariants `swap` relies on: every id fits the
/// chain's u24, and no bin is kept at `(0, 0)`. Deserialization goes through it too.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "PancakeswapInfinityBinStateData")]
pub struct PancakeswapInfinityBinState {
    /// Bin the next swap starts in. u24 on chain.
    pub(crate) active_id: u32,
    /// Whether the active bin is in the on-chain tree. A swap starts in the active bin either way,
    /// but once it moves off a bin outside the tree, no swap reaches that bin again until a mint
    /// re-adds it.
    pub(crate) active_in_tree: bool,
    /// Price ratio between neighbouring bins, in basis points.
    pub(crate) bin_step: u16,
    /// LP fee in pips (`1e6` denominator).
    pub(crate) lp_fee: u32,
    /// Protocol fee in pips charged when selling token0, the low 12 bits of the packed fee.
    pub(crate) protocol_fee_zero_for_one: u16,
    /// Protocol fee in pips charged when selling token1, the high 12 bits.
    pub(crate) protocol_fee_one_for_zero: u16,
    /// `bin id -> (reserve_x, reserve_y)`, absolute reserves as indexed from `reserveOfBin`.
    pub(crate) bins: BTreeMap<u32, (u128, u128)>,
}

/// The wire shape of a state. `TryFrom` applies the invariants.
#[derive(Deserialize)]
struct PancakeswapInfinityBinStateData {
    active_id: u32,
    active_in_tree: bool,
    bin_step: u16,
    lp_fee: u32,
    protocol_fee_zero_for_one: u16,
    protocol_fee_one_for_zero: u16,
    bins: BTreeMap<u32, (u128, u128)>,
}

impl TryFrom<PancakeswapInfinityBinStateData> for PancakeswapInfinityBinState {
    type Error = SimulationError;

    fn try_from(data: PancakeswapInfinityBinStateData) -> Result<Self, Self::Error> {
        Self::new(
            data.active_id,
            data.active_in_tree,
            data.bin_step,
            data.lp_fee,
            data.protocol_fee_zero_for_one,
            data.protocol_fee_one_for_zero,
            data.bins,
        )
    }
}

/// Outcome of an exact-input swap.
#[derive(Debug)]
struct BinSwapResult {
    /// Output the swap pays, over every bin it took from.
    amount_out: u128,
    /// Bins stepped into beyond the first.
    bins_crossed: u64,
    new_state: PancakeswapInfinityBinState,
}

impl PancakeswapInfinityBinState {
    /// A pool state, checked: `active_id` and every bin id below `BIN_ID_COUNT`, and no bin at
    /// `(0, 0)`, which the walk would pay a crossing for.
    pub fn new(
        active_id: u32,
        active_in_tree: bool,
        bin_step: u16,
        lp_fee: u32,
        protocol_fee_zero_for_one: u16,
        protocol_fee_one_for_zero: u16,
        bins: BTreeMap<u32, (u128, u128)>,
    ) -> Result<Self, SimulationError> {
        let invalid = |message: String| SimulationError::InvalidInput(message, None);
        if active_id >= BIN_ID_COUNT {
            return Err(invalid(format!("active bin {active_id} is wider than the u24 on chain")));
        }
        for (&id, &reserves) in &bins {
            if id >= BIN_ID_COUNT {
                return Err(invalid(format!("bin {id} is wider than the u24 on chain")));
            }
            if reserves == (0, 0) {
                return Err(invalid(format!("bin {id} holds nothing and must not be kept")));
            }
        }
        Ok(Self {
            active_id,
            active_in_tree,
            bin_step,
            lp_fee,
            protocol_fee_zero_for_one,
            protocol_fee_one_for_zero,
            bins,
        })
    }

    /// Exact-input swap across as many bins as the input reaches.
    ///
    /// Errors `InvalidInput("Out of liquidity")` when the bins run out first: on chain the whole
    /// swap reverts (`BinPool__OutOfLiquidity`), so a partial fill would quote an unexecutable
    /// trade.
    ///
    /// [`BinPool.sol#L111-L222`](https://github.com/pancakeswap/infinity-core/blob/7c04695f/src/pool-bin/libraries/BinPool.sol#L111-L222)
    fn swap(&self, swap_for_y: bool, amount_in: u128) -> Result<BinSwapResult, SimulationError> {
        // Fixed before the loop, not per bin. Protocol fee is charged first, LP fee on the rest.
        let protocol_fee = self.protocol_fee(swap_for_y);
        let swap_fee = self.swap_fee(swap_for_y);
        let mut new_state = self.clone();
        let mut active = self.active_id;
        let mut left = amount_in;
        let mut amount_out = 0u128;
        let mut bins_crossed = 0u64;

        loop {
            let (x, y) = new_state
                .bins
                .get(&active)
                .copied()
                .unwrap_or((0, 0));
            let reserve_out = if swap_for_y { y } else { x };

            // A bin with nothing on the output side is skipped, not an error.
            if reserve_out > 0 {
                let price = get_price_from_id(active, self.bin_step)?;
                let step = get_amounts_out(reserve_out, swap_fee, price, swap_for_y, left)?;
                let (new_x, new_y) =
                    credit_bin(active, (x, y), swap_for_y, price, &step, protocol_fee, swap_fee)?;

                if new_x == 0 && new_y == 0 {
                    new_state.bins.remove(&active);
                } else {
                    new_state
                        .bins
                        .insert(active, (new_x, new_y));
                }

                left -= step.amount_in_with_fee;
                amount_out = amount_out
                    .checked_add(step.amount_out)
                    .ok_or_else(|| {
                        SimulationError::InvalidInput(
                            format!("output overflows u128 at bin {active}"),
                            None,
                        )
                    })?;
            }

            if left == 0 {
                break;
            }
            let Some(next) = new_state.next_bin_with_liquidity(swap_for_y, active) else {
                return Err(SimulationError::InvalidInput("Out of liquidity".to_string(), None));
            };
            active = next;
            bins_crossed += 1;
        }

        if amount_out == 0 {
            return Err(SimulationError::InvalidInput(
                format!("swap of {amount_in} produced no output"),
                None,
            ));
        }
        // The pool narrows the output with `safeInt128` before returning it.
        //
        // [`BinPool.sol#L210-L212`](https://github.com/pancakeswap/infinity-core/blob/7c04695f/src/pool-bin/libraries/BinPool.sol#L210-L212)
        if amount_out > MAX_POOL_AMOUNT {
            return Err(SimulationError::InvalidInput(
                format!("output {amount_out} exceeds int128"),
                None,
            ));
        }

        // The walk only steps through tree bins, so a start bin outside the tree is out of reach
        // once left. Whatever dust it keeps on chain would otherwise be quoted the other way.
        //
        // [`BinPool.sol#L293-L297`](https://github.com/pancakeswap/infinity-core/blob/7c04695f/src/pool-bin/libraries/BinPool.sol#L293-L297)
        if active != self.active_id {
            if !self.active_in_tree {
                new_state.bins.remove(&self.active_id);
            }
            new_state.active_in_tree = true;
        }
        new_state.active_id = active;
        Ok(BinSwapResult { amount_out, bins_crossed, new_state })
    }

    /// Next bin with liquidity, excluding `from`: lower id selling x, higher selling y.
    ///
    /// Matches `findFirstRight` / `findFirstLeft`, which step off `from` before searching despite
    /// what their natspec says, because the caller has already spent that bin.
    ///
    /// [`BinPool.sol#L293-L297`](https://github.com/pancakeswap/infinity-core/blob/7c04695f/src/pool-bin/libraries/BinPool.sol#L293-L297)
    fn next_bin_with_liquidity(&self, swap_for_y: bool, from: u32) -> Option<u32> {
        if swap_for_y {
            self.bins
                .range(..from)
                .next_back()
                .map(|(&id, _)| id)
        } else {
            self.bins
                .range(from + 1..)
                .next()
                .map(|(&id, _)| id)
        }
    }

    /// First bin a swap takes output from: the active bin, else the next one in the swap's
    /// direction with reserves on the output side. The same bins `swap` would skip are skipped.
    fn first_bin_with_output(&self, swap_for_y: bool) -> Option<u32> {
        let has_output = |id: u32| {
            self.bins
                .get(&id)
                .is_some_and(|&(x, y)| if swap_for_y { y > 0 } else { x > 0 })
        };
        let mut bin = self.active_id;
        while !has_output(bin) {
            bin = self.next_bin_with_liquidity(swap_for_y, bin)?;
        }
        Some(bin)
    }

    /// Protocol fee in pips for one direction: `zero2one` when selling x.
    fn protocol_fee(&self, swap_for_y: bool) -> u16 {
        if swap_for_y {
            self.protocol_fee_zero_for_one
        } else {
            self.protocol_fee_one_for_zero
        }
    }

    /// Combined fee in pips charged on a bin: protocol fee first, LP fee on the remainder.
    fn swap_fee(&self, swap_for_y: bool) -> u32 {
        let protocol_fee = self.protocol_fee(swap_for_y);
        if protocol_fee == 0 {
            self.lp_fee
        } else {
            calculate_swap_fee(protocol_fee, self.lp_fee)
        }
    }
}

#[typetag::serde]
impl ProtocolSim for PancakeswapInfinityBinState {
    /// Swap fee as a ratio: protocol fee first, LP fee on the remainder.
    ///
    /// The protocol fee is directional, so one number only exists when both halves agree. The
    /// trait allows a panic otherwise, as `uniswap_v4/state.rs` does. The pools indexed so far
    /// carry equal halves, e.g. 3 pips each on the Base USDC/USDT pool.
    #[expect(
        clippy::unimplemented,
        reason = "fee() returns no error and the trait documents the panic for asymmetric fees"
    )]
    fn fee(&self) -> f64 {
        if self.protocol_fee_zero_for_one != self.protocol_fee_one_for_zero {
            unimplemented!(
                "Bin pools charge a directional protocol fee; use spot_price or get_amount_out"
            )
        }
        self.swap_fee(true) as f64 / f64::from(PIPS_DENOMINATOR)
    }

    /// Price of the bin a buy of `base` fills first, fee markup included. Constant sum inside a
    /// bin, so it holds exactly until that bin empties, unlike a CLMM's marginal price.
    ///
    /// That bin is not always the active one: a burn never moves `activeId`, and a swap that ends
    /// with no input left can stop on a bin whose output side it just drained. When no bin holds
    /// `base`, the active bin's price is returned, as Uniswap v3/v4 price an empty pool; that the
    /// trade cannot fill is `get_limits`' and `get_amount_out`'s to report.
    fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError> {
        // Buying base means selling quote: selling y (`swap_for_y` false) when base is x.
        let base_is_x = base < quote;
        let swap_for_y = !base_is_x;
        let bin = self
            .first_bin_with_output(swap_for_y)
            .unwrap_or(self.active_id);
        let price = u256_to_f64(get_price_from_id(bin, self.bin_step)?)? / 2.0f64.powi(128);

        // `price` is y per x, already quote-per-base when base is the lower address.
        let (token0, token1) = if base_is_x { (base, quote) } else { (quote, base) };
        let correction = 10f64.powi(decimals_exponent(token0.decimals, token1.decimals)?);
        let price = if base_is_x { price * correction } else { 1.0 / (price * correction) };
        let swap_fee = self.swap_fee(swap_for_y);
        Ok(add_fee_markup(price, swap_fee as f64 / f64::from(PIPS_DENOMINATOR)))
    }

    fn get_amount_out(
        &self,
        amount_in: BigUint,
        token_in: &Token,
        token_out: &Token,
    ) -> Result<GetAmountOutResult, SimulationError> {
        // currency0 is the lower address, so selling it is the x -> y direction.
        let swap_for_y = token_in < token_out;
        let amount = amount_in
            .to_u128()
            .filter(|amount| *amount <= MAX_POOL_AMOUNT)
            .ok_or_else(|| {
                SimulationError::InvalidInput(
                    format!("amount_in exceeds int128: {amount_in}"),
                    None,
                )
            })?;

        let BinSwapResult { amount_out, bins_crossed, new_state } =
            self.swap(swap_for_y, amount)?;
        let gas = SWAP_BASE_GAS + bins_crossed * GAS_PER_BIN_CROSSED;

        Ok(GetAmountOutResult::new(
            BigUint::from(amount_out),
            BigUint::from(gas),
            Box::new(new_state),
        ))
    }

    /// Maximum input the pool absorbs, and its output. `(0, 0)` when the direction is dry.
    ///
    /// A floor: the sum over whole bins, in walk order, of the input that drains each one and the
    /// reserve it pays out. The walk stops at the first bin a swap could not fill: its drain input
    /// exceeds `u128`, crediting it would breach `MAX_LIQUIDITY_PER_BIN` or overflow, or a total
    /// would pass `i128::MAX`, which is how the pool takes the input and returns the output. Dust
    /// whose output rounds to zero still errors, as it does on chain.
    fn get_limits(
        &self,
        sell_token: Bytes,
        buy_token: Bytes,
    ) -> Result<(BigUint, BigUint), SimulationError> {
        let swap_for_y = sell_token < buy_token;
        let bins = if swap_for_y {
            Either::Left(self.bins.range(..=self.active_id).rev())
        } else {
            Either::Right(self.bins.range(self.active_id..))
        };

        let protocol_fee = self.protocol_fee(swap_for_y);
        let swap_fee = self.swap_fee(swap_for_y);
        let mut total_in = 0u128;
        let mut total_out = 0u128;
        for (&id, &(x, y)) in bins {
            let reserve_out = if swap_for_y { y } else { x };
            if reserve_out == 0 {
                continue;
            }
            let price = get_price_from_id(id, self.bin_step)?;
            let Some(step) = get_drain_amounts(reserve_out, swap_fee, price, swap_for_y)? else {
                break;
            };
            // Draining credits the bin what `swap` would, so the walk stops where `swap` errors.
            // A partial fill might still fit, but a floor can stop short.
            if credit_bin(id, (x, y), swap_for_y, price, &step, protocol_fee, swap_fee).is_err() {
                break;
            }
            let (Some(next_in), Some(next_out)) = (
                total_in.checked_add(step.amount_in_with_fee),
                total_out.checked_add(step.amount_out),
            ) else {
                break;
            };
            if next_in > MAX_POOL_AMOUNT || next_out > MAX_POOL_AMOUNT {
                break;
            }
            total_in = next_in;
            total_out = next_out;
        }
        Ok((BigUint::from(total_in), BigUint::from(total_out)))
    }

    /// Applies an indexer delta. The attribute encodings are documented in [`super::attributes`].
    fn delta_transition(
        &mut self,
        delta: ProtocolStateDelta,
        _tokens: &HashMap<Bytes, Token>,
        _balances: &Balances,
    ) -> Result<(), TransitionError> {
        for (key, value) in delta.updated_attributes.iter() {
            match key.as_str() {
                "active_id" => self.active_id = decode_bin_id_attribute(key, value)?,
                "active_in_tree" => self.active_in_tree = decode_bool_attribute(key, value)?,
                "protocol_fees/zero2one" => {
                    self.protocol_fee_zero_for_one = decode_u16_attribute(key, value)?
                }
                "protocol_fees/one2zero" => {
                    self.protocol_fee_one_for_zero = decode_u16_attribute(key, value)?
                }
                _ if key.starts_with("bins/") => apply_reserve(&mut self.bins, key, value)?,
                _ => {}
            }
        }

        for key in delta.deleted_attributes.iter() {
            if key.starts_with("bins/") {
                let (id, _) = parse_bin_key(key)?;
                self.bins.remove(&id);
            }
        }
        // The substreams classifies a bin written to zero as a deletion while still carrying the
        // zero values, so both arrive. A bin left in the map costs the next swap a crossing.
        self.bins
            .retain(|_, reserves| *reserves != (0, 0));

        Ok(())
    }

    fn clone_box(&self) -> Box<dyn ProtocolSim> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn eq(&self, other: &dyn ProtocolSim) -> bool {
        other
            .as_any()
            .downcast_ref::<PancakeswapInfinityBinState>()
            .is_some_and(|other| self == other)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use alloy::{
        primitives::{aliases::U24, Address, Bytes as AlloyBytes, FixedBytes, U256},
        providers::{Provider, ProviderBuilder},
        rpc::types::TransactionRequest,
        sol,
        sol_types::SolCall,
    };
    use approx::assert_relative_eq;
    use rstest::rstest;
    use tycho_client::feed::{dto, synchronizer::ComponentWithState};
    use tycho_common::models::Chain;

    use super::{
        super::{attributes::reserve_attributes, math::constants::REAL_ID_SHIFT},
        *,
    };
    use crate::evm::protocol::{
        pancakeswap_infinity_bin::attributes::decode_u32_attribute,
        test_utils::try_decode_snapshot_with_defaults,
    };

    /// Pool with `bin_step` 10 and a 100-pip LP fee, no protocol fee, so `swap_fee` is 100 pips
    /// and the expectations below reuse the `math::bin` vectors.
    fn bin_state(active_id: u32, bins: &[(u32, (u128, u128))]) -> PancakeswapInfinityBinState {
        PancakeswapInfinityBinState::new(
            active_id,
            true,
            10,
            100,
            0,
            0,
            bins.iter().copied().collect(),
        )
        .unwrap()
    }

    /// Deserialization goes through `new`, so a wire state cannot skip the invariants.
    #[rstest]
    #[case::wide_active_id(r#"{"active_id":16777216,"active_in_tree":true,"bin_step":10,"lp_fee":100,"protocol_fee_zero_for_one":0,"protocol_fee_one_for_zero":0,"bins":{}}"#, "wider than the u24")]
    #[case::wide_bin_id(r#"{"active_id":1,"active_in_tree":true,"bin_step":10,"lp_fee":100,"protocol_fee_zero_for_one":0,"protocol_fee_one_for_zero":0,"bins":{"16777216":[1,1]}}"#, "wider than the u24")]
    #[case::empty_bin(r#"{"active_id":1,"active_in_tree":true,"bin_step":10,"lp_fee":100,"protocol_fee_zero_for_one":0,"protocol_fee_one_for_zero":0,"bins":{"1":[0,0]}}"#, "holds nothing")]
    fn test_deserialize_applies_the_invariants(#[case] json: &str, #[case] message: &str) {
        let err = serde_json::from_str::<PancakeswapInfinityBinState>(json)
            .expect_err("an invalid wire state must not deserialize");
        assert!(err.to_string().contains(message), "{err}");
    }

    /// Fits inside the active bin: no crossing, no move, reserves shift by the step's amounts.
    #[test]
    fn test_swap_within_active_bin() {
        let state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000_000_000))]);

        let result = state.swap(true, 1_000_000).unwrap();

        assert_eq!(result.amount_out, 999_900);
        assert_eq!(result.bins_crossed, 0);
        assert_eq!(
            result.new_state,
            bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (1_000_000, 1_000_000_000 - 999_900))])
        );
    }

    /// Drains the active bin and finishes in the next one down. Per-bin expectations come from the
    /// pinned `BinHelper.getAmountsOut`: 1001 in for 1000 out, then 999 in for 997 out.
    #[test]
    fn test_swap_crosses_into_next_bin() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (0, 1_000))],
        );

        let result = state.swap(true, 2_000).unwrap();

        assert_eq!(result.amount_out, 1_997);
        assert_eq!(result.bins_crossed, 1);
        assert_eq!(
            result.new_state,
            bin_state(
                REAL_ID_SHIFT - 1,
                &[(REAL_ID_SHIFT - 1, (999, 4_003)), (REAL_ID_SHIFT, (1_001, 0))]
            )
        );
    }

    /// With a protocol fee the pool keeps less than it charges: the protocol's share leaves the
    /// Vault, so the bin is credited `amount_in_with_fee` minus that share. Crediting the full
    /// amount would overstate every reserve after a swap and mis-price the next quote.
    ///
    /// Figures from the pinned Solidity for the pool the harness indexes, 3 pips per side over a
    /// 7-pip LP fee: swap fee 10 pips, so 1e6 in pays 10 units of fee, 3 of which go to the
    /// protocol and 7 stay in the bin.
    #[test]
    fn test_swap_withholds_the_protocol_fee_from_the_bin() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000_000_000))]);
        state.lp_fee = 7;
        state.protocol_fee_zero_for_one = 3;
        state.protocol_fee_one_for_zero = 3;

        let result = state.swap(true, 1_000_000).unwrap();

        let mut expected = state;
        expected
            .bins
            .insert(REAL_ID_SHIFT, (999_997, 1_000_000_000 - 999_990));
        assert_eq!(result.amount_out, 999_990, "output nets the whole 10-pip swap fee");
        assert_eq!(result.new_state, expected, "the protocol's 3 units never reach the bin");
    }

    /// Selling y walks up: drains the active bin, skips a bin holding only y, finishes two bins
    /// further. Per-bin figures from the pinned `BinHelper.getAmountsOut` with `swapForY` false:
    /// 1001 in for 1000 out, then 1999 in for 1992 out.
    #[test]
    fn test_swap_selling_y_walks_up() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[
                (REAL_ID_SHIFT, (1_000, 500)),
                (REAL_ID_SHIFT + 1, (0, 2_000)),
                (REAL_ID_SHIFT + 3, (4_000, 0)),
            ],
        );

        let result = state.swap(false, 3_000).unwrap();

        assert_eq!(result.amount_out, 2_992);
        assert_eq!(result.bins_crossed, 2);
        assert_eq!(
            result.new_state,
            bin_state(
                REAL_ID_SHIFT + 3,
                &[
                    (REAL_ID_SHIFT, (0, 1_501)),
                    // The y-only bin is untouched.
                    (REAL_ID_SHIFT + 1, (0, 2_000)),
                    (REAL_ID_SHIFT + 3, (2_008, 1_999)),
                ]
            )
        );
    }

    /// A burn down to `MINIMUM_SHARE` takes the active bin out of the tree but leaves its dust.
    /// A swap still starts there, but once it moves off, the chain cannot reach the bin again:
    /// the returned state must not keep it, or a quote the other way would walk into it.
    #[test]
    fn test_swap_off_an_active_dust_bin_drops_it() {
        let mut state =
            bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (1, 1))]);
        state.active_in_tree = false;

        let result = state.swap(true, 2_000).unwrap();

        // The dust's single unit of y comes out first; its 2 units of input are then lost.
        let mut expected = bin_state(REAL_ID_SHIFT - 1, &[(REAL_ID_SHIFT - 1, (1_998, 3_005))]);
        expected.active_in_tree = true;
        assert_eq!(result.amount_out, 1_996);
        assert_eq!(result.bins_crossed, 1);
        assert_eq!(result.new_state, expected);
        assert_eq!(
            result
                .new_state
                .swap(false, 1_000)
                .unwrap()
                .bins_crossed,
            0,
            "no bin above"
        );
    }

    /// A swap that stays in an active dust bin keeps it, dust and all: it is still the start.
    #[test]
    fn test_swap_within_an_active_dust_bin_keeps_it() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);
        state.active_in_tree = false;

        let result = state.swap(true, 500).unwrap();

        let mut expected = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (500, 1_000 - 499))]);
        expected.active_in_tree = false;
        assert_eq!(result.new_state, expected);
    }

    /// Five bins around the reference id, 1e9 of each token in each.
    fn five_bins() -> PancakeswapInfinityBinState {
        let bins: Vec<_> = (REAL_ID_SHIFT - 2..=REAL_ID_SHIFT + 2)
            .map(|id| (id, (1_000_000_000, 1_000_000_000)))
            .collect();
        bin_state(REAL_ID_SHIFT, &bins)
    }

    /// Protocol fees across several bins, from a copy of `BinPool.swap`'s exact-input loop run
    /// on the pinned libraries. Unequal halves catch the two directions' fees being swapped:
    /// equal ones cannot. The max-fee case pairs the 10% LP cap with the 4000-pip protocol cap,
    /// which `calculateSwapFee` lets add up past 10%.
    #[rstest]
    #[case::selling_x_charges_zero_for_one(
        true, 100, 100, 400, 2_497_504_491, -2,
        [(-2, (1_498_549_858, 502_495_509)), (-1, (2_001_100_121, 0)), (0, (2_000_100_021, 0))]
    )]
    #[case::selling_y_charges_one_for_zero(
        false, 100, 100, 400, 2_496_755_989, 2,
        [(0, (0, 2_000_100_051)), (1, (0, 2_001_100_151)), (2, (503_244_011, 1_497_799_798))]
    )]
    #[case::max_lp_and_protocol_fee(
        true, 100_000, 4_000, 4_000, 2_239_520_717, -2,
        [(-2, (1_266_666_666, 760_479_283)), (-1, (2_112_222_224, 0)), (0, (2_111_111_112, 0))]
    )]
    fn test_swap_withholds_directional_protocol_fee_across_bins(
        #[case] swap_for_y: bool,
        #[case] lp_fee: u32,
        #[case] zero_for_one: u16,
        #[case] one_for_zero: u16,
        #[case] expected_out: u128,
        #[case] final_offset: i32,
        // Bins the swap touched, as `(offset from REAL_ID_SHIFT, reserves)`.
        #[case] touched: [(i32, (u128, u128)); 3],
    ) {
        let mut state = five_bins();
        state.lp_fee = lp_fee;
        state.protocol_fee_zero_for_one = zero_for_one;
        state.protocol_fee_one_for_zero = one_for_zero;

        let result = state
            .swap(swap_for_y, 2_500_000_000)
            .unwrap();

        let mut expected = state;
        expected.active_id = REAL_ID_SHIFT
            .checked_add_signed(final_offset)
            .unwrap();
        for (offset, reserves) in touched {
            expected.bins.insert(
                REAL_ID_SHIFT
                    .checked_add_signed(offset)
                    .unwrap(),
                reserves,
            );
        }
        assert_eq!(result.amount_out, expected_out);
        assert_eq!(result.new_state, expected, "the two untouched bins must be as they were");
    }

    /// Input of exactly the bin's `max_amount_in` (1_000_100_011, from the pinned
    /// `getAmountsOut`) drains it and stops there: nothing is left, so the walk does not step on.
    /// One less takes the partial-fill branch and leaves a unit of y behind.
    #[rstest]
    #[case::exactly_max_drains(1_000_100_011, 1_000_000_000, (1_000_100_011, 0))]
    #[case::one_below_max_is_partial(1_000_100_010, 999_999_999, (1_000_100_010, 1))]
    fn test_swap_at_the_bin_capacity(
        #[case] amount_in: u128,
        #[case] expected_out: u128,
        #[case] active_reserves: (u128, u128),
    ) {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 1_000_000_000)), (REAL_ID_SHIFT, (0, 1_000_000_000))],
        );

        let result = state.swap(true, amount_in).unwrap();

        assert_eq!(result.amount_out, expected_out);
        assert_eq!(result.bins_crossed, 0);
        assert_eq!(result.new_state.active_id, REAL_ID_SHIFT);
        assert_eq!(result.new_state.bins[&REAL_ID_SHIFT], active_reserves);
        assert_eq!(result.new_state.bins[&(REAL_ID_SHIFT - 1)], (0, 1_000_000_000));
    }

    /// A bin at `MAX_LIQUIDITY_PER_BIN` (y = cap >> 128 at price 1) grows by the fee a swap
    /// leaves in it, which reverts `BinPool__MaxLiquidityPerBinExceeded` on chain.
    #[test]
    fn test_swap_errors_past_max_liquidity_per_bin() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT, (0, 191_757_638_537_527_648_490_752_896_198_554))],
        );

        let err = state
            .swap(true, 1_000_000_000_000)
            .expect_err("a swap past the per-bin cap must not quote");
        assert!(
            err.to_string()
                .contains("MAX_LIQUIDITY_PER_BIN"),
            "{err}"
        );
    }

    /// The protocol's cut leaves a drained bin, not the input it took, so limits computed from
    /// each bin's `max_amount_in` still quote exactly with unequal protocol fees.
    #[rstest]
    #[case::selling_x(true)]
    #[case::selling_y(false)]
    fn test_limits_quote_exactly_with_protocol_fees(#[case] selling_x: bool) {
        let mut state = five_bins();
        state.protocol_fee_zero_for_one = 100;
        state.protocol_fee_one_for_zero = 400;
        let (token_in, token_out) = if selling_x { (usdc(), usdt()) } else { (usdt(), usdc()) };

        let (max_in, max_out) = state
            .get_limits(token_in.address.clone(), token_out.address.clone())
            .unwrap();

        assert_eq!(
            state
                .get_amount_out(max_in.clone(), &token_in, &token_out)
                .unwrap()
                .amount,
            max_out
        );
        let over = state
            .get_amount_out(max_in + BigUint::from(1u64), &token_in, &token_out)
            .expect_err("one unit over the limit still quoted");
        assert!(
            over.to_string()
                .contains("Out of liquidity"),
            "{over}"
        );
    }

    /// A bin with nothing on the output side costs a crossing, not an error.
    #[test]
    fn test_swap_skips_bin_without_output_reserve() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (5_000, 0))],
        );

        let result = state.swap(true, 999).unwrap();

        assert_eq!(result.amount_out, 997);
        assert_eq!(result.bins_crossed, 1);
        assert_eq!(
            result.new_state,
            bin_state(
                REAL_ID_SHIFT - 1,
                &[(REAL_ID_SHIFT - 1, (999, 4_003)), (REAL_ID_SHIFT, (5_000, 0))]
            )
        );
    }

    /// Both on-chain reverts: `BinPool__OutOfLiquidity` when the bins run out before the input
    /// does, and `BinPool__InsufficientAmountUnSpecified` when the output rounds to zero. Neither
    /// may come back as a partial fill or a zero quote, which would not execute.
    #[rstest]
    #[case::bins_run_out(REAL_ID_SHIFT, (0, 1_000), 5_000, "Out of liquidity")]
    #[case::output_rounds_to_zero(REAL_ID_SHIFT - 5_000, (0, 1_000_000_000), 1, "produced no output")]
    fn test_swap_errors(
        #[case] active_id: u32,
        #[case] reserves: (u128, u128),
        #[case] amount_in: u128,
        #[case] message: &str,
    ) {
        let state = bin_state(active_id, &[(active_id, reserves)]);

        let err = state
            .swap(true, amount_in)
            .expect_err("quoted where the pool would revert");
        assert!(err.to_string().contains(message), "{err}");
    }

    /// Crediting the input to a bin can overflow its `u128` reserve, and summing the output over
    /// bins can overflow `u128` before the `int128` check sees it. Each is an error, not a wrap.
    #[test]
    fn test_swap_errors_on_reserve_and_output_overflow() {
        let state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (u128::MAX - 10, 1_000))]);
        let err = state
            .swap(true, 500)
            .expect_err("the credited x overflows the bin");
        assert!(
            err.to_string()
                .contains("reserve overflows crediting"),
            "{err}"
        );

        // Priced near 2^-60 y per x, two bins pay out more x together than `u128` holds for an
        // input that fits comfortably.
        let id = REAL_ID_SHIFT - 41_616;
        let state = bin_state(id, &[(id, (u128::MAX - 1, 0)), (id + 1, (10, 0))]);
        let err = state
            .swap(false, 1 << 70)
            .expect_err("the summed output overflows u128");
        assert!(
            err.to_string()
                .contains("output overflows u128"),
            "{err}"
        );
    }

    /// Selling x walks down, selling y walks up, the spent bin is excluded either way, and running
    /// out in the swap direction is what makes `swap` error rather than partially fill.
    #[rstest]
    #[case::sells_x_walks_down(&[REAL_ID_SHIFT - 5, REAL_ID_SHIFT, REAL_ID_SHIFT + 7], true, Some(REAL_ID_SHIFT - 5))]
    #[case::sells_y_walks_up(&[REAL_ID_SHIFT - 5, REAL_ID_SHIFT, REAL_ID_SHIFT + 7], false, Some(REAL_ID_SHIFT + 7))]
    #[case::nothing_below(&[REAL_ID_SHIFT], true, None)]
    #[case::nothing_above(&[REAL_ID_SHIFT], false, None)]
    fn test_next_bin_with_liquidity(
        #[case] ids: &[u32],
        #[case] swap_for_y: bool,
        #[case] expected: Option<u32>,
    ) {
        let bins: Vec<_> = ids
            .iter()
            .map(|id| (*id, (1_000, 1_000)))
            .collect();
        let state = bin_state(REAL_ID_SHIFT, &bins);

        assert_eq!(state.next_bin_with_liquidity(swap_for_y, REAL_ID_SHIFT), expected);
    }

    fn token(address: &str, symbol: &str) -> Token {
        Token::new(&Bytes::from_str(address).unwrap(), symbol, 6, 0, &[], Chain::Base, 100)
    }

    /// Base USDC, the lower address of the pair, so currency0 and the x side.
    fn usdc() -> Token {
        token("0x833589fcd6edb6e08f4c7c32d4f71b54bda02913", "USDC")
    }

    /// Base USDT, currency1 and the y side.
    fn usdt() -> Token {
        token("0xfde4c96c8593536e31f229ea8f37b2ada2699bb2", "USDT")
    }

    /// An 18-decimal token sorting above both, so it is always the y side.
    fn wei() -> Token {
        Token::new(
            &Bytes::from_str("0xffffffffffffffffffffffffffffffffffffffff").unwrap(),
            "WEI",
            18,
            0,
            &[],
            Chain::Base,
            100,
        )
    }

    /// Reserves sit on the x side and the tokens are passed y -> x, so a direction taken from
    /// anything but address order errors instead of quoting. No crossing, so gas is the base.
    #[test]
    fn test_get_amount_out_follows_address_order() {
        let state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (1_000_000_000, 0))]);

        let result = state
            .get_amount_out(BigUint::from(1_000_000u64), &usdt(), &usdc())
            .unwrap();

        assert_eq!(result.amount, BigUint::from(999_900u64));
        assert_eq!(result.gas, BigUint::from(205_000u64));
    }

    /// On chain the amount is an `int128`, so anything above that is rejected, not truncated.
    #[test]
    fn test_get_amount_out_rejects_amount_above_int128() {
        let state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000_000_000))]);
        let too_large = BigUint::from(i128::MAX as u128 + 1);

        let err = state
            .get_amount_out(too_large, &usdc(), &usdt())
            .expect_err("amounts above int128 must be rejected, not truncated");
        assert!(
            err.to_string()
                .contains("amount_in exceeds int128"),
            "{err}"
        );
    }

    /// A bin priced near 2^-60 y per x holding almost `u128::MAX` of x pays out more x than
    /// `int128` holds for a modest input. The pool reverts on `safeInt128`, so the quote errors,
    /// and `get_limits` stops short of that bin.
    #[test]
    fn test_output_above_int128_is_rejected() {
        let id = REAL_ID_SHIFT - 41_616;
        let state = bin_state(id, &[(id, (u128::MAX - 1, 0))]);
        let target_out = i128::MAX as u128 + (u128::MAX - i128::MAX as u128) / 2;
        let price = get_price_from_id(id, state.bin_step).unwrap();
        let amount_in = (U256::from(target_out) * price) >> 128;

        let err = state
            .get_amount_out(BigUint::from(u128::try_from(amount_in).unwrap()), &usdt(), &usdc())
            .unwrap_err();
        assert!(
            err.to_string()
                .contains("exceeds int128"),
            "{err}"
        );

        // The only bin pays out past `int128`, so nothing is quotable.
        let (max_in, max_out) = state
            .get_limits(usdt().address, usdc().address)
            .unwrap();
        assert_eq!((max_in, max_out), (BigUint::ZERO, BigUint::ZERO));
    }

    /// Drains the active bin and finishes in the next, so gas carries one crossing.
    #[test]
    fn test_get_amount_out_charges_gas_per_crossing() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (0, 1_000))],
        );

        let result = state
            .get_amount_out(BigUint::from(2_000u64), &usdc(), &usdt())
            .unwrap();

        assert_eq!(result.amount, BigUint::from(1_997u64));
        assert_eq!(result.gas, BigUint::from(217_100u64), "base plus one crossing");
    }

    /// Priced at bin `2^23 + 100` rather than 1.0, so an inverted ratio is visible, and against an
    /// 18-decimal token, since two 6-decimal tokens hide a dropped decimals correction. Expected
    /// values come from the pinned `pow` with the 100-pip markup applied.
    #[rstest]
    #[case::base_is_x(usdc(), usdt(), 1.105226220342802)]
    #[case::base_is_y(usdt(), usdc(), 0.9049731282105973)]
    #[case::quote_has_18_decimals(usdc(), wei(), 1.105226220342802e-12)]
    fn test_spot_price(#[case] base: Token, #[case] quote: Token, #[case] expected: f64) {
        let state = bin_state(REAL_ID_SHIFT + 100, &[(REAL_ID_SHIFT + 100, (1_000, 1_000))]);

        assert_relative_eq!(
            state.spot_price(&base, &quote).unwrap(),
            expected,
            max_relative = 1e-12
        );
    }

    /// The active bin holds nothing to buy, as after a burn, so the price is the bin the buy
    /// fills first: liquidity sits at `2^23 + 100`, so the expectations match `test_spot_price`.
    /// The bin in between holds only the side being sold and is skipped, as `swap` skips it.
    #[rstest]
    #[case::buying_x_walks_up(
        usdc(), usdt(), REAL_ID_SHIFT + 90,
        &[(REAL_ID_SHIFT + 95, (0, 1_000)), (REAL_ID_SHIFT + 100, (1_000, 0))],
        1.105226220342802
    )]
    #[case::buying_y_walks_down(
        usdt(), usdc(), REAL_ID_SHIFT + 110,
        &[(REAL_ID_SHIFT + 100, (0, 1_000)), (REAL_ID_SHIFT + 105, (1_000, 0))],
        0.9049731282105973
    )]
    fn test_spot_price_skips_bins_without_base(
        #[case] base: Token,
        #[case] quote: Token,
        #[case] active_id: u32,
        #[case] bins: &[(u32, (u128, u128))],
        #[case] expected: f64,
    ) {
        let state = bin_state(active_id, bins);

        assert_relative_eq!(
            state.spot_price(&base, &quote).unwrap(),
            expected,
            max_relative = 1e-12
        );
    }

    /// Buying x sells y, so the `one2zero` protocol fee applies: swap fee 1100 pips over the
    /// 100-pip LP fee (`calculateSwapFee`), applied to the `base_is_x` price without its markup.
    #[test]
    fn test_spot_price_charges_the_fee_of_selling_quote() {
        let mut state = bin_state(REAL_ID_SHIFT + 100, &[(REAL_ID_SHIFT + 100, (1_000, 1_000))]);
        state.protocol_fee_one_for_zero = 1_000;

        assert_relative_eq!(
            state
                .spot_price(&usdc(), &usdt())
                .unwrap(),
            1.105226220342802 * (1.0 - 0.0001) / (1.0 - 0.0011),
            max_relative = 1e-12
        );
    }

    /// No bin holds the token being bought: the active bin's price, as an empty Uniswap v3 pool
    /// still has one, while the quote path reports that nothing can fill. Active bin `2^23 + 100`,
    /// so the expectation matches `test_spot_price`'s `base_is_y` case.
    #[test]
    fn test_spot_price_falls_back_to_the_active_bin_without_base_liquidity() {
        let state = bin_state(REAL_ID_SHIFT + 100, &[(REAL_ID_SHIFT + 100, (1_000, 0))]);

        assert_relative_eq!(
            state
                .spot_price(&usdt(), &usdc())
                .unwrap(),
            0.9049731282105973,
            max_relative = 1e-12
        );
        let refused = state
            .get_amount_out(BigUint::from(1_000u64), &usdc(), &usdt())
            .expect_err("nothing to buy, so the quote must still refuse");
        assert!(
            refused
                .to_string()
                .contains("Out of liquidity"),
            "{refused}"
        );
    }

    #[test]
    fn test_fee_charges_protocol_then_lp() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (1_000, 1_000))]);
        state.lp_fee = 3_000;
        state.protocol_fee_zero_for_one = 1_000;
        state.protocol_fee_one_for_zero = 1_000;

        assert_relative_eq!(state.fee(), 0.003997);
    }

    #[test]
    #[should_panic(expected = "directional protocol fee")]
    fn test_fee_rejects_asymmetric_protocol_fee() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (1_000, 1_000))]);
        state.protocol_fee_zero_for_one = 1_000;

        state.fee();
    }

    #[test]
    fn test_limits_bound_what_can_be_quoted() {
        let state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (0, 1_000))],
        );

        let (max_in, max_out) = state
            .get_limits(usdc().address, usdt().address)
            .unwrap();

        // Everything on the output side is reachable, and the limit itself still quotes.
        assert_eq!(max_out, BigUint::from(6_000u64));
        assert_eq!(
            state
                .get_amount_out(max_in.clone(), &usdc(), &usdt())
                .unwrap()
                .amount,
            max_out
        );
        // One wei past it runs out of bins.
        let over = state
            .get_amount_out(max_in + BigUint::from(1u64), &usdc(), &usdt())
            .expect_err("one wei over the limit still quoted");
        assert!(
            over.to_string()
                .contains("Out of liquidity"),
            "{over}"
        );
    }

    /// Crediting a drained bin can push it over `MAX_LIQUIDITY_PER_BIN`, where `swap` errors, so
    /// the limit stops before such a bin: the limit quotes, one unit more does not.
    #[rstest]
    #[case::capped_bin_is_second(REAL_ID_SHIFT - 1, 1_000)]
    #[case::capped_bin_is_active(REAL_ID_SHIFT, 0)]
    fn test_limits_stop_before_a_bin_the_cap_blocks(
        #[case] capped_bin: u32,
        #[case] expected_out: u128,
    ) {
        let mut bins = vec![(REAL_ID_SHIFT, (0, 1_000)), (REAL_ID_SHIFT - 1, (0, 5_000))];
        bins.retain(|(id, _)| *id != capped_bin);
        bins.push((capped_bin, (0, 191_757_638_537_527_648_490_752_896_198_554)));
        let state = bin_state(REAL_ID_SHIFT, &bins);

        let (max_in, max_out) = state
            .get_limits(usdc().address, usdt().address)
            .unwrap();

        assert_eq!(max_out, BigUint::from(expected_out));
        if expected_out > 0 {
            assert_eq!(
                state
                    .get_amount_out(max_in.clone(), &usdc(), &usdt())
                    .unwrap()
                    .amount,
                max_out
            );
        }
        let over = state
            .get_amount_out(max_in + BigUint::from(1u64), &usdc(), &usdt())
            .expect_err("one unit over the limit enters the capped bin");
        assert!(
            over.to_string()
                .contains("MAX_LIQUIDITY_PER_BIN"),
            "{over}"
        );
    }

    /// A bin far from the active one prices its reserve past `u128`, where `get_drain_amounts`
    /// is `None` and the pool reverts on `safe128`. The limit stops before it: the limit quotes,
    /// one unit more does not.
    #[test]
    fn test_limits_stop_before_a_bin_whose_drain_input_overflows() {
        let mut state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT, (1_000, 0)), (REAL_ID_SHIFT + 7_000, (10u128.pow(18), 0))],
        );
        state.bin_step = 100;

        let (max_in, max_out) = state
            .get_limits(usdt().address, usdc().address)
            .unwrap();

        assert_eq!(max_out, BigUint::from(1_000u64));
        assert_eq!(
            state
                .get_amount_out(max_in.clone(), &usdt(), &usdc())
                .unwrap()
                .amount,
            max_out
        );
        let over = state
            .get_amount_out(max_in + BigUint::from(1u64), &usdt(), &usdc())
            .expect_err("one unit over the limit enters the far bin");
        assert!(
            over.to_string()
                .contains("exceeds u128"),
            "{over}"
        );
    }

    #[test]
    fn test_limits_are_zero_when_direction_is_dry() {
        let state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);

        assert_eq!(
            state
                .get_limits(usdt().address, usdc().address)
                .unwrap(),
            (BigUint::ZERO, BigUint::ZERO)
        );
    }

    fn delta(updated: &[(&str, Bytes)], deleted: &[&str]) -> ProtocolStateDelta {
        ProtocolStateDelta {
            component_id: "test_pool".to_string(),
            updated_attributes: updated
                .iter()
                .map(|(key, value)| (key.to_string(), value.clone()))
                .collect(),
            deleted_attributes: deleted
                .iter()
                .map(|key| key.to_string())
                .collect(),
        }
    }

    /// Applies a delta to a state, panicking on a decode error.
    fn apply(state: &mut PancakeswapInfinityBinState, delta: ProtocolStateDelta) {
        state
            .delta_transition(delta, &HashMap::new(), &Balances::default())
            .unwrap();
    }

    #[rstest]
    #[case::leaves_the_tree(vec![0x00], false)]
    #[case::back_in_the_tree(vec![0x01], true)]
    fn test_delta_moves_active_bin_in_and_out_of_the_tree(
        #[case] flag: Vec<u8>,
        #[case] expected: bool,
    ) {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);
        state.active_in_tree = !expected;

        apply(&mut state, delta(&[("active_in_tree", Bytes::from(flag))], &[]));

        assert_eq!(state.active_in_tree, expected);
    }

    /// A bin's reserve pair replaces that bin's reserves and leaves every other bin alone; one
    /// half on its own leaves the other half as it was.
    #[test]
    fn test_delta_updates_bin_reserves() {
        let mut state = bin_state(
            REAL_ID_SHIFT,
            &[(REAL_ID_SHIFT - 1, (0, 5_000)), (REAL_ID_SHIFT, (0, 1_000))],
        );
        let pair = reserve_attributes(REAL_ID_SHIFT, 7, 11);
        let pair: Vec<_> = pair
            .iter()
            .map(|(key, value)| (key.as_str(), value.clone()))
            .collect();

        apply(&mut state, delta(&pair, &[]));
        assert_eq!(state.bins[&REAL_ID_SHIFT], (7, 11));
        assert_eq!(state.bins[&(REAL_ID_SHIFT - 1)], (0, 5_000));

        let x_only = format!("bins/{}/reserve_x", REAL_ID_SHIFT - 1);
        apply(&mut state, delta(&[(&x_only, Bytes::from(vec![3]))], &[]));
        assert_eq!(state.bins[&(REAL_ID_SHIFT - 1)], (3, 5_000));
    }

    /// A bin can be emptied two ways: zero values, or its keys in `deleted_attributes`. The
    /// substreams classifies a zeroed bin as a deletion while still carrying the zero values, so
    /// both arrive in practice, and a bin left in the map costs the next swap a crossing over
    /// nothing.
    #[rstest]
    #[case::zero_values(reserve_attributes(REAL_ID_SHIFT, 0, 0).to_vec(), vec![])]
    #[case::one_half_zeroed_to_empty(
        vec![(format!("bins/{REAL_ID_SHIFT}/reserve_y"), Bytes::from(vec![0]))],
        vec![]
    )]
    #[case::deleted_attributes(vec![], vec![format!("bins/{REAL_ID_SHIFT}/reserve_x"), format!("bins/{REAL_ID_SHIFT}/reserve_y")])]
    #[case::one_half_deleted(vec![], vec![format!("bins/{REAL_ID_SHIFT}/reserve_y")])]
    fn test_delta_removes_emptied_bin(
        #[case] updated: Vec<(String, Bytes)>,
        #[case] deleted: Vec<String>,
    ) {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);
        let updated: Vec<_> = updated
            .iter()
            .map(|(key, value)| (key.as_str(), value.clone()))
            .collect();
        let deleted: Vec<_> = deleted
            .iter()
            .map(String::as_str)
            .collect();

        apply(&mut state, delta(&updated, &deleted));

        assert!(!state.bins.contains_key(&REAL_ID_SHIFT), "emptied bin stayed in the map");
    }

    /// `active_id` is emitted signed big-endian, so ids at or above `0x800000` arrive as 4 bytes
    /// with a leading `0x00`. The indexed pool sits above that boundary, and a fixed-width decoder
    /// passes the 3-byte case while failing this one.
    #[test]
    fn test_delta_moves_active_id() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);

        apply(&mut state, delta(&[("active_id", Bytes::from(vec![0x00, 0x80, 0x00, 0x05]))], &[]));

        assert_eq!(state.active_id, REAL_ID_SHIFT + 5);
    }

    /// Protocol fees move on `ProtocolFeeUpdated`. The LP fee is the PoolKey's and never moves,
    /// so a `fee` attribute is ignored like any unknown key. Values are big-endian and minimal
    /// length, so 1000 pips is `[0x03, 0xe8]`.
    #[test]
    fn test_delta_updates_protocol_fees_only() {
        let mut state = bin_state(REAL_ID_SHIFT, &[(REAL_ID_SHIFT, (0, 1_000))]);

        apply(
            &mut state,
            delta(
                &[
                    ("fee", Bytes::from(vec![0x01, 0xf4])),
                    ("protocol_fees/zero2one", Bytes::from(vec![0x03, 0xe8])),
                    ("protocol_fees/one2zero", Bytes::from(vec![0x07, 0xd0])),
                    ("balance_owner", Bytes::from(vec![0x01])),
                ],
                &[],
            ),
        );

        assert_eq!(state.lp_fee, 100, "the LP fee is static");
        assert_eq!(state.protocol_fee_zero_for_one, 1000);
        assert_eq!(state.protocol_fee_one_for_zero, 2000);
    }

    sol! {
        /// [`IQuoter.sol`](https://github.com/pancakeswap/infinity-periphery/blob/56a7a4f5076d57d5ed90c38a00421ecbac0d649c/src/interfaces/IQuoter.sol),
        /// the Bin lens.
        struct PoolKey {
            address currency0;
            address currency1;
            address hooks;
            address poolManager;
            uint24 fee;
            bytes32 parameters;
        }

        struct QuoteExactSingleParams {
            PoolKey poolKey;
            bool zeroForOne;
            uint128 exactAmount;
            bytes hookData;
        }

        function quoteExactInputSingle(QuoteExactSingleParams memory params)
            external
            returns (uint256 amountOut, uint256 gasEstimate);
    }

    /// [`BinQuoter.sol`](https://github.com/pancakeswap/infinity-periphery/blob/56a7a4f5076d57d5ed90c38a00421ecbac0d649c/src/pool-bin/lens/BinQuoter.sol)
    /// on Base.
    const BIN_QUOTER: &str = "0xc631f4b0fc2dd68ad45f74b2942628db117dd359";
    /// Block the snapshot fixture was captured at. The quote has to be read at the same height.
    const SNAPSHOT_BLOCK: u64 = 32_980_292;

    fn bin_snapshot() -> ComponentWithState {
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("tests/assets/decoder/pancakeswap_infinity_bin_snapshot.json");
        let json = std::fs::read_to_string(path).expect("snapshot fixture");

        // Only the wire type is `Deserialize`; the decoder takes the converted one.
        serde_json::from_str::<dto::ComponentWithState>(&json)
            .expect("snapshot deserializes")
            .into()
    }

    /// The fixture is a capture of what the package emitted for a live pool, so decoding it pins
    /// the decoder to the package's attribute schema. Every other test builds attributes with
    /// this crate's own helpers, where the two can drift apart unnoticed.
    #[tokio::test]
    async fn test_snapshot_fixture_decodes() {
        let state =
            try_decode_snapshot_with_defaults::<PancakeswapInfinityBinState>(bin_snapshot())
                .await
                .expect("snapshot decodes");

        assert_eq!(state.active_id, REAL_ID_SHIFT);
        assert!(state.active_in_tree);
        assert_eq!(state.bin_step, 10);
        assert_eq!(state.lp_fee, 7);
        assert_eq!((state.protocol_fee_zero_for_one, state.protocol_fee_one_for_zero), (3, 3));
        // 29 bins each side of the active one, 4.237288 USDC or USDT each, and the active bin
        // half and half.
        assert_eq!(state.bins.len(), 59);
        assert_eq!(state.bins[&(REAL_ID_SHIFT - 29)], (0, 4_237_288));
        assert_eq!(state.bins[&REAL_ID_SHIFT], (2_118_644, 2_118_644));
        assert_eq!(state.bins[&(REAL_ID_SHIFT + 29)], (4_237_288, 0));
    }

    /// The pool key the quoter needs, taken from the snapshot's static attributes rather than
    /// rebuilt, so the test cannot disagree with the indexer about which pool it is asking about.
    fn pool_key(snapshot: &ComponentWithState) -> PoolKey {
        let statics = &snapshot.component.static_attributes;
        let address = |value: &Bytes| Address::from_slice(value.as_ref());

        PoolKey {
            currency0: address(&snapshot.component.tokens[0]),
            currency1: address(&snapshot.component.tokens[1]),
            hooks: Address::ZERO,
            poolManager: address(&statics["pool_manager"]),
            fee: U24::from(
                decode_u32_attribute("key_lp_fee", &statics["key_lp_fee"])
                    .expect("key_lp_fee is a u24"),
            ),
            parameters: FixedBytes::from_slice(statics["parameters"].as_ref()),
        }
    }

    /// The port against the chain, end to end: the state decoded from a real snapshot must quote
    /// what `BinQuoter` quotes for the same input, to the wei.
    ///
    /// Everything else in this module checks the port against the pinned Solidity libraries in
    /// isolation. This is the only check that catches a divergence in how they are composed, or a
    /// snapshot that decodes into the wrong shape.
    #[tokio::test]
    #[ignore = "Requires BASE_RPC_URL pointing at a Base archive node"]
    async fn test_quotes_match_bin_quoter() {
        // Not RPC_URL: by repo convention that is Ethereum, where the quoter has no code.
        let rpc_url = std::env::var("BASE_RPC_URL").expect("BASE_RPC_URL must point at Base");
        let provider = ProviderBuilder::new()
            .connect(&rpc_url)
            .await
            .expect("provider");

        let snapshot = bin_snapshot();
        let key = pool_key(&snapshot);
        let token_x =
            Token::new(&snapshot.component.tokens[0].clone(), "X", 6, 0, &[], Chain::Base, 100);
        let token_y =
            Token::new(&snapshot.component.tokens[1].clone(), "Y", 6, 0, &[], Chain::Base, 100);
        let state = try_decode_snapshot_with_defaults::<PancakeswapInfinityBinState>(snapshot)
            .await
            .expect("snapshot decodes");

        // A revert is `None`, so "no quote" compares on both sides. Only a revert counts: a
        // transport failure or any other error response would otherwise pass every expected
        // revert.
        let quoter = |zero_for_one: bool, amount_in: u128| {
            let params = QuoteExactSingleParams {
                poolKey: key.clone(),
                zeroForOne: zero_for_one,
                exactAmount: amount_in,
                hookData: AlloyBytes::new(),
            };
            let request = TransactionRequest::default()
                .to(Address::from_str(BIN_QUOTER).unwrap())
                .input(
                    quoteExactInputSingleCall { params }
                        .abi_encode()
                        .into(),
                );
            let provider = provider.clone();
            async move {
                let returned = match provider
                    .call(request)
                    .block(SNAPSHOT_BLOCK.into())
                    .await
                {
                    Ok(returned) => returned,
                    Err(err)
                        if err
                            .as_error_resp()
                            .is_some_and(|resp| resp.as_revert_data().is_some()) =>
                    {
                        return None
                    }
                    Err(err) => panic!("quoter call failed: {err}"),
                };
                let quoted = quoteExactInputSingleCall::abi_decode_returns(&returned)
                    .expect("quoter response")
                    .amountOut;
                Some(BigUint::from_bytes_be(&quoted.to_be_bytes::<32>()))
            }
        };

        for (zero_for_one, token_in, token_out) in
            [(true, &token_x, &token_y), (false, &token_y, &token_x)]
        {
            let limit = state
                .get_limits(token_in.address.clone(), token_out.address.clone())
                .expect("limits")
                .0
                .to_u128()
                .expect("limit fits u128");

            // Dust (zero output, so both sides must refuse it), inside the active bin, a few
            // crossings, about 20 crossings (the fixture's bins hold ~4.24 USDC), and the limit.
            let mut crossed = false;
            for amount_in in [1, 1_000, 10_000_000, 100_000_000, limit] {
                let ours = state
                    .get_amount_out(BigUint::from(amount_in), token_in, token_out)
                    .ok();
                crossed |= ours
                    .as_ref()
                    .is_some_and(|ours| ours.gas > BigUint::from(SWAP_BASE_GAS));

                assert_eq!(
                    ours.map(|ours| ours.amount),
                    quoter(zero_for_one, amount_in).await,
                    "zero_for_one={zero_for_one} amount={amount_in}: port and BinQuoter disagree"
                );
            }
            assert!(crossed, "zero_for_one={zero_for_one}: no amount crossed a bin");

            // One past the limit runs out of bins: the quoter reverts, the port must error.
            let over = state
                .get_amount_out(BigUint::from(limit + 1), token_in, token_out)
                .expect_err("zero_for_one={zero_for_one}: port quoted past its own limit");
            assert!(
                over.to_string()
                    .contains("Out of liquidity"),
                "{over}"
            );
            assert_eq!(
                quoter(zero_for_one, limit + 1).await,
                None,
                "zero_for_one={zero_for_one}: BinQuoter filled past the port's limit"
            );
        }
    }
}