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
//! Core definitions for pool synchronization
//!
//! This module defines the core structures and traits used in the pool synchronization system.
//! It includes enumerations for supported pool types, a unified `Pool` enum, and a trait for
//! fetching and decoding pool creation events.

use crate::chain::Chain;
use crate::impl_pool_info;
use alloy::primitives::{Address, Log};
use alloy::providers::Provider;
use alloy::transports::Transport;
use alloy::network::Network;
use pool_structure::{TickInfo, UniswapV2Pool, UniswapV3Pool};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{fmt, sync::Arc};
use alloy::primitives::U128;
use alloy::primitives::U256;


pub mod pool_structure;
pub use v2_builder::build_pools as build_v2_pools;
pub use v3_builder::build_pools as build_v3_pools;
mod gen;
mod v2_builder;
mod v3_builder;
pub mod uniswap;
pub mod sushiswap;
pub mod pancake_swap;
pub mod aerodome;


/// Enumerates the supported pool types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PoolType {
    UniswapV2,
    SushiSwapV2,
    PancakeSwapV2,

    UniswapV3,
    SushiSwapV3,
    PancakeSwapV3,

    Aerodome,
}

/// Represents a populated pool from any of the supported protocols
#[derive(Debug, Clone,  Serialize, Deserialize)]
pub enum Pool {
    UniswapV2(UniswapV2Pool),
    SushiSwapV2(UniswapV2Pool),
    PancakeSwapV2(UniswapV2Pool),

    UniswapV3(UniswapV3Pool),
    SushiSwapV3(UniswapV3Pool),
    PancakeSwapV3(UniswapV3Pool),

    Aerodome(UniswapV2Pool),
}

impl Pool {
    pub fn new_v2(pool_type: PoolType, pool: UniswapV2Pool) -> Self {
        match pool_type {
            PoolType::UniswapV2 => Pool::UniswapV2(pool),
            PoolType::SushiSwapV2 => Pool::SushiSwapV2(pool),
            PoolType::PancakeSwapV2 => Pool::PancakeSwapV2(pool),
            PoolType::Aerodome => Pool::Aerodome(pool),
            _ => panic!("Invalid pool type")
        }
    }

    pub fn new_v3(pool_type: PoolType, pool: UniswapV3Pool) -> Self {
        match pool_type {
            PoolType::UniswapV3 => Pool::UniswapV3(pool),
            PoolType::SushiSwapV3 => Pool::SushiSwapV3(pool),
            PoolType::PancakeSwapV3 => Pool::PancakeSwapV3(pool),
            _ => panic!("Invalid pool type")
        }
    }
}


impl fmt::Display for PoolType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}


// Implement the PoolInfo trait for all pool variants that are supported
impl_pool_info!(
    Pool, 
    UniswapV2, 
    SushiSwapV2,
    PancakeSwapV2,
    UniswapV3, 
    SushiSwapV3, 
    PancakeSwapV3,
    Aerodome
);

/* 
*/


/// Defines common functionality for fetching and decoding pool creation events
///
/// This trait provides a unified interface for different pool types to implement
/// their specific logic for identifying and parsing pool creation events.
pub trait PoolFetcher: Send + Sync {
    /// Returns the type of pool this fetcher is responsible for
    fn pool_type(&self) -> PoolType;

    /// Returns the factory address for the given chain
    fn factory_address(&self, chain: Chain) -> Address;

    /// Returns the event signature for pool creation
    fn pair_created_signature(&self) -> &str;

    /// Attempts to create a `Pool` instance from a log entry
    fn log_to_address(&self, log: &Log) -> Address;
}

