1234567891011121314151617use frame_support::{dispatch::DispatchResult, ensure, fail};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_balances_adapter::{Pallet as PalletNativeFungible, NativeFungibleHandle};29use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};30use pallet_refungible::{31 Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,32};33use up_data_structs::{34 CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,35 CollectionId, CollectionFlags,36};3738#[cfg(not(feature = "refungible"))]39use pallet_common::unsupported;4041pub enum CollectionDispatchT<T>42where43 T: pallet_fungible::Config44 + pallet_nonfungible::Config45 + pallet_refungible::Config46 + pallet_balances_adapter::Config,47{48 Fungible(FungibleHandle<T>),49 Nonfungible(NonfungibleHandle<T>),50 Refungible(RefungibleHandle<T>),51 NativeFungible(NativeFungibleHandle<T>),52}5354impl<T> CollectionDispatch<T> for CollectionDispatchT<T>55where56 T: pallet_common::Config57 + pallet_unique::Config58 + pallet_fungible::Config59 + pallet_nonfungible::Config60 + pallet_refungible::Config61 + pallet_balances_adapter::Config,62{63 fn check_is_internal(&self) -> DispatchResult {64 match self {65 Self::Fungible(h) => h.check_is_internal(),66 Self::Nonfungible(h) => h.check_is_internal(),67 Self::Refungible(h) => h.check_is_internal(),68 Self::NativeFungible(h) => h.check_is_internal(),69 }70 }7172 fn create(73 sender: T::CrossAccountId,74 payer: T::CrossAccountId,75 data: CreateCollectionData<T::AccountId>,76 flags: CollectionFlags,77 ) -> Result<CollectionId, DispatchError> {78 let id = match data.mode {79 CollectionMode::NFT => {80 <PalletNonfungible<T>>::init_collection(sender, payer, data, flags)?81 }82 CollectionMode::Fungible(decimal_points) => {83 84 ensure!(85 decimal_points <= MAX_DECIMAL_POINTS,86 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded87 );88 <PalletFungible<T>>::init_collection(sender, payer, data, flags)?89 }9091 #[cfg(feature = "refungible")]92 CollectionMode::ReFungible => {93 <PalletRefungible<T>>::init_collection(sender, payer, data, flags)?94 }9596 #[cfg(not(feature = "refungible"))]97 CollectionMode::ReFungible => return unsupported!(T),98 };99 Ok(id)100 }101102 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {103 if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {104 fail!(<pallet_common::Error<T>>::UnsupportedOperation);105 }106107 let collection = <CollectionHandle<T>>::try_get(collection_id)?;108 collection.check_is_internal()?;109110 match collection.mode {111 CollectionMode::ReFungible => {112 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?113 }114 CollectionMode::Fungible(_) => {115 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?116 }117 CollectionMode::NFT => {118 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?119 }120 }121 Ok(())122 }123124 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {125 if collection_id == CollectionId(0) {126 return Ok(Self::NativeFungible(NativeFungibleHandle::new()));127 }128129 let handle = <CollectionHandle<T>>::try_get(collection_id)?;130 Ok(match handle.mode {131 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),132 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),133 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),134 })135 }136137 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {138 match self {139 Self::Fungible(h) => h,140 Self::Nonfungible(h) => h,141 Self::Refungible(h) => h,142 Self::NativeFungible(h) => h,143 }144 }145}146147impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>148where149 T: pallet_common::Config150 + pallet_unique::Config151 + pallet_fungible::Config152 + pallet_nonfungible::Config153 + pallet_refungible::Config154 + pallet_balances_adapter::Config,155 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,156{157 fn is_reserved(target: &H160) -> bool {158 map_eth_to_id(target).is_some()159 }160 fn is_used(target: &H160) -> bool {161 map_eth_to_id(target)162 .map(<CollectionById<T>>::contains_key)163 .unwrap_or(false)164 }165 fn get_code(target: &H160) -> Option<Vec<u8>> {166 if let Some(collection_id) = map_eth_to_id(target) {167 let collection = <CollectionById<T>>::get(collection_id)?;168 Some(169 match collection.mode {170 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,171 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,172 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,173 }174 .to_owned(),175 )176 } else if let Some((collection_id, _token_id)) =177 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)178 {179 let collection = <CollectionById<T>>::get(collection_id)?;180 if collection.mode != CollectionMode::ReFungible {181 return None;182 }183 184 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())185 } else {186 None187 }188 }189 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {190 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {191 if collection_id == CollectionId(0) {192 <NativeFungibleHandle<T>>::new().call(handle)193 } else {194 let collection = <CollectionHandle<T>>::new_with_gas_limit(195 collection_id,196 handle.remaining_gas(),197 )?;198199 match collection.mode {200 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),201 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),202 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),203 }204 }205 } else if let Some((collection_id, token_id)) =206 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(207 &handle.code_address(),208 ) {209 let collection =210 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;211 if collection.mode != CollectionMode::ReFungible {212 return None;213 }214215 let h = RefungibleHandle::cast(collection);216 217 RefungibleTokenHandle(h, token_id).call(handle)218 } else {219 None220 }221 }222}