git.delta.rocks / unique-network / refs/commits / 335fdfbf30ef

difftreelog

source

pallets/unique/src/dispatch.rs3.2 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use 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;2930// TODO: move to benchmarking31/// Price of [`dispatch_call`] call with noop `call` argument32pub fn dispatch_weight<T: Config>() -> Weight {33	// Read collection34	<T as frame_system::Config>::DbWeight::get().reads(1)35	// Dynamic dispatch?36	+ 6_000_00037	// submit_logs is measured as part of collection pallets38}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}6869/// Helper function to implement substrate calls for common collection methods70pub 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}