123use frame_support::{4 dispatch::{5 DispatchErrorWithPostInfo, DispatchResult, DispatchResultWithPostInfo, Pays,6 PostDispatchInfo,7 },8 traits::Get,9};10use sp_runtime::DispatchError;11use sp_weights::Weight;12use up_data_structs::{CollectionId, CreateCollectionData};1314use crate::{pallet::Config, CommonCollectionOperations};15161718pub fn dispatch_weight<T: Config>() -> Weight {19 20 <T as frame_system::Config>::DbWeight::get().reads(1)21 22 + Weight::from_parts(6_000_000, 0)23 24}252627282930pub fn dispatch_tx<31 T: Config,32 C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,33>(34 collection: CollectionId,35 call: C,36) -> DispatchResultWithPostInfo {37 let dispatched = T::CollectionDispatch::dispatch(collection)38 .and_then(|dispatched| {39 dispatched.check_is_internal()?;40 Ok(dispatched)41 })42 .map_err(|error| DispatchErrorWithPostInfo {43 post_info: PostDispatchInfo {44 actual_weight: Some(dispatch_weight::<T>()),45 pays_fee: Pays::Yes,46 },47 error,48 })?;49 let mut result = call(dispatched.as_dyn());50 match &mut result {51 Ok(PostDispatchInfo {52 actual_weight: Some(weight),53 ..54 })55 | Err(DispatchErrorWithPostInfo {56 post_info: PostDispatchInfo {57 actual_weight: Some(weight),58 ..59 },60 ..61 }) => *weight += dispatch_weight::<T>(),62 _ => {}63 }64 result65}666768pub trait CollectionDispatch<T: Config> {69 70 fn check_is_internal(&self) -> DispatchResult;7172 73 74 75 76 fn create(77 sender: T::CrossAccountId,78 payer: T::CrossAccountId,79 data: CreateCollectionData<T::CrossAccountId>,80 ) -> Result<CollectionId, DispatchError>;8182 83 84 85 86 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;8788 89 90 91 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>92 where93 Self: Sized;9495 96 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;97}