1use frame_support::{2 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo},3 traits::Get,4 weights::Weight,5};6use nft_data_structs::{CollectionId, CollectionMode, Pays, PostDispatchInfo};7use pallet_common::{CollectionHandle, CommonCollectionOperations};8use pallet_fungible::FungibleHandle;9use pallet_nonfungible::NonfungibleHandle;10use pallet_refungible::RefungibleHandle;1112use crate::Config;13141516pub fn dispatch_weight<T: Config>() -> Weight {17 18 <T as frame_system::Config>::DbWeight::get().reads(1)19 20 + 6_000_00021 22}2324pub enum Dispatched<T: Config> {25 Fungible(FungibleHandle<T>),26 Nonfungible(NonfungibleHandle<T>),27 Refungible(RefungibleHandle<T>),28}29impl<T: Config> Dispatched<T> {30 pub fn dispatch(handle: CollectionHandle<T>) -> Self {31 match handle.mode {32 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),33 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),34 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),35 }36 }37 fn into_inner(self) -> CollectionHandle<T> {38 match self {39 Dispatched::Fungible(f) => f.into_inner(),40 Dispatched::Nonfungible(f) => f.into_inner(),41 Dispatched::Refungible(f) => f.into_inner(),42 }43 }44 pub fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {45 match self {46 Dispatched::Fungible(h) => h,47 Dispatched::Nonfungible(h) => h,48 Dispatched::Refungible(h) => h,49 }50 }51}525354pub fn dispatch_call<55 T: Config,56 C: FnOnce(&dyn pallet_common::CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,57>(58 collection: CollectionId,59 call: C,60) -> DispatchResultWithPostInfo {61 let handle =62 CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {63 post_info: PostDispatchInfo {64 actual_weight: Some(dispatch_weight::<T>()),65 pays_fee: Pays::Yes,66 },67 error,68 })?;69 let dispatched = Dispatched::dispatch(handle);70 let mut result = call(dispatched.as_dyn());71 match &mut result {72 Ok(PostDispatchInfo {73 actual_weight: Some(weight),74 ..75 })76 | Err(DispatchErrorWithPostInfo {77 post_info: PostDispatchInfo {78 actual_weight: Some(weight),79 ..80 },81 ..82 }) => *weight += dispatch_weight::<T>(),83 _ => {}84 }8586 dispatched.into_inner().submit_logs();87 result88}