Skip to content

Commit

Permalink
added pallet_evm genesis state for local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chexware committed Apr 10, 2024
1 parent ce4b2f4 commit 6580581
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions node/src/chain_spec/metaverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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![
Expand Down
1 change: 1 addition & 0 deletions pallets/evm-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
40 changes: 40 additions & 0 deletions pallets/evm-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -107,6 +108,7 @@ pub trait AddressMapping<AccountId> {
#[frame_support::pallet]
pub mod pallet {
use super::*;
use sp_core::H160;

pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -180,6 +182,44 @@ pub mod pallet {
#[pallet::getter(fn evm_addresses)]
pub type EvmAddresses<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, EvmAddress, OptionQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub is_testnet_genesis: bool,
pub _marker: PhantomData<T>,
}

#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
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);

<Accounts<T>>::insert(alice_evm, &alice_substrate);
<EvmAddresses<T>>::insert(&alice_substrate, alice_evm);

<Pallet<T>>::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);

<Accounts<T>>::insert(test_account_evm, &test_account_substrate);
<EvmAddresses<T>>::insert(&test_account_substrate, test_account_evm);

<Pallet<T>>::deposit_event(Event::ClaimAccount {
account_id: test_account_substrate,
evm_address: test_account_evm,
});
}
}
}

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand Down
2 changes: 1 addition & 1 deletion runtime/metaverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ construct_runtime!(
EVM: pallet_evm::{Pallet, Call, Storage, Config<T>, Event<T>},
Ethereum: pallet_ethereum::{Pallet, Call, Storage, Event, Config<T>, Origin},
BaseFee: pallet_base_fee::{Pallet, Call, Storage, Config<T>, Event},
EvmMapping: evm_mapping::{Pallet, Call, Storage, Event<T>},
EvmMapping: evm_mapping::{Pallet, Call, Storage, Config<T>, Event<T>},
EvmChainId: pallet_evm_chain_id::{Pallet, Storage, Config<T>},

// ink! Smart Contracts.
Expand Down

0 comments on commit 6580581

Please sign in to comment.