impl PoolType {
    pub async fn build_pools_from_addrs<P, T, N>(
        &self,
        provider: Arc<P>,
        addresses: Vec<Address>,
    ) -> Vec<Pool>
    where
        P: Provider<T, N> + Sync + 'static,
        T: Transport + Sync + Clone,
        N: Network 
    {
        match self {
            PoolType::UniswapV2 => v2_builder::build_pools(provider, addresses, PoolType::UniswapV2).await,
            PoolType::SushiSwapV2 => v2_builder::build_pools(provider, addresses, PoolType::SushiSwapV2).await,
            PoolType::PancakeSwapV2 => v2_builder::build_pools(provider, addresses, PoolType::PancakeSwapV2).await,
            PoolType::UniswapV3 => v3_builder::build_pools(provider, addresses, PoolType::UniswapV3).await,
            PoolType::SushiSwapV3 => v3_builder::build_pools(provider, addresses, PoolType::SushiSwapV3).await,
            PoolType::PancakeSwapV3 => v3_builder::build_pools(provider, addresses, PoolType::PancakeSwapV3).await,
            PoolType::Aerodome => v2_builder::build_pools(provider, addresses, PoolType::Aerodome).await,
        }
    }

}
/// Defines common methods that are used to access information about the pools
pub trait PoolInfo {
    fn address(&self) -> Address;
    fn token0_address(&self) -> Address;
    fn token1_address(&self) -> Address;
    fn token0_name(&self) -> String;
    fn token1_name(&self) -> String;
    fn token0_decimals(&self) -> u8;
    fn token1_decimals(&self) -> u8;
    fn pool_type(&self) -> PoolType;
    fn fee(&self) -> u32;
}

pub trait V2PoolInfo {
    fn token0_reserves(&self) -> U128;
    fn token1_reserves(&self) -> U128;
}

pub trait V3PoolInfo {
    fn fee(&self) -> u32;
    fn tick_spacing(&self) -> i32;
    fn tick_bitmap(&self) -> HashMap<i16, U256>;
    fn ticks(&self) -> HashMap<i32, TickInfo>;
}


/// Macro for generating getter methods for all of the suppored pools
#[macro_export]
macro_rules! impl_pool_info {
    ($enum_name:ident, $($variant:ident),+) => {
        impl PoolInfo for $enum_name {
            fn address(&self) -> Address {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.address,

                    )+
                }
            }

            fn token0_address(&self) -> Address {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token0,
                    )+
                }
            }

            fn token1_address(&self) -> Address {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token1,
                    )+
                }
            }

            fn token0_name(&self) -> String {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token0_name.clone(),
                    )+
                }
            }
            fn token1_name(&self) -> String {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token1_name.clone(),
                    )+
                }
            }

            fn token0_decimals(&self) -> u8 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token0_decimals,
                    )+
                }
            }
            fn token1_decimals(&self) -> u8 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token1_decimals,
                    )+
                }
            }

            fn pool_type(&self) -> PoolType {
                match self {
                    $(
                        $enum_name::$variant(_) => PoolType::$variant,
                    )+
                }
            }

            fn fee(&self) -> u32 {
                match self {
                    Pool::UniswapV3(pool) | Pool::SushiSwapV3(pool) | Pool::PancakeSwapV3(pool) => pool.fee,
                    _ => 0
                }
            }
        }
    };
}

/* 
#[macro_export]
macro_rules! impl_v2_pool_info {
    ($enum_name:ident, $($variant:ident),+) => {
        impl V2PoolInfo for $enum_name {
            fn token0_reserves(&self) -> U128 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token0_reserves,
                    )+
                    _ => panic!("Not a V2 pool"),
                }
            }

            fn token1_reserves(&self) -> U128 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.token1_reserves,
                    )+
                    _ => panic!("Not a V2 pool"),
                }
            }
        }
    };
}

#[macro_export]
macro_rules! impl_v3_pool_info {
    ($enum_name:ident, $($variant:ident),+) => {
        impl V3PoolInfo for $enum_name {
            fn fee(&self) -> u32 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.fee,
                    )+
                    _ => panic!("Not a V3 pool"),
                }
            }

            fn tick_spacing(&self) -> i32 {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.tick_spacing,
                    )+
                    _ => panic!("Not a V3 pool"),
                }
            }

            fn tick_bitmap(&self) -> &HashMap<i16, U256> {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.tick_bitmap,
                    )+
                    _ => panic!("Not a V3 pool"),
                }
            }

            fn ticks(&self) -> &HashMap<i32, TickInfo> {
                match self {
                    $(
                        $enum_name::$variant(pool) => pool.ticks,
                    )+
                    _ => panic!("Not a V3 pool"),
                }
            }
        }
    };
}
*/