git.delta.rocks / unique-network / refs/commits / 30ec3c77e171

difftreelog

source

runtime/common/dispatch.rs6.9 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_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_raw(70		sender: T::CrossAccountId,71		payer: Option<T::CrossAccountId>,72		is_special_collection: bool,73		data: CreateCollectionData<T::CrossAccountId>,74	) -> Result<CollectionId, DispatchError> {75		match data.mode {76			CollectionMode::Fungible(decimal_points) => {77				// check params78				ensure!(79					decimal_points <= MAX_DECIMAL_POINTS,80					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded81				);82			}8384			#[cfg(not(feature = "refungible"))]85			CollectionMode::ReFungible => return unsupported!(T),8687			_ => {}88		};8990		<PalletCommon<T>>::init_collection(sender, payer, is_special_collection, data)91	}9293	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {94		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {95			fail!(<pallet_common::Error<T>>::UnsupportedOperation);96		}9798		let collection = <CollectionHandle<T>>::try_get(collection_id)?;99100		match collection.mode {101			CollectionMode::ReFungible => {102				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?103			}104			CollectionMode::Fungible(_) => {105				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?106			}107			CollectionMode::NFT => {108				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?109			}110		}111		Ok(())112	}113114	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {115		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {116			return Ok(Self::NativeFungible(117				NativeFungibleHandle::new_with_gas_limit(u64::MAX),118			));119		}120121		let handle = <CollectionHandle<T>>::try_get(collection_id)?;122		Ok(match handle.mode {123			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),124			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),125			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),126		})127	}128129	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {130		match self {131			Self::Fungible(h) => h,132			Self::Nonfungible(h) => h,133			Self::Refungible(h) => h,134			Self::NativeFungible(h) => h,135		}136	}137}138139impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>140where141	T: pallet_common::Config142		+ pallet_unique::Config143		+ pallet_fungible::Config144		+ pallet_nonfungible::Config145		+ pallet_refungible::Config146		+ pallet_balances_adapter::Config,147	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,148{149	fn is_reserved(target: &H160) -> bool {150		map_eth_to_id(target).is_some()151	}152	fn is_used(target: &H160) -> bool {153		map_eth_to_id(target)154			.map(<CollectionById<T>>::contains_key)155			.unwrap_or(false)156	}157	fn get_code(target: &H160) -> Option<Vec<u8>> {158		if let Some(collection_id) = map_eth_to_id(target) {159			let collection = <CollectionById<T>>::get(collection_id)?;160			Some(161				match collection.mode {162					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,163					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,164					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,165				}166				.to_owned(),167			)168		} else if let Some((collection_id, _token_id)) =169			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)170		{171			let collection = <CollectionById<T>>::get(collection_id)?;172			if collection.mode != CollectionMode::ReFungible {173				return None;174			}175			// TODO: check token existence176			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())177		} else {178			None179		}180	}181	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {182		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {183			if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {184				<NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)185			} else {186				let collection = <CollectionHandle<T>>::new_with_gas_limit(187					collection_id,188					handle.remaining_gas(),189				)?;190191				match collection.mode {192					CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),193					CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),194					CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),195				}196			}197		} else if let Some((collection_id, token_id)) =198			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(199				&handle.code_address(),200			) {201			let collection =202				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;203			if collection.mode != CollectionMode::ReFungible {204				return None;205			}206207			let h = RefungibleHandle::cast(collection);208			// TODO: check token existence209			RefungibleTokenHandle(h, token_id).call(handle)210		} else {211			None212		}213	}214}