1234567891011121314151617use frame_support::{dispatch::DispatchResult, ensure, fail};18use pallet_balances_adapter::NativeFungibleHandle;19pub use pallet_common::dispatch::CollectionDispatch;20use pallet_common::{21 erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,22 CommonCollectionOperations, Pallet as PalletCommon,23};24use pallet_evm::{PrecompileHandle, PrecompileResult};25use pallet_fungible::{FungibleHandle, Pallet as PalletFungible};26use pallet_nonfungible::{NonfungibleHandle, Pallet as PalletNonfungible};27use pallet_refungible::{28 erc_token::RefungibleTokenHandle, Pallet as PalletRefungible, RefungibleHandle,29};30use sp_core::H160;31use sp_runtime::DispatchError;32use sp_std::{borrow::ToOwned, vec::Vec};33use up_data_structs::{34 mapping::TokenAddressMapping, CollectionId, CollectionMode, CreateCollectionData,35 MAX_DECIMAL_POINTS,36};3738pub enum CollectionDispatchT<T>39where40 T: pallet_fungible::Config41 + pallet_nonfungible::Config42 + pallet_refungible::Config43 + pallet_balances_adapter::Config,44{45 Fungible(FungibleHandle<T>),46 Nonfungible(NonfungibleHandle<T>),47 Refungible(RefungibleHandle<T>),48 NativeFungible(NativeFungibleHandle<T>),49}5051impl<T> CollectionDispatch<T> for CollectionDispatchT<T>52where53 T: pallet_common::Config54 + pallet_unique::Config55 + pallet_fungible::Config56 + pallet_nonfungible::Config57 + pallet_refungible::Config58 + pallet_balances_adapter::Config,59{60 fn check_is_internal(&self) -> DispatchResult {61 match self {62 Self::Fungible(h) => h.check_is_internal(),63 Self::Nonfungible(h) => h.check_is_internal(),64 Self::Refungible(h) => h.check_is_internal(),65 Self::NativeFungible(h) => h.check_is_internal(),66 }67 }6869 fn create(70 sender: T::CrossAccountId,71 payer: Option<T::CrossAccountId>,72 data: CreateCollectionData<T::CrossAccountId>,73 ) -> Result<CollectionId, DispatchError> {74 match data.mode {75 CollectionMode::Fungible(decimal_points) => {76 77 ensure!(78 decimal_points <= MAX_DECIMAL_POINTS,79 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded80 );81 }8283 #[cfg(not(feature = "refungible"))]84 CollectionMode::ReFungible => return unsupported!(T),8586 _ => {}87 };8889 <PalletCommon<T>>::init_collection(sender, payer, data)90 }9192 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {93 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {94 fail!(<pallet_common::Error<T>>::UnsupportedOperation);95 }9697 let collection = <CollectionHandle<T>>::try_get(collection_id)?;9899 match collection.mode {100 CollectionMode::ReFungible => {101 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?102 }103 CollectionMode::Fungible(_) => {104 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?105 }106 CollectionMode::NFT => {107 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?108 }109 }110 Ok(())111 }112113 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {114 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {115 return Ok(Self::NativeFungible(116 NativeFungibleHandle::new_with_gas_limit(u64::MAX),117 ));118 }119120 let handle = <CollectionHandle<T>>::try_get(collection_id)?;121 Ok(match handle.mode {122 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),123 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),124 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),125 })126 }127128 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {129 match self {130 Self::Fungible(h) => h,131 Self::Nonfungible(h) => h,132 Self::Refungible(h) => h,133 Self::NativeFungible(h) => h,134 }135 }136}137138impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>139where140 T: pallet_common::Config141 + pallet_unique::Config142 + pallet_fungible::Config143 + pallet_nonfungible::Config144 + pallet_refungible::Config145 + pallet_balances_adapter::Config,146 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,147{148 fn is_reserved(target: &H160) -> bool {149 map_eth_to_id(target).is_some()150 }151 fn is_used(target: &H160) -> bool {152 map_eth_to_id(target)153 .map(<CollectionById<T>>::contains_key)154 .unwrap_or(false)155 }156 fn get_code(target: &H160) -> Option<Vec<u8>> {157 if let Some(collection_id) = map_eth_to_id(target) {158 let collection = <CollectionById<T>>::get(collection_id)?;159 Some(160 match collection.mode {161 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,162 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,163 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,164 }165 .to_owned(),166 )167 } else if let Some((collection_id, _token_id)) =168 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)169 {170 let collection = <CollectionById<T>>::get(collection_id)?;171 if collection.mode != CollectionMode::ReFungible {172 return None;173 }174 175 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())176 } else {177 None178 }179 }180 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {181 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {182 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {183 <NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)184 } else {185 let collection = <CollectionHandle<T>>::new_with_gas_limit(186 collection_id,187 handle.remaining_gas(),188 )?;189190 match collection.mode {191 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),192 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),193 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),194 }195 }196 } else if let Some((collection_id, token_id)) =197 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(198 &handle.code_address(),199 ) {200 let collection =201 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;202 if collection.mode != CollectionMode::ReFungible {203 return None;204 }205206 let h = RefungibleHandle::cast(collection);207 208 RefungibleTokenHandle(h, token_id).call(handle)209 } else {210 None211 }212 }213}