diff --git a/Cargo.lock b/Cargo.lock index ce02c694..b7663989 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7636,6 +7636,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "hex-literal", "libsecp256k1", "orml-currencies", "orml-tokens", diff --git a/node/src/chain_spec/metaverse.rs b/node/src/chain_spec/metaverse.rs index cc1fe36e..5ee320ce 100644 --- a/node/src/chain_spec/metaverse.rs +++ b/node/src/chain_spec/metaverse.rs @@ -17,8 +17,8 @@ use xcm::v3::{Junction, MultiLocation}; use metaverse_runtime::{ constants::currency::*, opaque::SessionKeys, wasm_binary_unwrap, AccountId, AssetManagerConfig, AuraConfig, BalancesConfig, BaseFeeConfig, CollatorSelectionConfig, DemocracyConfig, EVMConfig, EstateConfig, EvmChainIdConfig, - GenesisAccount, GenesisConfig, GrandpaConfig, MintingRateInfo, OracleMembershipConfig, SessionConfig, Signature, - SudoConfig, SystemConfig, + EvmMappingConfig, GenesisAccount, GenesisConfig, GrandpaConfig, MintingRateInfo, OracleMembershipConfig, + SessionConfig, Signature, SudoConfig, SystemConfig, }; use primitives::{AssetMetadata, Balance}; @@ -322,6 +322,10 @@ fn testnet_genesis( _marker: Default::default(), chain_id: 0x7fa, }, + evm_mapping: EvmMappingConfig { + _marker: Default::default(), + is_testnet_genesis: true, + }, asset_manager: AssetManagerConfig { _config: Default::default(), assets_info: vec![ diff --git a/pallets/evm-mapping/Cargo.toml b/pallets/evm-mapping/Cargo.toml index cf2d0cbd..53839644 100644 --- a/pallets/evm-mapping/Cargo.toml +++ b/pallets/evm-mapping/Cargo.toml @@ -9,6 +9,7 @@ serde = { workspace = true, optional = true } codec = { workspace = true, package = "parity-scale-codec" } libsecp256k1 = { workspace = true, optional = true } scale-info = { workspace = true } +hex-literal = { workspace = true } sp-core = { workspace = true } sp-io = { workspace = true } diff --git a/pallets/evm-mapping/src/lib.rs b/pallets/evm-mapping/src/lib.rs index 673206e1..a9810c00 100644 --- a/pallets/evm-mapping/src/lib.rs +++ b/pallets/evm-mapping/src/lib.rs @@ -35,6 +35,7 @@ use frame_support::{ transactional, }; use frame_system::{ensure_signed, pallet_prelude::*}; +use hex_literal::hex; use orml_traits::currency::TransferAll; pub use pallet::*; use primitives::{AccountIndex, EvmAddress}; @@ -107,6 +108,7 @@ pub trait AddressMapping { #[frame_support::pallet] pub mod pallet { use super::*; + use sp_core::H160; pub(crate) type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -180,6 +182,44 @@ pub mod pallet { #[pallet::getter(fn evm_addresses)] pub type EvmAddresses = StorageMap<_, Twox64Concat, T::AccountId, EvmAddress, OptionQuery>; + #[pallet::genesis_config] + #[derive(frame_support::DefaultNoBound)] + pub struct GenesisConfig { + pub is_testnet_genesis: bool, + pub _marker: PhantomData, + } + + #[pallet::genesis_build] + impl BuildGenesisConfig for GenesisConfig { + fn build(&self) { + if (self.is_testnet_genesis) { + // Add EVM address mapping for 5EMjsd14hMBsWVvC7bBaSC7FZQYxw1jQcZHA3Vho3pwtcbfM + let alice_evm = H160::from_slice(&hex_literal::hex!("d43593c715fdd31c61141abd04a99fd6822c8558")); + let alice_substrate = T::AddressMapping::get_account_id(&alice_evm); + + >::insert(alice_evm, &alice_substrate); + >::insert(&alice_substrate, alice_evm); + + >::deposit_event(Event::ClaimAccount { + account_id: alice_substrate, + evm_address: alice_evm, + }); + + // Add EVM address for metamask test account 5EMjsczhnpcwi7AcfreSB9vdvHzbcF4EBHNYjFNwooaQ9U2W + let test_account_evm = H160::from_slice(&hex_literal::hex!("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b")); + let test_account_substrate = T::AddressMapping::get_account_id(&test_account_evm); + + >::insert(test_account_evm, &test_account_substrate); + >::insert(&test_account_substrate, test_account_evm); + + >::deposit_event(Event::ClaimAccount { + account_id: test_account_substrate, + evm_address: test_account_evm, + }); + } + } + } + #[pallet::pallet] pub struct Pallet(_); diff --git a/runtime/metaverse/src/lib.rs b/runtime/metaverse/src/lib.rs index 91c2757c..452e0601 100644 --- a/runtime/metaverse/src/lib.rs +++ b/runtime/metaverse/src/lib.rs @@ -1565,7 +1565,7 @@ construct_runtime!( EVM: pallet_evm::{Pallet, Call, Storage, Config, Event}, Ethereum: pallet_ethereum::{Pallet, Call, Storage, Event, Config, Origin}, BaseFee: pallet_base_fee::{Pallet, Call, Storage, Config, Event}, - EvmMapping: evm_mapping::{Pallet, Call, Storage, Event}, + EvmMapping: evm_mapping::{Pallet, Call, Storage, Config, Event}, EvmChainId: pallet_evm_chain_id::{Pallet, Storage, Config}, // ink! Smart Contracts.