1234567891011121314151617use frame_support::{dispatch::DispatchResult, ensure};18use pallet_evm::{PrecompileHandle, PrecompileResult};19use sp_core::H160;20use sp_runtime::DispatchError;21use sp_std::{borrow::ToOwned, vec::Vec};22use pallet_common::{23 CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,24 eth::map_eth_to_id,25};26pub use pallet_common::dispatch::CollectionDispatch;27use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};28use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};29use pallet_refungible::{30 Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,31};32use up_data_structs::{33 CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,34 CollectionId,35};3637#[cfg(not(feature = "refungible"))]38use pallet_common::unsupported;3940pub enum CollectionDispatchT<T>41where42 T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,43{44 Fungible(FungibleHandle<T>),45 Nonfungible(NonfungibleHandle<T>),46 Refungible(RefungibleHandle<T>),47}48impl<T> CollectionDispatch<T> for CollectionDispatchT<T>49where50 T: pallet_common::Config51 + pallet_unique::Config52 + pallet_fungible::Config53 + pallet_nonfungible::Config54 + pallet_refungible::Config,55{56 fn create(57 sender: T::CrossAccountId,58 payer: T::CrossAccountId,59 data: CreateCollectionData<T::AccountId>,60 ) -> Result<CollectionId, DispatchError> {61 let id = match data.mode {62 CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, payer, data, false)?,63 CollectionMode::Fungible(decimal_points) => {64 65 ensure!(66 decimal_points <= MAX_DECIMAL_POINTS,67 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded68 );69 <PalletFungible<T>>::init_collection(sender, payer, data)?70 }7172 #[cfg(feature = "refungible")]73 CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, payer, data)?,7475 #[cfg(not(feature = "refungible"))]76 CollectionMode::ReFungible => return unsupported!(T),77 };78 Ok(id)79 }8081 fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {82 match collection.mode {83 CollectionMode::ReFungible => {84 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?85 }86 CollectionMode::Fungible(_) => {87 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?88 }89 CollectionMode::NFT => {90 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?91 }92 }93 Ok(())94 }9596 fn dispatch(handle: CollectionHandle<T>) -> Self {97 match handle.mode {98 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),99 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),100 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),101 }102 }103104 fn into_inner(self) -> CollectionHandle<T> {105 match self {106 Self::Fungible(f) => f.into_inner(),107 Self::Nonfungible(f) => f.into_inner(),108 Self::Refungible(f) => f.into_inner(),109 }110 }111112 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {113 match self {114 Self::Fungible(h) => h,115 Self::Nonfungible(h) => h,116 Self::Refungible(h) => h,117 }118 }119}120121impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>122where123 T: pallet_common::Config124 + pallet_unique::Config125 + pallet_fungible::Config126 + pallet_nonfungible::Config127 + pallet_refungible::Config,128 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,129{130 fn is_reserved(target: &H160) -> bool {131 map_eth_to_id(target).is_some()132 }133 fn is_used(target: &H160) -> bool {134 map_eth_to_id(target)135 .map(<CollectionById<T>>::contains_key)136 .unwrap_or(false)137 }138 fn get_code(target: &H160) -> Option<Vec<u8>> {139 if let Some(collection_id) = map_eth_to_id(target) {140 let collection = <CollectionById<T>>::get(collection_id)?;141 Some(142 match collection.mode {143 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,144 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,145 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,146 }147 .to_owned(),148 )149 } else if let Some((collection_id, _token_id)) =150 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)151 {152 let collection = <CollectionById<T>>::get(collection_id)?;153 if collection.mode != CollectionMode::ReFungible {154 return None;155 }156 157 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())158 } else {159 None160 }161 }162 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {163 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {164 let collection =165 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;166 let dispatched = Self::dispatch(collection);167168 match dispatched {169 Self::Fungible(h) => h.call(handle),170 Self::Nonfungible(h) => h.call(handle),171 Self::Refungible(h) => h.call(handle),172 }173 } else if let Some((collection_id, token_id)) =174 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(175 &handle.code_address(),176 ) {177 let collection =178 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;179 if collection.mode != CollectionMode::ReFungible {180 return None;181 }182183 let h = RefungibleHandle::cast(collection);184 185 RefungibleTokenHandle(h, token_id).call(handle)186 } else {187 None188 }189 }190}