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
mod action_classifier;
mod bench_struct_methods;
mod discovery_classifier;
mod function_metrics;
mod libmdbx_test;
mod transpose;

use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput, ItemFn};

use crate::action_classifier::{ActionDispatch, ActionMacro};

#[proc_macro]
/// the action impl macro deals with automatically parsing the data needed for
/// underlying actions. The use is as followed
/// ```ignore
/// action_impl!(ProtocolPath, PathToCall, CallType, [LogType / 's], [logs: bool , call_data: bool, return_data: bool])
/// ```
/// The generated structs name will be as the following:
///  <LastIdentInProtocolPath> + <LastIdentInPathToCall>
/// Example:
/// a macro invoked with
///     Protocol::UniswapV2,
///     crate::UniswapV2::swapCall,
///
/// becomes: UniswapV2swapCall.
/// This is done to avoid naming conflicts between classifiers as this is name
/// will always be unique.
///
/// The Array of log types are expected to be in the order that they are emitted
/// in. Otherwise the decoding will fail
///
///  ## Examples
/// ```ignore
/// action_impl!(
///     Protocol::UniswapV2,
///     crate::UniswapV2::swapCall,
///     Swap,
///     [..Swap],
///     logs: true,
///     |index,
///     from_address: Address,
///     target_address: Address,
///     msg_sender: Address,
///     log_data: UniswapV2swapCallLogs| { <body> });
///
/// action_impl!(
///     Protocol::UniswapV2,
///     crate::UniswapV2::mintCall,
///     Mint,
///     [..Mint],
///     logs: true,
///     call_data: true,
///     |index,
///      from_address: Address,
///      target_address: Address,
///      msg_sender: Address,
///      call_data: mintCall,
///      log_data: UniswapV2mintCallLogs|  { <body> });
/// ```
///
/// # Logs Config
/// NOTE: all log modifiers are compatible with each_other
/// ## Log Ignore Before
/// if you want to ignore all logs that occurred before a certain log,
/// prefix the log with .. ex `..Mint`.
///
/// ## Log Repeating
/// if a log is repeating and dynamic in length, use `*` after the log
/// to mark that there is a arbitrary amount of these logs emitted.
/// ex `Transfer*` or `..Transfer*`
///
/// ## Fallback logs.
/// in the case that you might need a fallback log, these can be defined by
/// wrapping the names in parens. e.g (Transfer | SpecialTransfer).
/// this will try to decode transfer first and if it fails, special transfer.
/// Fallback logs are configurable with other log parsing options. this means
/// you can do something like ..(Transfer | SpecialTransfer) or ..(Transfer |
/// SpecialTransfer)*
///
///
/// the fields `call_data`, `return_data` and `log_data` are only put into the
/// closure if specified they are always in this order, for example if you put
///  
///  ```return_data: true```
///  then then the closure would be as followed
///  ```|index, from_address, target_address, return_data|```
///
/// for
///  ```ignore
///  log_data: true,
///  call_data: true
///  ````
///  ```|index, from_address, target_address, return_data, log_data|```
pub fn action_impl(input: TokenStream) -> TokenStream {
    parse_macro_input!(input as ActionMacro)
        .expand()
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro]
/// action_dispatch macro crates a struct that automatically dispatches
/// the given trace information to the proper action classifier. its invoked as
/// the following:
/// ```ignore
/// action_dispatch!(<DispatchStructName>, [action_classifier_names..],);
/// ```
/// an actual example would be
/// ```ignore
/// # use brontes_macros::{action_dispatch, action_impl};
/// # use brontes_pricing::Protocol;
/// # use brontes_types::normalized_actions::NormalizedSwap;
/// # use alloy_primitives::Address;
/// # use brontes_database::libmdbx::tx::CompressedLibmdbxTx;
///
/// action_impl!(
///     Protocol::UniswapV2,
///     crate::UniswapV2::swapCall,
///     Swap,
///     [Ignore<Sync>, Swap],
///     call_data: true,
///     logs: true,
///     |trace_index,
///     from_address: Address,
///     target_address: Address,
///      msg_sender: Address,
///     call_data: swapCall,
///     log_data: UniswapV2swapCallLogs,
///     db_tx: &DB| {
///         todo!()
///     }
/// );
///
/// action_dispatch!(ClassifierDispatch, UniswapV2swapCall);
/// ```
pub fn action_dispatch(input: TokenStream) -> TokenStream {
    parse_macro_input!(input as ActionDispatch)
        .expand()
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro]
/// the discovery impl macro deals with automatically parsing the data needed
/// for discovering new pools.
/// ```ignore
/// discovery_impl!(DiscoveryName, Path::To::Factory::DeployCall, factory address, Parse Fn);
/// ```
/// where Parse Fn
/// ```ignore
/// |deployed_address: Address, decoded_call_data: DeployCall, provider: Arc<T>| { <body> }
/// ```
pub fn discovery_impl(input: TokenStream) -> TokenStream {
    discovery_classifier::discovery_impl(input.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro]
/// # Eth Curve Pool Discovery
/// Curve is weird since each factory contract (7 of them) has multiple
/// implementations of each create base/plain/meta pool, so it has it's own impl
/// ### Fields
/// 1. `Protocol` (enum in types) - Curve version
/// 2. Path to the `sol!` generated abi for the factory
/// 3. `x` concatenated with the factory address
/// 4. A tuple with the fields (x, y, z)
///     - x: number of base pools
///     - y: number of metapools
///     - z: number of plain pools
///
/// ### Example
/// ```ignore
/// curve_discovery_impl!(
///     CurvecrvUSD,
///     crate::raw::pools::impls::CurvecrvUSDFactory,
///     x4f8846ae9380b90d2e71d5e3d042dff3e7ebb40d,
///     (1, 2, 3)
/// );
/// ```
pub fn curve_discovery_impl(input: TokenStream) -> TokenStream {
    discovery_classifier::curve::curve_discovery_impl(input.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro]
/// discovery dispatch macro creates a struct that automatically dispatches
/// possible CREATE traces to the proper discovery classifier
/// ```ignore
/// discovery_dispatch!(<DispatchStructName>, [discovery_impl_name..],);
/// ```
pub fn discovery_dispatch(input: TokenStream) -> TokenStream {
    discovery_classifier::discovery_dispatch(input.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro_attribute]
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as ItemFn);
    libmdbx_test::parse(item, attr.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro_attribute]
pub fn bench_time(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as ItemFn);
    bench_struct_methods::parse(item, attr.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

#[proc_macro_derive(Transposable)]
pub fn transposable(item: TokenStream) -> TokenStream {
    let i_struct = parse_macro_input!(item as DeriveInput);
    transpose::parse(i_struct)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Simple utils for counters and gauges when it comes to tracking function
/// metrics, NOTE: tracks call once function has returned; early returns won't
/// be counted
#[proc_macro_attribute]
pub fn metrics_call(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as ItemFn);
    function_metrics::parse(item, attr.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}