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

difftreelog

source

pallets/common/src/dispatch.rs2.9 KiBsourcehistory
1//! Module with interfaces for dispatching collections.23use frame_support::{4	dispatch::{5		DispatchResultWithPostInfo, PostDispatchInfo, Weight, DispatchErrorWithPostInfo,6		DispatchResult,7	},8	weights::Pays,9	traits::Get,10};11use up_data_structs::{CollectionId, CreateCollectionData};1213use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};1415// TODO: move to benchmarking16/// Price of [`dispatch_tx`] call with noop `call` argument17pub fn dispatch_weight<T: Config>() -> Weight {18	// Read collection19	<T as frame_system::Config>::DbWeight::get().reads(1)20	// Dynamic dispatch?21	+ 6_000_00022	// submit_logs is measured as part of collection pallets23}2425/// Helper function to implement substrate calls for common collection methods.26///27/// * `collection` - The collection on which to call the method.28/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].29pub fn dispatch_tx<30	T: Config,31	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,32>(33	collection: CollectionId,34	call: C,35) -> DispatchResultWithPostInfo {36	let handle =37		CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {38			post_info: PostDispatchInfo {39				actual_weight: Some(dispatch_weight::<T>()),40				pays_fee: Pays::Yes,41			},42			error,43		})?;44	handle45		.check_is_internal()46		.map_err(|error| DispatchErrorWithPostInfo {47			post_info: PostDispatchInfo {48				actual_weight: Some(dispatch_weight::<T>()),49				pays_fee: Pays::Yes,50			},51			error,52		})?;53	let dispatched = T::CollectionDispatch::dispatch(handle);54	let mut result = call(dispatched.as_dyn());55	match &mut result {56		Ok(PostDispatchInfo {57			actual_weight: Some(weight),58			..59		})60		| Err(DispatchErrorWithPostInfo {61			post_info: PostDispatchInfo {62				actual_weight: Some(weight),63				..64			},65			..66		}) => *weight += dispatch_weight::<T>(),67		_ => {}68	}69	result70}7172/// Interface for working with different collections through the dispatcher.73pub trait CollectionDispatch<T: Config> {74	/// Create a collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).75	///76	/// * `sender` - The user who will become the owner of the collection.77	/// * `data` - Description of the created collection.78	fn create(79		sender: T::CrossAccountId,80		data: CreateCollectionData<T::AccountId>,81	) -> DispatchResult;8283	/// Delete the collection.84	///85	/// * `sender` - The owner of the collection.86	/// * `handle` - Collection handle.87	fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;8889	/// Get a specialized collection from the handle.90	///91	/// * `handle` - Collection handle.92	fn dispatch(handle: CollectionHandle<T>) -> Self;9394	/// Get the collection handle for the corresponding implementation.95	fn into_inner(self) -> CollectionHandle<T>;9697	/// Get the implementation of [`CommonCollectionOperations`].98	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;99}