Skip to main content

avalanche_types/
units.rs

1//! Units for AVAX and other EVM-based subnets.
2
3use primitive_types::U256;
4
5pub const KIB: u64 = 1024; // 1 kibibyte
6pub const MIB: u64 = 1024 * KIB; // 1 mebibyte
7pub const GIB: u64 = 1024 * MIB; // 1 gibibyte
8
9pub const NANO_AVAX: u64 = 1;
10pub const MICRO_AVAX: u64 = 1000 * NANO_AVAX;
11pub const MILLI_AVAX: u64 = 1000 * MICRO_AVAX;
12
13/// On the X-Chain, one AVAX is 10^9 units.
14/// On the P-Chain, one AVAX is 10^9 units.
15/// ref. <https://snowtrace.io/unitconverter>
16pub const AVAX: u64 = 1000 * MILLI_AVAX;
17
18pub const KILO_AVAX: u64 = 1000 * AVAX;
19pub const MEGA_AVAX: u64 = 1000 * KILO_AVAX;
20
21/// On the C-Chain, one AVAX is 10^18 units.
22/// ref. <https://snowtrace.io/unitconverter>
23pub const AVAX_EVM_CHAIN: u64 = 1000 * MEGA_AVAX;
24
25/// Converts the nano AVAX to AVAX unit for X and P chain.
26pub fn cast_xp_navax_to_avax(navax: U256) -> u64 {
27    // ref. "ethers::utils::Units::Ether"
28    let avax_unit = U256::from(10).checked_pow(U256::from(9)).unwrap();
29    let avaxs = navax.checked_div(avax_unit).unwrap();
30    if avaxs >= U256::from(u64::MAX) {
31        u64::MAX
32    } else {
33        avaxs.as_u64()
34    }
35}
36
37/// RUST_LOG=debug cargo test --package avalanche-types --lib -- units::test_cast_xp_navax_to_avax --exact --show-output
38#[test]
39fn test_cast_xp_navax_to_avax() {
40    assert_eq!(cast_xp_navax_to_avax(U256::max_value()), u64::MAX);
41    assert_eq!(cast_xp_navax_to_avax(U256::from(u64::MAX)), 18446744073);
42    assert_eq!(cast_xp_navax_to_avax(U256::from(100)), 0);
43}
44
45/// Converts the X/P chain AVAX unit to nano-AVAX.
46/// On the X and P-Chain, one AVAX is 10^9 units.
47/// ref. <https://snowtrace.io/unitconverter>
48/// If it overflows, it resets to U256::MAX.
49pub fn cast_avax_to_xp_navax(avax: U256) -> U256 {
50    // ref. "ethers::utils::Units::Ether"
51    let avax_unit = U256::from(10).checked_pow(U256::from(9)).unwrap();
52    if let Some(navaxs) = avax.checked_mul(avax_unit) {
53        navaxs
54    } else {
55        U256::max_value()
56    }
57}
58
59/// RUST_LOG=debug cargo test --package avalanche-types --lib -- units::test_cast_avax_to_xp_navax --exact --show-output
60#[test]
61fn test_cast_avax_to_xp_navax() {
62    assert_eq!(cast_avax_to_xp_navax(U256::max_value()), U256::max_value());
63    assert_eq!(
64        cast_avax_to_xp_navax(U256::from(1)),
65        U256::from_dec_str("1000000000").unwrap()
66    );
67    assert_eq!(
68        cast_avax_to_xp_navax(U256::from(10)),
69        U256::from_dec_str("10000000000").unwrap()
70    );
71    assert_eq!(
72        cast_avax_to_xp_navax(U256::from(500)),
73        U256::from_dec_str("500000000000").unwrap()
74    );
75}
76
77/// Converts the nano AVAX to AVAX/i64 unit for C-chain and other EVM-based subnets.
78///
79/// On the C-Chain, one AVAX is 10^18 units.
80/// ref. <https://snowtrace.io/unitconverter>
81///
82/// If it overflows, it resets to i64::MAX.
83pub fn cast_evm_navax_to_avax_i64(navax: U256) -> i64 {
84    // ref. "ethers::utils::Units::Ether"
85    let avax_unit = U256::from(10).checked_pow(U256::from(18)).unwrap();
86    let avaxs = navax.checked_div(avax_unit).unwrap();
87    if avaxs >= U256::from(u64::MAX) {
88        i64::MAX
89    } else {
90        let converted = avaxs.as_u64();
91        if converted >= i64::MAX as u64 {
92            i64::MAX
93        } else {
94            converted as i64
95        }
96    }
97}
98
99/// RUST_LOG=debug cargo test --package avalanche-types --lib -- units::test_cast_evm_navax_to_avax_i64 --exact --show-output
100#[test]
101fn test_cast_evm_navax_to_avax_i64() {
102    assert_eq!(cast_evm_navax_to_avax_i64(U256::max_value()), i64::MAX);
103    assert_eq!(cast_evm_navax_to_avax_i64(U256::from(i64::MAX)), 9);
104    assert_eq!(cast_evm_navax_to_avax_i64(U256::from(100)), 0);
105}
106
107/// Converts the EVM AVAX unit to nano-AVAX.
108/// On the C-Chain, one AVAX is 10^18 units.
109/// ref. <https://snowtrace.io/unitconverter>
110/// If it overflows, it resets to U256::MAX.
111pub fn cast_avax_to_evm_navax(avax: U256) -> U256 {
112    // ref. "ethers::utils::Units::Ether"
113    let avax_unit = U256::from(10).checked_pow(U256::from(18)).unwrap();
114    if let Some(navaxs) = avax.checked_mul(avax_unit) {
115        navaxs
116    } else {
117        U256::max_value()
118    }
119}
120
121/// RUST_LOG=debug cargo test --package avalanche-types --lib -- units::test_cast_avax_to_evm_navax --exact --show-output
122#[test]
123fn test_cast_avax_to_evm_navax() {
124    assert_eq!(cast_avax_to_evm_navax(U256::max_value()), U256::max_value());
125    assert_eq!(
126        cast_avax_to_evm_navax(U256::from(1)),
127        U256::from_dec_str("1000000000000000000").unwrap()
128    );
129    assert_eq!(
130        cast_avax_to_evm_navax(U256::from(10)),
131        U256::from_dec_str("10000000000000000000").unwrap()
132    );
133    assert_eq!(
134        cast_avax_to_evm_navax(U256::from(500)),
135        U256::from_dec_str("500000000000000000000").unwrap()
136    );
137}