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 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 }55 result56}5758pub trait CollectionDispatch<T: Config> {59 fn create(sender: T::AccountId, data: CreateCollectionData<T::AccountId>) -> DispatchResult;60 fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;6162 fn dispatch(handle: CollectionHandle<T>) -> Self;63 fn into_inner(self) -> CollectionHandle<T>;6465 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;66}