git.delta.rocks / unique-network / refs/commits / 9d0fd83a5da6

difftreelog

fix use CollectionIssuer enum for collection creation

Daniel Shiposha2023-11-24parent: #b4d745d.patch.diff
in: master

6 files changed

modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
11use sp_weights::Weight;11use sp_weights::Weight;
12use up_data_structs::{CollectionId, CreateCollectionData};12use up_data_structs::{CollectionId, CreateCollectionData};
1313
14use crate::{pallet::Config, CommonCollectionOperations};14use crate::{pallet::Config, CollectionIssuer, CommonCollectionOperations};
1515
16// TODO: move to benchmarking16// TODO: move to benchmarking
17/// Price of [`dispatch_tx`] call with noop `call` argument17/// Price of [`dispatch_tx`] call with noop `call` argument
72 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).72 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).
73 ///73 ///
74 /// * `sender` - The user who will become the owner of the collection.74 /// * `sender` - The user who will become the owner of the collection.
75 /// * `payer` - If set, the user who pays the collection creation deposit.75 /// * `issuer` - An entity that creates the collection.
76 /// * `data` - Description of the created collection.76 /// * `data` - Description of the created collection.
77 fn create(77 fn create(
78 sender: T::CrossAccountId,78 sender: T::CrossAccountId,
79 payer: Option<T::CrossAccountId>,79 issuer: CollectionIssuer<T::CrossAccountId>,
80 data: CreateCollectionData<T::CrossAccountId>,80 data: CreateCollectionData<T::CrossAccountId>,
81 ) -> Result<CollectionId, DispatchError> {81 ) -> Result<CollectionId, DispatchError>;
82 Self::create_raw(sender, payer, false, data)
83 }
84
85 /// Function for creating regular and special collections.
86 ///
87 /// * `sender` - The user who will become the owner of the collection.
88 /// * `payer` - If set, the user who pays the collection creation deposit.
89 /// * `data` - Description of the created collection.
90 /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
91 fn create_raw(
92 sender: T::CrossAccountId,
93 payer: Option<T::CrossAccountId>,
94 is_special_collection: bool,
95 data: CreateCollectionData<T::CrossAccountId>,
96 ) -> Result<CollectionId, DispatchError>;
9782
98 /// Delete the collection.83 /// Delete the collection.
99 ///84 ///
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -944,6 +944,15 @@
 	}
 }
 
