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
use colored::Colorize;
use reth_primitives::B256;
use tracing::debug;

use crate::ParserMetricEvents;

/// metric event for traces
#[derive(Clone, Debug)]
pub enum TraceMetricEvent {
    /// recorded a new block trace
    BlockMetricRecieved(BlockStats),
    /// recorded a new tx trace
    TransactionMetricRecieved(TransactionStats),
    /// recorded a new individual tx trace
    TraceMetricRecieved(TraceStats),
}

impl From<TraceMetricEvent> for ParserMetricEvents {
    fn from(val: TraceMetricEvent) -> Self {
        ParserMetricEvents::TraceMetricRecieved(val)
    }
}

#[derive(Clone, Debug)]
pub struct BlockStats {
    pub block_num: u64,
    pub txs:       Vec<TransactionStats>,
    pub err:       Option<TraceParseErrorKind>,
}

impl BlockStats {
    pub fn new(block_num: u64, err: Option<TraceParseErrorKind>) -> Self {
        Self { block_num, txs: Vec::new(), err }
    }

    pub fn trace(&self) {
        let msg = format!(
            "{} -- Block Number: {}",
            "Successfuly Parsed Block".to_string().bright_blue().bold(),
            self.block_num
        );

        debug!("{}", msg);
    }
}

#[derive(Clone, Debug)]
pub struct TransactionStats {
    pub block_num: u64,
    pub tx_hash:   B256,
    pub tx_idx:    u16,
    pub traces:    Vec<TraceStats>,
    pub err:       Option<TraceParseErrorKind>,
}

impl TransactionStats {
    pub fn new(
        block_num: u64,
        tx_hash: B256,
        tx_idx: u16,
        err: Option<TraceParseErrorKind>,
    ) -> Self {
        Self { block_num, tx_hash, tx_idx, traces: Vec::new(), err }
    }

    pub fn trace(&self) {
        let msg = format!(
            "{} -- Tx Hash: {:#x}",
            "Successfully Parsed Transaction".bright_green().bold(),
            self.tx_hash
        );

        debug!("{}", msg);
    }
}

#[derive(Clone, Copy, Debug)]
pub struct TraceStats {
    pub block_num: u64,
    pub tx_hash:   B256,
    pub tx_idx:    u16,
    pub trace_idx: u16,
    pub err:       Option<TraceParseErrorKind>,
}

impl TraceStats {
    pub fn new(
        block_num: u64,
        tx_hash: B256,
        tx_idx: u16,
        trace_idx: u16,
        err: Option<TraceParseErrorKind>,
    ) -> Self {
        Self { block_num, tx_hash, tx_idx, trace_idx, err }
    }

    pub fn trace(&self, total_len: usize) {
        let tx_hash = format!("{:#x}", self.tx_hash);
        let message = format!(
            "{}",
            format!("Starting Transaction Trace {} / {}", self.trace_idx + 1, &total_len)
                .bright_blue()
                .bold()
        );
        debug!(message = message, tx_hash = tx_hash);
    }
}

/// enum for error
#[derive(Debug, Clone, Copy)]
pub enum TraceParseErrorKind {
    TracesMissingBlock,
    TracesMissingTx,
    EmptyInput,
    AbiParseError,
    EthApiError,
    InvalidFunctionSelector,
    AbiDecodingFailed,
    ChannelSendError,
    EtherscanChainNotSupported,
    EtherscanExecutionFailed,
    EtherscanBalanceFailed,
    EtherscanNotProxy,
    EtherscanMissingImplementationAddress,
    EtherscanBlockNumberByTimestampFailed,
    EtherscanTransactionReceiptFailed,
    EtherscanGasEstimationFailed,
    EtherscanBadStatusCode,
    EtherscanEnvVarNotFound,
    EtherscanReqwest,
    EtherscanSerde,
    EtherscanContractCodeNotVerified,
    EtherscanEmptyResult,
    EtherscanRateLimitExceeded,
    EtherscanIO,
    EtherscanLocalNetworksNotSupported,
    EtherscanErrorResponse,
    EtherscanUnknown,
    EtherscanBuilder,
    EtherscanMissingSolcVersion,
    EtherscanInvalidApiKey,
    EtherscanBlockedByCloudflare,
    EtherscanCloudFlareSecurityChallenge,
    EtherscanPageNotFound,
    EtherscanCacheError,
    EthApiEmptyRawTransactionData,
    EthApiFailedToDecodeSignedTransaction,
    EthApiInvalidTransactionSignature,
    EthApiPoolError,
    EthApiUnknownBlockNumber,
    EthApiUnknownBlockOrTxIndex,
    EthApiInvalidBlockRange,
    EthApiPrevrandaoNotSet,
    EthApiConflictingFeeFieldsInRequest,
    EthApiInvalidTransaction,
    EthApiInvalidBlockData,
    EthApiBothStateAndStateDiffInOverride,
    EthApiInternal,
    EthApiSigning,
    EthApiTransactionNotFound,
    EthApiUnsupported,
    EthApiInvalidParams,
    EthApiInvalidTracerConfig,
    EthApiInvalidRewardPercentiles,
    EthApiInternalTracingError,
    EthApiInternalEthError,
    EthApiInternalJsTracerError,
    EthApiUnknownSafeOrFinalizedBlock,
    EthApiExecutionTimedOut,
    EthApiCallInputError,
    AlloyError,
    Eyre,
}