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
use primitive_types::U256;
pub const NANO_AVAX: u64 = 1;
pub const MICRO_AVAX: u64 = 1000 * NANO_AVAX;
pub const MILLI_AVAX: u64 = 1000 * MICRO_AVAX;
pub const AVAX: u64 = 1000 * MILLI_AVAX;
pub const KILO_AVAX: u64 = 1000 * AVAX;
pub const MEGA_AVAX: u64 = 1000 * KILO_AVAX;
pub const AVAX_EVM_CHAIN: u64 = 1000 * MEGA_AVAX;
pub fn cast_xp_navax_to_avax(navax: U256) -> u64 {
let avax_unit = U256::from(10).checked_pow(U256::from(9)).unwrap();
let avaxs = navax.checked_div(avax_unit).unwrap();
if avaxs >= U256::from(u64::MAX) {
u64::MAX
} else {
let converted = avaxs.as_u64();
if converted >= u64::MAX as u64 {
u64::MAX
} else {
converted as u64
}
}
}
#[test]
fn test_cast_xp_navax_to_avax() {
assert_eq!(cast_xp_navax_to_avax(U256::max_value()), u64::MAX);
assert_eq!(cast_xp_navax_to_avax(U256::from(u64::MAX)), 18446744073);
assert_eq!(cast_xp_navax_to_avax(U256::from(100)), 0);
}
pub fn cast_avax_to_xp_navax(avax: U256) -> U256 {
let avax_unit = U256::from(10).checked_pow(U256::from(9)).unwrap();
if let Some(navaxs) = avax.checked_mul(avax_unit) {
navaxs
} else {
U256::max_value()
}
}
#[test]
fn test_cast_avax_to_xp_navax() {
assert_eq!(cast_avax_to_xp_navax(U256::max_value()), U256::max_value());
assert_eq!(
cast_avax_to_xp_navax(U256::from(1)),
U256::from_dec_str("1000000000").unwrap()
);
assert_eq!(
cast_avax_to_xp_navax(U256::from(10)),
U256::from_dec_str("10000000000").unwrap()
);
assert_eq!(
cast_avax_to_xp_navax(U256::from(500)),
U256::from_dec_str("500000000000").unwrap()
);
}
pub fn cast_evm_navax_to_avax_i64(navax: U256) -> i64 {
let avax_unit = U256::from(10).checked_pow(U256::from(18)).unwrap();
let avaxs = navax.checked_div(avax_unit).unwrap();
if avaxs >= U256::from(u64::MAX) {
i64::MAX
} else {
let converted = avaxs.as_u64();
if converted >= i64::MAX as u64 {
i64::MAX
} else {
converted as i64
}
}
}
#[test]
fn test_cast_evm_navax_to_avax_i64() {
assert_eq!(cast_evm_navax_to_avax_i64(U256::max_value()), i64::MAX);
assert_eq!(cast_evm_navax_to_avax_i64(U256::from(i64::MAX)), 9);
assert_eq!(cast_evm_navax_to_avax_i64(U256::from(100)), 0);
}
pub fn cast_avax_to_evm_navax(avax: U256) -> U256 {
let avax_unit = U256::from(10).checked_pow(U256::from(18)).unwrap();
if let Some(navaxs) = avax.checked_mul(avax_unit) {
navaxs
} else {
U256::max_value()
}
}
#[test]
fn test_cast_avax_to_evm_navax() {
assert_eq!(cast_avax_to_evm_navax(U256::max_value()), U256::max_value());
assert_eq!(
cast_avax_to_evm_navax(U256::from(1)),
U256::from_dec_str("1000000000000000000").unwrap()
);
assert_eq!(
cast_avax_to_evm_navax(U256::from(10)),
U256::from_dec_str("10000000000000000000").unwrap()
);
assert_eq!(
cast_avax_to_evm_navax(U256::from(500)),
U256::from_dec_str("500000000000000000000").unwrap()
);
}