Skip to content

Commit

Permalink
chore: add evm conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Oct 25, 2024
1 parent 93ad2f5 commit fb6e909
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use pallet_ethereum::{
Call::transact, PostLogContent, Transaction as EthereumTransaction, TransactionAction,
TransactionData,
};
use pallet_evm::{Account as EVMAccount, FeeCalculator, HashedAddressMapping, Runner};
use pallet_evm::{
Account as EVMAccount, BalanceConverter, FeeCalculator, HashedAddressMapping, Runner,
};

// Subnet emission API
use pallet_subnet_emission_api::SubnetConsensus;
Expand Down Expand Up @@ -519,7 +521,7 @@ parameter_types! {
impl pallet_evm::Config for Runtime {
type FeeCalculator = BaseFee;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type BalanceConverter = ();
type BalanceConverter = EvmBalanceConverter;
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = pallet_evm::EnsureAddressTruncated;
Expand Down Expand Up @@ -635,6 +637,25 @@ impl<B: BlockT> fp_rpc::ConvertTransaction<<B as BlockT>::Extrinsic> for Transac
}
}

const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;

pub struct EvmBalanceConverter;

impl BalanceConverter for EvmBalanceConverter {
fn into_evm_balance(value: U256) -> Option<U256> {
U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(value))
.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
}

fn into_substrate_balance(value: U256) -> Option<U256> {
if value <= U256::from(u64::MAX) {
value.checked_div(U256::from(EVM_DECIMALS_FACTOR))
} else {
None
}
}
}

// The address format for describing accounts.
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
// Block header type as expected by this runtime.
Expand Down

0 comments on commit fb6e909

Please sign in to comment.