1234567891011121314151617use frame_support::{18 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo},19 traits::Get,20 weights::Weight,21};22use up_data_structs::{CollectionId, CollectionMode, Pays, PostDispatchInfo};23use pallet_common::{CollectionHandle, CommonCollectionOperations};24use pallet_fungible::FungibleHandle;25use pallet_nonfungible::NonfungibleHandle;26use pallet_refungible::RefungibleHandle;2728use crate::Config;29303132pub fn dispatch_weight<T: Config>() -> Weight {33 34 <T as frame_system::Config>::DbWeight::get().reads(1)35 36 + 6_000_00037 38}3940pub enum Dispatched<T: Config> {41 Fungible(FungibleHandle<T>),42 Nonfungible(NonfungibleHandle<T>),43 Refungible(RefungibleHandle<T>),44}45impl<T: Config> Dispatched<T> {46 pub fn dispatch(handle: CollectionHandle<T>) -> Self {47 match handle.mode {48 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),49 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),50 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),51 }52 }53 fn into_inner(self) -> CollectionHandle<T> {54 match self {55 Dispatched::Fungible(f) => f.into_inner(),56 Dispatched::Nonfungible(f) => f.into_inner(),57 Dispatched::Refungible(f) => f.into_inner(),58 }59 }60 pub fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {61 match self {62 Dispatched::Fungible(h) => h,63 Dispatched::Nonfungible(h) => h,64 Dispatched::Refungible(h) => h,65 }66 }67}686970pub fn dispatch_call<71 T: Config,72 C: FnOnce(&dyn pallet_common::CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,73>(74 collection: CollectionId,75 call: C,76) -> DispatchResultWithPostInfo {77 let handle =78 CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {79 post_info: PostDispatchInfo {80 actual_weight: Some(dispatch_weight::<T>()),81 pays_fee: Pays::Yes,82 },83 error,84 })?;85 let dispatched = Dispatched::dispatch(handle);86 let mut result = call(dispatched.as_dyn());87 match &mut result {88 Ok(PostDispatchInfo {89 actual_weight: Some(weight),90 ..91 })92 | Err(DispatchErrorWithPostInfo {93 post_info: PostDispatchInfo {94 actual_weight: Some(weight),95 ..96 },97 ..98 }) => *weight += dispatch_weight::<T>(),99 _ => {}100 }101102 dispatched.into_inner().submit_logs();103 result104}