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 => {63 <PalletNonfungible<T>>::init_collection(sender, payer, data, false)?64 }65 CollectionMode::Fungible(decimal_points) => {66 67 ensure!(68 decimal_points <= MAX_DECIMAL_POINTS,69 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded70 );71 <PalletFungible<T>>::init_collection(sender, payer, data)?72 }7374 #[cfg(feature = "refungible")]75 CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, payer, data)?,7677 #[cfg(not(feature = "refungible"))]78 CollectionMode::ReFungible => return unsupported!(T),79 };80 Ok(id)81 }8283 fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {84 match collection.mode {85 CollectionMode::ReFungible => {86 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?87 }88 CollectionMode::Fungible(_) => {89 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?90 }91 CollectionMode::NFT => {92 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?93 }94 }95 Ok(())96 }9798 fn dispatch(handle: CollectionHandle<T>) -> Self {99 match handle.mode {100 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),101 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),102 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),103 }104 }105106 fn into_inner(self) -> CollectionHandle<T> {107 match self {108 Self::Fungible(f) => f.into_inner(),109 Self::Nonfungible(f) => f.into_inner(),110 Self::Refungible(f) => f.into_inner(),111 }112 }113114 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {115 match self {116 Self::Fungible(h) => h,117 Self::Nonfungible(h) => h,118 Self::Refungible(h) => h,119 }120 }121}122123impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>124where125 T: pallet_common::Config126 + pallet_unique::Config127 + pallet_fungible::Config128 + pallet_nonfungible::Config129 + pallet_refungible::Config,130 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,131{132 fn is_reserved(target: &H160) -> bool {133 map_eth_to_id(target).is_some()134 }135 fn is_used(target: &H160) -> bool {136 map_eth_to_id(target)137 .map(<CollectionById<T>>::contains_key)138 .unwrap_or(false)139 }140 fn get_code(target: &H160) -> Option<Vec<u8>> {141 if let Some(collection_id) = map_eth_to_id(target) {142 let collection = <CollectionById<T>>::get(collection_id)?;143 Some(144 match collection.mode {145 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,146 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,147 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,148 }149 .to_owned(),150 )151 } else if let Some((collection_id, _token_id)) =152 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)153 {154 let collection = <CollectionById<T>>::get(collection_id)?;155 if collection.mode != CollectionMode::ReFungible {156 return None;157 }158 159 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())160 } else {161 None162 }163 }164 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {165 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {166 let collection =167 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;168 let dispatched = Self::dispatch(collection);169170 match dispatched {171 Self::Fungible(h) => h.call(handle),172 Self::Nonfungible(h) => h.call(handle),173 Self::Refungible(h) => h.call(handle),174 }175 } else if let Some((collection_id, token_id)) =176 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(177 &handle.code_address(),178 ) {179 let collection =180 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;181 if collection.mode != CollectionMode::ReFungible {182 return None;183 }184185 let h = RefungibleHandle::cast(collection);186 187 RefungibleTokenHandle(h, token_id).call(handle)188 } else {189 None190 }191 }192}