+/// An issuer of a collection.
+pub enum CollectionIssuer<CrossAccountId> {
+	/// A user who creates the collection.
+	User(CrossAccountId),
+
+	/// The internal mechanisms are creating the collection.
+	Internals,
+}
+
 fn check_token_permissions<T: Config>(
 	collection_admin_permitted: bool,
 	token_owner_permitted: bool,
@@ -1128,32 +1137,34 @@
 	/// Create new collection.
 	///
 	/// * `owner` - The owner of the collection.
-	/// * `payer` - If set, the user that will pay a deposit for the collection creation.
-	/// * `data` - Description of the created collection.
+	/// * `issuer` - An entity that creates the collection.
 	/// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
 	pub fn init_collection(
 		owner: T::CrossAccountId,
-		payer: Option<T::CrossAccountId>,
-		is_special_collection: bool,
+		issuer: CollectionIssuer<T::CrossAccountId>,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError> {
-		if !is_special_collection {
-			ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
-		}
+		match issuer {
+			CollectionIssuer::User(payer) => {
+				ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
 
-		// Take a (non-refundable) deposit of collection creation
-		if let Some(payer) = payer {
-			let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
-			imbalance.subsume(<T as Config>::Currency::deposit(
-				&T::TreasuryAccountId::get(),
-				T::CollectionCreationPrice::get(),
-				Precision::Exact,
-			)?);
-			let credit =
-				<T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)
-					.map_err(|_| Error::<T>::NotSufficientFounds)?;
+				// Take a (non-refundable) deposit of collection creation
+				let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
+				imbalance.subsume(<T as Config>::Currency::deposit(
+					&T::TreasuryAccountId::get(),
+					T::CollectionCreationPrice::get(),
+					Precision::Exact,
+				)?);
+				let credit = <T as Config>::Currency::settle(
+					payer.as_sub(),
+					imbalance,
+					Preservation::Preserve,
+				)
+				.map_err(|_| Error::<T>::NotSufficientFounds)?;
 
-			debug_assert!(credit.peek().is_zero())
+				debug_assert!(credit.peek().is_zero());
+			}
+			CollectionIssuer::Internals => {}
 		}
 
 		{
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -56,6 +56,7 @@
 
 #[frame_support::pallet]
 pub mod module {
+	use pallet_common::CollectionIssuer;
 	use up_data_structs::{
 		CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,
 	};
@@ -169,12 +170,9 @@
 				.try_into()
 				.expect("description length < max description length; qed");
 
-			let payer = None;
-			let is_special_collection = true;
-			let collection_id = T::CollectionDispatch::create_raw(
+			let collection_id = T::CollectionDispatch::create(
 				foreign_collection_owner,
-				payer,
-				is_special_collection,
+				CollectionIssuer::Internals,
 				CreateCollectionData {
 					name,
 					token_prefix,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -26,7 +26,7 @@
 	dispatch::CollectionDispatch,
 	erc::{static_property::key, CollectionHelpersEvents},
 	eth::{self, collection_id_to_address, map_eth_to_id},
-	CollectionById, CollectionHandle, Pallet as PalletCommon,
+	CollectionById, CollectionHandle, CollectionIssuer, Pallet as PalletCommon,
 };
 use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
 use pallet_evm_coder_substrate::{
@@ -110,9 +110,12 @@
 	let collection_helpers_address =
 		T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
 
-	let collection_id =
-		T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
-			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+	let collection_id = T::CollectionDispatch::create(
+		caller,
+		CollectionIssuer::User(collection_helpers_address),
+		data,
+	)
+	.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 	let address = pallet_common::eth::collection_id_to_address(collection_id);
 	Ok(address)
 }
@@ -241,9 +244,12 @@
 		let collection_helpers_address =
 			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
 
-		let collection_id =
-			T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
-				.map_err(dispatch_to_evm::<T>)?;
+		let collection_id = T::CollectionDispatch::create(
+			caller,
+			CollectionIssuer::User(collection_helpers_address),
+			data,
+		)
+		.map_err(dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
 		Ok(address)
@@ -276,9 +282,12 @@
 		check_sent_amount_equals_collection_creation_price::<T>(value)?;
 		let collection_helpers_address =
 			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
-		let collection_id =
-			T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
-				.map_err(dispatch_to_evm::<T>)?;
+		let collection_id = T::CollectionDispatch::create(
+			caller,
+			CollectionIssuer::User(collection_helpers_address),
+			data,
+		)
+		.map_err(dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
 		Ok(address)
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -93,7 +93,8 @@
 	use frame_system::{ensure_root, ensure_signed};
 	use pallet_common::{
 		dispatch::{dispatch_tx, CollectionDispatch},
-		CollectionHandle, CommonWeightInfo, Pallet as PalletCommon, RefungibleExtensionsWeightInfo,
+		CollectionHandle, CollectionIssuer, CommonWeightInfo, Pallet as PalletCommon,
+		RefungibleExtensionsWeightInfo,
 	};
 	use pallet_evm::account::CrossAccountId;
 	use pallet_structure::weights::WeightInfo as StructureWeightInfo;
@@ -401,7 +402,11 @@
 
 			// =========
 			let sender = T::CrossAccountId::from_sub(sender);
-			let _id = T::CollectionDispatch::create(sender.clone(), Some(sender), data)?;
+			let _id = T::CollectionDispatch::create(
+				sender.clone(),
+				CollectionIssuer::User(sender),
+				data,
+			)?;
 
 			Ok(())
 		}
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
--- a/runtime/common/dispatch.rs
+++ b/runtime/common/dispatch.rs
@@ -18,7 +18,7 @@
 use pallet_balances_adapter::NativeFungibleHandle;
 pub use pallet_common::dispatch::CollectionDispatch;
 use pallet_common::{
-	erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,
+	erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle, CollectionIssuer,
 	CommonCollectionOperations, Pallet as PalletCommon,
 };
 use pallet_evm::{PrecompileHandle, PrecompileResult};
@@ -66,10 +66,9 @@
 		}
 	}
 
-	fn create_raw(
+	fn create(
 		sender: T::CrossAccountId,
-		payer: Option<T::CrossAccountId>,
-		is_special_collection: bool,
+		issuer: CollectionIssuer<T::CrossAccountId>,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError> {
 		match data.mode {
@@ -87,7 +86,7 @@
 			_ => {}
 		};
 
-		<PalletCommon<T>>::init_collection(sender, payer, is_special_collection, data)
+		<PalletCommon<T>>::init_collection(sender, issuer, data)
 	}
 
 	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {