1use primitive_types::U256;
4
5pub const KIB: u64 = 1024; pub const MIB: u64 = 1024 * KIB; pub const GIB: u64 = 1024 * MIB; pub const NANO_AVAX: u64 = 1;
10pub const MICRO_AVAX: u64 = 1000 * NANO_AVAX;
11pub const MILLI_AVAX: u64 = 1000 * MICRO_AVAX;
12
13pub const AVAX: u64 = 1000 * MILLI_AVAX;
17
18pub const KILO_AVAX: u64 = 1000 * AVAX;
19pub const MEGA_AVAX: u64 = 1000 * KILO_AVAX;
20
21pub const AVAX_EVM_CHAIN: u64 = 1000 * MEGA_AVAX;
24
25pub fn cast_xp_navax_to_avax(navax: U256) -> u64 {
27 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#[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
45pub fn cast_avax_to_xp_navax(avax: U256) -> U256 {
50 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#[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
77pub fn cast_evm_navax_to_avax_i64(navax: U256) -> i64 {
84 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#[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
107pub fn cast_avax_to_evm_navax(avax: U256) -> U256 {
112 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#[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}