git.delta.rocks / unique-network / refs/commits / 52730495c578

difftreelog

patch: Change dispathed call for create item (return collection id).

Trubnikov Sergey2022-07-25parent: #6434da3.patch.diff
in: master

4 files changed

modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
before · pallets/common/src/dispatch.rs
1//! Module with interfaces for dispatching collections.23use frame_support::{4	dispatch::{5		DispatchResultWithPostInfo, PostDispatchInfo, Weight, DispatchErrorWithPostInfo,6		DispatchResult,7	},8	weights::Pays,9	traits::Get,10};11use up_data_structs::{CollectionId, CreateCollectionData};1213use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};1415// TODO: move to benchmarking16/// Price of [`dispatch_tx`] call with noop `call` argument17pub fn dispatch_weight<T: Config>() -> Weight {18	// Read collection19	<T as frame_system::Config>::DbWeight::get().reads(1)20	// Dynamic dispatch?21	+ 6_000_00022	// submit_logs is measured as part of collection pallets23}2425/// Helper function to implement substrate calls for common collection methods.26///27/// * `collection` - The collection on which to call the method.28/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].29pub fn dispatch_tx<30	T: Config,31	C: FnOnce(&dyn CommonCollectionOperations<T>) -> DispatchResultWithPostInfo,32>(33	collection: CollectionId,34	call: C,35) -> DispatchResultWithPostInfo {36	let handle =37		CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {38			post_info: PostDispatchInfo {39				actual_weight: Some(dispatch_weight::<T>()),40				pays_fee: Pays::Yes,41			},42			error,43		})?;44	handle45		.check_is_internal()46		.map_err(|error| DispatchErrorWithPostInfo {47			post_info: PostDispatchInfo {48				actual_weight: Some(dispatch_weight::<T>()),49				pays_fee: Pays::Yes,50			},51			error,52		})?;53	let dispatched = T::CollectionDispatch::dispatch(handle);54	let mut result = call(dispatched.as_dyn());55	match &mut result {56		Ok(PostDispatchInfo {57			actual_weight: Some(weight),58			..59		})60		| Err(DispatchErrorWithPostInfo {61			post_info: PostDispatchInfo {62				actual_weight: Some(weight),63				..64			},65			..66		}) => *weight += dispatch_weight::<T>(),67		_ => {}68	}69	result70}7172/// Interface for working with different collections through the dispatcher.73pub trait CollectionDispatch<T: Config> {74	/// Create a collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).75	///76	/// * `sender` - The user who will become the owner of the collection.77	/// * `data` - Description of the created collection.78	fn create(79		sender: T::CrossAccountId,80		data: CreateCollectionData<T::AccountId>,81	) -> DispatchResult;8283	/// Delete the collection.84	///85	/// * `sender` - The owner of the collection.86	/// * `handle` - Collection handle.87	fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;8889	/// Get a specialized collection from the handle.90	///91	/// * `handle` - Collection handle.92	fn dispatch(handle: CollectionHandle<T>) -> Self;9394	/// Get the collection handle for the corresponding implementation.95	fn into_inner(self) -> CollectionHandle<T>;9697	/// Get the implementation of [`CommonCollectionOperations`].98	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;99}
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -32,6 +32,7 @@
 		static_property::{key, value as property_value},
 		CollectionHelpersEvents,
 	},
+	dispatch::CollectionDispatch,
 };
 use crate::{SelfWeightOf, Config, weights::WeightInfo};
 
@@ -179,9 +180,8 @@
 			Default::default(),
 			false,
 		)?;
-		let collection_id =
-			<pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)
-				.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+		let collection_id = T::CollectionDispatch::create(caller, data)
+			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
 		Ok(address)
@@ -207,9 +207,8 @@
 			base_uri_value,
 			true,
 		)?;
-		let collection_id =
-			<pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, true)
-				.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+		let collection_id = T::CollectionDispatch::create(caller, data)
+			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
 		Ok(address)
@@ -233,7 +232,7 @@
 			Default::default(),
 			false,
 		)?;
-		let collection_id = <pallet_refungible::Pallet<T>>::init_collection(caller.clone(), data)
+		let collection_id = T::CollectionDispatch::create(caller, data)
 			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
@@ -260,7 +259,7 @@
 			base_uri_value,
 			true,
 		)?;
-		let collection_id = <pallet_refungible::Pallet<T>>::init_collection(caller.clone(), data)
+		let collection_id = T::CollectionDispatch::create(caller, data)
 			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -352,7 +352,7 @@
 
 			// =========
 
-			T::CollectionDispatch::create(T::CrossAccountId::from_sub(sender), data)?;
+			let _id = T::CollectionDispatch::create(T::CrossAccountId::from_sub(sender), data)?;
 
 			Ok(())
 		}
modifiedruntime/common/src/dispatch.rsdiffbeforeafterboth
--- a/runtime/common/src/dispatch.rs
+++ b/runtime/common/src/dispatch.rs
@@ -17,6 +17,7 @@
 use frame_support::{dispatch::DispatchResult, ensure};
 use pallet_evm::{PrecompileHandle, PrecompileResult};
 use sp_core::H160;
+use sp_runtime::DispatchError;
 use sp_std::{borrow::ToOwned, vec::Vec};
 use pallet_common::{
 	CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,
@@ -30,6 +31,7 @@
 };
 use up_data_structs::{
 	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,
+	CollectionId,
 };
 
 pub enum CollectionDispatchT<T>
@@ -51,8 +53,8 @@
 	fn create(
 		sender: T::CrossAccountId,
 		data: CreateCollectionData<T::AccountId>,
-	) -> DispatchResult {
-		let _id = match data.mode {
+	) -> Result<CollectionId, DispatchError> {
+		let id = match data.mode {
 			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data, false)?,
 			CollectionMode::Fungible(decimal_points) => {
 				// check params
@@ -64,7 +66,7 @@
 			}
 			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,
 		};
-		Ok(())
+		Ok(id)
 	}
 
 	fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {