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
pub mod dex_quote {
    use std::str::FromStr;

    use alloy_primitives::Address;
    use itertools::Itertools;
    use malachite::{Natural, Rational};
    use serde::{
        de::{Deserialize, Deserializer},
        Serialize, Serializer,
    };

    use crate::{db::dex::DexPrices, pair::Pair, FastHashMap};

    type DexPriceQuotesVec = Vec<(
        (String, String),
        (
            (Vec<u64>, Vec<u64>),
            (Vec<u64>, Vec<u64>),
            (Vec<u64>, Vec<u64>),
            (String, String),
            bool,
            u64,
        ),
    )>;

    #[allow(dead_code)]
    pub fn serialize<S>(
        value: &Option<FastHashMap<Pair, DexPrices>>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let to_ser: DexPriceQuotesVec = if let Some(quotes) = value {
            quotes
                .iter()
                .map(|(pair, dex_price)| {
                    (
                        (format!("{:?}", pair.0), format!("{:?}", pair.1)),
                        (
                            (
                                dex_price.pre_state.numerator_ref().to_limbs_asc(),
                                dex_price.pre_state.denominator_ref().to_limbs_asc(),
                            ),
                            (
                                dex_price.post_state.numerator_ref().to_limbs_asc(),
                                dex_price.post_state.denominator_ref().to_limbs_asc(),
                            ),
                            (
                                dex_price.pool_liquidity.numerator_ref().to_limbs_asc(),
                                dex_price.pool_liquidity.denominator_ref().to_limbs_asc(),
                            ),
                            (
                                format!("{:?}", dex_price.goes_through.0),
                                format!("{:?}", dex_price.goes_through.1),
                            ),
                            dex_price.is_transfer,
                            dex_price.first_hop_connections as u64,
                        ),
                    )
                })
                .collect_vec()
        } else {
            vec![]
        };

        to_ser.serialize(serializer)
    }

    #[allow(dead_code)]
    pub fn deserialize<'de, D>(
        deserializer: D,
    ) -> Result<Option<FastHashMap<Pair, DexPrices>>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let des: DexPriceQuotesVec = Deserialize::deserialize(deserializer)?;

        if des.is_empty() {
            return Ok(None)
        }

        let val = des
            .into_iter()
            .map(
                |(
                    (pair0, pair1),
                    ((pre_num, pre_den), (post_num, post_den), (liq_num, liq_den), (g0, g1), t, c),
                )| {
                    (
                        Pair(
                            Address::from_str(&pair0).unwrap(),
                            Address::from_str(&pair1).unwrap(),
                        ),
                        DexPrices {
                            pre_state:             Rational::from_naturals(
                                Natural::from_owned_limbs_asc(pre_num),
                                Natural::from_owned_limbs_asc(pre_den),
                            ),
                            post_state:            Rational::from_naturals(
                                Natural::from_owned_limbs_asc(post_num),
                                Natural::from_owned_limbs_asc(post_den),
                            ),
                            pool_liquidity:        Rational::from_naturals(
                                Natural::from_owned_limbs_asc(liq_num),
                                Natural::from_owned_limbs_asc(liq_den),
                            ),
                            goes_through:          Pair(
                                Address::from_str(&g0).unwrap(),
                                Address::from_str(&g1).unwrap(),
                            ),
                            is_transfer:           t,
                            first_hop_connections: c as usize,
                        },
                    )
                },
            )
            .collect::<FastHashMap<Pair, DexPrices>>();

        Ok(Some(val))
    }
}