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
use std::sync::Arc;

use alloy_primitives::{Address, FixedBytes};
use brontes_database::libmdbx::LibmdbxReader;
use brontes_metrics::inspectors::OutlierMetrics;
use brontes_types::{
    db::{
        dex::{BlockPrice, PriceAt},
        metadata::Metadata,
        token_info::TokenInfoWithAddress,
    },
    mev::{
        AddressBalanceDeltas, Bundle, BundleHeader, Mev, MevType, TokenBalanceDelta,
        TransactionAccounting,
    },
    normalized_actions::{
        Action, NormalizedAggregator, NormalizedBatch, NormalizedFlashLoan, NormalizedSwap,
        NormalizedTransfer,
    },
    pair::Pair,
    utils::ToFloatNearest,
    ActionIter, FastHashMap, FastHashSet, GasDetails, TxInfo,
};
use itertools::Itertools;
use malachite::{
    num::{
        arithmetic::traits::Reciprocal,
        basic::traits::{One, Zero},
    },
    Rational,
};
use reth_primitives::TxHash;

const CONNECTION_TH: usize = 2;
const LOW_LIQ_TH: Rational = Rational::const_from_unsigned(50_000u64);

#[derive(Debug)]
pub struct SharedInspectorUtils<'db, DB: LibmdbxReader> {
    pub(crate) quote: Address,
    pub(crate) db:    &'db DB,
    pub metrics:      Option<OutlierMetrics>,
}

impl<'db, DB: LibmdbxReader> SharedInspectorUtils<'db, DB> {
    pub fn new(quote_address: Address, db: &'db DB, metrics: Option<OutlierMetrics>) -> Self {
        SharedInspectorUtils { quote: quote_address, db, metrics }
    }
}
type TokenDeltas = FastHashMap<Address, Rational>;
type AddressDeltas = FastHashMap<Address, TokenDeltas>;
type PossibleSwapDetails = Vec<(TokenInfoWithAddress, bool, Rational, Address, u64)>;

