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
use alloy_primitives::{Address, TxHash};

use crate::{
    db::{address_metadata::ContractType, searcher::SearcherInfo},
    mev::MevType,
    normalized_actions::NormalizedEthTransfer,
    FastHashSet, GasDetails,
};

#[derive(Debug, Clone)]
pub struct TxInfo {
    pub block_number: u64,
    pub tx_index:     u64,
    pub eoa:          Address,

    // is none if the contract is classified, or emits logs
    // or is verified
    pub mev_contract:           Option<Address>,
    pub contract_type:          Option<ContractType>,
    pub tx_hash:                TxHash,
    pub gas_details:            GasDetails,
    pub is_classified:          bool,
    pub is_cex_dex_call:        bool,
    pub is_private:             bool,
    pub is_verified_contract:   bool,
    pub searcher_eoa_info:      Option<SearcherInfo>,
    pub searcher_contract_info: Option<SearcherInfo>,
    pub total_eth_value:        Vec<NormalizedEthTransfer>,
}

impl TxInfo {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        block_number: u64,
        tx_index: u64,
        eoa: Address,
        mev_contract: Option<Address>,
        contract_type: Option<ContractType>,
        tx_hash: TxHash,
        gas_details: GasDetails,
        is_classified: bool,
        is_cex_dex_call: bool,
        is_private: bool,
        is_verified_contract: bool,
        searcher_eoa_info: Option<SearcherInfo>,
        searcher_contract_info: Option<SearcherInfo>,
        total_eth_value: Vec<NormalizedEthTransfer>,
    ) -> Self {
        Self {
            total_eth_value,
            tx_index,
            block_number,
            mev_contract,
            contract_type,
            eoa,
            tx_hash,
            gas_details,
            is_classified,
            is_cex_dex_call,
            is_private,
            is_verified_contract,
            searcher_eoa_info,
            searcher_contract_info,
        }
    }

    pub fn get_total_eth_value(&self) -> &[NormalizedEthTransfer] {
        &self.total_eth_value
    }

    pub fn split_to_storage_info(self) -> (TxHash, GasDetails) {
        (self.tx_hash, self.gas_details)
    }

    pub fn get_searcher_eao_info(&self) -> Option<&SearcherInfo> {
        self.searcher_eoa_info.as_ref()
    }

    pub fn get_searcher_contract_info(&self) -> Option<&SearcherInfo> {
        self.searcher_contract_info.as_ref()
    }

    pub fn collect_address_set_for_accounting(&self) -> FastHashSet<Address> {
        let mut mev_addresses: FastHashSet<Address> = vec![self.eoa]
            .into_iter()
            .chain(self.mev_contract)
            .collect();

        self.get_sibling_searchers(&mut mev_addresses);
        mev_addresses
    }

    pub fn get_sibling_searchers(&self, searchers: &mut FastHashSet<Address>) {
        if let Some(ref searcher_info) = self.searcher_eoa_info {
            for address in searcher_info.get_sibling_searchers() {
                searchers.insert(*address);
            }
        }

        if let Some(ref searcher_info) = self.searcher_contract_info {
            for address in searcher_info.get_sibling_searchers() {
                searchers.insert(*address);
            }
        }
    }

    pub fn is_searcher_of_type(&self, mev_type: MevType) -> bool {
        self.searcher_eoa_info
            .as_ref()
            .map_or(false, |info| info.is_searcher_of_type(mev_type))
            || self
                .searcher_contract_info
                .as_ref()
                .map_or(false, |info| info.is_searcher_of_type(mev_type))
    }

    pub fn is_searcher_of_type_with_count_threshold(
        &self,
        mev_type: MevType,
        threshold: u64,
    ) -> bool {
        self.searcher_eoa_info
            .as_ref()
            .map_or(false, |info| info.is_searcher_of_type_with_threshold(mev_type, threshold))
            || self
                .searcher_contract_info
                .as_ref()
                .map_or(false, |info| info.is_searcher_of_type_with_threshold(mev_type, threshold))
    }

    pub fn is_labelled_searcher_of_type(&self, mev_type: MevType) -> bool {
        self.searcher_eoa_info
            .as_ref()
            .map_or(false, |info| info.is_labelled_searcher_of_type(mev_type))
            || self
                .searcher_contract_info
                .as_ref()
                .map_or(false, |info| info.is_labelled_searcher_of_type(mev_type))
    }

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

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

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

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

pub fn collect_address_set_for_accounting(tx_infos: &[TxInfo]) -> FastHashSet<Address> {
    let mut mev_addresses: FastHashSet<Address> = tx_infos
        .iter()
        .flat_map(|tx_info| std::iter::once(tx_info.eoa).chain(tx_info.mev_contract))
        .collect();

    for tx_info in tx_infos {
        tx_info.get_sibling_searchers(&mut mev_addresses);
    }

    mev_addresses
}