From 7fc58aab0106dc49d5eb0c3c466f7ef6de6467f8 Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 17 Jan 2025 00:34:12 +0800 Subject: [PATCH] Create wrapper types for EVM and Substrate balances --- frame/ethereum/src/mock.rs | 12 +- frame/evm/precompile/dispatch/src/mock.rs | 13 +- .../precompile/storage-cleaner/src/mock.rs | 12 +- frame/evm/src/lib.rs | 118 +++++++++++++----- frame/evm/src/mock.rs | 13 +- frame/evm/src/runner/stack.rs | 14 +-- frame/evm/src/tests.rs | 6 +- precompiles/tests-external/lib.rs | 12 +- template/runtime/src/lib.rs | 13 +- 9 files changed, 135 insertions(+), 78 deletions(-) diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index c74ca34ec2..f100c53752 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -32,7 +32,7 @@ use sp_runtime::{ AccountId32, BuildStorage, }; // Frontier -use pallet_evm::{AddressMapping, BalanceConverter, EnsureAddressTruncated, FeeCalculator}; +use pallet_evm::{AddressMapping, BalanceConverter, EnsureAddressTruncated, EvmBalance, FeeCalculator, SubstrateBalance}; use super::*; use crate::IntermediateStateRoot; @@ -163,13 +163,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -177,13 +178,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None } diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index 8b20e0074a..5c7244252b 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -29,8 +29,7 @@ use sp_runtime::traits::{BlakeTwo256, IdentityLookup}; use fp_evm::{ExitError, ExitReason, Transfer}; use pallet_evm::{ - BalanceConverter, Context, EnsureAddressNever, EnsureAddressRoot, FeeCalculator, - IdentityAddressMapping, PrecompileHandle, + BalanceConverter, Context, EnsureAddressNever, EnsureAddressRoot, EvmBalance, FeeCalculator, IdentityAddressMapping, PrecompileHandle, SubstrateBalance }; frame_support::construct_runtime! { @@ -126,13 +125,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -140,13 +140,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None } diff --git a/frame/evm/precompile/storage-cleaner/src/mock.rs b/frame/evm/precompile/storage-cleaner/src/mock.rs index 488a93ea1c..a75f813cfb 100644 --- a/frame/evm/precompile/storage-cleaner/src/mock.rs +++ b/frame/evm/precompile/storage-cleaner/src/mock.rs @@ -19,7 +19,7 @@ use crate::{StorageCleanerPrecompile, StorageCleanerPrecompileCall}; use frame_support::{parameter_types, weights::Weight}; -use pallet_evm::{BalanceConverter, EnsureAddressNever, EnsureAddressRoot, IdentityAddressMapping}; +use pallet_evm::{BalanceConverter, EnsureAddressNever, EnsureAddressRoot, EvmBalance, IdentityAddressMapping, SubstrateBalance}; use precompile_utils::{precompile_set::*, testing::*}; use sp_core::{ConstU32, H256, U256}; use sp_runtime::{ @@ -128,13 +128,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -142,13 +143,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None } diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index de839396ab..cced5d4633 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -950,14 +950,14 @@ impl Pallet { let nonce = frame_system::Pallet::::account_nonce(&account_id); let balance = T::Currency::reducible_balance(&account_id, Preservation::Preserve, Fortitude::Polite); - let balance_u256 = U256::from(UniqueSaturatedInto::::unique_saturated_into(balance)); + let balance_sub = SubstrateBalance::from(UniqueSaturatedInto::::unique_saturated_into(balance)); let balance_eth = - T::BalanceConverter::into_evm_balance(balance_u256).unwrap_or(U256::from(0)); + T::BalanceConverter::into_evm_balance(balance_sub).unwrap_or(EvmBalance::from(0)); ( Account { nonce: U256::from(UniqueSaturatedInto::::unique_saturated_into(nonce)), - balance: balance_eth, + balance: balance_eth.into(), }, T::DbWeight::get().reads(2), ) @@ -979,7 +979,7 @@ pub trait OnChargeEVMTransaction { /// Before the transaction is executed the payment of the transaction fees /// need to be secured. - fn withdraw_fee(who: &H160, fee: U256) -> Result>; + fn withdraw_fee(who: &H160, fee: EvmBalance) -> Result>; /// After the transaction was executed the actual fee can be calculated. /// This function should refund any overpaid fees and optionally deposit @@ -988,8 +988,8 @@ pub trait OnChargeEVMTransaction { /// Returns the `NegativeImbalance` - if any - produced by the priority fee. fn correct_and_deposit_fee( who: &H160, - corrected_fee: U256, - base_fee: U256, + corrected_fee: EvmBalance, + base_fee: EvmBalance, already_withdrawn: Self::LiquidityInfo, ) -> Self::LiquidityInfo; @@ -1021,8 +1021,8 @@ where // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; - fn withdraw_fee(who: &H160, fee: U256) -> Result> { - if fee.is_zero() { + fn withdraw_fee(who: &H160, fee: EvmBalance) -> Result> { + if fee.0.is_zero() { return Ok(None); } let account_id = T::AddressMapping::into_account_id(*who); @@ -1033,7 +1033,7 @@ where let imbalance = C::withdraw( &account_id, - fee_sub.unique_saturated_into(), + fee_sub.0.unique_saturated_into(), WithdrawReasons::FEE, ExistenceRequirement::AllowDeath, ) @@ -1043,8 +1043,8 @@ where fn correct_and_deposit_fee( who: &H160, - corrected_fee: U256, - base_fee: U256, + corrected_fee: EvmBalance, + base_fee: EvmBalance, already_withdrawn: Self::LiquidityInfo, // Expects substrate balance ) -> Self::LiquidityInfo { if let Some(paid) = already_withdrawn { @@ -1052,12 +1052,12 @@ where // Convert corrected fee into substrate balance let corrected_fee_sub = - T::BalanceConverter::into_substrate_balance(corrected_fee).unwrap_or(U256::from(0)); + T::BalanceConverter::into_substrate_balance(corrected_fee).unwrap_or(SubstrateBalance::from(0)); // Calculate how much refund we should return let refund_amount = paid .peek() - .saturating_sub(corrected_fee_sub.unique_saturated_into()); + .saturating_sub(corrected_fee_sub.0.unique_saturated_into()); // refund to the account that paid the fees. If this fails, the // account might have dropped below the existential balance. In @@ -1091,9 +1091,9 @@ where // Convert base fee into substrate balance let base_fee_sub = - T::BalanceConverter::into_substrate_balance(base_fee).unwrap_or(U256::from(0)); + T::BalanceConverter::into_substrate_balance(base_fee).unwrap_or(SubstrateBalance::from(0)); - let (base_fee, tip) = adjusted_paid.split(base_fee_sub.unique_saturated_into()); + let (base_fee, tip) = adjusted_paid.split(base_fee_sub.0.unique_saturated_into()); // Handle base fee. Can be either burned, rationed, etc ... OU::on_unbalanced(base_fee); return Some(tip); // Returns substrate balance @@ -1128,8 +1128,8 @@ where // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; - fn withdraw_fee(who: &H160, fee: U256) -> Result> { - if fee.is_zero() { + fn withdraw_fee(who: &H160, fee: EvmBalance) -> Result> { + if fee.0.is_zero() { return Ok(None); } let account_id = T::AddressMapping::into_account_id(*who); @@ -1140,7 +1140,7 @@ where let imbalance = F::withdraw( &account_id, - fee_sub.unique_saturated_into(), + fee_sub.0.unique_saturated_into(), Precision::Exact, Preservation::Preserve, Fortitude::Polite, @@ -1151,8 +1151,8 @@ where fn correct_and_deposit_fee( who: &H160, - corrected_fee: U256, - base_fee: U256, + corrected_fee: EvmBalance, + base_fee: EvmBalance, already_withdrawn: Self::LiquidityInfo, // Expects substrate balance ) -> Self::LiquidityInfo { if let Some(paid) = already_withdrawn { @@ -1160,12 +1160,12 @@ where // Convert corrected fee into substrate balance let corrected_fee_sub = - T::BalanceConverter::into_substrate_balance(corrected_fee).unwrap_or(U256::from(0)); + T::BalanceConverter::into_substrate_balance(corrected_fee).unwrap_or(SubstrateBalance::from(0)); // Calculate how much refund we should return let refund_amount = paid .peek() - .saturating_sub(corrected_fee_sub.unique_saturated_into()); + .saturating_sub(corrected_fee_sub.0.unique_saturated_into()); // refund to the account that paid the fees. let refund_imbalance = F::deposit(&account_id, refund_amount, Precision::BestEffort) .unwrap_or_else(|_| Debt::::zero()); @@ -1178,9 +1178,9 @@ where // Convert base fee into substrate balance let base_fee_sub = - T::BalanceConverter::into_substrate_balance(base_fee).unwrap_or(U256::from(0)); + T::BalanceConverter::into_substrate_balance(base_fee).unwrap_or(SubstrateBalance::from(0)); - let (base_fee, tip) = adjusted_paid.split(base_fee_sub.unique_saturated_into()); + let (base_fee, tip) = adjusted_paid.split(base_fee_sub.0.unique_saturated_into()); // Handle base fee. Can be either burned, rationed, etc ... OU::on_unbalanced(base_fee); return Some(tip); // Returns substrate balance @@ -1210,14 +1210,14 @@ where // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; - fn withdraw_fee(who: &H160, fee: U256) -> Result> { + fn withdraw_fee(who: &H160, fee: EvmBalance) -> Result> { EVMFungibleAdapter::::withdraw_fee(who, fee) } fn correct_and_deposit_fee( who: &H160, - corrected_fee: U256, - base_fee: U256, + corrected_fee: EvmBalance, + base_fee: EvmBalance, already_withdrawn: Self::LiquidityInfo, ) -> Self::LiquidityInfo { as OnChargeEVMTransaction>::correct_and_deposit_fee( @@ -1250,22 +1250,72 @@ impl OnCreate for Tuple { } } +#[derive(Clone, Copy)] +pub struct SubstrateBalance(U256); + +impl SubstrateBalance { + pub fn new(value: U256) -> Self { + SubstrateBalance(value) + } + + pub fn into_u256(self) -> U256 { + self.0 + } +} + +impl From for SubstrateBalance { + fn from(value: u128) -> Self { + SubstrateBalance(U256::from(value)) + } +} + +impl From for U256 { + fn from(value: SubstrateBalance) -> Self { + value.0 + } +} + +#[derive(Clone, Copy)] +pub struct EvmBalance(U256); + +impl EvmBalance { + pub fn new(value: U256) -> Self { + EvmBalance(value) + } + + pub fn into_u256(self) -> U256 { + self.0 + } +} + +impl From for EvmBalance { + fn from(value: u128) -> Self { + EvmBalance(U256::from(value)) + } +} + +impl From for U256 { + fn from(value: EvmBalance) -> Self { + value.0 + } +} + pub trait BalanceConverter { /// Convert from Substrate balance to EVM balance (U256) with correct decimals - fn into_evm_balance(value: U256) -> Option; + fn into_evm_balance(value: SubstrateBalance) -> Option; /// Convert from EVM (U256) balance to Substrate balance with correct decimals - fn into_substrate_balance(value: U256) -> Option; + fn into_substrate_balance(value: EvmBalance) -> Option; } impl BalanceConverter for () { - fn into_evm_balance(value: U256) -> Option { - Some(U256::from( - UniqueSaturatedInto::::unique_saturated_into(value), + fn into_evm_balance(value: SubstrateBalance) -> Option { + Some(EvmBalance::from( + UniqueSaturatedInto::::unique_saturated_into(value.0), )) } - fn into_substrate_balance(value: U256) -> Option { - Some(value) + fn into_substrate_balance(value: EvmBalance) -> Option { + Some(SubstrateBalance(value.0)) } } diff --git a/frame/evm/src/mock.rs b/frame/evm/src/mock.rs index 2f42a8bef8..32da2c5960 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -30,8 +30,7 @@ use sp_runtime::{ }; use crate::{ - BalanceConverter, EnsureAddressNever, EnsureAddressRoot, FeeCalculator, IdentityAddressMapping, - IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, PrecompileSet, + BalanceConverter, EnsureAddressNever, EnsureAddressRoot, EvmBalance, FeeCalculator, IdentityAddressMapping, IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, PrecompileSet, SubstrateBalance }; frame_support::construct_runtime! { @@ -139,13 +138,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -153,13 +153,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None } diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index ea2eada195..69f1d1c850 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -47,9 +47,7 @@ use fp_evm::{ }; use crate::{ - runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountStorages, AddressMapping, - BalanceConverter, BalanceOf, BlockHashMapping, Config, Error, Event, FeeCalculator, - OnChargeEVMTransaction, OnCreate, Pallet, RunnerError, + runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountStorages, AddressMapping, BalanceConverter, BalanceOf, BlockHashMapping, Config, Error, Event, EvmBalance, FeeCalculator, OnChargeEVMTransaction, OnCreate, Pallet, RunnerError }; #[cfg(feature = "forbid-evm-reentrancy")] @@ -231,7 +229,7 @@ where // Deduct fee from the `source` account. Returns `None` if `total_fee` is Zero. // === Note: This fee gets converted to substrate decimals in `withdraw_fee` === - let fee = T::OnChargeTransaction::withdraw_fee(&source, total_fee) + let fee = T::OnChargeTransaction::withdraw_fee(&source, EvmBalance::new(total_fee)) .map_err(|e| RunnerError { error: e, weight })?; // Execute the EVM call. @@ -301,9 +299,9 @@ where let actual_priority_fee = T::OnChargeTransaction::correct_and_deposit_fee( &source, // Actual fee after evm execution, including tip. - actual_fee, + EvmBalance::new(actual_fee), // Base fee. - actual_base_fee, + EvmBalance::new(actual_base_fee), // Fee initially withdrawn. fee, ); @@ -927,13 +925,13 @@ where let target = T::AddressMapping::into_account_id(transfer.target); // Adjust decimals - let value_sub = T::BalanceConverter::into_substrate_balance(transfer.value) + let value_sub = T::BalanceConverter::into_substrate_balance(EvmBalance::new(transfer.value)) .ok_or(ExitError::OutOfFund)?; T::Currency::transfer( &source, &target, - value_sub.unique_saturated_into(), + value_sub.0.unique_saturated_into(), ExistenceRequirement::AllowDeath, ) .map_err(|_| ExitError::OutOfFund) diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 07da3e7720..5601538364 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -737,11 +737,11 @@ fn fee_deduction() { assert_eq!(Balances::free_balance(substrate_addr), 100); // Deduct fees as 10 units - let imbalance = <::OnChargeTransaction as OnChargeEVMTransaction>::withdraw_fee(&evm_addr, U256::from(10e9 as u64)).unwrap(); + let imbalance = <::OnChargeTransaction as OnChargeEVMTransaction>::withdraw_fee(&evm_addr, EvmBalance::from(10e9 as u128)).unwrap(); assert_eq!(Balances::free_balance(substrate_addr), 90); // Refund fees as 5 units - <::OnChargeTransaction as OnChargeEVMTransaction>::correct_and_deposit_fee(&evm_addr, U256::from(5e9 as u64), U256::from(5e9 as u64), imbalance); + <::OnChargeTransaction as OnChargeEVMTransaction>::correct_and_deposit_fee(&evm_addr, EvmBalance::from(5e9 as u128), EvmBalance::from(5e9 as u128), imbalance); assert_eq!(Balances::free_balance(substrate_addr), 95); }); } @@ -789,7 +789,7 @@ fn ed_0_refund_patch_is_required() { let _ = <::OnChargeTransaction as OnChargeEVMTransaction>::withdraw_fee( &evm_addr, - U256::from(100), + EvmBalance::from(100), ) .unwrap(); assert_eq!(Balances::free_balance(substrate_addr), 0); diff --git a/precompiles/tests-external/lib.rs b/precompiles/tests-external/lib.rs index 7d83e0e3f6..a931b949c5 100644 --- a/precompiles/tests-external/lib.rs +++ b/precompiles/tests-external/lib.rs @@ -34,7 +34,7 @@ use sp_runtime::{ }; // Frontier use fp_evm::{ExitReason, ExitRevert, PrecompileFailure, PrecompileHandle}; -use pallet_evm::{BalanceConverter, EnsureAddressNever, EnsureAddressRoot}; +use pallet_evm::{BalanceConverter, EnsureAddressNever, EnsureAddressRoot, EvmBalance, SubstrateBalance}; use precompile_utils::{ precompile_set::*, solidity::{codec::Writer, revert::revert}, @@ -234,13 +234,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -248,13 +249,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None } diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index a639d1ed43..64c536a1e3 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -52,8 +52,7 @@ use fp_evm::weight_per_gas; use fp_rpc::TransactionStatus; use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction}; use pallet_evm::{ - Account as EVMAccount, BalanceConverter, EnsureAccountId20, FeeCalculator, - IdentityAddressMapping, Runner, + Account as EVMAccount, BalanceConverter, EnsureAccountId20, EvmBalance, FeeCalculator, IdentityAddressMapping, Runner, SubstrateBalance }; // A few exports that help ease life for downstream crates. @@ -353,13 +352,14 @@ pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { /// Convert from Substrate balance (u64) to EVM balance (U256) - fn into_evm_balance(value: U256) -> Option { + fn into_evm_balance(value: SubstrateBalance) -> Option { value + .into_u256() .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|evm_value| { // Ensure the result fits within the maximum U256 value if evm_value <= U256::MAX { - Some(evm_value) + Some(EvmBalance::new(evm_value)) } else { None } @@ -367,13 +367,14 @@ impl BalanceConverter for SubtensorEvmBalanceConverter { } /// Convert from EVM balance (U256) to Substrate balance (u64) - fn into_substrate_balance(value: U256) -> Option { + fn into_substrate_balance(value: EvmBalance) -> Option { value + .into_u256() .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { - Some(substrate_value) + Some(SubstrateBalance::new(substrate_value)) } else { None }