impl<DB: LibmdbxReader> SharedInspectorUtils<'_, DB> {
    pub fn get_metrics(&self) -> Option<&OutlierMetrics> {
        self.metrics.as_ref()
    }

    /// Calculates the USD value of the token balance deltas by address
    pub fn usd_delta_by_address(
        &self,
        tx_position: u64,
        at: PriceAt,
        deltas: &AddressDeltas,
        metadata: Arc<Metadata>,
        cex: bool,
        at_or_before: bool,
        filter_fn: impl Fn(&Address, Option<Rational>) -> Option<Rational>,
    ) -> Option<FastHashMap<Address, Rational>> {
        let mut usd_deltas = FastHashMap::default();

        for (address, token_deltas) in deltas {
            for (token_addr, amount) in token_deltas {
                if amount == &Rational::ZERO {
                    continue
                }

                let pair = Pair(*token_addr, self.quote);
                let price = if cex {
                    metadata
                        .cex_quotes
                        .get_quote_from_most_liquid_exchange(
                            &pair,
                            metadata.microseconds_block_timestamp(),
                            Some(1_000_000),
                        )?
                        .price_maker
                        .1
                } else if at_or_before {
                    filter_fn(
                        address,
                        metadata
                            .dex_quotes
                            .as_ref()?
                            .price_at_or_before(pair, tx_position as usize)
                            .map(|price| price.get_price(at))
                            .clone(),
                    )?
                } else {
                    filter_fn(
                        address,
                        metadata
                            .dex_quotes
                            .as_ref()?
                            .price_at(pair, tx_position as usize)
                            .map(|price| price.get_price(at))
                            .clone(),
                    )?
                };

                let usd_amount = amount.clone() * price.clone();

                *usd_deltas.entry(*address).or_insert(Rational::ZERO) += usd_amount;
            }
        }

        Some(usd_deltas)
    }

    // will flatten nested and filter out actions that aren't swap, transfer or
    // eth_transfer
    pub fn flatten_nested_actions_default<'a>(
        &self,
        iter: impl Iterator<Item = Action> + 'a,
    ) -> impl Iterator<Item = Action> + 'a {
        self.flatten_nested_actions(iter, &|action| {
            action.is_swap() || action.is_transfer() || action.is_eth_transfer()
        })
    }

    pub fn flatten_nested_actions<'a, F>(
        &self,
        iter: impl Iterator<Item = Action> + 'a,
        filter_actions: &'a F,
    ) -> impl Iterator<Item = Action> + 'a
    where
        F: for<'b> Fn(&'b Action) -> bool + 'a,
    {
        iter.flatten_specified(Action::try_aggregator_ref, move |actions: NormalizedAggregator| {
            actions
                .child_actions
                .into_iter()
                .filter(&filter_actions)
                .collect::<Vec<_>>()
        })
        .flatten_specified(Action::try_flash_loan_ref, move |action: NormalizedFlashLoan| {
            action
                .fetch_underlying_actions()
                .filter(&filter_actions)
                .collect::<Vec<_>>()
        })
        .flatten_specified(Action::try_batch_ref, move |action: NormalizedBatch| {
            action
                .fetch_underlying_actions()
                .filter(&filter_actions)
                .collect::<Vec<_>>()
        })
    }

    /// tries to convert transfer over to swaps
    pub fn try_create_swaps(
        &self,
        transfers: &[NormalizedTransfer],
        invalid_addresses: FastHashSet<Address>,
    ) -> Vec<NormalizedSwap> {
        let mut pools: FastHashMap<Address, PossibleSwapDetails> = FastHashMap::default();

        for t in transfers {
            // we do this so if the transfer is from a mev contract or a searcher, it gets
            // ignored
            if invalid_addresses.contains(&t.from) {
                continue
            }

            pools.entry(t.to).or_default().push((
                t.token.clone(),
                true,
                t.amount.clone(),
                t.from,
                t.trace_index,
            ));

            pools.entry(t.from).or_default().push((
                t.token.clone(),
                false,
                t.amount.clone(),
                t.to,
                t.trace_index,
            ));
        }

        pools
            .into_iter()
            .filter_map(|(pool, mut possible_swaps)| {
                if possible_swaps.len() != 2 {
                    return None
                }

                let (f_token, f_direction, f_am, f_addr, f_trace) = possible_swaps.pop()?;
                let (s_token, s_direction, s_am, s_addr, s_trace) = possible_swaps.pop()?;

                if s_token == f_token || s_direction == f_direction {
                    return None
                }
                let trace_index = std::cmp::min(f_trace, s_trace);

                let (amount_in, amount_out, token_in, token_out, from, recip) = if f_direction {
                    (f_am, s_am, f_token, s_token, f_addr, s_addr)
                } else {
                    (s_am, f_am, s_token, f_token, s_addr, f_addr)
                };

                Some(NormalizedSwap {
                    pool,
                    amount_in,
                    amount_out,
                    token_out,
                    token_in,
                    from,
                    recipient: recip,
                    trace_index,
                    ..Default::default()
                })
            })
            .collect()
    }

    pub fn get_token_value_dex(
        &self,
        tx_index: usize,
        at: PriceAt,
        token_address: Address,
        amount: &Rational,
        metadata: &Arc<Metadata>,
    ) -> Option<Rational> {
        if token_address == self.quote {
            return Some(amount.clone())
        }
        let price = self.get_token_price_on_dex(tx_index, at, token_address, metadata)?;
        Some(price * amount)
    }

    pub fn get_token_value_dex_block(
        &self,
        block_price: BlockPrice,
        token_address: Address,
        amount: &Rational,
        metadata: &Arc<Metadata>,
    ) -> Option<Rational> {
        if token_address == self.quote {
            return Some(amount.clone())
        }
        let price = self.get_token_price_on_dex_block(block_price, token_address, metadata)?;
        Some(price * amount)
    }

    pub fn get_token_price_on_dex(
        &self,
        tx_index: usize,
        at: PriceAt,
        token_address: Address,
        metadata: &Arc<Metadata>,
    ) -> Option<Rational> {
        if token_address == self.quote {
            return Some(Rational::ONE)
        }

        let pair = Pair(token_address, self.quote);

        Some(
            metadata
                .dex_quotes
                .as_ref()?
                .price_at(pair, tx_index)?
                .get_price(at),
        )
    }

    pub fn get_token_price_on_dex_block(
        &self,
        block: BlockPrice,
        token_address: Address,
        metadata: &Arc<Metadata>,
    ) -> Option<Rational> {
        if token_address == self.quote {
            return Some(Rational::ONE)
        }

        let pair = Pair(token_address, self.quote);

        metadata.dex_quotes.as_ref()?.price_for_block(pair, block)
    }

    pub fn build_bundle_header_searcher_activity(
        &self,
        bundle_deltas: Vec<AddressDeltas>,
        bundle_txes: Vec<TxHash>,
        info: &TxInfo,
        mut profit_usd: f64,
        price_type: BlockPrice,
        gas_details: &[GasDetails],
        metadata: Arc<Metadata>,
        mev_type: MevType,
        no_pricing_calculated: bool,
    ) -> BundleHeader {
        if no_pricing_calculated {
            profit_usd = 0.0;
        }

        let balance_deltas =
            self.get_bundle_accounting(bundle_txes, bundle_deltas, |this, token, amount| {
                this.get_token_value_dex_block(price_type, token, &amount, &metadata)
            });

        let bribe_usd = gas_details
            .iter()
            .map(|details| {
                metadata
                    .get_gas_price_usd(details.gas_paid(), self.quote)
                    .to_float()
            })
            .sum::<f64>();

        let fund = info
            .get_searcher_contract_info()
            .map(|i| i.fund)
            .or_else(|| info.get_searcher_eao_info().map(|f| f.fund))
            .unwrap_or_default();

        BundleHeader {
            block_number: metadata.block_num,
            tx_index: info.tx_index,
            tx_hash: info.tx_hash,
            eoa: info.eoa,
            fund,
            mev_contract: info.mev_contract,
            profit_usd,
            bribe_usd,
            mev_type,
            no_pricing_calculated,
            balance_deltas,
        }
    }

    pub fn build_bundle_header(
        &self,
        bundle_deltas: Vec<AddressDeltas>,
        bundle_txes: Vec<TxHash>,
        info: &TxInfo,
        mut profit_usd: f64,
        gas_details: &[GasDetails],
        metadata: Arc<Metadata>,
        mev_type: MevType,
        no_pricing_calculated: bool,
        price_f: impl Fn(&Self, Address, Rational) -> Option<Rational>,
    ) -> BundleHeader {
        if no_pricing_calculated {
            profit_usd = 0.0;
        }

        let balance_deltas = self.get_bundle_accounting(bundle_txes, bundle_deltas, price_f);

        let bribe_usd = gas_details
            .iter()
            .map(|details| {
                metadata
                    .get_gas_price_usd(details.gas_paid(), self.quote)
                    .to_float()
            })
            .sum::<f64>();

        if profit_usd > bribe_usd * 100.0 {
            self.metrics
                .as_ref()
                .inspect(|m| m.inspector_100x_profit(mev_type));
        }

        let fund = info
            .get_searcher_contract_info()
            .map(|i| i.fund)
            .or_else(|| info.get_searcher_eao_info().map(|f| f.fund))
            .unwrap_or_default();

        BundleHeader {
            block_number: metadata.block_num,
            tx_index: info.tx_index,
            tx_hash: info.tx_hash,
            fund,
            eoa: info.eoa,
            mev_contract: info.mev_contract,
            profit_usd,
            bribe_usd,
            mev_type,
            no_pricing_calculated,
            balance_deltas,
        }
    }

    pub fn get_full_block_price(
        &self,
        price_type: BlockPrice,
        mev_addresses: FastHashSet<Address>,
        deltas: &AddressDeltas,
        metadata: Arc<Metadata>,
    ) -> Option<Rational> {
        let mut usd_deltas = FastHashMap::default();

        for (address, token_deltas) in deltas {
            for (token_addr, amount) in token_deltas {
                let pair = Pair(*token_addr, self.quote);
                let price = metadata
                    .dex_quotes
                    .as_ref()?
                    .price_for_block(pair, price_type)?;

                let usd_amount = amount.clone() * price.clone();

                *usd_deltas.entry(*address).or_insert(Rational::ZERO) += usd_amount;
            }
        }
        let sum = usd_deltas
            .iter()
            .filter_map(|(address, delta)| mev_addresses.contains(address).then_some(delta))
            .fold(Rational::ZERO, |acc, delta| acc + delta);

        Some(sum)
    }

    pub fn get_deltas_usd(
        &self,
        tx_index: u64,
        at: PriceAt,
        mev_addresses: &FastHashSet<Address>,
        deltas: &AddressDeltas,
        metadata: Arc<Metadata>,
        at_or_before: bool,
    ) -> Option<Rational> {
        let addr_usd_deltas = self.usd_delta_by_address(
            tx_index,
            at,
            deltas,
            metadata.clone(),
            false,
            at_or_before,
            |address, price| {
                // if not mev address, we just zero
                (!mev_addresses.contains(address))
                    .then(|| price.clone().unwrap_or_default())
                    // if mev address, return value
                    .or(price)
            },
        )?;

        let sum = addr_usd_deltas
            .iter()
            .filter_map(|(address, delta)| mev_addresses.contains(address).then_some(delta))
            .fold(Rational::ZERO, |acc, delta| acc + delta);

        Some(sum)
    }

    pub fn get_bundle_accounting(
        &self,
        bundle_txes: Vec<FixedBytes<32>>,
        bundle_deltas: Vec<AddressDeltas>,
        price_f: impl Fn(&Self, Address, Rational) -> Option<Rational>,
    ) -> Vec<TransactionAccounting> {
        bundle_txes
            .into_iter()
            .zip(bundle_deltas)
            .map(|(tx_hash, deltas)| {
                let address_deltas: Vec<AddressBalanceDeltas> = deltas
                    .into_iter()
                    .map(|(address, token_deltas)| {
                        let deltas: Vec<TokenBalanceDelta> = token_deltas
                            .into_iter()
                            .map(|(token, amount)| {
                                //TODO: For cex-dex if we merge swap we won't have the intermediary
                                //TODO: price so it will be marked as zero in the deltas
                                let usd_value =
                                    price_f(self, token, amount.clone()).unwrap_or(Rational::ZERO);
                                TokenBalanceDelta {
                                    token:     self
                                        .db
                                        .try_fetch_token_info(token)
                                        .ok()
                                        .unwrap_or_default(),
                                    amount:    amount.to_float(),
                                    usd_value: usd_value.to_float(),
                                }
                            })
                            .collect();

                        let name = self.fetch_address_name(address);

                        AddressBalanceDeltas { address, name, token_deltas: deltas }
                    })
                    .collect();

                TransactionAccounting { tx_hash, address_deltas }
            })
            .collect()
    }

    pub fn fetch_address_name(&self, address: Address) -> Option<String> {
        let protocol_name = self
            .db
            .get_protocol_details(address)
            .ok()
            .map(|protocol| protocol.protocol.to_string());

        protocol_name.or_else(|| {
            self.db
                .try_fetch_searcher_info(address, Some(address))
                .ok()
                .and_then(|(searcher_eoa, searcher_contract)| {
                    searcher_eoa
                        .map(|eoa| eoa.describe())
                        .or_else(|| searcher_contract.map(|contract| contract.describe()))
                })
                .or_else(|| {
                    self.db
                        .try_fetch_builder_info(address)
                        .ok()
                        .and_then(|builder_info| builder_info.map(|info| info.describe()))
                })
                .or_else(|| {
                    self.db
                        .try_fetch_address_metadata(address)
                        .ok()
                        .and_then(|metadata| metadata.map(|info| info.describe()))?
                })
        })
    }

    /// Evaluates the validity of swap prices against DEX quoted prices within a
    /// given metadata context.
    ///
    /// This function iterates over each token involved in the provided swaps
    /// and compares the effective swap rates against the DEX quoted prices
    /// for corresponding token pairs. It computes the difference
    /// between the effective price and the DEX pricing rate. If any swap
    /// exhibits a price difference exceeding `MAX_PRICE_DIFF`, it logs a
    /// warning and captures relevant metrics. The function returns `true`
    /// if all evaluated swaps have price differences within the acceptable
    /// range.
    pub fn valid_pricing<'a>(
        &self,
        metadata: Arc<Metadata>,
        swaps: &[NormalizedSwap],
        tokens: impl Iterator<Item = &'a Address>,
        idx: usize,
        max_price_diff: Rational,
        mev_type: MevType,
    ) -> bool {
        if swaps.is_empty() {
            return true
        }

        let pcts = tokens
            .flat_map(|token| {
                swaps
                    .iter()
                    .filter(move |swap| {
                        &swap.token_in.address == token || &swap.token_out.address == token
                    })
                    .filter_map(|swap| {
                        let effective_price = swap.swap_rate();

                        let am_in_price = metadata
                            .dex_quotes
                            .as_ref()?
                            .price_at(Pair(swap.token_in.address, self.quote), idx)?;

                        let am_out_price = metadata
                            .dex_quotes
                            .as_ref()?
                            .price_at(Pair(swap.token_out.address, self.quote), idx)?;

                        let min_connected = std::cmp::min(
                            am_in_price.first_hop_connections,
                            am_out_price.first_hop_connections,
                        );

                        let min_liquid = std::cmp::min(
                            am_out_price.pool_liquidity.clone(),
                            am_in_price.pool_liquidity.clone(),
                        );

                        // we reciprocal amount out because we won't have pricing for quote <> token
                        // out but we will have flipped
                        let dex_pricing_rate =
                            (am_out_price.get_price(PriceAt::Average).reciprocal()
                                * am_in_price.get_price(PriceAt::Average))
                            .reciprocal();


                        let pct = if effective_price > dex_pricing_rate {
                            if effective_price == Rational::ZERO {
                                return None
                            }
                            (&effective_price - &dex_pricing_rate) / &effective_price
                        } else {
                            if dex_pricing_rate == Rational::ZERO {
                                return None
                            }
                            (&dex_pricing_rate - &effective_price) / &dex_pricing_rate
                        };


                        if pct > max_price_diff
                            && min_connected < CONNECTION_TH
                                && min_liquid < LOW_LIQ_TH {
                            self.get_metrics().inspect(|m| {
                                m.bad_dex_pricing(
                                    mev_type,
                                    Pair(swap.token_in.address, swap.token_out.address),
                                )
                            });

                            let price_delta_pct = pct.clone().to_float() * 100.0;

                            tracing::debug!(
                                mev_type = ?mev_type,
                                effective_price = %format!("{:.6}", effective_price.to_float()),
                                dex_pricing_rate = %format!("{:.6}", dex_pricing_rate.to_float()),
                                swap = %swap,
                                price_delta_pct = %format!("{:.2}%", price_delta_pct),
                                "Price delta of {price_delta_pct:.2}% exceeds threshold for {mev_type:?} MEV"
                            );
                            return Some(pct)
                        }
                        None

                    })
            })
            .collect_vec();

        if pcts.is_empty() {
            return true
        }

        pcts.into_iter()
            .max()
            .filter(|delta| delta.le(&max_price_diff))
            .is_some()
    }

    /// Because of the recursive split nature of the search,
    /// we can sometimes get overlap which leads to double counting and
    /// false positives that are unwanted. to combat this
    /// we check all hashes and will check for duplicates.
    /// when a duplicate arises, we will always take the bundle
    /// with more transactions as it is the most correct
    #[allow(clippy::comparison_chain)]
    pub fn dedup_bundles(bundles: Vec<Bundle>) -> Vec<Bundle> {
        let mut bundles = bundles
            .into_iter()
            .map(|bundle| (bundle.data.mev_transaction_hashes(), bundle))
            .collect_vec();

        let len = bundles.len();
        let mut removals = Vec::new();

        for i in 0..len {
            if removals.contains(&i) {
                continue
            }

            for j in 0..len {
                if i == j || removals.contains(&j) {
                    continue
                }

                let i_hash = &bundles[i].0;
                let j_hash = &bundles[j].0;
                if i_hash.iter().any(|hash| j_hash.contains(hash)) {
                    let bundle_0 = &bundles[i].1;
                    let bundle_1 = &bundles[j].1;
                    tracing::trace!(
                        "comparing headers\n header1={:#?} \n header2={:#?}",
                        bundle_0.header,
                        bundle_1.header
                    );

                    if i_hash.len() > j_hash.len() {
                        removals.push(j);
                    } else if i_hash.len() < j_hash.len() {
                        removals.push(i);
                    } else {
                        // if same, take bundle with lower profit as it is most
                        // likey, more correct
                        if bundles[i].1.header.profit_usd > bundles[j].1.header.profit_usd {
                            removals.push(i);
                        } else {
                            removals.push(j);
                        }
                    }
                }
            }
        }
        removals.sort_unstable_by(|a, b| b.cmp(a));
        removals.dedup();

        removals.into_iter().for_each(|idx| {
            bundles.remove(idx);
        });

        bundles.into_iter().map(|res| res.1).collect_vec()
    }

    pub fn cex_try_convert_transfer_to_swap(
        &self,
        transfers: Vec<NormalizedTransfer>,
        info: &TxInfo,
        mev_type: MevType,
    ) -> Option<NormalizedSwap> {
        if !(transfers.len() == 2 && (info.is_labelled_searcher_of_type(mev_type) || cfg!(test))) {
            return None
        }
        let ingore_addresses = info.collect_address_set_for_accounting();

        self.try_create_swaps(&transfers, ingore_addresses).pop()
    }

    pub fn cex_merge_possible_swaps(swaps: Vec<NormalizedSwap>) -> Vec<NormalizedSwap> {
        let mut matching: FastHashMap<TokenInfoWithAddress, Vec<&NormalizedSwap>> =
            FastHashMap::default();

        for swap in &swaps {
            matching
                .entry(swap.token_in.clone())
                .or_default()
                .push(swap);
            matching
                .entry(swap.token_out.clone())
                .or_default()
                .push(swap);
        }

        let mut res = vec![];
        let mut voided = FastHashSet::default();

        for (intermediary, swaps) in matching {
            res.extend(swaps.into_iter().combinations(2).filter_map(|mut swaps| {
                let s0 = swaps.remove(0);
                let s1 = swaps.remove(0);

                if voided.contains(s0) || voided.contains(s1) {
                    return None
                }
                // if s0 is first hop
                if s0.token_out == intermediary
                    && s0.token_out == s1.token_in
                    && s0.amount_out == s1.amount_in
                {
                    voided.insert(s0.clone());
                    voided.insert(s1.clone());
                    Some(NormalizedSwap {
                        from: s0.from,
                        recipient: s1.recipient,
                        token_in: s0.token_in.clone(),
                        token_out: s1.token_out.clone(),
                        amount_in: s0.amount_in.clone(),
                        amount_out: s1.amount_out.clone(),
                        protocol: s0.protocol,
                        pool: s0.pool,
                        ..Default::default()
                    })
                } else if s0.token_in == s1.token_out && s0.amount_in == s1.amount_out {
                    voided.insert(s0.clone());
                    voided.insert(s1.clone());
                    Some(NormalizedSwap {
                        from: s1.from,
                        recipient: s0.recipient,
                        token_in: s1.token_in.clone(),
                        token_out: s0.token_out.clone(),
                        amount_in: s1.amount_in.clone(),
                        amount_out: s0.amount_out.clone(),
                        protocol: s1.protocol,
                        pool: s1.pool,
                        ..Default::default()
                    })
                } else {
                    None
                }
            }));
        }

        swaps
            .into_iter()
            .filter(|s| !voided.contains(s))
            .chain(res)
            .collect()
    }
}

