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
use std::{
    fmt::Display,
    ops::{Deref, DerefMut},
};

use alloy_primitives::Address;
use clickhouse::{DbRow, Row};
use redefined::{self_convert_redefined, Redefined};
use rkyv::{Archive, Deserialize as rDeserialize, Serialize as rSerialize};
use serde::{ser::SerializeStruct, Deserialize, Serialize};

use super::clickhouse_serde::token_info::token_info_des;
use crate::{
    constants::{USDC_ADDRESS, USDT_ADDRESS, WETH_ADDRESS},
    db::redefined_types::primitives::AddressRedefined,
    implement_table_value_codecs_with_zc,
    serde_utils::addresss,
};

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize, Redefined)]
#[redefined_attr(derive(Debug, PartialEq, Clone, Serialize, rSerialize, rDeserialize, Archive))]
pub struct TokenInfoWithAddress {
    #[serde(with = "addresss")]
    pub address: Address,
    #[redefined(same_fields)]
    #[serde(deserialize_with = "token_info_des::deserialize")]
    pub inner:   TokenInfo,
}

impl TokenInfoWithAddress {
    pub fn native_eth() -> Self {
        Self {
            inner:   TokenInfo { decimals: 18, symbol: "ETH".to_string() },
            address: WETH_ADDRESS,
        }
    }

    pub fn weth() -> Self {
        Self {
            inner:   TokenInfo { decimals: 18, symbol: "WETH".to_string() },
            address: WETH_ADDRESS,
        }
    }

    pub fn usdt() -> Self {
        Self {
            inner:   TokenInfo { decimals: 6, symbol: "USDT".to_string() },
            address: USDT_ADDRESS,
        }
    }

    pub fn usdc() -> Self {
        Self {
            inner:   TokenInfo { decimals: 6, symbol: "USDC".to_string() },
            address: USDC_ADDRESS,
        }
    }

    pub fn clickhouse_fmt(&self) -> (String, String) {
        (format!("{:?}", self.address), self.inner.symbol.clone())
    }
}

impl Display for TokenInfoWithAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "symbol: {}", self.inner.symbol)
    }
}

impl Deref for TokenInfoWithAddress {
    type Target = TokenInfo;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for TokenInfoWithAddress {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl Serialize for TokenInfoWithAddress {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut ser_struct = serializer.serialize_struct("TokenInfoWithAddress", 3)?;

        ser_struct.serialize_field("address", &format!("{:?}", self.address))?;
        ser_struct.serialize_field("symbol", &self.symbol)?;
        ser_struct.serialize_field("decimals", &self.decimals)?;

        ser_struct.end()
    }
}

impl DbRow for TokenInfoWithAddress {
    const COLUMN_NAMES: &'static [&'static str] = &["address", "symbol", "decimals"];
}

#[derive(
    Debug,
    Clone,
    Default,
    Row,
    Deserialize,
    Serialize,
    rSerialize,
    rDeserialize,
    Archive,
    PartialEq,
    Eq,
    Hash,
)]
pub struct TokenInfo {
    pub decimals: u8,
    pub symbol:   String,
}

impl TokenInfo {
    pub fn new(decimals: u8, symbol: String) -> Self {
        Self { symbol, decimals }
    }
}

self_convert_redefined!(TokenInfo);
implement_table_value_codecs_with_zc!(TokenInfo);