git.delta.rocks / unique-network / refs/commits / 3f6fffb62aea

difftreelog

source

runtime/common/src/dispatch.rs5.0 KiBsourcehistory
1use frame_support::{dispatch::DispatchResult, ensure};2use pallet_evm::{PrecompileHandle, PrecompileResult};3use sp_core::{H160, U256};4use sp_std::{borrow::ToOwned, vec::Vec};5use pallet_common::{6	CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,7	eth::map_eth_to_id,8};9pub use pallet_common::dispatch::CollectionDispatch;10use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};11use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};12use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle, erc::RefungibleTokenHandle};13use up_data_structs::{14	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,15};1617pub enum CollectionDispatchT<T>18where19	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,20{21	Fungible(FungibleHandle<T>),22	Nonfungible(NonfungibleHandle<T>),23	Refungible(RefungibleHandle<T>),24}25impl<T> CollectionDispatch<T> for CollectionDispatchT<T>26where27	T: pallet_common::Config28		+ pallet_unique::Config29		+ pallet_fungible::Config30		+ pallet_nonfungible::Config31		+ pallet_refungible::Config,32{33	fn create(34		sender: T::CrossAccountId,35		data: CreateCollectionData<T::AccountId>,36	) -> DispatchResult {37		let _id = match data.mode {38			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data)?,39			CollectionMode::Fungible(decimal_points) => {40				// check params41				ensure!(42					decimal_points <= MAX_DECIMAL_POINTS,43					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded44				);45				<PalletFungible<T>>::init_collection(sender, data)?46			}47			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,48		};49		Ok(())50	}5152	fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {53		match collection.mode {54			CollectionMode::ReFungible => {55				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?56			}57			CollectionMode::Fungible(_) => {58				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?59			}60			CollectionMode::NFT => {61				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?62			}63		}64		Ok(())65	}6667	fn dispatch(handle: CollectionHandle<T>) -> Self {68		match handle.mode {69			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),70			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),71			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),72		}73	}7475	fn into_inner(self) -> CollectionHandle<T> {76		match self {77			Self::Fungible(f) => f.into_inner(),78			Self::Nonfungible(f) => f.into_inner(),79			Self::Refungible(f) => f.into_inner(),80		}81	}8283	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {84		match self {85			Self::Fungible(h) => h,86			Self::Nonfungible(h) => h,87			Self::Refungible(h) => h,88		}89	}90}9192impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>93where94	T: pallet_common::Config95		+ pallet_unique::Config96		+ pallet_fungible::Config97		+ pallet_nonfungible::Config98		+ pallet_refungible::Config,99{100	fn is_reserved(target: &H160) -> bool {101		map_eth_to_id(target).is_some()102	}103	fn is_used(target: &H160) -> bool {104		map_eth_to_id(target)105			.map(<CollectionById<T>>::contains_key)106			.unwrap_or(false)107	}108	fn get_code(target: &H160) -> Option<Vec<u8>> {109		if let Some(collection_id) = map_eth_to_id(target) {110			let collection = <CollectionById<T>>::get(collection_id)?;111			Some(112				match collection.mode {113					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,114					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,115					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,116				}117				.to_owned(),118			)119		} else if let Some((collection_id, _token_id)) =120			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)121		{122			let collection = <CollectionById<T>>::get(collection_id)?;123			if collection.mode != CollectionMode::ReFungible {124				return None;125			}126			// TODO: check token existence127			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())128		} else {129			None130		}131	}132	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {133		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {134			let collection =135				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;136			let dispatched = Self::dispatch(collection);137138			match dispatched {139				Self::Fungible(h) => h.call(handle),140				Self::Nonfungible(h) => h.call(handle),141				Self::Refungible(h) => h.call(handle),142			}143		} else if let Some((collection_id, token_id)) =144			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(145				&handle.code_address(),146			) {147			let collection =148				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;149			if collection.mode != CollectionMode::ReFungible {150				return None;151			}152153			let h = RefungibleHandle::cast(collection);154			// TODO: check token existence155			RefungibleTokenHandle(h, token_id).call(handle)156		} else {157			None158		}159	}160}