git.delta.rocks / unique-network / refs/commits / 9d0fd83a5da6

difftreelog

fix use CollectionIssuer enum for collection creation

Daniel Shiposha2023-11-24parent: #b4d745d.patch.diff
in: master

6 files changed

modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
11use sp_weights::Weight;11use sp_weights::Weight;
12use up_data_structs::{CollectionId, CreateCollectionData};12use up_data_structs::{CollectionId, CreateCollectionData};
1313
14use crate::{pallet::Config, CommonCollectionOperations};14use crate::{pallet::Config, CollectionIssuer, CommonCollectionOperations};
1515
16// TODO: move to benchmarking16// TODO: move to benchmarking
17/// Price of [`dispatch_tx`] call with noop `call` argument17/// Price of [`dispatch_tx`] call with noop `call` argument
72 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).72 /// Create a regular collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).
73 ///73 ///
74 /// * `sender` - The user who will become the owner of the collection.74 /// * `sender` - The user who will become the owner of the collection.
75 /// * `payer` - If set, the user who pays the collection creation deposit.75 /// * `issuer` - An entity that creates the collection.
76 /// * `data` - Description of the created collection.76 /// * `data` - Description of the created collection.
77 fn create(77 fn create(
78 sender: T::CrossAccountId,78 sender: T::CrossAccountId,
79 payer: Option<T::CrossAccountId>,79 issuer: CollectionIssuer<T::CrossAccountId>,
80 data: CreateCollectionData<T::CrossAccountId>,80 data: CreateCollectionData<T::CrossAccountId>,
81 ) -> Result<CollectionId, DispatchError> {81 ) -> Result<CollectionId, DispatchError>;
82 Self::create_raw(sender, payer, false, data)
83 }
84
85 /// 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>;
9782
98 /// Delete the collection.83 /// Delete the collection.
99 ///84 ///
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
944 }944 }
945}945}
946
947/// An issuer of a collection.
948pub enum CollectionIssuer<CrossAccountId> {
949 /// A user who creates the collection.
950 User(CrossAccountId),
951
952 /// The internal mechanisms are creating the collection.
953 Internals,
954}
946955
947fn check_token_permissions<T: Config>(956fn check_token_permissions<T: Config>(
948 collection_admin_permitted: bool,957 collection_admin_permitted: bool,
1128 /// Create new collection.1137 /// Create new collection.
1129 ///1138 ///
1130 /// * `owner` - The owner of the collection.1139 /// * `owner` - The owner of the collection.
1131 /// * `payer` - If set, the user that will pay a deposit for the collection creation.
1132 /// * `data` - Description of the created collection.1140 /// * `issuer` - An entity that creates the collection.
1133 /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.1141 /// * `is_special_collection` -- Whether this collection is a special one, i.e. can have special flags set.
1134 pub fn init_collection(1142 pub fn init_collection(
1135 owner: T::CrossAccountId,1143 owner: T::CrossAccountId,
1136 payer: Option<T::CrossAccountId>,1144 issuer: CollectionIssuer<T::CrossAccountId>,
1137 is_special_collection: bool,
1138 data: CreateCollectionData<T::CrossAccountId>,1145 data: CreateCollectionData<T::CrossAccountId>,
1139 ) -> Result<CollectionId, DispatchError> {1146 ) -> Result<CollectionId, DispatchError> {
1140 if !is_special_collection {1147 match issuer {
1141 ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
1142 }
1143
1144 // Take a (non-refundable) deposit of collection creation
1145 if let Some(payer) = payer {1148 CollectionIssuer::User(payer) => {
1149 ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
1150
1151 // Take a (non-refundable) deposit of collection creation
1146 let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();1152 let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
1147 imbalance.subsume(<T as Config>::Currency::deposit(1153 imbalance.subsume(<T as Config>::Currency::deposit(
1148 &T::TreasuryAccountId::get(),1154 &T::TreasuryAccountId::get(),
1162 )
1154 .map_err(|_| Error::<T>::NotSufficientFounds)?;1163 .map_err(|_| Error::<T>::NotSufficientFounds)?;
11551164
1156 debug_assert!(credit.peek().is_zero())1165 debug_assert!(credit.peek().is_zero());
1157 }1166 }
1167 CollectionIssuer::Internals => {}
1168 }
11581169
1159 {1170 {
1160 ensure!(1171 ensure!(
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
5656
57#[frame_support::pallet]57#[frame_support::pallet]
58pub mod module {58pub mod module {
59 use pallet_common::CollectionIssuer;
59 use up_data_structs::{60 use up_data_structs::{
60 CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,61 CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,
61 };62 };
169 .try_into()170 .try_into()
170 .expect("description length < max description length; qed");171 .expect("description length < max description length; qed");
171172
172 let payer = None;
173 let is_special_collection = true;
174 let collection_id = T::CollectionDispatch::create_raw(173 let collection_id = T::CollectionDispatch::create(
175 foreign_collection_owner,174 foreign_collection_owner,
176 payer,175 CollectionIssuer::Internals,
177 is_special_collection,
178 CreateCollectionData {176 CreateCollectionData {
179 name,177 name,
180 token_prefix,178 token_prefix,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
26 dispatch::CollectionDispatch,26 dispatch::CollectionDispatch,
27 erc::{static_property::key, CollectionHelpersEvents},27 erc::{static_property::key, CollectionHelpersEvents},
28 eth::{self, collection_id_to_address, map_eth_to_id},28 eth::{self, collection_id_to_address, map_eth_to_id},
29 CollectionById, CollectionHandle, Pallet as PalletCommon,29 CollectionById, CollectionHandle, CollectionIssuer, Pallet as PalletCommon,
30};30};
31use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};31use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
32use pallet_evm_coder_substrate::{32use pallet_evm_coder_substrate::{
111 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());111 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
112112
113 let collection_id =113 let collection_id = T::CollectionDispatch::create(
114 T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)114 caller,
115 CollectionIssuer::User(collection_helpers_address),
116 data,
117 )
115 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;118 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
116 let address = pallet_common::eth::collection_id_to_address(collection_id);119 let address = pallet_common::eth::collection_id_to_address(collection_id);
242 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());245 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
243246
244 let collection_id =247 let collection_id = T::CollectionDispatch::create(
245 T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)248 caller,
249 CollectionIssuer::User(collection_helpers_address),
250 data,
251 )
246 .map_err(dispatch_to_evm::<T>)?;252 .map_err(dispatch_to_evm::<T>)?;
247253
277 let collection_helpers_address =283 let collection_helpers_address =
278 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());284 T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());
279 let collection_id =285 let collection_id = T::CollectionDispatch::create(
280 T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)286 caller,
287 CollectionIssuer::User(collection_helpers_address),
288 data,
289 )
281 .map_err(dispatch_to_evm::<T>)?;290 .map_err(dispatch_to_evm::<T>)?;
282291
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
93 use frame_system::{ensure_root, ensure_signed};93 use frame_system::{ensure_root, ensure_signed};
94 use pallet_common::{94 use pallet_common::{
95 dispatch::{dispatch_tx, CollectionDispatch},95 dispatch::{dispatch_tx, CollectionDispatch},
96 CollectionHandle, CommonWeightInfo, Pallet as PalletCommon, RefungibleExtensionsWeightInfo,96 CollectionHandle, CollectionIssuer, CommonWeightInfo, Pallet as PalletCommon,
97 RefungibleExtensionsWeightInfo,
97 };98 };
98 use pallet_evm::account::CrossAccountId;99 use pallet_evm::account::CrossAccountId;
401402
402 // =========403 // =========
403 let sender = T::CrossAccountId::from_sub(sender);404 let sender = T::CrossAccountId::from_sub(sender);
404 let _id = T::CollectionDispatch::create(sender.clone(), Some(sender), data)?;405 let _id = T::CollectionDispatch::create(
406 sender.clone(),
407 CollectionIssuer::User(sender),
408 data,
409 )?;
405410
406 Ok(())411 Ok(())
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
18use pallet_balances_adapter::NativeFungibleHandle;18use pallet_balances_adapter::NativeFungibleHandle;
19pub use pallet_common::dispatch::CollectionDispatch;19pub use pallet_common::dispatch::CollectionDispatch;
20use pallet_common::{20use pallet_common::{
21 erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle,21 erc::CommonEvmHandler, eth::map_eth_to_id, CollectionById, CollectionHandle, CollectionIssuer,
22 CommonCollectionOperations, Pallet as PalletCommon,22 CommonCollectionOperations, Pallet as PalletCommon,
23};23};
24use pallet_evm::{PrecompileHandle, PrecompileResult};24use pallet_evm::{PrecompileHandle, PrecompileResult};
66 }66 }
67 }67 }
6868
69 fn create_raw(69 fn create(
70 sender: T::CrossAccountId,70 sender: T::CrossAccountId,
71 payer: Option<T::CrossAccountId>,71 issuer: CollectionIssuer<T::CrossAccountId>,
72 is_special_collection: bool,
73 data: CreateCollectionData<T::CrossAccountId>,72 data: CreateCollectionData<T::CrossAccountId>,
74 ) -> Result<CollectionId, DispatchError> {73 ) -> Result<CollectionId, DispatchError> {
75 match data.mode {74 match data.mode {
87 _ => {}86 _ => {}
88 };87 };
8988
90 <PalletCommon<T>>::init_collection(sender, payer, is_special_collection, data)89 <PalletCommon<T>>::init_collection(sender, issuer, data)
91 }90 }
9291
93 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {92 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {