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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! 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::network::Network;
use alloy::primitives::U128;
use alloy::primitives::U256;
use alloy::primitives::{Address, Log};
use alloy::providers::Provider;
use alloy::transports::Transport;
use pool_structure::{TickInfo, UniswapV2Pool, UniswapV3Pool};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{fmt, sync::Arc};

pub use v2_builder::build_pools as build_v2_pools;
pub use v2_builder::process_sync_data;
pub use v3_builder::build_pools as build_v3_pools;
pub use v3_builder::process_tick_data;
pub mod pool_structure;
pub mod aerodrome;
pub mod base_swap;
pub mod alien_base;
pub mod pancake_swap;
pub mod sushiswap;
pub mod uniswap;
mod v2_builder;
mod gen;
mod v3_builder;

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

    UniswapV3,
    SushiSwapV3,
    PancakeSwapV3,

    Aerodrome,
    Slipstream,

    BaseSwapV2,
    BaseSwapV3,

    AlienBase,

}

impl PoolType {
    pub fn is_v2(&self) -> bool {
        self == &PoolType::UniswapV2
            || self == &PoolType::SushiSwapV2
            || self == &PoolType::PancakeSwapV2
            || self == &PoolType::Aerodrome
            || self == &PoolType::BaseSwapV2
    }

    pub fn is_v3(&self) -> bool {
        self == &PoolType::UniswapV3
            || self == &PoolType::SushiSwapV3
            || self == &PoolType::PancakeSwapV3
            || self == &PoolType::Slipstream
            || self == &PoolType::BaseSwapV3
            || self == &PoolType::AlienBase
    }
}

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

    UniswapV3(UniswapV3Pool),
    SushiSwapV3(UniswapV3Pool),
    PancakeSwapV3(UniswapV3Pool),
    Slipstream(UniswapV3Pool),
    BaseSwapV3(UniswapV3Pool),
    AlienBase(UniswapV3Pool)

}

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::Aerodrome => Pool::Aerodrome(pool),
            PoolType::BaseSwapV2 => Pool::BaseSwapV2(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),
            PoolType::Slipstream => Pool::Slipstream(pool),
            PoolType::BaseSwapV3 => Pool::BaseSwapV3(pool),
            PoolType::AlienBase => Pool::AlienBase(pool),
            _ => panic!("Invalid pool type"),
        }
    }

    pub fn is_v2(&self) -> bool {
        match self {
            Pool::UniswapV2(_) => true,
            Pool::SushiSwapV2(_) => true,
            Pool::PancakeSwapV2(_) => true,
            Pool::Aerodrome(_) => true,
            Pool::BaseSwapV2(_) => true,
            _ => false,
        }
    }

    pub fn is_v3(&self) -> bool {
        match self {
            Pool::UniswapV3(_) => true,
            Pool::SushiSwapV3(_) => true,
            Pool::PancakeSwapV3(_) => true,
            Pool::Slipstream(_) => true,
            Pool::BaseSwapV3(_) => true,
            Pool::AlienBase(_) => true,
            _ => false,
        }
    }

    pub fn get_v3(&self) -> Option<&UniswapV3Pool> {
        match self {
            Pool::UniswapV3(pool) => Some(pool),
            Pool::SushiSwapV3(pool) => Some(pool),
            Pool::PancakeSwapV3(pool) => Some(pool),
            Pool::Slipstream(pool) => Some(pool),
            Pool::BaseSwapV3(pool) => Some(pool),
            Pool::AlienBase(pool) => Some(pool),
            _ => None,
        }
    }

    pub fn get_v2(&self) -> Option<&UniswapV2Pool> {
        match self {
            Pool::UniswapV2(pool) => Some(pool),
            Pool::SushiSwapV2(pool) => Some(pool),
            Pool::PancakeSwapV2(pool) => Some(pool),
            Pool::Aerodrome(pool) => Some(pool),
            Pool::BaseSwapV2(pool) => Some(pool),
            _ => None,
        }
    }

    pub fn get_v3_mut(&mut self) -> Option<&mut UniswapV3Pool> {
        match self {
            Pool::UniswapV3(pool) => Some(pool),
            Pool::SushiSwapV3(pool) => Some(pool),
            Pool::PancakeSwapV3(pool) => Some(pool),
            Pool::Slipstream(pool) => Some(pool),
            Pool::BaseSwapV3(pool) => Some(pool),
            Pool::AlienBase(pool) => Some(pool),
            _ => None,
        }
    }

    pub fn get_v2_mut(&mut self) -> Option<&mut UniswapV2Pool> {
        match self {
            Pool::UniswapV2(pool) => Some(pool),
            Pool::SushiSwapV2(pool) => Some(pool),
            Pool::PancakeSwapV2(pool) => Some(pool),
            Pool::Aerodrome(pool) => Some(pool),
            Pool::BaseSwapV2(pool) => Some(pool),
            _ => None,
        }
    }
}

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,
    Aerodrome,
    Slipstream,
    BaseSwapV2,
    BaseSwapV3,
    AlienBase
);

/*
*/

/// 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::Aerodrome => {
                v2_builder::build_pools(provider, addresses, PoolType::Aerodrome).await
            }
            PoolType::BaseSwapV2 => {
                v2_builder::build_pools(provider, addresses, PoolType::BaseSwapV2).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::Slipstream => {
                v3_builder::build_pools(provider, addresses, PoolType::Slipstream).await
            }
            PoolType::BaseSwapV3 => {
                v3_builder::build_pools(provider, addresses, PoolType::BaseSwapV3).await
            }
            PoolType::AlienBase => {
                v3_builder::build_pools(provider, addresses, PoolType::AlienBase).await
            }
            _ => panic!("Invalid pool type"),
        }
    }
}
/// 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;
    fn stable(&self) -> bool;
}

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::Slipstream(pool) => pool.fee,
                    _ => 0
                }
            }

            fn stable(&self) -> bool {
                match self {
                    Pool::Aerodrome(pool) => pool.stable.unwrap(),
                    _=> false
                }
            }
        }
    };
}