loom_evm_utils/
nweth.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
use alloy::primitives::{Address, U256};
use loom_defi_address_book::TokenAddress;
use std::ops::{Add, Mul};

pub struct NWETH {}

impl NWETH {
    const NWETH_EXP_U128: u128 = 10u128.pow(18);

    const NWETH_EXP: f64 = 10u64.pow(18) as f64;
    const GWEI_EXP_U128: u128 = 10u128.pow(9);
    const GWEI_EXP: f64 = 10u64.pow(9) as f64;
    const WEI_EXP_U128: u128 = 10u128.pow(18);
    const WEI_EXP: f64 = 10u64.pow(18) as f64;

    pub const ADDRESS: Address = TokenAddress::WETH;
    pub const NATIVE_ADDRESS: Address = Address::ZERO;

    #[inline]
    pub fn to_float(value: U256) -> f64 {
        let divider = U256::from(Self::NWETH_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) / Self::NWETH_EXP)
        }
    }

    #[inline]
    pub fn to_float_gwei(value: u128) -> f64 {
        let div = value / Self::GWEI_EXP_U128;
        let rem = value % Self::GWEI_EXP_U128;

        div as f64 + ((rem as f64) / Self::GWEI_EXP)
    }

    #[inline]
    pub fn to_float_wei(value: u128) -> f64 {
        let div = value / Self::WEI_EXP_U128;
        let rem = value % Self::WEI_EXP_U128;

        div as f64 + ((rem as f64) / Self::WEI_EXP)
    }

    #[inline]
    pub fn from_float(value: f64) -> U256 {
        let multiplier = U256::from(value as i64);
        let modulus = U256::from(((value - value.round()) * 10_i64.pow(18) as f64) as u64);
        multiplier.mul(U256::from(10).pow(U256::from(18))).add(modulus)
    }

    #[inline]
    pub fn get_exp() -> U256 {
        U256::from(Self::NWETH_EXP_U128)
    }

    #[inline]
    pub fn address() -> Address {
        Self::ADDRESS
    }
}