git.delta.rocks / unique-network / refs/commits / 008a188c3ea9

difftreelog

source

pallets/common/src/dispatch.rs1.9 KiBsourcehistory
1use frame_support::{2	dispatch::{3		DispatchResultWithPostInfo, PostDispatchInfo, Weight, DispatchErrorWithPostInfo,4		DispatchResult,5	},6	weights::Pays,7	traits::Get,8};9use up_data_structs::{CollectionId, CreateCollectionData};1011use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};1213// TODO: move to benchmarking14/// Price of [`dispatch_call`] call with noop `call` argument15pub fn dispatch_weight<T: Config>() -> Weight {16	// Read collection17	<T as frame_system::Config>::DbWeight::get().reads(1)18	// Dynamic dispatch?19	+ 6_000_00020	// submit_logs is measured as part of collection pallets21}2223/// Helper function to implement substrate calls for common collection methods24pub fn dispatch_call<25	T: Config,26	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,27>(28	collection: CollectionId,29	call: C,30) -> DispatchResultWithPostInfo {31	let handle =32		CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {33			post_info: PostDispatchInfo {34				actual_weight: Some(dispatch_weight::<T>()),35				pays_fee: Pays::Yes,36			},37			error,38		})?;39	let dispatched = T::CollectionDispatch::dispatch(handle);40	let mut result = call(dispatched.as_dyn());41	match &mut result {42		Ok(PostDispatchInfo {43			actual_weight: Some(weight),44			..45		})46		| Err(DispatchErrorWithPostInfo {47			post_info: PostDispatchInfo {48				actual_weight: Some(weight),49				..50			},51			..52		}) => *weight += dispatch_weight::<T>(),53		_ => {}54	}5556	dispatched.into_inner().submit_logs();57	result58}5960pub trait CollectionDispatch<T: Config> {61	fn create(sender: T::AccountId, data: CreateCollectionData<T::AccountId>) -> DispatchResult;62	fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;6364	fn dispatch(handle: CollectionHandle<T>) -> Self;65	fn into_inner(self) -> CollectionHandle<T>;6667	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;68}