123use 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};14151617pub fn dispatch_weight<T: Config>() -> Weight {18 19 <T as frame_system::Config>::DbWeight::get().reads(1)20 21 + 6_000_00022 23}242526272829pub 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}717273pub trait CollectionDispatch<T: Config> {74 75 76 77 78 fn create(79 sender: T::CrossAccountId,80 data: CreateCollectionData<T::AccountId>,81 ) -> DispatchResult;8283 84 85 86 87 fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;8889 90 91 92 fn dispatch(handle: CollectionHandle<T>) -> Self;9394 95 fn into_inner(self) -> CollectionHandle<T>;9697 98 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;99}