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
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
    bracketed, parenthesized,
    parse::Parse,
    spanned::Spanned,
    token::{Paren, Star},
    Error, ExprClosure, Ident, LitBool, Path, Token,
};

use super::{data_preparation::CallDataParsing, logs::LogConfig, ACTION_SIG_NAME};

pub struct ActionMacro {
    // required for all
    protocol_path:          Path,
    path_to_call:           Path,
    action_type:            Ident,
    exchange_name_w_call:   Ident,
    log_types:              Vec<LogConfig>,
    /// whether we want logs or not
    give_logs:              bool,
    /// whether we want return data or not
    give_returns:           bool,
    /// whether we want call_data or not
    give_call_data:         bool,
    // whether we pass down logs from delegate call in the same call frame
    include_delegated_logs: bool,
    /// The closure that we use to construct the normalized type
    call_function:          ExprClosure,
}

impl ActionMacro {
    pub fn expand(self) -> syn::Result<TokenStream> {
        let Self {
            exchange_name_w_call,
            protocol_path,
            action_type,
            path_to_call,
            log_types,
            give_logs,
            give_call_data,
            include_delegated_logs,
            give_returns,
            call_function,
        } = self;

        let call_data = CallDataParsing::new(
            give_logs,
            give_call_data,
            give_returns,
            include_delegated_logs,
            &exchange_name_w_call,
            &action_type,
            &path_to_call,
            &log_types,
            call_function,
        );

        let call_fn_name =
            Ident::new(&format!("{ACTION_SIG_NAME}_{}", exchange_name_w_call), Span::call_site());

        let mut return_import = path_to_call.clone();
        let mut call = return_import
            .segments
            .pop()
            .ok_or(syn::Error::new(return_import.span(), "invalid call import type"))?;
        let call_ident = call.value().ident.to_string();
        let solidity = call_ident[0..call_ident.len() - 4].to_string() + "Return";

        call.value_mut().ident = Ident::new(&solidity, call.span());
        return_import.segments.push(call.into_value());

        let dex_price_return = if action_type.to_string().to_lowercase().as_str()
            == "poolconfigupdate"
        {
            quote!(Ok(::brontes_pricing::types::DexPriceMsg::DiscoveredPool(result)))
        } else {
            quote!(
                Ok(::brontes_pricing::types::DexPriceMsg::Update(
                    ::brontes_pricing::types::PoolUpdate {
                        block,
                        tx_idx,
                        logs: call_info.logs.clone().to_vec(),
                        action: ::brontes_types::normalized_actions::Action::#action_type(result)
                    },
                ))
            )
        };

        Ok(quote! {
            #[allow(unused_imports)]
            use #path_to_call;
            #[allow(unused_imports)]
            use #return_import;

            #[allow(non_snake_case)]
            pub const fn #call_fn_name() -> [u8; 5] {
                ::alloy_primitives::FixedBytes::new(
                        <#path_to_call as ::alloy_sol_types::SolCall>::SELECTOR
                    )
                    .concat_const(
                    ::alloy_primitives::FixedBytes::new(
                        [#protocol_path.to_byte()]
                        )
                    ).0
            }

            #[derive(Debug, Default)]
            pub struct #exchange_name_w_call;

            impl crate::IntoAction for #exchange_name_w_call {
                fn decode_call_trace<DB: ::brontes_database::libmdbx::LibmdbxReader
                    + ::brontes_database::libmdbx::DBWriter>(
                    &self,
                    call_info: ::brontes_types::structured_trace::CallFrameInfo<'_>,
                    block: u64,
                    tx_idx: u64,
                    db_tx: &DB
                    ) -> ::eyre::Result<::brontes_pricing::types::DexPriceMsg> {
                    #call_data
                    #dex_price_return
                }
            }
        })
    }
}

impl Parse for ActionMacro {
    fn parse(mut input: syn::parse::ParseStream) -> syn::Result<Self> {
        let protocol_path = parse_protocol_path(&mut input)?;
        input.parse::<Token![,]>()?;

        let path_to_call = parse_decode_fn_path(&mut input)?;
        input.parse::<Token![,]>()?;

        let action_type: Ident = input.parse()?;
        input.parse::<Token![,]>()?;

        let possible_logs = parse_logs(&mut input)?;
        input.parse::<Token![,]>()?;

        let (logs, return_data, call_data, include_delegated_logs) = parse_config(&mut input)?;
        let call_function = parse_closure(&mut input)?;

        let uppercase_path_to_call = uppercase_first_char(
            &path_to_call.segments[path_to_call.segments.len() - 1]
                .ident
                .to_string(),
        );

        let exchange_name_w_call = Ident::new(
            &format!(
                "{}{}",
                protocol_path.segments[protocol_path.segments.len() - 1].ident,
                uppercase_path_to_call
            ),
            Span::call_site(),
        );

        Ok(Self {
            path_to_call,
            give_returns: return_data,
            log_types: possible_logs,
            call_function,
            give_logs: logs,
            give_call_data: call_data,
            include_delegated_logs,
            action_type,
            protocol_path,
            exchange_name_w_call,
        })
    }
}

