1234567891011121314151617#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]19#![deny(missing_docs)]2021pub use pallet::*;22#[cfg(feature = "runtime-benchmarks")]23pub mod benchmarking;24pub mod weights;2526#[frame_support::pallet]27pub mod pallet {28 use frame_support::{29 pallet_prelude::{*, DispatchResult},30 traits::IsType,31 };32 use frame_system::pallet_prelude::{*, OriginFor};33 use sp_core::{H160, H256};34 use sp_std::vec::Vec;35 use super::weights::WeightInfo;36 use pallet_evm::{PrecompileHandle, Pallet as PalletEvm};3738 #[pallet::config]39 pub trait Config: frame_system::Config + pallet_evm::Config {40 41 type WeightInfo: WeightInfo;42 43 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;44 }4546 type SelfWeightOf<T> = <T as Config>::WeightInfo;4748 #[pallet::pallet]49 pub struct Pallet<T>(_);5051 #[pallet::event]52 pub enum Event<T: Config> {53 54 TestEvent,55 }5657 #[pallet::error]58 pub enum Error<T> {59 60 AccountNotEmpty,61 62 AccountIsNotMigrating,63 64 BadEvent,65 }6667 #[pallet::storage]68 pub(super) type MigrationPending<T: Config> =69 StorageMap<Hasher = Twox64Concat, Key = H160, Value = bool, QueryKind = ValueQuery>;7071 #[pallet::call]72 impl<T: Config> Pallet<T> {73 74 75 #[pallet::call_index(0)]76 #[pallet::weight(<SelfWeightOf<T>>::begin())]77 pub fn begin(origin: OriginFor<T>, address: H160) -> DispatchResult {78 ensure_root(origin)?;79 ensure!(80 <PalletEvm<T>>::is_account_empty(&address) && !<MigrationPending<T>>::get(&address),81 <Error<T>>::AccountNotEmpty,82 );8384 <MigrationPending<T>>::insert(address, true);85 Ok(())86 }8788 89 90 #[pallet::call_index(1)]91 #[pallet::weight(<SelfWeightOf<T>>::set_data(data.len() as u32))]92 pub fn set_data(93 origin: OriginFor<T>,94 address: H160,95 data: Vec<(H256, H256)>,96 ) -> DispatchResult {97 ensure_root(origin)?;98 ensure!(99 <MigrationPending<T>>::get(&address),100 <Error<T>>::AccountIsNotMigrating,101 );102103 for (k, v) in data {104 <pallet_evm::AccountStorages<T>>::insert(&address, k, v);105 }106 Ok(())107 }108109 110 111 112 #[pallet::call_index(2)]113 #[pallet::weight(<SelfWeightOf<T>>::finish(code.len() as u32))]114 pub fn finish(origin: OriginFor<T>, address: H160, code: Vec<u8>) -> DispatchResult {115 ensure_root(origin)?;116 ensure!(117 <MigrationPending<T>>::get(&address),118 <Error<T>>::AccountIsNotMigrating,119 );120121 <pallet_evm::AccountCodes<T>>::insert(&address, code);122 <MigrationPending<T>>::remove(address);123 Ok(())124 }125126 127 #[pallet::call_index(3)]128 #[pallet::weight(<SelfWeightOf<T>>::insert_eth_logs(logs.len() as u32))]129 pub fn insert_eth_logs(origin: OriginFor<T>, logs: Vec<ethereum::Log>) -> DispatchResult {130 ensure_root(origin)?;131 for log in logs {132 <pallet_evm::Pallet<T>>::deposit_log(log);133 }134 135 Ok(())136 }137138 139 #[pallet::call_index(4)]140 #[pallet::weight(<SelfWeightOf<T>>::insert_events(events.len() as u32))]141 pub fn insert_events(origin: OriginFor<T>, events: Vec<Vec<u8>>) -> DispatchResult {142 ensure_root(origin)?;143 for event in events {144 <frame_system::Pallet<T>>::deposit_event(145 <T as frame_system::Config>::RuntimeEvent::decode(&mut event.as_slice())146 .map_err(|_| <Error<T>>::BadEvent)?,147 );148 }149 Ok(())150 }151152 153 #[pallet::call_index(5)]154 #[pallet::weight(<T as frame_system::Config>::DbWeight::get().reads_writes(10, 10))]155 pub fn remove_rmrk_data(origin: OriginFor<T>) -> DispatchResult {156 use sp_io::hashing::twox_128;157 ensure_root(origin)?;158 let _ = sp_io::storage::clear_prefix(&twox_128(b"RmrkEquip"), Some(5));159 let _ = sp_io::storage::clear_prefix(&twox_128(b"RmrkCore"), Some(5));160 Ok(())161 }162 }163164 165 pub struct OnMethodCall<T>(PhantomData<T>);166 impl<T: Config> pallet_evm::OnMethodCall<T> for OnMethodCall<T> {167 fn is_reserved(contract: &H160) -> bool {168 <MigrationPending<T>>::get(&contract)169 }170171 fn is_used(_contract: &H160) -> bool {172 false173 }174175 fn call(_handle: &mut impl PrecompileHandle) -> Option<pallet_evm::PrecompileResult> {176 None177 }178179 fn get_code(_contract: &H160) -> Option<Vec<u8>> {180 None181 }182 }183}