From 9d0fd83a5da6bdfd91b8bf92f2f18fba4156e96b Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Fri, 24 Nov 2023 14:59:03 +0000 Subject: [PATCH] fix: use CollectionIssuer enum for collection creation --- --- a/pallets/common/src/dispatch.rs +++ b/pallets/common/src/dispatch.rs @@ -11,7 +11,7 @@ use sp_weights::Weight; use up_data_structs::{CollectionId, CreateCollectionData}; -use crate::{pallet::Config, CommonCollectionOperations}; +use crate::{pallet::Config, CollectionIssuer, CommonCollectionOperations}; // TODO: move to benchmarking /// Price of [`dispatch_tx`] call with noop `call` argument @@ -72,26 +72,11 @@ /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode). /// /// * `sender` - The user who will become the owner of the collection. - /// * `payer` - If set, the user who pays the collection creation deposit. + /// * `issuer` - An entity that creates the collection. /// * `data` - Description of the created collection. fn create( sender: T::CrossAccountId, - payer: Option, - data: CreateCollectionData, - ) -> Result { - Self::create_raw(sender, payer, false, data) - } - - /// Function for creating regular and special collections. - /// - /// * `sender` - The user who will become the owner of the collection. - /// * `payer` - If set, the user who pays the collection creation deposit. - /// * `data` - Description of the created collection. - /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set. - fn create_raw( - sender: T::CrossAccountId, - payer: Option, - is_special_collection: bool, + issuer: CollectionIssuer, data: CreateCollectionData, ) -> Result; --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -944,6 +944,15 @@ } } +/// An issuer of a collection. +pub enum CollectionIssuer { + /// A user who creates the collection. + User(CrossAccountId), + + /// The internal mechanisms are creating the collection. + Internals, +} + fn check_token_permissions( 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, - is_special_collection: bool, + issuer: CollectionIssuer, data: CreateCollectionData, ) -> Result { - if !is_special_collection { - ensure!(data.flags.is_allowed_for_user(), >::NoPermission); - } + match issuer { + CollectionIssuer::User(payer) => { + ensure!(data.flags.is_allowed_for_user(), >::NoPermission); - // Take a (non-refundable) deposit of collection creation - if let Some(payer) = payer { - let mut imbalance = ::Currency>>::zero(); - imbalance.subsume(::Currency::deposit( - &T::TreasuryAccountId::get(), - T::CollectionCreationPrice::get(), - Precision::Exact, - )?); - let credit = - ::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve) - .map_err(|_| Error::::NotSufficientFounds)?; + // Take a (non-refundable) deposit of collection creation + let mut imbalance = ::Currency>>::zero(); + imbalance.subsume(::Currency::deposit( + &T::TreasuryAccountId::get(), + T::CollectionCreationPrice::get(), + Precision::Exact, + )?); + let credit = ::Currency::settle( + payer.as_sub(), + imbalance, + Preservation::Preserve, + ) + .map_err(|_| Error::::NotSufficientFounds)?; - debug_assert!(credit.peek().is_zero()) + debug_assert!(credit.peek().is_zero()); + } + CollectionIssuer::Internals => {} } { --- 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, --- 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(::ContractAddress::get()); - let collection_id = - T::CollectionDispatch::create(caller, Some(collection_helpers_address), data) - .map_err(pallet_evm_coder_substrate::dispatch_to_evm::)?; + let collection_id = T::CollectionDispatch::create( + caller, + CollectionIssuer::User(collection_helpers_address), + data, + ) + .map_err(pallet_evm_coder_substrate::dispatch_to_evm::)?; 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(::ContractAddress::get()); - let collection_id = - T::CollectionDispatch::create(caller, Some(collection_helpers_address), data) - .map_err(dispatch_to_evm::)?; + let collection_id = T::CollectionDispatch::create( + caller, + CollectionIssuer::User(collection_helpers_address), + data, + ) + .map_err(dispatch_to_evm::)?; let address = pallet_common::eth::collection_id_to_address(collection_id); Ok(address) @@ -276,9 +282,12 @@ check_sent_amount_equals_collection_creation_price::(value)?; let collection_helpers_address = T::CrossAccountId::from_eth(::ContractAddress::get()); - let collection_id = - T::CollectionDispatch::create(caller, Some(collection_helpers_address), data) - .map_err(dispatch_to_evm::)?; + let collection_id = T::CollectionDispatch::create( + caller, + CollectionIssuer::User(collection_helpers_address), + data, + ) + .map_err(dispatch_to_evm::)?; let address = pallet_common::eth::collection_id_to_address(collection_id); Ok(address) --- 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(()) } --- 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, - is_special_collection: bool, + issuer: CollectionIssuer, data: CreateCollectionData, ) -> Result { match data.mode { @@ -87,7 +86,7 @@ _ => {} }; - >::init_collection(sender, payer, is_special_collection, data) + >::init_collection(sender, issuer, data) } fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult { -- gitstuff