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};12131415pub fn dispatch_weight<T: Config>() -> Weight {16 17 <T as frame_system::Config>::DbWeight::get().reads(1)18 19 + 6_000_00020 21}222324pub 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 handle.check_is_internal().map_err(|error| DispatchErrorWithPostInfo {40 post_info: PostDispatchInfo {41 actual_weight: Some(dispatch_weight::<T>()),42 pays_fee: Pays::Yes,43 },44 error,45 })?;46 let dispatched = T::CollectionDispatch::dispatch(handle);47 let mut result = call(dispatched.as_dyn());48 match &mut result {49 Ok(PostDispatchInfo {50 actual_weight: Some(weight),51 ..52 })53 | Err(DispatchErrorWithPostInfo {54 post_info: PostDispatchInfo {55 actual_weight: Some(weight),56 ..57 },58 ..59 }) => *weight += dispatch_weight::<T>(),60 _ => {}61 }62 result63}6465pub trait CollectionDispatch<T: Config> {66 fn create(67 sender: T::CrossAccountId,68 data: CreateCollectionData<T::AccountId>,69 ) -> DispatchResult;70 fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;7172 fn dispatch(handle: CollectionHandle<T>) -> Self;73 fn into_inner(self) -> CollectionHandle<T>;7475 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;76}