fn parse_closure(input: &mut syn::parse::ParseStream) -> syn::Result<ExprClosure> {
    let call_function: ExprClosure = input.parse()?;
    if call_function.asyncness.is_some() {
        return Err(syn::Error::new(input.span(), "closure cannot be async"))
    }

    if !input.is_empty() {
        return Err(syn::Error::new(
            input.span(),
            "There should be no values after the call function",
        ))
    }

    if call_function.asyncness.is_some() {
        return Err(syn::Error::new(input.span(), "closure cannot be async"))
    }

    if !input.is_empty() {
        return Err(syn::Error::new(
            input.span(),
            "There should be no values after the call function",
        ))
    }

    Ok(call_function)
}

fn parse_config(input: &mut syn::parse::ParseStream) -> syn::Result<(bool, bool, bool, bool)> {
    let mut logs = false;
    let mut return_data = false;
    let mut call_data = false;
    let mut include_delegated_logs = false;

    while !input.peek(Token![|]) {
        let arg: Ident = input.parse()?;
        input.parse::<Token![:]>()?;
        let enabled: LitBool = input.parse()?;

        match arg.to_string().to_lowercase().as_str() {
            "logs" => logs = enabled.value(),
            "call_data" => call_data = enabled.value(),
            "return_data" => return_data = enabled.value(),
            "include_delegated_logs" => include_delegated_logs = enabled.value(),
            _ => {
                return Err(Error::new(
                    arg.span(),
                    format!(
                        "{} is not a valid config option, valid options are: \n logs , call_data, \
                         return_data , include_delegated_logs",
                        arg,
                    ),
                ))
            }
        }
        input.parse::<Token![,]>()?;
    }

    Ok((logs, return_data, call_data, include_delegated_logs))
}

fn parse_protocol_path(input: &mut syn::parse::ParseStream) -> syn::Result<Path> {
    let protocol_path: Path = input.parse().map_err(|_| {
        syn::Error::new(input.span(), "No Protocol Found, Should be Protocol::<ProtocolVarient>")
    })?;

    if protocol_path.segments.len() < 2 {
        return Err(syn::Error::new(
            protocol_path.span(),
            "incorrect path, Should be Protocol::<ProtocolVarient>",
        ))
    }

    let should_protocol = &protocol_path.segments[protocol_path.segments.len() - 2].ident;
    if !should_protocol.to_string().starts_with("Protocol") {
        return Err(syn::Error::new(
            should_protocol.span(),
            "incorrect path, Should be Protocol::<ProtocolVarient>",
        ))
    }
    Ok(protocol_path)
}

fn parse_decode_fn_path(input: &mut syn::parse::ParseStream) -> syn::Result<Path> {
    let fn_path: Path = input.parse().map_err(|_| {
        syn::Error::new(
            input.span(),
            "No path to alloy fn found, Should be path::to::alloy::call::fn_nameCall",
        )
    })?;

    if fn_path.segments.len() < 2 {
        return Err(syn::Error::new(
            fn_path.span(),
            "incorrect path, Should be <crate>::<path_to>::ProtocolModName::FnCall",
        ))
    }

    Ok(fn_path)
}

fn parse_logs(input: &mut syn::parse::ParseStream) -> syn::Result<Vec<LogConfig>> {
    let mut log_types = Vec::new();
    let content;
    bracketed!(content in input);

    loop {
        let mut can_repeat = false;
        let mut ignore_before = false;

        if content.peek(Token![..]) {
            let _ = content.parse::<Token![..]>()?;

            ignore_before = true;
        }

        let fallbacks;
        // have fallback
        let buf = if content.peek(Paren) {
            parenthesized!(fallbacks in content);
            &fallbacks
        } else {
            &content
        };

        let Ok(log_type) = buf.parse::<Ident>() else {
            break;
        };

        let mut fallback = Vec::new();

        while buf.peek(Token![|]) {
            let _ = buf.parse::<Token![|]>()?;
            let Ok(log_type) = buf.parse::<Ident>() else {
                break;
            };
            fallback.push(log_type);
        }

        if content.peek(Star) {
            let _ = content.parse::<Star>()?;
            can_repeat = true;
        }

        log_types.push(LogConfig {
            ignore_before,
            can_repeat,
            log_ident: log_type,
            log_fallbacks: fallback,
        });

        let Ok(_) = content.parse::<Token![,]>() else {
            break;
        };
    }

    Ok(log_types)
}

fn uppercase_first_char(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}