@@ -1121,15 +1121,26 @@ const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;
1121
1121
pub struct SubtensorEvmBalanceConverter ;
1122
1122
1123
1123
impl BalanceConverter for SubtensorEvmBalanceConverter {
1124
+ /// Convert from Substrate balance (u64) to EVM balance (U256)
1124
1125
fn into_evm_balance ( value : U256 ) -> Option < U256 > {
1125
- value. checked_mul ( U256 :: from ( EVM_DECIMALS_FACTOR ) )
1126
+ value
1127
+ . checked_mul ( U256 :: from ( EVM_DECIMALS_FACTOR ) )
1128
+ . and_then ( |evm_value| {
1129
+ // Ensure the result fits within the maximum U256 value
1130
+ if evm_value <= U256 :: MAX {
1131
+ Some ( evm_value)
1132
+ } else {
1133
+ None
1134
+ }
1135
+ } )
1126
1136
}
1127
1137
1138
+ /// Convert from EVM balance (U256) to Substrate balance (u64)
1128
1139
fn into_substrate_balance ( value : U256 ) -> Option < U256 > {
1129
1140
value
1130
1141
. checked_div ( U256 :: from ( EVM_DECIMALS_FACTOR ) )
1131
1142
. and_then ( |substrate_value| {
1132
- // Ensure the result fits within the Subtensor Balance type (u64)
1143
+ // Ensure the result fits within the TAO balance type (u64)
1133
1144
if substrate_value <= U256 :: from ( u64:: MAX ) {
1134
1145
Some ( substrate_value)
1135
1146
} else {
@@ -2008,9 +2019,6 @@ impl_runtime_apis! {
2008
2019
}
2009
2020
}
2010
2021
2011
- // #[cfg(test)]
2012
- // mod tests {
2013
-
2014
2022
#[ test]
2015
2023
fn check_whitelist ( ) {
2016
2024
use crate :: * ;
@@ -2033,4 +2041,72 @@ fn check_whitelist() {
2033
2041
// System Events
2034
2042
assert ! ( whitelist. contains( "26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7" ) ) ;
2035
2043
}
2036
- // }
2044
+
2045
+ #[ test]
2046
+ fn test_into_substrate_balance_valid ( ) {
2047
+ // Valid conversion within u64 range
2048
+ let evm_balance = U256 :: from ( 1_000_000_000_000_000_000u128 ) ; // 1 TAO in EVM
2049
+ let expected_substrate_balance = U256 :: from ( 1_000_000_000u128 ) ; // 1 TAO in Substrate
2050
+
2051
+ let result = SubtensorEvmBalanceConverter :: into_substrate_balance ( evm_balance) ;
2052
+ assert_eq ! ( result, Some ( expected_substrate_balance) ) ;
2053
+ }
2054
+
2055
+ #[ test]
2056
+ fn test_into_substrate_balance_large_value ( ) {
2057
+ // Maximum valid balance for u64
2058
+ let evm_balance = U256 :: from ( u64:: MAX ) * U256 :: from ( EVM_DECIMALS_FACTOR ) ; // Max u64 TAO in EVM
2059
+ let expected_substrate_balance = U256 :: from ( u64:: MAX ) ;
2060
+
2061
+ let result = SubtensorEvmBalanceConverter :: into_substrate_balance ( evm_balance) ;
2062
+ assert_eq ! ( result, Some ( expected_substrate_balance) ) ;
2063
+ }
2064
+
2065
+ #[ test]
2066
+ fn test_into_substrate_balance_exceeds_u64 ( ) {
2067
+ // EVM balance that exceeds u64 after conversion
2068
+ let evm_balance = ( U256 :: from ( u64:: MAX ) + U256 :: from ( 1 ) ) * U256 :: from ( EVM_DECIMALS_FACTOR ) ;
2069
+
2070
+ let result = SubtensorEvmBalanceConverter :: into_substrate_balance ( evm_balance) ;
2071
+ assert_eq ! ( result, None ) ; // Exceeds u64, should return None
2072
+ }
2073
+
2074
+ #[ test]
2075
+ fn test_into_substrate_balance_precision_loss ( ) {
2076
+ // EVM balance with precision loss
2077
+ let evm_balance = U256 :: from ( 1_000_000_000_123_456_789u128 ) ; // 1 TAO + extra precision in EVM
2078
+ let expected_substrate_balance = U256 :: from ( 1_000_000_000u128 ) ; // Truncated to 1 TAO in Substrate
2079
+
2080
+ let result = SubtensorEvmBalanceConverter :: into_substrate_balance ( evm_balance) ;
2081
+ assert_eq ! ( result, Some ( expected_substrate_balance) ) ;
2082
+ }
2083
+
2084
+ #[ test]
2085
+ fn test_into_substrate_balance_zero_value ( ) {
2086
+ // Zero balance should convert to zero
2087
+ let evm_balance = U256 :: from ( 0 ) ;
2088
+ let expected_substrate_balance = U256 :: from ( 0 ) ;
2089
+
2090
+ let result = SubtensorEvmBalanceConverter :: into_substrate_balance ( evm_balance) ;
2091
+ assert_eq ! ( result, Some ( expected_substrate_balance) ) ;
2092
+ }
2093
+
2094
+ #[ test]
2095
+ fn test_into_evm_balance_valid ( ) {
2096
+ // Valid conversion from Substrate to EVM
2097
+ let substrate_balance = U256 :: from ( 1_000_000_000u128 ) ; // 1 TAO in Substrate
2098
+ let expected_evm_balance = U256 :: from ( 1_000_000_000_000_000_000u128 ) ; // 1 TAO in EVM
2099
+
2100
+ let result = SubtensorEvmBalanceConverter :: into_evm_balance ( substrate_balance) ;
2101
+ assert_eq ! ( result, Some ( expected_evm_balance) ) ;
2102
+ }
2103
+
2104
+ #[ test]
2105
+ fn test_into_evm_balance_overflow ( ) {
2106
+ // Substrate balance larger than u64::MAX but valid within U256
2107
+ let substrate_balance = U256 :: from ( u64:: MAX ) + U256 :: from ( 1 ) ; // Large balance
2108
+ let expected_evm_balance = substrate_balance * U256 :: from ( EVM_DECIMALS_FACTOR ) ;
2109
+
2110
+ let result = SubtensorEvmBalanceConverter :: into_evm_balance ( substrate_balance) ;
2111
+ assert_eq ! ( result, Some ( expected_evm_balance) ) ; // Should return the scaled value
2112
+ }
0 commit comments