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

difftreelog

refactor create collection with optional payer

Daniel Shiposha2023-10-18parent: #383b7ef.patch.diff
in: master

5 files changed

modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
before · pallets/common/src/dispatch.rs
1//! Module with interfaces for dispatching collections.23use frame_support::{4	dispatch::{5		DispatchErrorWithPostInfo, DispatchResult, DispatchResultWithPostInfo, Pays,6		PostDispatchInfo,7	},8	traits::Get,9};10use sp_runtime::DispatchError;11use sp_weights::Weight;12use up_data_structs::{CollectionId, CreateCollectionData};1314use crate::{pallet::Config, CommonCollectionOperations};1516// TODO: move to benchmarking17/// Price of [`dispatch_tx`] call with noop `call` argument18pub fn dispatch_weight<T: Config>() -> Weight {19	// Read collection20	<T as frame_system::Config>::DbWeight::get().reads(1)21	// Dynamic dispatch?22	+ Weight::from_parts(6_000_000, 0)23	// submit_logs is measured as part of collection pallets24}2526/// Helper function to implement substrate calls for common collection methods.27///28/// * `collection` - The collection on which to call the method.29/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].30pub fn dispatch_tx<31	T: Config,32	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,33>(34	collection: CollectionId,35	call: C,36) -> DispatchResultWithPostInfo {37	let dispatched = T::CollectionDispatch::dispatch(collection)38		.and_then(|dispatched| {39			dispatched.check_is_internal()?;40			Ok(dispatched)41		})42		.map_err(|error| DispatchErrorWithPostInfo {43			post_info: PostDispatchInfo {44				actual_weight: Some(dispatch_weight::<T>()),45				pays_fee: Pays::Yes,46			},47			error,48		})?;49	let mut result = call(dispatched.as_dyn());50	match &mut result {51		Ok(PostDispatchInfo {52			actual_weight: Some(weight),53			..54		})55		| Err(DispatchErrorWithPostInfo {56			post_info: PostDispatchInfo {57				actual_weight: Some(weight),58				..59			},60			..61		}) => *weight += dispatch_weight::<T>(),62		_ => {}63	}64	result65}6667/// Interface for working with different collections through the dispatcher.68pub trait CollectionDispatch<T: Config> {69	/// Check if the collection is internal.70	fn check_is_internal(&self) -> DispatchResult;7172	/// Create a collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).73	///74	/// * `sender` - The user who will become the owner of the collection.75	/// * `payer` - The user who pays the collection creation fee.76	/// * `data` - Description of the created collection.77	fn create(78		sender: T::CrossAccountId,79		payer: T::CrossAccountId,80		data: CreateCollectionData<T::CrossAccountId>,81	) -> Result<CollectionId, DispatchError>;8283	/// Create a foreign collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).84	///85	/// * `sender` - The user who will become the owner of the collection.86	/// * `data` - Description of the created collection.87	fn create_foreign(88		sender: T::CrossAccountId,89		data: CreateCollectionData<T::CrossAccountId>,90	) -> Result<CollectionId, DispatchError>;9192	/// Delete the collection.93	///94	/// * `sender` - The owner of the collection.95	/// * `handle` - Collection handle.96	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;9798	/// Get a specialized collection from the handle.99	///100	/// * `handle` - Collection handle.101	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>102	where103		Self: Sized;104105	/// Get the implementation of [`CommonCollectionOperations`].106	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;107}
after · pallets/common/src/dispatch.rs
1//! Module with interfaces for dispatching collections.23use frame_support::{4	dispatch::{5		DispatchErrorWithPostInfo, DispatchResult, DispatchResultWithPostInfo, Pays,6		PostDispatchInfo,7	},8	traits::Get,9};10use sp_runtime::DispatchError;11use sp_weights::Weight;12use up_data_structs::{CollectionId, CreateCollectionData};1314use crate::{pallet::Config, CommonCollectionOperations};1516// TODO: move to benchmarking17/// Price of [`dispatch_tx`] call with noop `call` argument18pub fn dispatch_weight<T: Config>() -> Weight {19	// Read collection20	<T as frame_system::Config>::DbWeight::get().reads(1)21	// Dynamic dispatch?22	+ Weight::from_parts(6_000_000, 0)23	// submit_logs is measured as part of collection pallets24}2526/// Helper function to implement substrate calls for common collection methods.27///28/// * `collection` - The collection on which to call the method.29/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].30pub fn dispatch_tx<31	T: Config,32	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,33>(34	collection: CollectionId,35	call: C,36) -> DispatchResultWithPostInfo {37	let dispatched = T::CollectionDispatch::dispatch(collection)38		.and_then(|dispatched| {39			dispatched.check_is_internal()?;40			Ok(dispatched)41		})42		.map_err(|error| DispatchErrorWithPostInfo {43			post_info: PostDispatchInfo {44				actual_weight: Some(dispatch_weight::<T>()),45				pays_fee: Pays::Yes,46			},47			error,48		})?;49	let mut result = call(dispatched.as_dyn());50	match &mut result {51		Ok(PostDispatchInfo {52			actual_weight: Some(weight),53			..54		})55		| Err(DispatchErrorWithPostInfo {56			post_info: PostDispatchInfo {57				actual_weight: Some(weight),58				..59			},60			..61		}) => *weight += dispatch_weight::<T>(),62		_ => {}63	}64	result65}6667/// Interface for working with different collections through the dispatcher.68pub trait CollectionDispatch<T: Config> {69	/// Check if the collection is internal.70	fn check_is_internal(&self) -> DispatchResult;7172	/// Create a collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).73	///74	/// * `sender` - The user who will become the owner of the collection.75	/// * `payer` - If set, the user who pays the collection creation deposit.76	/// * `data` - Description of the created collection.77	fn create(78		sender: T::CrossAccountId,79		payer: Option<T::CrossAccountId>,80		data: CreateCollectionData<T::CrossAccountId>,81	) -> Result<CollectionId, DispatchError>;8283	/// Delete the collection.84	///85	/// * `sender` - The owner of the collection.86	/// * `handle` - Collection handle.87	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;8889	/// Get a specialized collection from the handle.90	///91	/// * `handle` - Collection handle.92	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>93	where94		Self: Sized;9596	/// Get the implementation of [`CommonCollectionOperations`].97	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;98}
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -152,8 +152,10 @@
 				.try_into()
 				.expect("description length < max description length; qed");
 
