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 77 fn create(78 sender: T::CrossAccountId,79 payer: T::CrossAccountId,80 data: CreateCollectionData<T::CrossAccountId>,81 ) -> Result<CollectionId, DispatchError>;8283 84 85 86 87 fn create_foreign(88 sender: T::CrossAccountId,89 data: CreateCollectionData<T::CrossAccountId>,90 ) -> Result<CollectionId, DispatchError>;9192 93 94 95 96 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;9798 99 100 101 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>102 where103 Self: Sized;104105 106 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;107}