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

difftreelog

source

runtime/common/src/dispatch.rs5.8 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};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};3637pub enum CollectionDispatchT<T>38where39	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,40{41	Fungible(FungibleHandle<T>),42	Nonfungible(NonfungibleHandle<T>),43	Refungible(RefungibleHandle<T>),44}45impl<T> CollectionDispatch<T> for CollectionDispatchT<T>46where47	T: pallet_common::Config48		+ pallet_unique::Config49		+ pallet_fungible::Config50		+ pallet_nonfungible::Config51		+ pallet_refungible::Config,52{53	fn create(54		sender: T::CrossAccountId,55		data: CreateCollectionData<T::AccountId>,56	) -> Result<CollectionId, DispatchError> {57		let id = match data.mode {58			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data, false)?,59			CollectionMode::Fungible(decimal_points) => {60				// check params61				ensure!(62					decimal_points <= MAX_DECIMAL_POINTS,63					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded64				);65				<PalletFungible<T>>::init_collection(sender, data)?66			}67			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,68		};69		Ok(id)70	}7172	fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {73		match collection.mode {74			CollectionMode::ReFungible => {75				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?76			}77			CollectionMode::Fungible(_) => {78				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?79			}80			CollectionMode::NFT => {81				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?82			}83		}84		Ok(())85	}8687	fn dispatch(handle: CollectionHandle<T>) -> Self {88		match handle.mode {89			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),90			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),91			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),92		}93	}9495	fn into_inner(self) -> CollectionHandle<T> {96		match self {97			Self::Fungible(f) => f.into_inner(),98			Self::Nonfungible(f) => f.into_inner(),99			Self::Refungible(f) => f.into_inner(),100		}101	}102103	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {104		match self {105			Self::Fungible(h) => h,106			Self::Nonfungible(h) => h,107			Self::Refungible(h) => h,108		}109	}110}111112impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>113where114	T: pallet_common::Config115		+ pallet_unique::Config116		+ pallet_fungible::Config117		+ pallet_nonfungible::Config118		+ pallet_refungible::Config,119	T::AccountId: From<[u8; 32]>,120{121	fn is_reserved(target: &H160) -> bool {122		map_eth_to_id(target).is_some()123	}124	fn is_used(target: &H160) -> bool {125		map_eth_to_id(target)126			.map(<CollectionById<T>>::contains_key)127			.unwrap_or(false)128	}129	fn get_code(target: &H160) -> Option<Vec<u8>> {130		if let Some(collection_id) = map_eth_to_id(target) {131			let collection = <CollectionById<T>>::get(collection_id)?;132			Some(133				match collection.mode {134					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,135					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,136					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,137				}138				.to_owned(),139			)140		} else if let Some((collection_id, _token_id)) =141			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)142		{143			let collection = <CollectionById<T>>::get(collection_id)?;144			if collection.mode != CollectionMode::ReFungible {145				return None;146			}147			// TODO: check token existence148			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())149		} else {150			None151		}152	}153	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {154		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {155			let collection =156				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;157			let dispatched = Self::dispatch(collection);158159			match dispatched {160				Self::Fungible(h) => h.call(handle),161				Self::Nonfungible(h) => h.call(handle),162				Self::Refungible(h) => h.call(handle),163			}164		} else if let Some((collection_id, token_id)) =165			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(166				&handle.code_address(),167			) {168			let collection =169				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;170			if collection.mode != CollectionMode::ReFungible {171				return None;172			}173174			let h = RefungibleHandle::cast(collection);175			// TODO: check token existence176			RefungibleTokenHandle(h, token_id).call(handle)177		} else {178			None179		}180	}181}