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

difftreelog

feat impl dispatchable methods

Trubnikov Sergey2023-04-26parent: #82643c5.patch.diff
in: master

5 files changed

modifiedpallets/balances-adapter/src/common.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/common.rs
+++ b/pallets/balances-adapter/src/common.rs
@@ -1,7 +1,12 @@
-use alloc::vec::Vec;
+use alloc::{vec, vec::Vec};
 use core::marker::PhantomData;
 use crate::{Config, NativeFungibleHandle};
-use pallet_common::{CommonCollectionOperations, CommonWeightInfo};
+use frame_support::{
+	traits::{Currency, ExistenceRequirement},
+	fail,
+};
+use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo, with_weight};
+use up_data_structs::TokenId;
 
 pub struct CommonWeights<T: Config>(PhantomData<T>);
 impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
@@ -92,7 +97,7 @@
 		data: up_data_structs::CreateItemData,
 		nesting_budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn create_multiple_items(
@@ -102,7 +107,7 @@
 		data: Vec<up_data_structs::CreateItemData>,
 		nesting_budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn create_multiple_items_ex(
@@ -111,26 +116,26 @@
 		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
 		nesting_budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn burn_item(
 		&self,
 		sender: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn burn_item_recursively(
 		&self,
 		sender: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		self_budget: &dyn up_data_structs::budget::Budget,
 		breadth_budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn set_collection_properties(
@@ -138,7 +143,7 @@
 		sender: <T>::CrossAccountId,
 		properties: Vec<up_data_structs::Property>,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn delete_collection_properties(
@@ -146,27 +151,27 @@
 		sender: &<T>::CrossAccountId,
 		property_keys: Vec<up_data_structs::PropertyKey>,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn set_token_properties(
 		&self,
 		sender: <T>::CrossAccountId,
-		token_id: up_data_structs::TokenId,
+		token_id: TokenId,
 		properties: Vec<up_data_structs::Property>,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn delete_token_properties(
 		&self,
 		sender: <T>::CrossAccountId,
-		token_id: up_data_structs::TokenId,
+		token_id: TokenId,
 		property_keys: Vec<up_data_structs::PropertyKey>,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn set_token_property_permissions(
@@ -174,28 +179,36 @@
 		sender: &<T>::CrossAccountId,
 		property_permissions: Vec<up_data_structs::PropertyKeyPermission>,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn transfer(
 		&self,
 		sender: <T>::CrossAccountId,
 		to: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		with_weight(
+			<T as Config>::Currency::transfer(
+				sender.as_sub(),
+				to.as_sub(),
+				amount.into(),
+				ExistenceRequirement::KeepAlive,
+			),
+			Default::default(),
+		)
 	}
 
 	fn approve(
 		&self,
 		sender: <T>::CrossAccountId,
 		spender: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn approve_from(
@@ -203,10 +216,10 @@
 		sender: <T>::CrossAccountId,
 		from: <T>::CrossAccountId,
 		to: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn transfer_from(
@@ -214,120 +227,129 @@
 		sender: <T>::CrossAccountId,
 		from: <T>::CrossAccountId,
 		to: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		with_weight(
+			<T as Config>::Currency::transfer(
+				sender.as_sub(),
+				to.as_sub(),
+				amount.into(),
+				ExistenceRequirement::KeepAlive,
+			),
+			Default::default(),
+		)
 	}
 
 	fn burn_from(
 		&self,
 		sender: <T>::CrossAccountId,
 		from: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		amount: u128,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn check_nesting(
 		&self,
 		sender: <T>::CrossAccountId,
-		from: (up_data_structs::CollectionId, up_data_structs::TokenId),
-		under: up_data_structs::TokenId,
+		from: (up_data_structs::CollectionId, TokenId),
+		under: TokenId,
 		budget: &dyn up_data_structs::budget::Budget,
 	) -> frame_support::sp_runtime::DispatchResult {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
-	fn nest(
-		&self,
-		under: up_data_structs::TokenId,
-		to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId),
-	) {
-		todo!()
-	}
+	fn nest(&self, under: TokenId, to_nest: (up_data_structs::CollectionId, TokenId)) {}
 
-	fn unnest(
-		&self,
-		under: up_data_structs::TokenId,
-		to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId),
-	) {
-		todo!()
-	}
+	fn unnest(&self, under: TokenId, to_nest: (up_data_structs::CollectionId, TokenId)) {}
 
-	fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<up_data_structs::TokenId> {
-		todo!()
+	fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {
+		let balance = <T as Config>::Currency::total_balance(account.as_sub());
+		if balance != 0 {
+			vec![TokenId::default()]
+		} else {
+			vec![]
+		}
 	}
 
-	fn collection_tokens(&self) -> Vec<up_data_structs::TokenId> {
-		todo!()
+	fn collection_tokens(&self) -> Vec<TokenId> {
+		vec![TokenId::default()]
 	}
 
-	fn token_exists(&self, token: up_data_structs::TokenId) -> bool {
-		todo!()
+	fn token_exists(&self, token: TokenId) -> bool {
+		token == TokenId::default()
 	}
 
-	fn last_token_id(&self) -> up_data_structs::TokenId {
-		todo!()
+	fn last_token_id(&self) -> TokenId {
+		TokenId::default()
 	}
 
 	fn token_owner(
 		&self,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 	) -> Result<<T>::CrossAccountId, up_data_structs::TokenOwnerError> {
-		todo!()
+		Err(up_data_structs::TokenOwnerError::MultipleOwners)
 	}
 
-	fn token_owners(&self, token: up_data_structs::TokenId) -> Vec<<T>::CrossAccountId> {
-		todo!()
+	fn token_owners(&self, token: TokenId) -> Vec<<T>::CrossAccountId> {
+		vec![]
 	}
 
 	fn token_property(
 		&self,
-		token_id: up_data_structs::TokenId,
+		token_id: TokenId,
 		key: &up_data_structs::PropertyKey,
 	) -> Option<up_data_structs::PropertyValue> {
-		todo!()
+		None
 	}
 
 	fn token_properties(
 		&self,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 		keys: Option<Vec<up_data_structs::PropertyKey>>,
 	) -> Vec<up_data_structs::Property> {
-		todo!()
+		vec![]
 	}
 
 	fn total_supply(&self) -> u32 {
-		todo!()
+		1
 	}
 
 	fn account_balance(&self, account: <T>::CrossAccountId) -> u32 {
-		todo!()
+		let balance: u128 = <T as Config>::Currency::free_balance(account.as_sub()).into();
+		(balance != 0).into()
 	}
 
-	fn balance(&self, account: <T>::CrossAccountId, token: up_data_structs::TokenId) -> u128 {
-		todo!()
+	fn balance(&self, account: <T>::CrossAccountId, token: TokenId) -> u128 {
+		if token != TokenId::default() {
+			return 0;
+		}
+		<T as Config>::Currency::free_balance(account.as_sub()).into()
 	}
 
-	fn total_pieces(&self, token: up_data_structs::TokenId) -> Option<u128> {
-		todo!()
+	fn total_pieces(&self, token: TokenId) -> Option<u128> {
+		if token != TokenId::default() {
+			return None;
+		}
+		let total = <T as Config>::Currency::total_issuance();
+		Some(total.into())
 	}
 
 	fn allowance(
 		&self,
 		sender: <T>::CrossAccountId,
 		spender: <T>::CrossAccountId,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 	) -> u128 {
-		todo!()
+		0
 	}
 
 	fn refungible_extensions(&self) -> Option<&dyn pallet_common::RefungibleExtensions<T>> {
-		todo!()
+		None
 	}
 
 	fn set_allowance_for_all(
@@ -336,17 +358,17 @@
 		operator: <T>::CrossAccountId,
 		approve: bool,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
 	fn allowance_for_all(&self, owner: <T>::CrossAccountId, operator: <T>::CrossAccountId) -> bool {
-		todo!()
+		false
 	}
 
 	fn repair_item(
 		&self,
-		token: up_data_structs::TokenId,
+		token: TokenId,
 	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
-		todo!()
+		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 }
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -44,7 +44,7 @@
 			Self::AccountId,
 			Balance = Self::CurrencyBalance,
 		>;
-		type CurrencyBalance: Into<U256> + TryFrom<U256>;
+		type CurrencyBalance: Into<U256> + TryFrom<U256> + PartialEq<u128> + From<u128> + Into<u128>;
 
 		type Decimals: Get<u8>;
 		type Name: Get<String>;
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -462,6 +462,7 @@
 	}
 
 	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
+	pub const NATIVE_FINGIBLE_COLLECTION_ID: CollectionId = CollectionId(0);
 
 	#[pallet::pallet]
 	#[pallet::storage_version(STORAGE_VERSION)]
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -440,6 +440,10 @@
 			collection_id: CollectionId,
 			address: T::CrossAccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
+
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			collection.check_is_internal()?;
@@ -467,6 +471,10 @@
 			collection_id: CollectionId,
 			address: T::CrossAccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
+
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			collection.check_is_internal()?;
@@ -493,6 +501,9 @@
 			collection_id: CollectionId,
 			new_owner: T::AccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let new_owner = T::CrossAccountId::from_sub(new_owner);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
@@ -522,6 +533,9 @@
 			collection_id: CollectionId,
 			new_admin_id: T::CrossAccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			<PalletCommon<T>>::toggle_admin(&collection, &sender, &new_admin_id, true)
@@ -548,6 +562,9 @@
 			collection_id: CollectionId,
 			account_id: T::CrossAccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			<PalletCommon<T>>::toggle_admin(&collection, &sender, &account_id, false)
@@ -573,6 +590,9 @@
 			collection_id: CollectionId,
 			new_sponsor: T::AccountId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			target_collection.set_sponsor(&sender, new_sponsor.clone())
@@ -597,6 +617,9 @@
 			origin: OriginFor<T>,
 			collection_id: CollectionId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = ensure_signed(origin)?;
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			target_collection.confirm_sponsorship(&sender)
@@ -617,6 +640,9 @@
 			origin: OriginFor<T>,
 			collection_id: CollectionId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			target_collection.remove_sponsor(&sender)
@@ -894,6 +920,9 @@
 			collection_id: CollectionId,
 			value: bool,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			target_collection.check_is_internal()?;
@@ -1146,6 +1175,9 @@
 			collection_id: CollectionId,
 			new_limit: CollectionLimits,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			<PalletCommon<T>>::update_limits(&sender, &mut target_collection, new_limit)
@@ -1170,6 +1202,9 @@
 			collection_id: CollectionId,
 			new_permission: CollectionPermissions,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			<PalletCommon<T>>::update_permissions(&sender, &mut target_collection, new_permission)
@@ -1238,6 +1273,9 @@
 			origin: OriginFor<T>,
 			collection_id: CollectionId,
 		) -> DispatchResult {
+			if collection_id == pallet_common::NATIVE_FINGIBLE_COLLECTION_ID {
+				fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+			}
 			ensure_root(origin)?;
 			<PalletCommon<T>>::repair_collection(collection_id)
 		}
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
before · runtime/common/dispatch.rs
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_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				// check params84				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		let collection = <CollectionHandle<T>>::try_get(collection_id)?;104		collection.check_is_internal()?;105106		match collection.mode {107			CollectionMode::ReFungible => {108				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?109			}110			CollectionMode::Fungible(_) => {111				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?112			}113			CollectionMode::NFT => {114				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?115			}116		}117		Ok(())118	}119120	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {121		if collection_id == CollectionId(0) {122			return Ok(Self::NativeFungible(NativeFungibleHandle::new()));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 == CollectionId(0) {188				<NativeFungibleHandle<T>>::new().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}
after · runtime/common/dispatch.rs
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::{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				// check params84				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			// TODO: check token existence184			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			// TODO: check token existence217			RefungibleTokenHandle(h, token_id).call(handle)218		} else {219			None220		}221	}222}