git.delta.rocks / unique-network / refs/commits / 71be677911ce

difftreelog

source

pallets/common/src/dispatch.rs2.9 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, 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	/// * `data` - Description of the created collection.76	fn create(77		sender: T::CrossAccountId,78		payer: T::CrossAccountId,79		data: CreateCollectionData<T::CrossAccountId>,80	) -> Result<CollectionId, DispatchError>;8182	/// Delete the collection.83	///84	/// * `sender` - The owner of the collection.85	/// * `handle` - Collection handle.86	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;8788	/// Get a specialized collection from the handle.89	///90	/// * `handle` - Collection handle.91	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>92	where93		Self: Sized;9495	/// Get the implementation of [`CommonCollectionOperations`].96	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;97}