#[cfg(test)]
pub mod test {
    use brontes_core::LibmdbxReadWriter;
    use brontes_types::{
        constants::{USDC_ADDRESS, USDT_ADDRESS, WETH_ADDRESS},
        normalized_actions::NormalizedSwap,
    };
    use malachite::Rational;

    use super::SharedInspectorUtils;

    #[test]
    pub fn test_multi_hop_cex_merge_swap() {
        let address0 = alloy_primitives::address!("76F36d497b51e48A288f03b4C1d7461e92247d5e");
        let address3 = alloy_primitives::address!("76F36d497b51e48A288f03b4C1d7461e92247d52");

        let pool1 = alloy_primitives::address!("16F36d497b51e48A288f03b4C1d7461e92247d52");
        let pool2 = alloy_primitives::address!("26F36d497b51e48A288f03b4C1d7461e92247d52");
        let pool3 = alloy_primitives::address!("36F36d497b51e48A288f03b4C1d7461e92247d52");

        let swap1 = NormalizedSwap {
            token_in: brontes_types::db::token_info::TokenInfoWithAddress {
                address: WETH_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 18,
                    symbol:   "WETH".to_string(),
                },
            },
            token_out: brontes_types::db::token_info::TokenInfoWithAddress {
                address: USDT_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 6,
                    symbol:   "USDT".to_string(),
                },
            },
            from: address0,
            pool: pool1,
            recipient: pool2,
            amount_in: Rational::from(1),
            amount_out: Rational::from(2),
            ..Default::default()
        };

        let swap2 = NormalizedSwap {
            token_in: brontes_types::db::token_info::TokenInfoWithAddress {
                address: USDT_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 6,
                    symbol:   "USDT".to_string(),
                },
            },
            token_out: brontes_types::db::token_info::TokenInfoWithAddress {
                address: USDC_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 6,
                    symbol:   "USDC".to_string(),
                },
            },
            from: pool1,
            pool: pool2,
            recipient: pool3,
            amount_in: Rational::from(2),
            amount_out: Rational::from(1),
            ..Default::default()
        };

        let swap3 = NormalizedSwap {
            token_in: brontes_types::db::token_info::TokenInfoWithAddress {
                address: USDC_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 6,
                    symbol:   "USDC".to_string(),
                },
            },
            token_out: brontes_types::db::token_info::TokenInfoWithAddress {
                address: WETH_ADDRESS,
                inner:   brontes_types::db::token_info::TokenInfo {
                    decimals: 18,
                    symbol:   "WETH".to_string(),
                },
            },
            from: pool2,
            pool: pool3,
            recipient: address3,
            amount_in: Rational::from(1),
            amount_out: Rational::from(3),
            ..Default::default()
        };

        // let expected = NormalizedSwap {
        //     token_in: brontes_types::db::token_info::TokenInfoWithAddress {
        //         address: WETH_ADDRESS,
        //         inner:   brontes_types::db::token_info::TokenInfo {
        //             decimals: 18,
        //             symbol:   "WETH".to_string(),
        //         },
        //     },
        //     token_out: brontes_types::db::token_info::TokenInfoWithAddress {
        //         address: WETH_ADDRESS,
        //         inner:   brontes_types::db::token_info::TokenInfo {
        //             decimals: 18,
        //             symbol:   "WETH".to_string(),
        //         },
        //     },
        //     from: address0,
        //     pool: pool1,
        //     recipient: address3,
        //     amount_in: Rational::from(1),
        //     amount_out: Rational::from(3),
        //     ..Default::default()
        // };
        let swaps = vec![swap1, swap2, swap3];
        let res = SharedInspectorUtils::<LibmdbxReadWriter>::cex_merge_possible_swaps(swaps);
        assert_eq!(res.len(), 2, "{:#?}", res);
    }
}