1use frame_support::{dispatch::DispatchResult, ensure};2use pallet_evm::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 41 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 127 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())128 } else {129 None130 }131 }132 fn call(133 source: &H160,134 target: &H160,135 gas_limit: u64,136 input: &[u8],137 value: U256,138 ) -> Option<PrecompileResult> {139 if let Some(collection_id) = map_eth_to_id(target) {140 let collection = <CollectionHandle<T>>::new_with_gas_limit(collection_id, gas_limit)?;141 let dispatched = Self::dispatch(collection);142143 match dispatched {144 Self::Fungible(h) => h.call(source, input, value),145 Self::Nonfungible(h) => h.call(source, input, value),146 Self::Refungible(h) => h.call(source, input, value),147 }148 } else if let Some((collection_id, token_id)) =149 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)150 {151 let collection = <CollectionHandle<T>>::new_with_gas_limit(collection_id, gas_limit)?;152 if collection.mode != CollectionMode::ReFungible {153 return None;154 }155156 let handle = RefungibleHandle::cast(collection);157 158 RefungibleTokenHandle(handle, token_id).call(source, input, value)159 } else {160 None161 }162 }163}