git.delta.rocks / unique-network / refs/commits / 3f2c4d36d2e0

difftreelog

source

pallets/common/src/dispatch.rs3.0 KiBsourcehistory
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, CollectionIssuer, 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 regular 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	/// * `issuer` - An entity that creates the collection.76	/// * `data` - Description of the created collection.77	fn create(78		sender: T::CrossAccountId,79		issuer: CollectionIssuer<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}