-			let collection_id = T::CollectionDispatch::create_foreign(
+			let payer = None;
+			let collection_id = T::CollectionDispatch::create(
 				foreign_collection_owner,
+				payer,
 				CreateCollectionData {
 					name,
 					description,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -110,8 +110,9 @@
 	let collection_helpers_address =
 		T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
 
-	let collection_id = T::CollectionDispatch::create(caller, collection_helpers_address, data)
-		.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+	let collection_id =
+		T::CollectionDispatch::create(caller, Some(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)
 }
@@ -240,8 +241,9 @@
 		let collection_helpers_address =
 			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
 
-		let collection_id = T::CollectionDispatch::create(caller, collection_helpers_address, data)
-			.map_err(dispatch_to_evm::<T>)?;
+		let collection_id =
+			T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
+				.map_err(dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
 		Ok(address)
@@ -274,8 +276,9 @@
 		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, collection_helpers_address, data)
-			.map_err(dispatch_to_evm::<T>)?;
+		let collection_id =
+			T::CollectionDispatch::create(caller, Some(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
@@ -401,7 +401,7 @@
 
 			// =========
 			let sender = T::CrossAccountId::from_sub(sender);
-			let _id = T::CollectionDispatch::create(sender.clone(), sender, data)?;
+			let _id = T::CollectionDispatch::create(sender.clone(), Some(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, unsupported, CollectionById, CollectionHandle,
+	erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,
 	CommonCollectionOperations, Pallet as PalletCommon,
 };
 use pallet_evm::{PrecompileHandle, PrecompileResult};
@@ -68,7 +68,7 @@
 
 	fn create(
 		sender: T::CrossAccountId,
-		payer: T::CrossAccountId,
+		payer: Option<T::CrossAccountId>,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError> {
 		match data.mode {
@@ -85,28 +85,7 @@
 
 			_ => {}
 		};
-
-		<PalletCommon<T>>::init_collection(sender, Some(payer), data)
-	}
 
-	fn create_foreign(
-		sender: <T>::CrossAccountId,
-		data: CreateCollectionData<<T>::CrossAccountId>,
-	) -> Result<CollectionId, DispatchError> {
-		match data.mode {
-			CollectionMode::Fungible(decimal_points) => {
-				// check params
-				ensure!(
-					decimal_points <= MAX_DECIMAL_POINTS,
-					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded
-				);
-			}
-
-			CollectionMode::ReFungible => return unsupported!(T),
-			_ => {}
-		};
-
-		let payer = None;
 		<PalletCommon<T>>::init_collection(sender, payer, data)
 	}