git.delta.rocks / unique-network / refs/commits / a64769ff6bff

difftreelog

source

runtime/common/dispatch.rs7.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use 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::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,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::CrossAccountId>,76	) -> Result<CollectionId, DispatchError> {77		let id = match data.mode {78			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, payer, data)?,79			CollectionMode::Fungible(decimal_points) => {80				// check params81				ensure!(82					decimal_points <= MAX_DECIMAL_POINTS,83					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded84				);85				<PalletFungible<T>>::init_collection(sender, payer, data)?86			}8788			#[cfg(feature = "refungible")]89			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, payer, data)?,9091			#[cfg(not(feature = "refungible"))]92			CollectionMode::ReFungible => return unsupported!(T),93		};94		Ok(id)95	}9697	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {98		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {99			fail!(<pallet_common::Error<T>>::UnsupportedOperation);100		}101102		let collection = <CollectionHandle<T>>::try_get(collection_id)?;103104		match collection.mode {105			CollectionMode::ReFungible => {106				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?107			}108			CollectionMode::Fungible(_) => {109				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?110			}111			CollectionMode::NFT => {112				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?113			}114		}115		Ok(())116	}117118	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {119		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {120			return Ok(Self::NativeFungible(121				NativeFungibleHandle::new_with_gas_limit(u64::MAX),122			));123		}124125		let handle = <CollectionHandle<T>>::try_get(collection_id)?;126		Ok(match handle.mode {127			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),128			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),129			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),130		})131	}132133	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {134		match self {135			Self::Fungible(h) => h,136			Self::Nonfungible(h) => h,137			Self::Refungible(h) => h,138			Self::NativeFungible(h) => h,139		}140	}141}142143impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>144where145	T: pallet_common::Config146		+ pallet_unique::Config147		+ pallet_fungible::Config148		+ pallet_nonfungible::Config149		+ pallet_refungible::Config150		+ pallet_balances_adapter::Config,151	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,152{153	fn is_reserved(target: &H160) -> bool {154		map_eth_to_id(target).is_some()155	}156	fn is_used(target: &H160) -> bool {157		map_eth_to_id(target)158			.map(<CollectionById<T>>::contains_key)159			.unwrap_or(false)160	}161	fn get_code(target: &H160) -> Option<Vec<u8>> {162		if let Some(collection_id) = map_eth_to_id(target) {163			let collection = <CollectionById<T>>::get(collection_id)?;164			Some(165				match collection.mode {166					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,167					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,168					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,169				}170				.to_owned(),171			)172		} else if let Some((collection_id, _token_id)) =173			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)174		{175			let collection = <CollectionById<T>>::get(collection_id)?;176			if collection.mode != CollectionMode::ReFungible {177				return None;178			}179			// TODO: check token existence180			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())181		} else {182			None183		}184	}185	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {186		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {187			if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {188				<NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)189			} else {190				let collection = <CollectionHandle<T>>::new_with_gas_limit(191					collection_id,192					handle.remaining_gas(),193				)?;194195				match collection.mode {196					CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),197					CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),198					CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),199				}200			}201		} else if let Some((collection_id, token_id)) =202			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(203				&handle.code_address(),204			) {205			let collection =206				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;207			if collection.mode != CollectionMode::ReFungible {208				return None;209			}210211			let h = RefungibleHandle::cast(collection);212			// TODO: check token existence213			RefungibleTokenHandle(h, token_id).call(handle)214		} else {215			None216		}217	}218}