difftreelog
fix use CollectionIssuer enum for collection creation
in: master
6 files changed
pallets/common/src/dispatch.rsdiffbeforeafterboth1//! Module with interfaces for dispatching collections.23use 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};1516// TODO: move to benchmarking17/// Price of [`dispatch_tx`] call with noop `call` argument18pub fn dispatch_weight<T: Config>() -> Weight {19 // Read collection20 <T as frame_system::Config>::DbWeight::get().reads(1)21 // Dynamic dispatch?22 + Weight::from_parts(6_000_000, 0)23 // submit_logs is measured as part of collection pallets24}2526/// Helper function to implement substrate calls for common collection methods.27///28/// * `collection` - The collection on which to call the method.29/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].30pub 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}6667/// Interface for working with different collections through the dispatcher.68pub trait CollectionDispatch<T: Config> {69 /// Check if the collection is internal.70 fn check_is_internal(&self) -> DispatchResult;7172 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).73 ///74 /// * `sender` - The user who will become the owner of the collection.75 /// * `payer` - If set, the user who pays the collection creation deposit.76 /// * `data` - Description of the created collection.77 fn create(78 sender: T::CrossAccountId,79 payer: Option<T::CrossAccountId>,80 data: CreateCollectionData<T::CrossAccountId>,81 ) -> Result<CollectionId, DispatchError> {82 Self::create_raw(sender, payer, false, data)83 }8485 /// Function for creating regular and special collections.86 ///87 /// * `sender` - The user who will become the owner of the collection.88 /// * `payer` - If set, the user who pays the collection creation deposit.89 /// * `data` - Description of the created collection.90 /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.91 fn create_raw(92 sender: T::CrossAccountId,93 payer: Option<T::CrossAccountId>,94 is_special_collection: bool,95 data: CreateCollectionData<T::CrossAccountId>,96 ) -> Result<CollectionId, DispatchError>;9798 /// Delete the collection.99 ///100 /// * `sender` - The owner of the collection.101 /// * `handle` - Collection handle.102 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;103104 /// Get a specialized collection from the handle.105 ///106 /// * `handle` - Collection handle.107 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>108 where109 Self: Sized;110111 /// Get the implementation of [`CommonCollectionOperations`].112 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;113}1//! Module with interfaces for dispatching collections.23use 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, CollectionIssuer, CommonCollectionOperations};1516// TODO: move to benchmarking17/// Price of [`dispatch_tx`] call with noop `call` argument18pub fn dispatch_weight<T: Config>() -> Weight {19 // Read collection20 <T as frame_system::Config>::DbWeight::get().reads(1)21 // Dynamic dispatch?22 + Weight::from_parts(6_000_000, 0)23 // submit_logs is measured as part of collection pallets24}2526/// Helper function to implement substrate calls for common collection methods.27///28/// * `collection` - The collection on which to call the method.29/// * `call` - The function in which to call the corresponding method from [`CommonCollectionOperations`].30pub 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}6667/// Interface for working with different collections through the dispatcher.68pub trait CollectionDispatch<T: Config> {69 /// Check if the collection is internal.70 fn check_is_internal(&self) -> DispatchResult;7172 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).73 ///74 /// * `sender` - The user who will become the owner of the collection.75 /// * `issuer` - An entity that creates the collection.76 /// * `data` - Description of the created collection.77 fn create(78 sender: T::CrossAccountId,79 issuer: CollectionIssuer<T::CrossAccountId>,80 data: CreateCollectionData<T::CrossAccountId>,81 ) -> Result<CollectionId, DispatchError>;8283 /// Delete the collection.84 ///85 /// * `sender` - The owner of the collection.86 /// * `handle` - Collection handle.87 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult;8889 /// Get a specialized collection from the handle.90 ///91 /// * `handle` - Collection handle.92 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>93 where94 Self: Sized;9596 /// Get the implementation of [`CommonCollectionOperations`].97 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;98}pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -944,6 +944,15 @@
}
}
+/// An issuer of a collection.
+pub enum CollectionIssuer<CrossAccountId> {
+ /// A user who creates the collection.
+ User(CrossAccountId),
+
+ /// The internal mechanisms are creating the collection.
+ Internals,
+}
+
fn check_token_permissions<T: Config>(
collection_admin_permitted: bool,
token_owner_permitted: bool,
@@ -1128,32 +1137,34 @@
/// Create new collection.
///
/// * `owner` - The owner of the collection.
- /// * `payer` - If set, the user that will pay a deposit for the collection creation.
- /// * `data` - Description of the created collection.
+ /// * `issuer` - An entity that creates the collection.
/// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
pub fn init_collection(
owner: T::CrossAccountId,
- payer: Option<T::CrossAccountId>,
- is_special_collection: bool,
+ issuer: CollectionIssuer<T::CrossAccountId>,
data: CreateCollectionData<T::CrossAccountId>,
) -> Result<CollectionId, DispatchError> {
- if !is_special_collection {
- ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
- }
+ match issuer {
+ CollectionIssuer::User(payer) => {
+ ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
- // Take a (non-refundable) deposit of collection creation
- if let Some(payer) = payer {
- let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
- imbalance.subsume(<T as Config>::Currency::deposit(
- &T::TreasuryAccountId::get(),
- T::CollectionCreationPrice::get(),
- Precision::Exact,
- )?);
- let credit =
- <T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)
- .map_err(|_| Error::<T>::NotSufficientFounds)?;
+ // Take a (non-refundable) deposit of collection creation
+ let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
+ imbalance.subsume(<T as Config>::Currency::deposit(
+ &T::TreasuryAccountId::get(),
+ T::CollectionCreationPrice::get(),
+ Precision::Exact,
+ )?);
+ let credit = <T as Config>::Currency::settle(
+ payer.as_sub(),
+ imbalance,
+ Preservation::Preserve,
+ )
+ .map_err(|_| Error::<T>::NotSufficientFounds)?;
- debug_assert!(credit.peek().is_zero())
+ debug_assert!(credit.peek().is_zero());
+ }
+ CollectionIssuer::Internals => {}
}
{
pallets/foreign-assets/src/lib.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -56,6 +56,7 @@
#[frame_support::pallet]
pub mod module {
+ use pallet_common::CollectionIssuer;
use up_data_structs::{
CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,
};
@@ -169,12 +170,9 @@
.try_into()
.expect("description length < max description length; qed");
- let payer = None;
- let is_special_collection = true;
- let collection_id = T::CollectionDispatch::create_raw(
+ let collection_id = T::CollectionDispatch::create(
foreign_collection_owner,
- payer,
- is_special_collection,
+ CollectionIssuer::Internals,
CreateCollectionData {
name,
token_prefix,
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -26,7 +26,7 @@
dispatch::CollectionDispatch,
erc::{static_property::key, CollectionHelpersEvents},
eth::{self, collection_id_to_address, map_eth_to_id},
- CollectionById, CollectionHandle, Pallet as PalletCommon,
+ CollectionById, CollectionHandle, CollectionIssuer, Pallet as PalletCommon,
};
use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
use pallet_evm_coder_substrate::{
@@ -110,9 +110,12 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
}
@@ -241,9 +244,12 @@
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
@@ -276,9 +282,12 @@
check_sent_amount_equals_collection_creation_price::<T>(value)?;
let collection_helpers_address =
T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
- let collection_id =
- T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)
- .map_err(dispatch_to_evm::<T>)?;
+ let collection_id = T::CollectionDispatch::create(
+ caller,
+ CollectionIssuer::User(collection_helpers_address),
+ data,
+ )
+ .map_err(dispatch_to_evm::<T>)?;
let address = pallet_common::eth::collection_id_to_address(collection_id);
Ok(address)
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -93,7 +93,8 @@
use frame_system::{ensure_root, ensure_signed};
use pallet_common::{
dispatch::{dispatch_tx, CollectionDispatch},
- CollectionHandle, CommonWeightInfo, Pallet as PalletCommon, RefungibleExtensionsWeightInfo,
+ CollectionHandle, CollectionIssuer, CommonWeightInfo, Pallet as PalletCommon,
+ RefungibleExtensionsWeightInfo,
};
use pallet_evm::account::CrossAccountId;
use pallet_structure::weights::WeightInfo as StructureWeightInfo;
@@ -401,7 +402,11 @@
// =========
let sender = T::CrossAccountId::from_sub(sender);
- let _id = T::CollectionDispatch::create(sender.clone(), Some(sender), data)?;
+ let _id = T::CollectionDispatch::create(
+ sender.clone(),
+ CollectionIssuer::User(sender),
+ data,
+ )?;
Ok(())
}
runtime/common/dispatch.rsdiffbeforeafterboth--- a/runtime/common/dispatch.rs
+++ b/runtime/common/dispatch.rs
@@ -18,7 +18,7 @@
use pallet_balances_adapter::NativeFungibleHandle;
pub use pallet_common::dispatch::CollectionDispatch;
use pallet_common::{
- erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,
+ erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle, CollectionIssuer,
CommonCollectionOperations, Pallet as PalletCommon,
};
use pallet_evm::{PrecompileHandle, PrecompileResult};
@@ -66,10 +66,9 @@
}
}
- fn create_raw(
+ fn create(
sender: T::CrossAccountId,
- payer: Option<T::CrossAccountId>,
- is_special_collection: bool,
+ issuer: CollectionIssuer<T::CrossAccountId>,
data: CreateCollectionData<T::CrossAccountId>,
) -> Result<CollectionId, DispatchError> {
match data.mode {
@@ -87,7 +86,7 @@
_ => {}
};
- <PalletCommon<T>>::init_collection(sender, payer, is_special_collection, data)
+ <PalletCommon<T>>::init_collection(sender, issuer, data)
}
fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {