pool_sync/
chain.rs

1//! Chain Support and Pool Type Management
2//!
3//! This module defines the supported blockchain networks (Chains) and manages
4//! the mapping of supported pool types for each chain.
5
6use crate::PoolType;
7use once_cell::sync::Lazy;
8use std::collections::{HashMap, HashSet};
9use std::fmt;
10
11/// Enum representing supported blockchain networks
12#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub enum Chain {
14    /// Ethereum mainnet
15    Ethereum,
16    /// Base chain
17    Base,
18    // Additional chains can be added here
19}
20
21/// Static mapping of supported pool types for each chain
22///
23/// This mapping is important because not all protocols are deployed on all chains,
24/// and the contract addresses for the same protocol may differ across chains.
25static CHAIN_POOLS: Lazy<HashMap<Chain, HashSet<PoolType>>> = Lazy::new(|| {
26    let mut m = HashMap::new();
27
28    // Protocols supported by Ethereum
29    m.insert(
30        Chain::Ethereum,
31        [
32            PoolType::UniswapV2,
33            PoolType::UniswapV3,
34            PoolType::SushiSwapV2,
35            PoolType::SushiSwapV3,
36            PoolType::PancakeSwapV2,
37            PoolType::PancakeSwapV3,
38            PoolType::MaverickV1,
39            PoolType::MaverickV2,
40            PoolType::CurveTwoCrypto,
41            PoolType::CurveTriCrypto,
42            PoolType::BalancerV2,
43        ]
44        .iter()
45        .cloned()
46        .collect(),
47    );
48
49    // Protocols supported by Base
50    m.insert(
51        Chain::Base,
52        [
53            PoolType::UniswapV2,
54            PoolType::UniswapV3,
55            PoolType::SushiSwapV2,
56            PoolType::SushiSwapV3,
57            PoolType::PancakeSwapV2,
58            PoolType::PancakeSwapV3,
59            PoolType::Aerodrome,
60            PoolType::Slipstream,
61            PoolType::BaseSwapV2,
62            PoolType::BaseSwapV3,
63            PoolType::AlienBaseV2,
64            PoolType::AlienBaseV3,
65            PoolType::MaverickV1,
66            PoolType::MaverickV2,
67            PoolType::CurveTwoCrypto,
68            PoolType::CurveTriCrypto,
69            PoolType::BalancerV2,
70            PoolType::SwapBasedV2,
71            PoolType::SwapBasedV3,
72            PoolType::DackieSwapV2,
73            PoolType::DackieSwapV3,
74        ]
75        .iter()
76        .cloned()
77        .collect(),
78    );
79
80    // Additional chains can be configured here
81
82    m
83});
84
85impl Chain {
86    /// Determines if a given pool type is supported on this chain
87    pub fn supported(&self, pool_type: &PoolType) -> bool {
88        CHAIN_POOLS
89            .get(self)
90            .map(|pools| pools.contains(pool_type))
91            .unwrap_or(false)
92    }
93}
94
95// Display implementation for Chain, used for file naming and debugging purposes
96impl fmt::Display for Chain {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{:?}", self)
99    }
100}