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
--- 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<T::CrossAccountId>,
-		data: CreateCollectionData<T::CrossAccountId>,
-	) -> Result<CollectionId, DispatchError> {
-		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<T::CrossAccountId>,
-		is_special_collection: bool,
+		issuer: CollectionIssuer<T::CrossAccountId>,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError>;
 
modifiedpallets/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 => {}
 		}
 
 		{
modifiedpallets/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,
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
before · pallets/unique/src/eth/mod.rs
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/>.1617//! Implementation of CollectionHelpers contract.18//!19use alloc::{collections::BTreeSet, format};20use core::marker::PhantomData;2122use ethereum as _;23use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};24use frame_support::{traits::Get, BoundedVec};25use pallet_common::{26	dispatch::CollectionDispatch,27	erc::{static_property::key, CollectionHelpersEvents},28	eth::{self, collection_id_to_address, map_eth_to_id},29	CollectionById, CollectionHandle, Pallet as PalletCommon,30};31use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};32use pallet_evm_coder_substrate::{33	dispatch_to_evm,34	execution::{Error, PreDispatch, Result},35	frontier_contract, SubstrateRecorder, WithRecorder,36};37use sp_std::vec::Vec;38use up_data_structs::{39	CollectionDescription, CollectionMode, CollectionName, CollectionPermissions,40	CollectionTokenPrefix, CreateCollectionData, NestingPermissions,41};4243use crate::{weights::WeightInfo, Config, Pallet, SelfWeightOf};4445frontier_contract! {46	macro_rules! EvmCollectionHelpers_result {...}47	impl<T: Config> Contract for EvmCollectionHelpers<T> {...}48}49/// See [`CollectionHelpersCall`]50pub struct EvmCollectionHelpers<T: Config>(SubstrateRecorder<T>);51impl<T: Config> WithRecorder<T> for EvmCollectionHelpers<T> {52	fn recorder(&self) -> &SubstrateRecorder<T> {53		&self.054	}5556	fn into_recorder(self) -> SubstrateRecorder<T> {57		self.058	}59}6061fn convert_data<T: Config>(62	caller: Caller,63	name: String,64	description: String,65	token_prefix: String,66) -> Result<(67	T::CrossAccountId,68	CollectionName,69	CollectionDescription,70	CollectionTokenPrefix,71)> {72	let caller = T::CrossAccountId::from_eth(caller);73	let name = name74		.encode_utf16()75		.collect::<Vec<u16>>()76		.try_into()77		.map_err(|_| error_field_too_long(stringify!(name), CollectionName::bound()))?;78	let description = description79		.encode_utf16()80		.collect::<Vec<u16>>()81		.try_into()82		.map_err(|_| {83			error_field_too_long(stringify!(description), CollectionDescription::bound())84		})?;85	let token_prefix = token_prefix.into_bytes().try_into().map_err(|_| {86		error_field_too_long(stringify!(token_prefix), CollectionTokenPrefix::bound())87	})?;88	Ok((caller, name, description, token_prefix))89}9091#[inline(always)]92fn create_collection_internal<T: Config>(93	caller: Caller,94	value: Value,95	name: String,96	collection_mode: CollectionMode,97	description: String,98	token_prefix: String,99) -> Result<Address> {100	let (caller, name, description, token_prefix) =101		convert_data::<T>(caller, name, description, token_prefix)?;102	let data = CreateCollectionData {103		name,104		mode: collection_mode,105		description,106		token_prefix,107		..Default::default()108	};109	check_sent_amount_equals_collection_creation_price::<T>(value)?;110	let collection_helpers_address =111		T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());112113	let collection_id =114		T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)115			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;116	let address = pallet_common::eth::collection_id_to_address(collection_id);117	Ok(address)118}119120fn check_sent_amount_equals_collection_creation_price<T: Config>(value: Value) -> Result<()> {121	let value = value.as_u128();122	let creation_price: u128 = T::CollectionCreationPrice::get()123		.try_into()124		.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait125		.expect("Collection creation price should be convertible to u128");126	if value != creation_price {127		return Err(format!(128			"Sent amount not equals to collection creation price ({creation_price})",129		)130		.into());131	}132	Ok(())133}134135/// @title Contract, which allows users to operate with collections136#[solidity_interface(name = CollectionHelpers, events(CollectionHelpersEvents), enum(derive(PreDispatch)), enum_attr(weight))]137impl<T> EvmCollectionHelpers<T>138where139	T: Config + pallet_common::Config + pallet_nonfungible::Config + pallet_refungible::Config,140	T::AccountId: From<[u8; 32]>,141{142	/// Create a collection143	/// @return address Address of the newly created collection144	#[weight(<SelfWeightOf<T>>::create_collection())]145	#[solidity(rename_selector = "createCollection")]146	fn create_collection(147		&mut self,148		caller: Caller,149		value: Value,150		data: eth::CreateCollectionData,151	) -> Result<Address> {152		let (caller, name, description, token_prefix) =153			convert_data::<T>(caller, data.name, data.description, data.token_prefix)?;154		if data.mode != eth::CollectionMode::Fungible && data.decimals != 0 {155			return Err("decimals are only supported for NFT and RFT collections".into());156		}157		let mode = match data.mode {158			eth::CollectionMode::Fungible => CollectionMode::Fungible(data.decimals),159			eth::CollectionMode::Nonfungible => CollectionMode::NFT,160			eth::CollectionMode::Refungible => CollectionMode::ReFungible,161		};162163		let properties: BoundedVec<_, _> = data164			.properties165			.into_iter()166			.map(eth::Property::try_into)167			.collect::<Result<Vec<_>>>()?168			.try_into()169			.map_err(|_| "too many properties")?;170171		let token_property_permissions =172			eth::TokenPropertyPermission::into_property_key_permissions(173				data.token_property_permissions,174			)?175			.try_into()176			.map_err(|_| "too many property permissions")?;177178		let limits = if !data.limits.is_empty() {179			Some(180				data.limits181					.into_iter()182					.collect::<Result<up_data_structs::CollectionLimits>>()?,183			)184		} else {185			None186		};187188		let pending_sponsor = data.pending_sponsor.into_option_sub_cross_account::<T>()?;189190		let restricted = if !data.nesting_settings.restricted.is_empty() {191			Some(192				data.nesting_settings193					.restricted194					.iter()195					.map(map_eth_to_id)196					.collect::<Option<BTreeSet<_>>>()197					.ok_or("can't convert address into collection id")?198					.try_into()199					.map_err(|_| "too many collections")?,200			)201		} else {202			None203		};204205		let admin_list = data206			.admin_list207			.into_iter()208			.map(|admin| admin.into_sub_cross_account::<T>())209			.collect::<Result<Vec<_>>>()?;210211		let flags = data.flags;212		if !flags.is_allowed_for_user() {213			return Err("internal flags were used".into());214		}215216		let data = CreateCollectionData {217			name,218			mode,219			description,220			token_prefix,221			properties,222			token_property_permissions,223			limits,224			pending_sponsor,225			access: None,226			permissions: Some(CollectionPermissions {227				access: None,228				mint_mode: None,229				nesting: Some(NestingPermissions {230					token_owner: data.nesting_settings.token_owner,231					collection_admin: data.nesting_settings.collection_admin,232					restricted,233					#[cfg(feature = "runtime-benchmarks")]234					permissive: true,235				}),236			}),237			admin_list,238			flags,239		};240		check_sent_amount_equals_collection_creation_price::<T>(value)?;241		let collection_helpers_address =242			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());243244		let collection_id =245			T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)246				.map_err(dispatch_to_evm::<T>)?;247248		let address = pallet_common::eth::collection_id_to_address(collection_id);249		Ok(address)250	}251252	/// Create an NFT collection253	/// @param name Name of the collection254	/// @param description Informative description of the collection255	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications256	/// @return address Address of the newly created collection257	#[weight(<SelfWeightOf<T>>::create_collection())]258	#[solidity(rename_selector = "createNFTCollection")]259	fn create_nft_collection(260		&mut self,261		caller: Caller,262		value: Value,263		name: String,264		description: String,265		token_prefix: String,266	) -> Result<Address> {267		let (caller, name, description, token_prefix) =268			convert_data::<T>(caller, name, description, token_prefix)?;269		let data = CreateCollectionData {270			name,271			mode: CollectionMode::NFT,272			description,273			token_prefix,274			..Default::default()275		};276		check_sent_amount_equals_collection_creation_price::<T>(value)?;277		let collection_helpers_address =278			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());279		let collection_id =280			T::CollectionDispatch::create(caller, Some(collection_helpers_address), data)281				.map_err(dispatch_to_evm::<T>)?;282283		let address = pallet_common::eth::collection_id_to_address(collection_id);284		Ok(address)285	}286	/// Create an NFT collection287	/// @param name Name of the collection288	/// @param description Informative description of the collection289	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications290	/// @return address Address of the newly created collection291	#[deprecated(note = "mathod was renamed to `create_nft_collection`, prefer it instead")]292	#[solidity(hide)]293	#[weight(<SelfWeightOf<T>>::create_collection())]294	fn create_nonfungible_collection(295		&mut self,296		caller: Caller,297		value: Value,298		name: String,299		description: String,300		token_prefix: String,301	) -> Result<Address> {302		create_collection_internal::<T>(303			caller,304			value,305			name,306			CollectionMode::NFT,307			description,308			token_prefix,309		)310	}311312	#[weight(<SelfWeightOf<T>>::create_collection())]313	#[solidity(rename_selector = "createRFTCollection")]314	fn create_rft_collection(315		&mut self,316		caller: Caller,317		value: Value,318		name: String,319		description: String,320		token_prefix: String,321	) -> Result<Address> {322		create_collection_internal::<T>(323			caller,324			value,325			name,326			CollectionMode::ReFungible,327			description,328			token_prefix,329		)330	}331332	#[weight(<SelfWeightOf<T>>::create_collection())]333	#[solidity(rename_selector = "createFTCollection")]334	fn create_fungible_collection(335		&mut self,336		caller: Caller,337		value: Value,338		name: String,339		decimals: u8,340		description: String,341		token_prefix: String,342	) -> Result<Address> {343		create_collection_internal::<T>(344			caller,345			value,346			name,347			CollectionMode::Fungible(decimals),348			description,349			token_prefix,350		)351	}352353	#[solidity(rename_selector = "makeCollectionERC721MetadataCompatible")]354	fn make_collection_metadata_compatible(355		&mut self,356		caller: Caller,357		collection: Address,358		base_uri: String,359	) -> Result<()> {360		let caller = T::CrossAccountId::from_eth(caller);361		let collection =362			pallet_common::eth::map_eth_to_id(&collection).ok_or("not a collection address")?;363		let mut collection =364			<CollectionHandle<T>>::new(collection).ok_or("collection not found")?;365366		if !matches!(367			collection.mode,368			CollectionMode::NFT | CollectionMode::ReFungible369		) {370			return Err("target collection should be either NFT or Refungible".into());371		}372373		self.recorder().consume_sstore()?;374		collection375			.check_is_owner_or_admin(&caller)376			.map_err(dispatch_to_evm::<T>)?;377378		if collection.flags.erc721metadata {379			return Err("target collection is already Erc721Metadata compatible".into());380		}381		collection.flags.erc721metadata = true;382383		let all_permissions = <pallet_common::CollectionPropertyPermissions<T>>::get(collection.id);384		if all_permissions.get(&key::url()).is_none() {385			self.recorder().consume_sstore()?;386			<PalletCommon<T>>::set_property_permission(387				&collection,388				&caller,389				up_data_structs::PropertyKeyPermission {390					key: key::url(),391					permission: up_data_structs::PropertyPermission {392						mutable: true,393						collection_admin: true,394						token_owner: false,395					},396				},397			)398			.map_err(dispatch_to_evm::<T>)?;399		}400		if all_permissions.get(&key::suffix()).is_none() {401			self.recorder().consume_sstore()?;402			<PalletCommon<T>>::set_property_permission(403				&collection,404				&caller,405				up_data_structs::PropertyKeyPermission {406					key: key::suffix(),407					permission: up_data_structs::PropertyPermission {408						mutable: true,409						collection_admin: true,410						token_owner: false,411					},412				},413			)414			.map_err(dispatch_to_evm::<T>)?;415		}416417		let all_properties = <pallet_common::CollectionProperties<T>>::get(collection.id);418		if all_properties.get(&key::base_uri()).is_none() && !base_uri.is_empty() {419			self.recorder().consume_sstore()?;420			<PalletCommon<T>>::set_collection_properties(421				&collection,422				&caller,423				[up_data_structs::Property {424					key: key::base_uri(),425					value: base_uri426						.into_bytes()427						.try_into()428						.map_err(|_| "base uri is too large")?,429				}]430				.into_iter(),431			)432			.map_err(dispatch_to_evm::<T>)?;433		}434435		self.recorder().consume_sstore()?;436		collection.save().map_err(dispatch_to_evm::<T>)?;437438		Ok(())439	}440441	#[weight(<SelfWeightOf<T>>::destroy_collection())]442	fn destroy_collection(&mut self, caller: Caller, collection_address: Address) -> Result<()> {443		let caller = T::CrossAccountId::from_eth(caller);444445		let collection_id = pallet_common::eth::map_eth_to_id(&collection_address)446			.ok_or("Invalid collection address format")?;447		<Pallet<T>>::destroy_collection_internal(caller, collection_id)448			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)449	}450451	/// Check if a collection exists452	/// @param collectionAddress Address of the collection in question453	/// @return bool Does the collection exist?454	fn is_collection_exist(&self, _caller: Caller, collection_address: Address) -> Result<bool> {455		if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {456			let collection_id = id;457			return Ok(<CollectionById<T>>::contains_key(collection_id));458		}459460		Ok(false)461	}462463	fn collection_creation_fee(&self) -> Result<Value> {464		let price: u128 = T::CollectionCreationPrice::get()465			.try_into()466			.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait467			.expect("Collection creation price should be convertible to u128");468		Ok(price.into())469	}470471	/// Returns address of a collection.472	/// @param collectionId  - CollectionId  of the collection473	/// @return eth mirror address of the collection474	fn collection_address(&self, collection_id: u32) -> Result<Address> {475		Ok(collection_id_to_address(collection_id.into()))476	}477478	/// Returns collectionId of a collection.479	/// @param collectionAddress  - Eth address of the collection480	/// @return collectionId of the collection481	fn collection_id(&self, collection_address: Address) -> Result<u32> {482		map_eth_to_id(&collection_address)483			.map(|id| id.0)484			.ok_or(Error::Revert(format!(485				"failed to convert address {collection_address} into collectionId."486			)))487	}488}489490/// Implements [`OnMethodCall`], which delegates call to [`EvmCollectionHelpers`]491pub struct CollectionHelpersOnMethodCall<T: Config>(PhantomData<*const T>);492impl<T: Config + pallet_nonfungible::Config + pallet_refungible::Config> OnMethodCall<T>493	for CollectionHelpersOnMethodCall<T>494where495	T::AccountId: From<[u8; 32]>,496{497	fn is_reserved(contract: &sp_core::H160) -> bool {498		contract == &T::ContractAddress::get()499	}500501	fn is_used(contract: &sp_core::H160) -> bool {502		contract == &T::ContractAddress::get()503	}504505	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {506		if handle.code_address() != T::ContractAddress::get() {507			return None;508		}509510		let helpers =511			EvmCollectionHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));512		pallet_evm_coder_substrate::call(handle, helpers)513	}514515	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {516		(contract == &T::ContractAddress::get())517			.then(|| include_bytes!("./stubs/CollectionHelpers.raw").to_vec())518	}519}520521generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);522generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);523524fn error_field_too_long(feild: &str, bound: usize) -> Error {525	Error::Revert(format!("{feild} is too long. Max length is {bound}."))526}
after · pallets/unique/src/eth/mod.rs
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/>.1617//! Implementation of CollectionHelpers contract.18//!19use alloc::{collections::BTreeSet, format};20use core::marker::PhantomData;2122use ethereum as _;23use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};24use frame_support::{traits::Get, BoundedVec};25use pallet_common::{26	dispatch::CollectionDispatch,27	erc::{static_property::key, CollectionHelpersEvents},28	eth::{self, collection_id_to_address, map_eth_to_id},29	CollectionById, CollectionHandle, CollectionIssuer, Pallet as PalletCommon,30};31use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};32use pallet_evm_coder_substrate::{33	dispatch_to_evm,34	execution::{Error, PreDispatch, Result},35	frontier_contract, SubstrateRecorder, WithRecorder,36};37use sp_std::vec::Vec;38use up_data_structs::{39	CollectionDescription, CollectionMode, CollectionName, CollectionPermissions,40	CollectionTokenPrefix, CreateCollectionData, NestingPermissions,41};4243use crate::{weights::WeightInfo, Config, Pallet, SelfWeightOf};4445frontier_contract! {46	macro_rules! EvmCollectionHelpers_result {...}47	impl<T: Config> Contract for EvmCollectionHelpers<T> {...}48}49/// See [`CollectionHelpersCall`]50pub struct EvmCollectionHelpers<T: Config>(SubstrateRecorder<T>);51impl<T: Config> WithRecorder<T> for EvmCollectionHelpers<T> {52	fn recorder(&self) -> &SubstrateRecorder<T> {53		&self.054	}5556	fn into_recorder(self) -> SubstrateRecorder<T> {57		self.058	}59}6061fn convert_data<T: Config>(62	caller: Caller,63	name: String,64	description: String,65	token_prefix: String,66) -> Result<(67	T::CrossAccountId,68	CollectionName,69	CollectionDescription,70	CollectionTokenPrefix,71)> {72	let caller = T::CrossAccountId::from_eth(caller);73	let name = name74		.encode_utf16()75		.collect::<Vec<u16>>()76		.try_into()77		.map_err(|_| error_field_too_long(stringify!(name), CollectionName::bound()))?;78	let description = description79		.encode_utf16()80		.collect::<Vec<u16>>()81		.try_into()82		.map_err(|_| {83			error_field_too_long(stringify!(description), CollectionDescription::bound())84		})?;85	let token_prefix = token_prefix.into_bytes().try_into().map_err(|_| {86		error_field_too_long(stringify!(token_prefix), CollectionTokenPrefix::bound())87	})?;88	Ok((caller, name, description, token_prefix))89}9091#[inline(always)]92fn create_collection_internal<T: Config>(93	caller: Caller,94	value: Value,95	name: String,96	collection_mode: CollectionMode,97	description: String,98	token_prefix: String,99) -> Result<Address> {100	let (caller, name, description, token_prefix) =101		convert_data::<T>(caller, name, description, token_prefix)?;102	let data = CreateCollectionData {103		name,104		mode: collection_mode,105		description,106		token_prefix,107		..Default::default()108	};109	check_sent_amount_equals_collection_creation_price::<T>(value)?;110	let collection_helpers_address =111		T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());112113	let collection_id = T::CollectionDispatch::create(114		caller,115		CollectionIssuer::User(collection_helpers_address),116		data,117	)118	.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;119	let address = pallet_common::eth::collection_id_to_address(collection_id);120	Ok(address)121}122123fn check_sent_amount_equals_collection_creation_price<T: Config>(value: Value) -> Result<()> {124	let value = value.as_u128();125	let creation_price: u128 = T::CollectionCreationPrice::get()126		.try_into()127		.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait128		.expect("Collection creation price should be convertible to u128");129	if value != creation_price {130		return Err(format!(131			"Sent amount not equals to collection creation price ({creation_price})",132		)133		.into());134	}135	Ok(())136}137138/// @title Contract, which allows users to operate with collections139#[solidity_interface(name = CollectionHelpers, events(CollectionHelpersEvents), enum(derive(PreDispatch)), enum_attr(weight))]140impl<T> EvmCollectionHelpers<T>141where142	T: Config + pallet_common::Config + pallet_nonfungible::Config + pallet_refungible::Config,143	T::AccountId: From<[u8; 32]>,144{145	/// Create a collection146	/// @return address Address of the newly created collection147	#[weight(<SelfWeightOf<T>>::create_collection())]148	#[solidity(rename_selector = "createCollection")]149	fn create_collection(150		&mut self,151		caller: Caller,152		value: Value,153		data: eth::CreateCollectionData,154	) -> Result<Address> {155		let (caller, name, description, token_prefix) =156			convert_data::<T>(caller, data.name, data.description, data.token_prefix)?;157		if data.mode != eth::CollectionMode::Fungible && data.decimals != 0 {158			return Err("decimals are only supported for NFT and RFT collections".into());159		}160		let mode = match data.mode {161			eth::CollectionMode::Fungible => CollectionMode::Fungible(data.decimals),162			eth::CollectionMode::Nonfungible => CollectionMode::NFT,163			eth::CollectionMode::Refungible => CollectionMode::ReFungible,164		};165166		let properties: BoundedVec<_, _> = data167			.properties168			.into_iter()169			.map(eth::Property::try_into)170			.collect::<Result<Vec<_>>>()?171			.try_into()172			.map_err(|_| "too many properties")?;173174		let token_property_permissions =175			eth::TokenPropertyPermission::into_property_key_permissions(176				data.token_property_permissions,177			)?178			.try_into()179			.map_err(|_| "too many property permissions")?;180181		let limits = if !data.limits.is_empty() {182			Some(183				data.limits184					.into_iter()185					.collect::<Result<up_data_structs::CollectionLimits>>()?,186			)187		} else {188			None189		};190191		let pending_sponsor = data.pending_sponsor.into_option_sub_cross_account::<T>()?;192193		let restricted = if !data.nesting_settings.restricted.is_empty() {194			Some(195				data.nesting_settings196					.restricted197					.iter()198					.map(map_eth_to_id)199					.collect::<Option<BTreeSet<_>>>()200					.ok_or("can't convert address into collection id")?201					.try_into()202					.map_err(|_| "too many collections")?,203			)204		} else {205			None206		};207208		let admin_list = data209			.admin_list210			.into_iter()211			.map(|admin| admin.into_sub_cross_account::<T>())212			.collect::<Result<Vec<_>>>()?;213214		let flags = data.flags;215		if !flags.is_allowed_for_user() {216			return Err("internal flags were used".into());217		}218219		let data = CreateCollectionData {220			name,221			mode,222			description,223			token_prefix,224			properties,225			token_property_permissions,226			limits,227			pending_sponsor,228			access: None,229			permissions: Some(CollectionPermissions {230				access: None,231				mint_mode: None,232				nesting: Some(NestingPermissions {233					token_owner: data.nesting_settings.token_owner,234					collection_admin: data.nesting_settings.collection_admin,235					restricted,236					#[cfg(feature = "runtime-benchmarks")]237					permissive: true,238				}),239			}),240			admin_list,241			flags,242		};243		check_sent_amount_equals_collection_creation_price::<T>(value)?;244		let collection_helpers_address =245			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());246247		let collection_id = T::CollectionDispatch::create(248			caller,249			CollectionIssuer::User(collection_helpers_address),250			data,251		)252		.map_err(dispatch_to_evm::<T>)?;253254		let address = pallet_common::eth::collection_id_to_address(collection_id);255		Ok(address)256	}257258	/// Create an NFT collection259	/// @param name Name of the collection260	/// @param description Informative description of the collection261	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications262	/// @return address Address of the newly created collection263	#[weight(<SelfWeightOf<T>>::create_collection())]264	#[solidity(rename_selector = "createNFTCollection")]265	fn create_nft_collection(266		&mut self,267		caller: Caller,268		value: Value,269		name: String,270		description: String,271		token_prefix: String,272	) -> Result<Address> {273		let (caller, name, description, token_prefix) =274			convert_data::<T>(caller, name, description, token_prefix)?;275		let data = CreateCollectionData {276			name,277			mode: CollectionMode::NFT,278			description,279			token_prefix,280			..Default::default()281		};282		check_sent_amount_equals_collection_creation_price::<T>(value)?;283		let collection_helpers_address =284			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());285		let collection_id = T::CollectionDispatch::create(286			caller,287			CollectionIssuer::User(collection_helpers_address),288			data,289		)290		.map_err(dispatch_to_evm::<T>)?;291292		let address = pallet_common::eth::collection_id_to_address(collection_id);293		Ok(address)294	}295	/// Create an NFT collection296	/// @param name Name of the collection297	/// @param description Informative description of the collection298	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications299	/// @return address Address of the newly created collection300	#[deprecated(note = "mathod was renamed to `create_nft_collection`, prefer it instead")]301	#[solidity(hide)]302	#[weight(<SelfWeightOf<T>>::create_collection())]303	fn create_nonfungible_collection(304		&mut self,305		caller: Caller,306		value: Value,307		name: String,308		description: String,309		token_prefix: String,310	) -> Result<Address> {311		create_collection_internal::<T>(312			caller,313			value,314			name,315			CollectionMode::NFT,316			description,317			token_prefix,318		)319	}320321	#[weight(<SelfWeightOf<T>>::create_collection())]322	#[solidity(rename_selector = "createRFTCollection")]323	fn create_rft_collection(324		&mut self,325		caller: Caller,326		value: Value,327		name: String,328		description: String,329		token_prefix: String,330	) -> Result<Address> {331		create_collection_internal::<T>(332			caller,333			value,334			name,335			CollectionMode::ReFungible,336			description,337			token_prefix,338		)339	}340341	#[weight(<SelfWeightOf<T>>::create_collection())]342	#[solidity(rename_selector = "createFTCollection")]343	fn create_fungible_collection(344		&mut self,345		caller: Caller,346		value: Value,347		name: String,348		decimals: u8,349		description: String,350		token_prefix: String,351	) -> Result<Address> {352		create_collection_internal::<T>(353			caller,354			value,355			name,356			CollectionMode::Fungible(decimals),357			description,358			token_prefix,359		)360	}361362	#[solidity(rename_selector = "makeCollectionERC721MetadataCompatible")]363	fn make_collection_metadata_compatible(364		&mut self,365		caller: Caller,366		collection: Address,367		base_uri: String,368	) -> Result<()> {369		let caller = T::CrossAccountId::from_eth(caller);370		let collection =371			pallet_common::eth::map_eth_to_id(&collection).ok_or("not a collection address")?;372		let mut collection =373			<CollectionHandle<T>>::new(collection).ok_or("collection not found")?;374375		if !matches!(376			collection.mode,377			CollectionMode::NFT | CollectionMode::ReFungible378		) {379			return Err("target collection should be either NFT or Refungible".into());380		}381382		self.recorder().consume_sstore()?;383		collection384			.check_is_owner_or_admin(&caller)385			.map_err(dispatch_to_evm::<T>)?;386387		if collection.flags.erc721metadata {388			return Err("target collection is already Erc721Metadata compatible".into());389		}390		collection.flags.erc721metadata = true;391392		let all_permissions = <pallet_common::CollectionPropertyPermissions<T>>::get(collection.id);393		if all_permissions.get(&key::url()).is_none() {394			self.recorder().consume_sstore()?;395			<PalletCommon<T>>::set_property_permission(396				&collection,397				&caller,398				up_data_structs::PropertyKeyPermission {399					key: key::url(),400					permission: up_data_structs::PropertyPermission {401						mutable: true,402						collection_admin: true,403						token_owner: false,404					},405				},406			)407			.map_err(dispatch_to_evm::<T>)?;408		}409		if all_permissions.get(&key::suffix()).is_none() {410			self.recorder().consume_sstore()?;411			<PalletCommon<T>>::set_property_permission(412				&collection,413				&caller,414				up_data_structs::PropertyKeyPermission {415					key: key::suffix(),416					permission: up_data_structs::PropertyPermission {417						mutable: true,418						collection_admin: true,419						token_owner: false,420					},421				},422			)423			.map_err(dispatch_to_evm::<T>)?;424		}425426		let all_properties = <pallet_common::CollectionProperties<T>>::get(collection.id);427		if all_properties.get(&key::base_uri()).is_none() && !base_uri.is_empty() {428			self.recorder().consume_sstore()?;429			<PalletCommon<T>>::set_collection_properties(430				&collection,431				&caller,432				[up_data_structs::Property {433					key: key::base_uri(),434					value: base_uri435						.into_bytes()436						.try_into()437						.map_err(|_| "base uri is too large")?,438				}]439				.into_iter(),440			)441			.map_err(dispatch_to_evm::<T>)?;442		}443444		self.recorder().consume_sstore()?;445		collection.save().map_err(dispatch_to_evm::<T>)?;446447		Ok(())448	}449450	#[weight(<SelfWeightOf<T>>::destroy_collection())]451	fn destroy_collection(&mut self, caller: Caller, collection_address: Address) -> Result<()> {452		let caller = T::CrossAccountId::from_eth(caller);453454		let collection_id = pallet_common::eth::map_eth_to_id(&collection_address)455			.ok_or("Invalid collection address format")?;456		<Pallet<T>>::destroy_collection_internal(caller, collection_id)457			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)458	}459460	/// Check if a collection exists461	/// @param collectionAddress Address of the collection in question462	/// @return bool Does the collection exist?463	fn is_collection_exist(&self, _caller: Caller, collection_address: Address) -> Result<bool> {464		if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {465			let collection_id = id;466			return Ok(<CollectionById<T>>::contains_key(collection_id));467		}468469		Ok(false)470	}471472	fn collection_creation_fee(&self) -> Result<Value> {473		let price: u128 = T::CollectionCreationPrice::get()474			.try_into()475			.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait476			.expect("Collection creation price should be convertible to u128");477		Ok(price.into())478	}479480	/// Returns address of a collection.481	/// @param collectionId  - CollectionId  of the collection482	/// @return eth mirror address of the collection483	fn collection_address(&self, collection_id: u32) -> Result<Address> {484		Ok(collection_id_to_address(collection_id.into()))485	}486487	/// Returns collectionId of a collection.488	/// @param collectionAddress  - Eth address of the collection489	/// @return collectionId of the collection490	fn collection_id(&self, collection_address: Address) -> Result<u32> {491		map_eth_to_id(&collection_address)492			.map(|id| id.0)493			.ok_or(Error::Revert(format!(494				"failed to convert address {collection_address} into collectionId."495			)))496	}497}498499/// Implements [`OnMethodCall`], which delegates call to [`EvmCollectionHelpers`]500pub struct CollectionHelpersOnMethodCall<T: Config>(PhantomData<*const T>);501impl<T: Config + pallet_nonfungible::Config + pallet_refungible::Config> OnMethodCall<T>502	for CollectionHelpersOnMethodCall<T>503where504	T::AccountId: From<[u8; 32]>,505{506	fn is_reserved(contract: &sp_core::H160) -> bool {507		contract == &T::ContractAddress::get()508	}509510	fn is_used(contract: &sp_core::H160) -> bool {511		contract == &T::ContractAddress::get()512	}513514	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {515		if handle.code_address() != T::ContractAddress::get() {516			return None;517		}518519		let helpers =520			EvmCollectionHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));521		pallet_evm_coder_substrate::call(handle, helpers)522	}523524	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {525		(contract == &T::ContractAddress::get())526			.then(|| include_bytes!("./stubs/CollectionHelpers.raw").to_vec())527	}528}529530generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);531generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);532533fn error_field_too_long(feild: &str, bound: usize) -> Error {534	Error::Revert(format!("{feild} is too long. Max length is {bound}."))535}
modifiedpallets/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(())
 		}
modifiedruntime/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 {