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;24#[allow(missing_docs)]25pub mod weights;2627#[frame_support::pallet]28pub mod pallet {29 use frame_support::{pallet_prelude::*, traits::IsType};30 use frame_system::pallet_prelude::*;31 use pallet_evm::{Pallet as PalletEvm, PrecompileHandle};32 use sp_core::{H160, H256};33 use sp_std::vec::Vec;3435 use super::weights::WeightInfo;3637 #[pallet::config]38 pub trait Config: frame_system::Config + pallet_evm::Config {39 40 type WeightInfo: WeightInfo;41 42 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;43 }4445 type SelfWeightOf<T> = <T as Config>::WeightInfo;4647 #[pallet::pallet]48 pub struct Pallet<T>(_);4950 #[pallet::event]51 pub enum Event<T: Config> {52 53 TestEvent,54 }5556 #[pallet::error]57 pub enum Error<T> {58 59 AccountNotEmpty,60 61 AccountIsNotMigrating,62 63 BadEvent,64 }6566 #[pallet::storage]67 pub(super) type MigrationPending<T: Config> =68 StorageMap<Hasher = Twox64Concat, Key = H160, Value = bool, QueryKind = ValueQuery>;6970 #[pallet::call]71 impl<T: Config> Pallet<T> {72 73 74 #[pallet::call_index(0)]75 #[pallet::weight(<SelfWeightOf<T>>::begin())]76 pub fn begin(origin: OriginFor<T>, address: H160) -> DispatchResult {77 ensure_root(origin)?;78 ensure!(79 <PalletEvm<T>>::is_account_empty(&address) && !<MigrationPending<T>>::get(address),80 <Error<T>>::AccountNotEmpty,81 );8283 <MigrationPending<T>>::insert(address, true);84 Ok(())85 }8687 88 89 #[pallet::call_index(1)]90 #[pallet::weight(<SelfWeightOf<T>>::set_data(data.len() as u32))]91 pub fn set_data(92 origin: OriginFor<T>,93 address: H160,94 data: Vec<(H256, H256)>,95 ) -> DispatchResult {96 ensure_root(origin)?;97 ensure!(98 <MigrationPending<T>>::get(address),99 <Error<T>>::AccountIsNotMigrating,100 );101102 for (k, v) in data {103 <pallet_evm::AccountStorages<T>>::insert(address, k, v);104 }105 Ok(())106 }107108 109 110 111 #[pallet::call_index(2)]112 #[pallet::weight(<SelfWeightOf<T>>::finish(code.len() as u32))]113 pub fn finish(origin: OriginFor<T>, address: H160, code: Vec<u8>) -> DispatchResult {114 ensure_root(origin)?;115 ensure!(116 <MigrationPending<T>>::get(address),117 <Error<T>>::AccountIsNotMigrating,118 );119120 <pallet_evm::AccountCodes<T>>::insert(address, code);121 <MigrationPending<T>>::remove(address);122 Ok(())123 }124125 126 #[pallet::call_index(3)]127 #[pallet::weight(<SelfWeightOf<T>>::insert_eth_logs(logs.len() as u32))]128 pub fn insert_eth_logs(origin: OriginFor<T>, logs: Vec<ethereum::Log>) -> DispatchResult {129 ensure_root(origin)?;130 for log in logs {131 <pallet_evm::Pallet<T>>::deposit_log(log);132 }133 134 Ok(())135 }136137 138 #[pallet::call_index(4)]139 #[pallet::weight(<SelfWeightOf<T>>::insert_events(events.len() as u32))]140 pub fn insert_events(origin: OriginFor<T>, events: Vec<Vec<u8>>) -> DispatchResult {141 ensure_root(origin)?;142 for event in events {143 <frame_system::Pallet<T>>::deposit_event(144 <T as frame_system::Config>::RuntimeEvent::decode(&mut event.as_slice())145 .map_err(|_| <Error<T>>::BadEvent)?,146 );147 }148 Ok(())149 }150151 152 #[pallet::call_index(5)]153 #[pallet::weight(<T as frame_system::Config>::DbWeight::get().reads_writes(10, 10))]154 pub fn remove_rmrk_data(origin: OriginFor<T>) -> DispatchResult {155 use sp_io::hashing::twox_128;156 ensure_root(origin)?;157 let _ = sp_io::storage::clear_prefix(&twox_128(b"RmrkEquip"), Some(5));158 let _ = sp_io::storage::clear_prefix(&twox_128(b"RmrkCore"), Some(5));159 Ok(())160 }161 }162163 164 pub struct OnMethodCall<T>(PhantomData<T>);165 impl<T: Config> pallet_evm::OnMethodCall<T> for OnMethodCall<T> {166 fn is_reserved(contract: &H160) -> bool {167 <MigrationPending<T>>::get(contract)168 }169170 fn is_used(_contract: &H160) -> bool {171 false172 }173174 fn call(_handle: &mut impl PrecompileHandle) -> Option<pallet_evm::PrecompileResult> {175 None176 }177178 fn get_code(_contract: &H160) -> Option<Vec<u8>> {179 None180 }181 }182}