loom_types_entities/
token.rs

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
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::ops::{Add, Div, Mul, Neg};
use std::sync::Arc;
use std::sync::RwLock;

use alloy_primitives::utils::Unit;
use alloy_primitives::{Address, I256, U256};
use loom_defi_address_book::TokenAddress;

const ONE_ETHER: U256 = Unit::ETHER.wei_const();

#[derive(Clone, Debug, Default)]
pub struct Token {
    address: Address,
    basic: bool,
    middle: bool,
    decimals: u8,
    name: Option<String>,
    symbol: Option<String>,
    eth_price: Arc<RwLock<Option<U256>>>,
}

pub type TokenWrapper = Arc<Token>;

impl Hash for Token {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.address.hash(state)
    }
}

impl PartialEq for Token {
    fn eq(&self, other: &Self) -> bool {
        self.address == other.get_address()
    }
}

impl Eq for Token {}

impl Ord for Token {
    fn cmp(&self, other: &Self) -> Ordering {
        self.address.cmp(&other.get_address())
    }
}

impl PartialOrd for Token {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Token {
    pub fn new(address: Address) -> Token {
        Token { address, decimals: 18, ..Token::default() }
    }

    pub fn new_with_data(
        address: Address,
        symbol: Option<String>,
        name: Option<String>,
        decimals: Option<u8>,
        basic: bool,
        middle: bool,
    ) -> Token {
        Token { address, symbol, name, decimals: decimals.unwrap_or(18), basic, middle, ..Default::default() }
    }

    pub fn get_eth_price(&self) -> Option<U256> {
        if self.is_weth() {
            Some(ONE_ETHER)
        } else {
            match self.eth_price.read() {
                Ok(x) => *x,
                _ => None,
            }
        }
    }

    pub fn set_eth_price(&self, price: Option<U256>) {
        if let Ok(mut x) = self.eth_price.write() {
            *x = price;
        }
    }

    pub fn calc_eth_value(&self, value: U256) -> Option<U256> {
        self.get_eth_price().map(|x| value.mul(ONE_ETHER).div(x))
    }

    pub fn calc_token_value_from_eth(&self, eth_value: U256) -> Option<U256> {
        let x = self.get_eth_price();
        x.map(|x| eth_value.mul(x).div(ONE_ETHER))
    }

    pub fn get_symbol(&self) -> String {
        self.symbol.clone().unwrap_or(self.address.to_string())
    }

    pub fn get_name(&self) -> String {
        self.name.clone().unwrap_or(self.address.to_string())
    }

    pub fn get_decimals(&self) -> u8 {
        self.decimals
    }

    pub fn get_exp(&self) -> U256 {
        if self.decimals == 18 {
            ONE_ETHER
        } else {
            U256::from(10).pow(U256::from(self.decimals))
        }
    }

    pub fn get_address(&self) -> Address {
        self.address
    }

    pub fn is_basic(&self) -> bool {
        self.basic
    }

    pub fn is_middle(&self) -> bool {
        self.middle
    }

    pub fn set_basic(&mut self) -> &mut Self {
        self.basic = true;
        self
    }

    pub fn set_middle(&mut self) -> &mut Self {
        self.middle = true;
        self
    }

    pub fn to_float(&self, value: U256) -> f64 {
        if self.decimals == 0 {
            0f64
        } else {
            let divider = self.get_exp();
            let ret = value.div_rem(divider);

            let div = u64::try_from(ret.0);
            let rem = u64::try_from(ret.1);

            if div.is_err() || rem.is_err() {
                0f64
            } else {
                div.unwrap_or_default() as f64 + ((rem.unwrap_or_default() as f64) / (10u64.pow(self.decimals as u32) as f64))
            }
        }
    }

    pub fn to_float_sign(&self, value: I256) -> f64 {
        let r: U256 = if value.is_positive() { value.into_raw() } else { value.neg().into_raw() };
        let f = self.to_float(r);
        if value.is_positive() {
            f
        } else {
            -f
        }
    }

    pub fn from_float(&self, value: f64) -> U256 {
        let multiplier = U256::from(value as i64);
        let modulus = U256::from(((value - value.round()) * (10 ^ self.decimals as i64) as f64) as u64);
        multiplier.mul(U256::from(10).pow(U256::from(self.decimals))).add(modulus)
    }

    pub fn is_weth(&self) -> bool {
        self.address == TokenAddress::WETH
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_to_float() {
        let weth_token = Token::new_with_data(TokenAddress::WETH, Some("WETH".to_string()), None, Some(18), true, false);
        let usdc_token = Token::new_with_data(TokenAddress::USDC, Some("USDC".to_string()), None, Some(6), false, false);
        let usdt_token = Token::new_with_data(TokenAddress::USDT, Some("USDT".to_string()), None, Some(6), false, false);
        let dai_token = Token::new_with_data(TokenAddress::DAI, Some("DAI".to_string()), None, Some(18), false, false);
        let wbtc_token = Token::new_with_data(TokenAddress::WBTC, Some("WBTC".to_string()), None, Some(8), false, false);

        let one_ether = U256::from(10).pow(U256::from(15));

        println!("{}", weth_token.to_float(one_ether));
    }
}