git.delta.rocks / unique-network / refs/commits / a64769ff6bff

difftreelog

source

pallets/common/src/lib.rs82.4 KiBsourcehistory
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//! # Common pallet18//!19//! The Common pallet provides functionality for handling collections.20//!21//! ## Overview22//!23//! The Common pallet provides an interface for common collection operations for different collection types24//! (see [CommonCollectionOperations]), as well as a generic dispatcher for these, see [dispatch] module.25//! It also provides this functionality to EVM, see [erc] and [eth] modules.26//!27//! The Common pallet provides functions for:28//!29//! - Setting and approving collection sponsor.30//! - Get\set\delete allow list.31//! - Get\set\delete collection properties.32//! - Get\set\delete collection property permissions.33//! - Get\set\delete token property permissions.34//! - Get\set\delete collection administrators.35//! - Checking access permissions.36//!37//! ### Terminology38//! **Collection sponsor** - For the collection, you can set a sponsor, at whose expense it will39//! be possible to mint tokens.40//!41//! **Allow list** - List of users who have the right to minting tokens.42//!43//! **Collection properties** - Collection properties are simply key-value stores where various44//! metadata can be placed.45//!46//! **Permissions on token properties** - For each property in the token can be set permission47//! to change, see [`PropertyPermission`].48//!49//! **Collection administrator** - For a collection, you can set administrators who have the right50//! to most actions on the collection.5152#![warn(missing_docs)]53#![cfg_attr(not(feature = "std"), no_std)]54extern crate alloc;5556use core::{57	ops::{Deref, DerefMut},58	slice::from_ref,59	marker::PhantomData,60};61use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};62use sp_std::vec::Vec;63use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};64use evm_coder::ToLog;65use frame_support::{66	dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},67	ensure,68	traits::{69		Get,70		fungible::{Balanced, Debt, Inspect},71		tokens::{Imbalance, Precision, Preservation},72	},73	dispatch::Pays,74	transactional, fail,75};76use up_data_structs::{77	AccessMode, COLLECTION_NUMBER_LIMIT, Collection, RpcCollection, RpcCollectionFlags,78	CollectionId, CreateItemData, MAX_TOKEN_PREFIX_LENGTH, COLLECTION_ADMINS_LIMIT, TokenId,79	TokenChild, CollectionStats, MAX_TOKEN_OWNERSHIP, CollectionMode, NFT_SPONSOR_TRANSFER_TIMEOUT,80	FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT,81	CUSTOM_DATA_LIMIT, CollectionLimits, CreateCollectionData, SponsorshipState, CreateItemExData,82	SponsoringRateLimit, budget::Budget, PhantomType, Property,83	CollectionProperties as CollectionPropertiesT, TokenProperties, PropertiesPermissionMap,84	PropertyKey, PropertyValue, PropertyPermission, PropertiesError, TokenOwnerError,85	PropertyKeyPermission, TokenData, TrySetProperty, PropertyScope, CollectionPermissions,86};87use up_pov_estimate_rpc::PovInfo;8889pub use pallet::*;90use sp_core::H160;91use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, traits::Zero};9293#[cfg(feature = "runtime-benchmarks")]94pub mod benchmarking;95pub mod dispatch;96pub mod erc;97pub mod eth;98pub mod helpers;99#[allow(missing_docs)]100pub mod weights;101102use weights::WeightInfo;103104/// Weight info.105pub type SelfWeightOf<T> = <T as Config>::WeightInfo;106107/// Collection handle contains information about collection data and id.108/// Also provides functionality to count consumed gas.109///110/// CollectionHandle is used as a generic wrapper for collections of all types (except native fungible).111/// It allows to perform common operations and queries on any collection type,112/// both completely general for all, as well as their respective implementations of [`CommonCollectionOperations`].113#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]114pub struct CollectionHandle<T: Config> {115	/// Collection id116	pub id: CollectionId,117	collection: Collection<T::AccountId>,118	/// Substrate recorder for counting consumed gas119	pub recorder: SubstrateRecorder<T>,120}121122impl<T: Config> WithRecorder<T> for CollectionHandle<T> {123	fn recorder(&self) -> &SubstrateRecorder<T> {124		&self.recorder125	}126	fn into_recorder(self) -> SubstrateRecorder<T> {127		self.recorder128	}129}130131impl<T: Config> CollectionHandle<T> {132	/// Same as [CollectionHandle::new] but with an explicit gas limit.133	pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {134		Self::new_with_recorder(id, SubstrateRecorder::new(gas_limit))135	}136137	/// Same as [CollectionHandle::new] but with an existed [`SubstrateRecorder`].138	pub fn new_with_recorder(id: CollectionId, recorder: SubstrateRecorder<T>) -> Option<Self> {139		<CollectionById<T>>::get(id).map(|collection| Self {140			id,141			collection,142			recorder,143		})144	}145146	/// Retrives collection data from storage and creates collection handle with default parameters.147	/// If collection not found return `None`148	pub fn new(id: CollectionId) -> Option<Self> {149		Self::new_with_gas_limit(id, u64::MAX)150	}151152	/// Same as [`CollectionHandle::new`] but if collection not found [CollectionNotFound](Error::CollectionNotFound) returned.153	pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {154		Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)155	}156157	/// Consume gas for reading.158	pub fn consume_store_reads(159		&self,160		reads: u64,161	) -> pallet_evm_coder_substrate::execution::Result<()> {162		self.recorder().consume_store_reads(reads)163	}164165	/// Consume gas for writing.166	pub fn consume_store_writes(167		&self,168		writes: u64,169	) -> pallet_evm_coder_substrate::execution::Result<()> {170		self.recorder().consume_store_writes(writes)171	}172173	/// Consume gas for reading and writing.174	pub fn consume_store_reads_and_writes(175		&self,176		reads: u64,177		writes: u64,178	) -> pallet_evm_coder_substrate::execution::Result<()> {179		self.recorder()180			.consume_store_reads_and_writes(reads, writes)181	}182183	/// Save collection to storage.184	pub fn save(&self) -> DispatchResult {185		<CollectionById<T>>::insert(self.id, &self.collection);186		Ok(())187	}188189	/// Set collection sponsor.190	///191	/// Unique collections allows sponsoring for certain actions.192	/// This method allows you to set the sponsor of the collection.193	/// In order for sponsorship to become active, it must be confirmed through [`Self::confirm_sponsorship`].194	pub fn set_sponsor(195		&mut self,196		sender: &T::CrossAccountId,197		sponsor: T::AccountId,198	) -> DispatchResult {199		self.check_is_internal()?;200		self.check_is_owner_or_admin(sender)?;201202		self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.clone());203204		<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor));205		<PalletEvm<T>>::deposit_log(206			erc::CollectionHelpersEvents::CollectionChanged {207				collection_id: eth::collection_id_to_address(self.id),208			}209			.to_log(T::ContractAddress::get()),210		);211212		self.save()213	}214215	/// Force set `sponsor`.216	///217	/// Differs from [`set_sponsor`][`Self::set_sponsor`] in that confirmation218	/// from the `sponsor` is not required.219	///220	/// # Arguments221	///222	/// * `sponsor`: ID of the account of the sponsor-to-be.223	pub fn force_set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {224		self.check_is_internal()?;225226		self.collection.sponsorship = SponsorshipState::Confirmed(sponsor.clone());227228		<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor.clone()));229		<Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sponsor));230		<PalletEvm<T>>::deposit_log(231			erc::CollectionHelpersEvents::CollectionChanged {232				collection_id: eth::collection_id_to_address(self.id),233			}234			.to_log(T::ContractAddress::get()),235		);236237		self.save()238	}239240	/// Confirm sponsorship241	///242	/// In order for the sponsorship to become active, the user set as the sponsor must confirm their participation.243	/// Before confirming sponsorship, the user must be specified as the sponsor of the collection via [`Self::set_sponsor`].244	pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> DispatchResult {245		self.check_is_internal()?;246		ensure!(247			self.collection.sponsorship.pending_sponsor() == Some(sender),248			Error::<T>::ConfirmSponsorshipFail249		);250251		self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());252253		<Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sender.clone()));254		<PalletEvm<T>>::deposit_log(255			erc::CollectionHelpersEvents::CollectionChanged {256				collection_id: eth::collection_id_to_address(self.id),257			}258			.to_log(T::ContractAddress::get()),259		);260261		self.save()262	}263264	/// Remove collection sponsor.265	pub fn remove_sponsor(&mut self, sender: &T::CrossAccountId) -> DispatchResult {266		self.check_is_internal()?;267		self.check_is_owner_or_admin(sender)?;268269		self.collection.sponsorship = SponsorshipState::Disabled;270271		<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));272		<PalletEvm<T>>::deposit_log(273			erc::CollectionHelpersEvents::CollectionChanged {274				collection_id: eth::collection_id_to_address(self.id),275			}276			.to_log(T::ContractAddress::get()),277		);278		self.save()279	}280281	/// Force remove `sponsor`.282	///283	/// Differs from `remove_sponsor` in that284	/// it doesn't require consent from the `owner` of the collection.285	pub fn force_remove_sponsor(&mut self) -> DispatchResult {286		self.check_is_internal()?;287288		self.collection.sponsorship = SponsorshipState::Disabled;289290		<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));291		<PalletEvm<T>>::deposit_log(292			erc::CollectionHelpersEvents::CollectionChanged {293				collection_id: eth::collection_id_to_address(self.id),294			}295			.to_log(T::ContractAddress::get()),296		);297		self.save()298	}299300	/// Checks that the collection was created with, and must be operated upon through **Unique API**.301	/// Now check only the `external` flag and if it's **true**, then return [`Error::CollectionIsExternal`] error.302	pub fn check_is_internal(&self) -> DispatchResult {303		if self.flags.external {304			return Err(<Error<T>>::CollectionIsExternal)?;305		}306307		Ok(())308	}309310	/// Checks that the collection was created with, and must be operated upon through an **assimilated API**.311	/// Now check only the `external` flag and if it's **false**, then return [`Error::CollectionIsInternal`] error.312	pub fn check_is_external(&self) -> DispatchResult {313		if !self.flags.external {314			return Err(<Error<T>>::CollectionIsInternal)?;315		}316317		Ok(())318	}319}320321impl<T: Config> Deref for CollectionHandle<T> {322	type Target = Collection<T::AccountId>;323324	fn deref(&self) -> &Self::Target {325		&self.collection326	}327}328329impl<T: Config> DerefMut for CollectionHandle<T> {330	fn deref_mut(&mut self) -> &mut Self::Target {331		&mut self.collection332	}333}334335impl<T: Config> CollectionHandle<T> {336	/// Checks if the `user` is the owner of the collection.337	pub fn check_is_owner(&self, user: &T::CrossAccountId) -> DispatchResult {338		ensure!(*user.as_sub() == self.owner, <Error<T>>::NoPermission);339		Ok(())340	}341342	/// Returns **true** if the `user` is the owner or administrator of the collection.343	pub fn is_owner_or_admin(&self, user: &T::CrossAccountId) -> bool {344		*user.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, user))345	}346347	/// Checks if the `user` is the owner or administrator of the collection.348	pub fn check_is_owner_or_admin(&self, user: &T::CrossAccountId) -> DispatchResult {349		ensure!(self.is_owner_or_admin(user), <Error<T>>::NoPermission);350		Ok(())351	}352353	/// Returns **true** if354	/// * the `user`is a collection owner or admin355	/// * the collection limits allow the owner/admins to transfer/burn any collection token356	pub fn ignores_token_restrictions(&self, user: &T::CrossAccountId) -> bool {357		self.limits.owner_can_transfer() && self.is_owner_or_admin(user)358	}359360	/// Return **true** if `user` does not have enough token parts, and he can ignore such restrictions.361	pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {362		self.limits.owner_can_transfer() && self.is_owner_or_admin(user)363	}364365	/// Checks if the user is in the allow list. If not [Error::AddressNotInAllowlist] returns.366	pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {367		ensure!(368			<Allowlist<T>>::get((self.id, user)),369			<Error<T>>::AddressNotInAllowlist370		);371		Ok(())372	}373374	/// Changes collection owner to another account375	/// #### Store read/writes376	/// 1 writes377	pub fn change_owner(378		&mut self,379		caller: T::CrossAccountId,380		new_owner: T::CrossAccountId,381	) -> DispatchResult {382		self.check_is_internal()?;383		self.check_is_owner(&caller)?;384		self.collection.owner = new_owner.as_sub().clone();385386		<Pallet<T>>::deposit_event(Event::<T>::CollectionOwnerChanged(387			self.id,388			new_owner.as_sub().clone(),389		));390		<PalletEvm<T>>::deposit_log(391			erc::CollectionHelpersEvents::CollectionChanged {392				collection_id: eth::collection_id_to_address(self.id),393			}394			.to_log(T::ContractAddress::get()),395		);396397		self.save()398	}399}400401#[frame_support::pallet]402pub mod pallet {403404	use super::*;405	use dispatch::CollectionDispatch;406	use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};407	use up_data_structs::{TokenId, mapping::TokenAddressMapping};408	use scale_info::TypeInfo;409	use weights::WeightInfo;410411	#[pallet::config]412	pub trait Config:413		frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::Config + TypeInfo414	{415		/// Weight information for functions of this pallet.416		type WeightInfo: WeightInfo;417418		/// Events compatible with [`frame_system::Config::Event`].419		type RuntimeEvent: IsType<<Self as frame_system::Config>::RuntimeEvent> + From<Event<Self>>;420421		/// Handler of accounts and payment.422		type Currency: Balanced<Self::AccountId> + Inspect<Self::AccountId>;423424		/// Set price to create a collection.425		#[pallet::constant]426		type CollectionCreationPrice: Get<427			<<Self as Config>::Currency as Inspect<Self::AccountId>>::Balance,428		>;429430		/// Dispatcher of operations on collections.431		type CollectionDispatch: CollectionDispatch<Self>;432433		/// Account which holds the chain's treasury.434		type TreasuryAccountId: Get<Self::AccountId>;435436		/// Address under which the CollectionHelper contract would be available.437		#[pallet::constant]438		type ContractAddress: Get<H160>;439440		/// Mapper for token addresses to Ethereum addresses.441		type EvmTokenAddressMapping: TokenAddressMapping<H160>;442443		/// Mapper for token addresses to [`CrossAccountId`].444		type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;445	}446447	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);448	/// Collection id for native fungible collction.449	pub const NATIVE_FUNGIBLE_COLLECTION_ID: CollectionId = CollectionId(0);450451	#[pallet::pallet]452	#[pallet::storage_version(STORAGE_VERSION)]453	pub struct Pallet<T>(_);454455	#[pallet::extra_constants]456	impl<T: Config> Pallet<T> {457		/// Maximum admins per collection.458		pub fn collection_admins_limit() -> u32 {459			COLLECTION_ADMINS_LIMIT460		}461	}462463	#[pallet::genesis_config]464	pub struct GenesisConfig<T>(PhantomData<T>);465466	impl<T: Config> Default for GenesisConfig<T> {467		fn default() -> Self {468			Self(Default::default())469		}470	}471472	#[pallet::genesis_build]473	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {474		fn build(&self) {475			StorageVersion::new(1).put::<Pallet<T>>();476		}477	}478479	impl<T: Config> Pallet<T> {480		/// Helper function that handles deposit events481		pub fn deposit_event(event: Event<T>) {482			let event = <T as Config>::RuntimeEvent::from(event);483			let event = event.into();484			<frame_system::Pallet<T>>::deposit_event(event)485		}486	}487488	#[pallet::event]489	pub enum Event<T: Config> {490		/// New collection was created491		CollectionCreated(492			/// Globally unique identifier of newly created collection.493			CollectionId,494			/// [`CollectionMode`] converted into _u8_.495			u8,496			/// Collection owner.497			T::AccountId,498		),499500		/// New collection was destroyed501		CollectionDestroyed(502			/// Globally unique identifier of collection.503			CollectionId,504		),505506		/// New item was created.507		ItemCreated(508			/// Id of the collection where item was created.509			CollectionId,510			/// Id of an item. Unique within the collection.511			TokenId,512			/// Owner of newly created item513			T::CrossAccountId,514			/// Always 1 for NFT515			u128,516		),517518		/// Collection item was burned.519		ItemDestroyed(520			/// Id of the collection where item was destroyed.521			CollectionId,522			/// Identifier of burned NFT.523			TokenId,524			/// Which user has destroyed its tokens.525			T::CrossAccountId,526			/// Amount of token pieces destroed. Always 1 for NFT.527			u128,528		),529530		/// Item was transferred531		Transfer(532			/// Id of collection to which item is belong.533			CollectionId,534			/// Id of an item.535			TokenId,536			/// Original owner of item.537			T::CrossAccountId,538			/// New owner of item.539			T::CrossAccountId,540			/// Amount of token pieces transfered. Always 1 for NFT.541			u128,542		),543544		/// Amount pieces of token owned by `sender` was approved for `spender`.545		Approved(546			/// Id of collection to which item is belong.547			CollectionId,548			/// Id of an item.549			TokenId,550			/// Original owner of item.551			T::CrossAccountId,552			/// Id for which the approval was granted.553			T::CrossAccountId,554			/// Amount of token pieces transfered. Always 1 for NFT.555			u128,556		),557558		/// A `sender` approves operations on all owned tokens for `spender`.559		ApprovedForAll(560			/// Id of collection to which item is belong.561			CollectionId,562			/// Owner of a wallet.563			T::CrossAccountId,564			/// Id for which operator status was granted or rewoked.565			T::CrossAccountId,566			/// Is operator status granted or revoked?567			bool,568		),569570		/// The colletion property has been added or edited.571		CollectionPropertySet(572			/// Id of collection to which property has been set.573			CollectionId,574			/// The property that was set.575			PropertyKey,576		),577578		/// The property has been deleted.579		CollectionPropertyDeleted(580			/// Id of collection to which property has been deleted.581			CollectionId,582			/// The property that was deleted.583			PropertyKey,584		),585586		/// The token property has been added or edited.587		TokenPropertySet(588			/// Identifier of the collection whose token has the property set.589			CollectionId,590			/// The token for which the property was set.591			TokenId,592			/// The property that was set.593			PropertyKey,594		),595596		/// The token property has been deleted.597		TokenPropertyDeleted(598			/// Identifier of the collection whose token has the property deleted.599			CollectionId,600			/// The token for which the property was deleted.601			TokenId,602			/// The property that was deleted.603			PropertyKey,604		),605606		/// The token property permission of a collection has been set.607		PropertyPermissionSet(608			/// ID of collection to which property permission has been set.609			CollectionId,610			/// The property permission that was set.611			PropertyKey,612		),613614		/// Address was added to the allow list.615		AllowListAddressAdded(616			/// ID of the affected collection.617			CollectionId,618			/// Address of the added account.619			T::CrossAccountId,620		),621622		/// Address was removed from the allow list.623		AllowListAddressRemoved(624			/// ID of the affected collection.625			CollectionId,626			/// Address of the removed account.627			T::CrossAccountId,628		),629630		/// Collection admin was added.631		CollectionAdminAdded(632			/// ID of the affected collection.633			CollectionId,634			/// Admin address.635			T::CrossAccountId,636		),637638		/// Collection admin was removed.639		CollectionAdminRemoved(640			/// ID of the affected collection.641			CollectionId,642			/// Removed admin address.643			T::CrossAccountId,644		),645646		/// Collection limits were set.647		CollectionLimitSet(648			/// ID of the affected collection.649			CollectionId,650		),651652		/// Collection owned was changed.653		CollectionOwnerChanged(654			/// ID of the affected collection.655			CollectionId,656			/// New owner address.657			T::AccountId,658		),659660		/// Collection permissions were set.661		CollectionPermissionSet(662			/// ID of the affected collection.663			CollectionId,664		),665666		/// Collection sponsor was set.667		CollectionSponsorSet(668			/// ID of the affected collection.669			CollectionId,670			/// New sponsor address.671			T::AccountId,672		),673674		/// New sponsor was confirm.675		SponsorshipConfirmed(676			/// ID of the affected collection.677			CollectionId,678			/// New sponsor address.679			T::AccountId,680		),681682		/// Collection sponsor was removed.683		CollectionSponsorRemoved(684			/// ID of the affected collection.685			CollectionId,686		),687	}688689	#[pallet::error]690	pub enum Error<T> {691		/// This collection does not exist.692		CollectionNotFound,693		/// Sender parameter and item owner must be equal.694		MustBeTokenOwner,695		/// No permission to perform action696		NoPermission,697		/// Destroying only empty collections is allowed698		CantDestroyNotEmptyCollection,699		/// Collection is not in mint mode.700		PublicMintingNotAllowed,701		/// Address is not in allow list.702		AddressNotInAllowlist,703704		/// Collection name can not be longer than 63 char.705		CollectionNameLimitExceeded,706		/// Collection description can not be longer than 255 char.707		CollectionDescriptionLimitExceeded,708		/// Token prefix can not be longer than 15 char.709		CollectionTokenPrefixLimitExceeded,710		/// Total collections bound exceeded.711		TotalCollectionsLimitExceeded,712		/// Exceeded max admin count713		CollectionAdminCountExceeded,714		/// Collection limit bounds per collection exceeded715		CollectionLimitBoundsExceeded,716		/// Tried to enable permissions which are only permitted to be disabled717		OwnerPermissionsCantBeReverted,718		/// Collection settings not allowing items transferring719		TransferNotAllowed,720		/// Account token limit exceeded per collection721		AccountTokenLimitExceeded,722		/// Collection token limit exceeded723		CollectionTokenLimitExceeded,724		/// Metadata flag frozen725		MetadataFlagFrozen,726727		/// Item does not exist728		TokenNotFound,729		/// Item is balance not enough730		TokenValueTooLow,731		/// Requested value is more than the approved732		ApprovedValueTooLow,733		/// Tried to approve more than owned734		CantApproveMoreThanOwned,735		/// Only spending from eth mirror could be approved736		AddressIsNotEthMirror,737738		/// Can't transfer tokens to ethereum zero address739		AddressIsZero,740741		/// The operation is not supported742		UnsupportedOperation,743744		/// Insufficient funds to perform an action745		NotSufficientFounds,746747		/// User does not satisfy the nesting rule748		UserIsNotAllowedToNest,749		/// Only tokens from specific collections may nest tokens under this one750		SourceCollectionIsNotAllowedToNest,751752		/// Tried to store more data than allowed in collection field753		CollectionFieldSizeExceeded,754755		/// Tried to store more property data than allowed756		NoSpaceForProperty,757758		/// Tried to store more property keys than allowed759		PropertyLimitReached,760761		/// Property key is too long762		PropertyKeyIsTooLong,763764		/// Only ASCII letters, digits, and symbols `_`, `-`, and `.` are allowed765		InvalidCharacterInPropertyKey,766767		/// Empty property keys are forbidden768		EmptyPropertyKey,769770		/// Tried to access an external collection with an internal API771		CollectionIsExternal,772773		/// Tried to access an internal collection with an external API774		CollectionIsInternal,775776		/// This address is not set as sponsor, use setCollectionSponsor first.777		ConfirmSponsorshipFail,778779		/// The user is not an administrator.780		UserIsNotCollectionAdmin,781	}782783	/// Storage of the count of created collections. Essentially contains the last collection ID.784	#[pallet::storage]785	pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;786787	/// Storage of the count of deleted collections.788	#[pallet::storage]789	pub type DestroyedCollectionCount<T> =790		StorageValue<Value = CollectionId, QueryKind = ValueQuery>;791792	/// Storage of collection info.793	#[pallet::storage]794	pub type CollectionById<T> = StorageMap<795		Hasher = Blake2_128Concat,796		Key = CollectionId,797		Value = Collection<<T as frame_system::Config>::AccountId>,798		QueryKind = OptionQuery,799	>;800801	/// Storage of collection properties.802	#[pallet::storage]803	#[pallet::getter(fn collection_properties)]804	pub type CollectionProperties<T> = StorageMap<805		Hasher = Blake2_128Concat,806		Key = CollectionId,807		Value = CollectionPropertiesT,808		QueryKind = ValueQuery,809	>;810811	/// Storage of token property permissions of a collection.812	#[pallet::storage]813	#[pallet::getter(fn property_permissions)]814	pub type CollectionPropertyPermissions<T> = StorageMap<815		Hasher = Blake2_128Concat,816		Key = CollectionId,817		Value = PropertiesPermissionMap,818		QueryKind = ValueQuery,819	>;820821	/// Storage of the amount of collection admins.822	#[pallet::storage]823	pub type AdminAmount<T> = StorageMap<824		Hasher = Blake2_128Concat,825		Key = CollectionId,826		Value = u32,827		QueryKind = ValueQuery,828	>;829830	/// List of collection admins.831	#[pallet::storage]832	pub type IsAdmin<T: Config> = StorageNMap<833		Key = (834			Key<Blake2_128Concat, CollectionId>,835			Key<Blake2_128Concat, T::CrossAccountId>,836		),837		Value = bool,838		QueryKind = ValueQuery,839	>;840841	/// Allowlisted collection users.842	#[pallet::storage]843	pub type Allowlist<T: Config> = StorageNMap<844		Key = (845			Key<Blake2_128Concat, CollectionId>,846			Key<Blake2_128Concat, T::CrossAccountId>,847		),848		Value = bool,849		QueryKind = ValueQuery,850	>;851852	/// Not used by code, exists only to provide some types to metadata.853	#[pallet::storage]854	pub type DummyStorageValue<T: Config> = StorageValue<855		Value = (856			CollectionStats,857			CollectionId,858			TokenId,859			TokenChild,860			PhantomType<(861				TokenData<T::CrossAccountId>,862				RpcCollection<T::AccountId>,863				// PoV Estimate Info864				PovInfo,865			)>,866		),867		QueryKind = OptionQuery,868	>;869}870871/// Value representation with delayed initialization time.872pub struct LazyValue<T, F: FnOnce() -> T> {873	value: Option<T>,874	f: Option<F>,875}876877impl<T, F: FnOnce() -> T> LazyValue<T, F> {878	/// Create a new LazyValue.879	pub fn new(f: F) -> Self {880		Self {881			value: None,882			f: Some(f),883		}884	}885886	/// Get the value. If it is called the first time, the value will be initialized.887	pub fn value(&mut self) -> &T {888		self.compute_value_if_not_already();889		self.value.as_ref().unwrap()890	}891892	/// Get the value. If it is called the first time, the value will be initialized.893	pub fn value_mut(&mut self) -> &mut T {894		self.compute_value_if_not_already();895		self.value.as_mut().unwrap()896	}897898	fn into_inner(mut self) -> T {899		self.compute_value_if_not_already();900		self.value.unwrap()901	}902903	/// Is value initialized?904	pub fn has_value(&self) -> bool {905		self.value.is_some()906	}907908	fn compute_value_if_not_already(&mut self) {909		if self.value.is_none() {910			self.value = Some(self.f.take().unwrap()())911		}912	}913}914915fn check_token_permissions<T, FCA, FTO, FTE>(916	collection_admin_permitted: bool,917	token_owner_permitted: bool,918	is_collection_admin: &mut LazyValue<bool, FCA>,919	is_token_owner: &mut LazyValue<Result<bool, DispatchError>, FTO>,920	is_token_exist: &mut LazyValue<bool, FTE>,921) -> DispatchResult922where923	T: Config,924	FCA: FnOnce() -> bool,925	FTO: FnOnce() -> Result<bool, DispatchError>,926	FTE: FnOnce() -> bool,927{928	if !(collection_admin_permitted && *is_collection_admin.value()929		|| token_owner_permitted && (*is_token_owner.value())?)930	{931		fail!(<Error<T>>::NoPermission);932	}933934	let token_exist_due_to_owner_check_success =935		is_token_owner.has_value() && (*is_token_owner.value())?;936937	// If the token owner check has occurred and succeeded,938	// we know the token exists (otherwise, the owner check must fail).939	if !token_exist_due_to_owner_check_success {940		// If the token owner check didn't occur,941		// we must check the token's existence ourselves.942		if !is_token_exist.value() {943			fail!(<Error<T>>::TokenNotFound);944		}945	}946947	Ok(())948}949950impl<T: Config> Pallet<T> {951	/// Enshure that receiver address is correct.952	///953	/// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens.954	pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {955		ensure!(956			&T::CrossAccountId::from_eth(H160([0; 20])) != receiver,957			<Error<T>>::AddressIsZero958		);959		Ok(())960	}961962	/// Get a vector of collection admins.963	pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {964		<IsAdmin<T>>::iter_prefix((collection,))965			.map(|(a, _)| a)966			.collect()967	}968969	/// Get a vector of users allowed to mint tokens.970	pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {971		<Allowlist<T>>::iter_prefix((collection,))972			.map(|(a, _)| a)973			.collect()974	}975976	/// Is `user` allowed to mint token in `collection`.977	pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {978		<Allowlist<T>>::get((collection, user))979	}980981	/// Get statistics of collections.982	pub fn collection_stats() -> CollectionStats {983		let created = <CreatedCollectionCount<T>>::get();984		let destroyed = <DestroyedCollectionCount<T>>::get();985		CollectionStats {986			created: created.0,987			destroyed: destroyed.0,988			alive: created.0 - destroyed.0,989		}990	}991992	/// Get the effective limits for the collection.993	pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {994		let collection = <CollectionById<T>>::get(collection)?;995		let limits = collection.limits;996		let effective_limits = CollectionLimits {997			account_token_ownership_limit: Some(limits.account_token_ownership_limit()),998			sponsored_data_size: Some(limits.sponsored_data_size()),999			sponsored_data_rate_limit: Some(1000				limits1001					.sponsored_data_rate_limit1002					.unwrap_or(SponsoringRateLimit::SponsoringDisabled),1003			),1004			token_limit: Some(limits.token_limit()),1005			sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(1006				match collection.mode {1007					CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1008					CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1009					CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1010				},1011			)),1012			sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),1013			owner_can_transfer: Some(limits.owner_can_transfer()),1014			owner_can_destroy: Some(limits.owner_can_destroy()),1015			transfers_enabled: Some(limits.transfers_enabled()),1016		};10171018		Some(effective_limits)1019	}10201021	/// Returns information about the `collection` adapted for rpc.1022	pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {1023		let Collection {1024			name,1025			description,1026			owner,1027			mode,1028			token_prefix,1029			sponsorship,1030			limits,1031			permissions,1032			flags,1033		} = <CollectionById<T>>::get(collection)?;10341035		let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)1036			.into_iter()1037			.map(|(key, permission)| PropertyKeyPermission { key, permission })1038			.collect();10391040		let properties = <CollectionProperties<T>>::get(collection)1041			.into_iter()1042			.map(|(key, value)| Property { key, value })1043			.collect();10441045		let permissions = CollectionPermissions {1046			access: Some(permissions.access()),1047			mint_mode: Some(permissions.mint_mode()),1048			nesting: Some(permissions.nesting().clone()),1049		};10501051		Some(RpcCollection {1052			name: name.into_inner(),1053			description: description.into_inner(),1054			owner,1055			mode,1056			token_prefix: token_prefix.into_inner(),1057			sponsorship,1058			limits,1059			permissions,1060			token_property_permissions,1061			properties,1062			read_only: flags.external,10631064			flags: RpcCollectionFlags {1065				foreign: flags.foreign,1066				erc721metadata: flags.erc721metadata,1067			},1068		})1069	}1070}10711072macro_rules! limit_default {1073	($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1074		$(1075			if let Some($new) = $new.$field {1076				let $old = $old.$field($($arg)?);1077				let _ = $new;1078				let _ = $old;1079				$check1080			} else {1081				$new.$field = $old.$field1082			}1083		)*1084	}};1085}1086macro_rules! limit_default_clone {1087	($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{1088		$(1089			if let Some($new) = $new.$field.clone() {1090				let $old = $old.$field($($arg)?);1091				let _ = $new;1092				let _ = $old;1093				$check1094			} else {1095				$new.$field = $old.$field.clone()1096			}1097		)*1098	}};1099}11001101impl<T: Config> Pallet<T> {1102	/// Create new collection.1103	///1104	/// * `owner` - The owner of the collection.1105	/// * `data` - Description of the created collection.1106	/// * `flags` - Extra flags to store.1107	pub fn init_collection(1108		owner: T::CrossAccountId,1109		payer: T::CrossAccountId,1110		data: CreateCollectionData<T::CrossAccountId>,1111	) -> Result<CollectionId, DispatchError> {1112		ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);1113		Self::init_collection_internal(owner, payer, data)1114	}11151116	/// Initializes the collection with ForeignCollection flag. Returns [CollectionId] on success, [DispatchError] otherwise.1117	pub fn init_foreign_collection(1118		owner: T::CrossAccountId,1119		payer: T::CrossAccountId,1120		mut data: CreateCollectionData<T::CrossAccountId>,1121	) -> Result<CollectionId, DispatchError> {1122		data.flags.foreign = true;1123		let id = Self::init_collection_internal(owner, payer, data)?;1124		Ok(id)1125	}11261127	fn init_collection_internal(1128		owner: T::CrossAccountId,1129		payer: T::CrossAccountId,1130		data: CreateCollectionData<T::CrossAccountId>,1131	) -> Result<CollectionId, DispatchError> {1132		{1133			ensure!(1134				data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,1135				Error::<T>::CollectionTokenPrefixLimitExceeded1136			);1137		}11381139		let created_count = <CreatedCollectionCount<T>>::get()1140			.01141			.checked_add(1)1142			.ok_or(ArithmeticError::Overflow)?;1143		let destroyed_count = <DestroyedCollectionCount<T>>::get().0;1144		let id = CollectionId(created_count);11451146		// bound Total number of collections1147		ensure!(1148			created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,1149			<Error<T>>::TotalCollectionsLimitExceeded1150		);11511152		// =========11531154		let collection = Collection {1155			owner: owner.as_sub().clone(),1156			name: data.name,1157			mode: data.mode.clone(),1158			description: data.description,1159			token_prefix: data.token_prefix,1160			sponsorship: data1161				.pending_sponsor1162				.map(|sponsor| SponsorshipState::Unconfirmed(sponsor.as_sub().clone()))1163				.unwrap_or_default(),1164			limits: data1165				.limits1166				.map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))1167				.unwrap_or_else(|| Ok(CollectionLimits::default()))?,1168			permissions: data1169				.permissions1170				.map(|permissions| {1171					Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions)1172				})1173				.unwrap_or_else(|| Ok(CollectionPermissions::default()))?,1174			flags: data.flags,1175		};11761177		let mut collection_properties = CollectionPropertiesT::new();1178		collection_properties1179			.try_set_from_iter(data.properties.into_iter())1180			.map_err(<Error<T>>::from)?;11811182		CollectionProperties::<T>::insert(id, collection_properties);11831184		let mut token_props_permissions = PropertiesPermissionMap::new();1185		token_props_permissions1186			.try_set_from_iter(data.token_property_permissions.into_iter())1187			.map_err(<Error<T>>::from)?;11881189		CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);11901191		let mut admin_amount = 0u32;1192		for admin in data.admin_list.iter() {1193			if !<IsAdmin<T>>::get((id, admin)) {1194				<IsAdmin<T>>::insert((id, admin), true);1195				admin_amount = admin_amount1196					.checked_add(1)1197					.ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1198			}1199		}1200		ensure!(1201			admin_amount <= Self::collection_admins_limit(),1202			<Error<T>>::CollectionAdminCountExceeded,1203		);1204		<AdminAmount<T>>::insert(id, admin_amount);12051206		// Take a (non-refundable) deposit of collection creation1207		{1208			let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();1209			imbalance.subsume(<T as Config>::Currency::deposit(1210				&T::TreasuryAccountId::get(),1211				T::CollectionCreationPrice::get(),1212				Precision::Exact,1213			)?);1214			let credit =1215				<T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)1216					.map_err(|_| Error::<T>::NotSufficientFounds)?;12171218			debug_assert!(credit.peek().is_zero())1219		}12201221		<CreatedCollectionCount<T>>::put(created_count);1222		<Pallet<T>>::deposit_event(Event::CollectionCreated(1223			id,1224			data.mode.id(),1225			owner.as_sub().clone(),1226		));1227		<PalletEvm<T>>::deposit_log(1228			erc::CollectionHelpersEvents::CollectionCreated {1229				owner: *owner.as_eth(),1230				collection_id: eth::collection_id_to_address(id),1231			}1232			.to_log(T::ContractAddress::get()),1233		);1234		<CollectionById<T>>::insert(id, collection);1235		Ok(id)1236	}12371238	/// Destroy collection.1239	///1240	/// * `collection` - Collection handler.1241	/// * `sender` - The owner or administrator of the collection.1242	pub fn destroy_collection(1243		collection: CollectionHandle<T>,1244		sender: &T::CrossAccountId,1245	) -> DispatchResult {1246		ensure!(1247			collection.limits.owner_can_destroy(),1248			<Error<T>>::NoPermission,1249		);1250		collection.check_is_owner(sender)?;12511252		let destroyed_collections = <DestroyedCollectionCount<T>>::get()1253			.01254			.checked_add(1)1255			.ok_or(ArithmeticError::Overflow)?;12561257		// =========12581259		<DestroyedCollectionCount<T>>::put(destroyed_collections);1260		<CollectionById<T>>::remove(collection.id);1261		<AdminAmount<T>>::remove(collection.id);1262		let _ = <IsAdmin<T>>::clear_prefix((collection.id,), u32::MAX, None);1263		let _ = <Allowlist<T>>::clear_prefix((collection.id,), u32::MAX, None);1264		<CollectionProperties<T>>::remove(collection.id);12651266		<Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));12671268		<PalletEvm<T>>::deposit_log(1269			erc::CollectionHelpersEvents::CollectionDestroyed {1270				collection_id: eth::collection_id_to_address(collection.id),1271			}1272			.to_log(T::ContractAddress::get()),1273		);1274		Ok(())1275	}12761277	/// This function sets or removes a collection properties according to1278	/// `properties_updates` contents:1279	/// * sets a property under the <key> with the value provided `(<key>, Some(<value>))`1280	/// * removes a property under the <key> if the value is `None` `(<key>, None)`.1281	///1282	/// This function fires an event for each property change.1283	/// In case of an error, all the changes (including the events) will be reverted1284	/// since the function is transactional.1285	#[transactional]1286	fn modify_collection_properties(1287		collection: &CollectionHandle<T>,1288		sender: &T::CrossAccountId,1289		properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,1290	) -> DispatchResult {1291		collection.check_is_owner_or_admin(sender)?;12921293		let mut stored_properties = <CollectionProperties<T>>::get(collection.id);12941295		for (key, value) in properties_updates {1296			match value {1297				Some(value) => {1298					stored_properties1299						.try_set(key.clone(), value)1300						.map_err(<Error<T>>::from)?;13011302					Self::deposit_event(Event::CollectionPropertySet(collection.id, key));1303					<PalletEvm<T>>::deposit_log(1304						erc::CollectionHelpersEvents::CollectionChanged {1305							collection_id: eth::collection_id_to_address(collection.id),1306						}1307						.to_log(T::ContractAddress::get()),1308					);1309				}1310				None => {1311					stored_properties.remove(&key).map_err(<Error<T>>::from)?;13121313					Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, key));1314					<PalletEvm<T>>::deposit_log(1315						erc::CollectionHelpersEvents::CollectionChanged {1316							collection_id: eth::collection_id_to_address(collection.id),1317						}1318						.to_log(T::ContractAddress::get()),1319					);1320				}1321			}1322		}13231324		<CollectionProperties<T>>::set(collection.id, stored_properties);13251326		Ok(())1327	}13281329	/// Sets or unsets the approval of a given operator.1330	///1331	/// The `operator` is allowed to transfer all token pieces of the `owner` on their behalf.1332	/// - `owner`: Token owner1333	/// - `operator`: Operator1334	/// - `approve`: Should operator status be granted or revoked?1335	pub fn set_allowance_for_all(1336		collection: &CollectionHandle<T>,1337		owner: &T::CrossAccountId,1338		operator: &T::CrossAccountId,1339		approve: bool,1340		set_allowance: impl FnOnce(),1341		log: evm_coder::ethereum::Log,1342	) -> DispatchResult {1343		if collection.permissions.access() == AccessMode::AllowList {1344			collection.check_allowlist(owner)?;1345			collection.check_allowlist(operator)?;1346		}13471348		Self::ensure_correct_receiver(operator)?;13491350		set_allowance();13511352		<PalletEvm<T>>::deposit_log(log);1353		Self::deposit_event(Event::ApprovedForAll(1354			collection.id,1355			owner.clone(),1356			operator.clone(),1357			approve,1358		));1359		Ok(())1360	}13611362	/// Set collection property.1363	///1364	/// * `collection` - Collection handler.1365	/// * `sender` - The owner or administrator of the collection.1366	/// * `property` - The property to set.1367	pub fn set_collection_property(1368		collection: &CollectionHandle<T>,1369		sender: &T::CrossAccountId,1370		property: Property,1371	) -> DispatchResult {1372		Self::set_collection_properties(collection, sender, [property].into_iter())1373	}13741375	/// Set a scoped collection property, where the scope is a special prefix1376	/// prohibiting a user access to change the property directly.1377	///1378	/// * `collection_id` - ID of the collection for which the property is being set.1379	/// * `scope` - Property scope.1380	/// * `property` - The property to set.1381	pub fn set_scoped_collection_property(1382		collection_id: CollectionId,1383		scope: PropertyScope,1384		property: Property,1385	) -> DispatchResult {1386		CollectionProperties::<T>::try_mutate(collection_id, |properties| {1387			properties.try_scoped_set(scope, property.key, property.value)1388		})1389		.map_err(<Error<T>>::from)?;13901391		Ok(())1392	}13931394	/// Set scoped collection properties, where the scope is a special prefix1395	/// prohibiting a user access to change the properties directly.1396	///1397	/// * `collection_id` - ID of the collection for which the properties is being set.1398	/// * `scope` - Property scope.1399	/// * `properties` - The properties to set.1400	pub fn set_scoped_collection_properties(1401		collection_id: CollectionId,1402		scope: PropertyScope,1403		properties: impl Iterator<Item = Property>,1404	) -> DispatchResult {1405		CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {1406			stored_properties.try_scoped_set_from_iter(scope, properties)1407		})1408		.map_err(<Error<T>>::from)?;14091410		Ok(())1411	}14121413	/// Set collection properties.1414	///1415	/// * `collection` - Collection handler.1416	/// * `sender` - The owner or administrator of the collection.1417	/// * `properties` - The properties to set.1418	pub fn set_collection_properties(1419		collection: &CollectionHandle<T>,1420		sender: &T::CrossAccountId,1421		properties: impl Iterator<Item = Property>,1422	) -> DispatchResult {1423		Self::modify_collection_properties(1424			collection,1425			sender,1426			properties.map(|property| (property.key, Some(property.value))),1427		)1428	}14291430	/// Delete collection property.1431	///1432	/// * `collection` - Collection handler.1433	/// * `sender` - The owner or administrator of the collection.1434	/// * `property` - The property to delete.1435	pub fn delete_collection_property(1436		collection: &CollectionHandle<T>,1437		sender: &T::CrossAccountId,1438		property_key: PropertyKey,1439	) -> DispatchResult {1440		Self::delete_collection_properties(collection, sender, [property_key].into_iter())1441	}14421443	/// Delete collection properties.1444	///1445	/// * `collection` - Collection handler.1446	/// * `sender` - The owner or administrator of the collection.1447	/// * `properties` - The properties to delete.1448	pub fn delete_collection_properties(1449		collection: &CollectionHandle<T>,1450		sender: &T::CrossAccountId,1451		property_keys: impl Iterator<Item = PropertyKey>,1452	) -> DispatchResult {1453		Self::modify_collection_properties(collection, sender, property_keys.map(|key| (key, None)))1454	}14551456	/// Set collection propetry permission without any checks.1457	///1458	/// Used for migrations.1459	///1460	/// * `collection` - Collection handler.1461	/// * `property_permissions` - Property permissions.1462	pub fn set_property_permission_unchecked(1463		collection: CollectionId,1464		property_permission: PropertyKeyPermission,1465	) -> DispatchResult {1466		<CollectionPropertyPermissions<T>>::try_mutate(collection, |permissions| {1467			permissions.try_set(property_permission.key, property_permission.permission)1468		})1469		.map_err(<Error<T>>::from)?;1470		Ok(())1471	}14721473	/// Set collection property permission.1474	///1475	/// * `collection` - Collection handler.1476	/// * `sender` - The owner or administrator of the collection.1477	/// * `property_permission` - Property permission.1478	pub fn set_property_permission(1479		collection: &CollectionHandle<T>,1480		sender: &T::CrossAccountId,1481		property_permission: PropertyKeyPermission,1482	) -> DispatchResult {1483		Self::set_scoped_property_permission(1484			collection,1485			sender,1486			PropertyScope::None,1487			property_permission,1488		)1489	}14901491	/// Set collection property permission with scope.1492	///1493	/// * `collection` - Collection handler.1494	/// * `sender` - The owner or administrator of the collection.1495	/// * `scope` - Property scope.1496	/// * `property_permission` - Property permission.1497	pub fn set_scoped_property_permission(1498		collection: &CollectionHandle<T>,1499		sender: &T::CrossAccountId,1500		scope: PropertyScope,1501		property_permission: PropertyKeyPermission,1502	) -> DispatchResult {1503		collection.check_is_owner_or_admin(sender)?;15041505		let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);1506		let current_permission = all_permissions.get(&property_permission.key);1507		if matches![1508			current_permission,1509			Some(PropertyPermission { mutable: false, .. })1510		] {1511			return Err(<Error<T>>::NoPermission.into());1512		}15131514		CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {1515			let property_permission = property_permission.clone();1516			permissions.try_scoped_set(1517				scope,1518				property_permission.key,1519				property_permission.permission,1520			)1521		})1522		.map_err(<Error<T>>::from)?;15231524		Self::deposit_event(Event::PropertyPermissionSet(1525			collection.id,1526			property_permission.key,1527		));1528		<PalletEvm<T>>::deposit_log(1529			erc::CollectionHelpersEvents::CollectionChanged {1530				collection_id: eth::collection_id_to_address(collection.id),1531			}1532			.to_log(T::ContractAddress::get()),1533		);15341535		Ok(())1536	}15371538	/// Set token property permission.1539	///1540	/// * `collection` - Collection handler.1541	/// * `sender` - The owner or administrator of the collection.1542	/// * `property_permissions` - Property permissions.1543	#[transactional]1544	pub fn set_token_property_permissions(1545		collection: &CollectionHandle<T>,1546		sender: &T::CrossAccountId,1547		property_permissions: Vec<PropertyKeyPermission>,1548	) -> DispatchResult {1549		Self::set_scoped_token_property_permissions(1550			collection,1551			sender,1552			PropertyScope::None,1553			property_permissions,1554		)1555	}15561557	/// Set token property permission with scope.1558	///1559	/// * `collection` - Collection handler.1560	/// * `sender` - The owner or administrator of the collection.1561	/// * `scope` - Property scope.1562	/// * `property_permissions` - Property permissions.1563	#[transactional]1564	pub fn set_scoped_token_property_permissions(1565		collection: &CollectionHandle<T>,1566		sender: &T::CrossAccountId,1567		scope: PropertyScope,1568		property_permissions: Vec<PropertyKeyPermission>,1569	) -> DispatchResult {1570		for prop_pemission in property_permissions {1571			Self::set_scoped_property_permission(collection, sender, scope, prop_pemission)?;1572		}15731574		Ok(())1575	}15761577	/// Get collection property.1578	pub fn get_collection_property(1579		collection_id: CollectionId,1580		key: &PropertyKey,1581	) -> Option<PropertyValue> {1582		Self::collection_properties(collection_id).get(key).cloned()1583	}15841585	/// Convert byte vector to property key vector.1586	pub fn bytes_keys_to_property_keys(1587		keys: Vec<Vec<u8>>,1588	) -> Result<Vec<PropertyKey>, DispatchError> {1589		keys.into_iter()1590			.map(|key| -> Result<PropertyKey, DispatchError> {1591				key.try_into()1592					.map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())1593			})1594			.collect::<Result<Vec<PropertyKey>, DispatchError>>()1595	}15961597	/// Get properties according to given keys.1598	pub fn filter_collection_properties(1599		collection_id: CollectionId,1600		keys: Option<Vec<PropertyKey>>,1601	) -> Result<Vec<Property>, DispatchError> {1602		let properties = Self::collection_properties(collection_id);16031604		let properties = keys1605			.map(|keys| {1606				keys.into_iter()1607					.filter_map(|key| {1608						properties.get(&key).map(|value| Property {1609							key,1610							value: value.clone(),1611						})1612					})1613					.collect()1614			})1615			.unwrap_or_else(|| {1616				properties1617					.into_iter()1618					.map(|(key, value)| Property { key, value })1619					.collect()1620			});16211622		Ok(properties)1623	}16241625	/// Get property permissions according to given keys.1626	pub fn filter_property_permissions(1627		collection_id: CollectionId,1628		keys: Option<Vec<PropertyKey>>,1629	) -> Result<Vec<PropertyKeyPermission>, DispatchError> {1630		let permissions = Self::property_permissions(collection_id);16311632		let key_permissions = keys1633			.map(|keys| {1634				keys.into_iter()1635					.filter_map(|key| {1636						permissions1637							.get(&key)1638							.map(|permission| PropertyKeyPermission {1639								key,1640								permission: permission.clone(),1641							})1642					})1643					.collect()1644			})1645			.unwrap_or_else(|| {1646				permissions1647					.into_iter()1648					.map(|(key, permission)| PropertyKeyPermission { key, permission })1649					.collect()1650			});16511652		Ok(key_permissions)1653	}16541655	/// Toggle `user` participation in the `collection`'s allow list.1656	/// #### Store read/writes1657	/// 1 writes1658	pub fn toggle_allowlist(1659		collection: &CollectionHandle<T>,1660		sender: &T::CrossAccountId,1661		user: &T::CrossAccountId,1662		allowed: bool,1663	) -> DispatchResult {1664		collection.check_is_owner_or_admin(sender)?;16651666		// =========16671668		if allowed {1669			<Allowlist<T>>::insert((collection.id, user), true);1670			Self::deposit_event(Event::<T>::AllowListAddressAdded(1671				collection.id,1672				user.clone(),1673			));1674		} else {1675			<Allowlist<T>>::remove((collection.id, user));1676			Self::deposit_event(Event::<T>::AllowListAddressRemoved(1677				collection.id,1678				user.clone(),1679			));1680		}16811682		<PalletEvm<T>>::deposit_log(1683			erc::CollectionHelpersEvents::CollectionChanged {1684				collection_id: eth::collection_id_to_address(collection.id),1685			}1686			.to_log(T::ContractAddress::get()),1687		);16881689		Ok(())1690	}16911692	/// Toggle `user` participation in the `collection`'s admin list.1693	/// #### Store read/writes1694	/// 2 reads, 2 writes1695	pub fn toggle_admin(1696		collection: &CollectionHandle<T>,1697		sender: &T::CrossAccountId,1698		user: &T::CrossAccountId,1699		admin: bool,1700	) -> DispatchResult {1701		collection.check_is_internal()?;1702		collection.check_is_owner(sender)?;17031704		let is_admin = <IsAdmin<T>>::get((collection.id, user));1705		if is_admin == admin {1706			if admin {1707				return Ok(());1708			} else {1709				return Err(Error::<T>::UserIsNotCollectionAdmin.into());1710			}1711		}1712		let amount = <AdminAmount<T>>::get(collection.id);17131714		// =========17151716		if admin {1717			let amount = amount1718				.checked_add(1)1719				.ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1720			ensure!(1721				amount <= Self::collection_admins_limit(),1722				<Error<T>>::CollectionAdminCountExceeded,1723			);17241725			<AdminAmount<T>>::insert(collection.id, amount);1726			<IsAdmin<T>>::insert((collection.id, user), true);17271728			Self::deposit_event(Event::<T>::CollectionAdminAdded(1729				collection.id,1730				user.clone(),1731			));1732		} else {1733			<AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));1734			<IsAdmin<T>>::remove((collection.id, user));17351736			Self::deposit_event(Event::<T>::CollectionAdminRemoved(1737				collection.id,1738				user.clone(),1739			));1740		}17411742		<PalletEvm<T>>::deposit_log(1743			erc::CollectionHelpersEvents::CollectionChanged {1744				collection_id: eth::collection_id_to_address(collection.id),1745			}1746			.to_log(T::ContractAddress::get()),1747		);17481749		Ok(())1750	}17511752	/// Update collection limits.1753	pub fn update_limits(1754		user: &T::CrossAccountId,1755		collection: &mut CollectionHandle<T>,1756		new_limit: CollectionLimits,1757	) -> DispatchResult {1758		collection.check_is_internal()?;1759		collection.check_is_owner_or_admin(user)?;17601761		collection.limits =1762			Self::clamp_limits(collection.mode.clone(), &collection.limits, new_limit)?;17631764		Self::deposit_event(Event::<T>::CollectionLimitSet(collection.id));1765		<PalletEvm<T>>::deposit_log(1766			erc::CollectionHelpersEvents::CollectionChanged {1767				collection_id: eth::collection_id_to_address(collection.id),1768			}1769			.to_log(T::ContractAddress::get()),1770		);17711772		collection.save()1773	}17741775	/// Merge set fields from `new_limit` to `old_limit`.1776	fn clamp_limits(1777		mode: CollectionMode,1778		old_limit: &CollectionLimits,1779		mut new_limit: CollectionLimits,1780	) -> Result<CollectionLimits, DispatchError> {1781		let limits = old_limit;1782		limit_default!(old_limit, new_limit,1783			account_token_ownership_limit => ensure!(1784				new_limit <= MAX_TOKEN_OWNERSHIP,1785				<Error<T>>::CollectionLimitBoundsExceeded,1786			),1787			sponsored_data_size => ensure!(1788				new_limit <= CUSTOM_DATA_LIMIT,1789				<Error<T>>::CollectionLimitBoundsExceeded,1790			),17911792			sponsored_data_rate_limit => {},1793			token_limit => ensure!(1794				old_limit >= new_limit && new_limit > 0,1795				<Error<T>>::CollectionTokenLimitExceeded1796			),17971798			sponsor_transfer_timeout(match mode {1799				CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1800				CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1801				CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1802			}) => ensure!(1803				new_limit <= MAX_SPONSOR_TIMEOUT,1804				<Error<T>>::CollectionLimitBoundsExceeded,1805			),1806			sponsor_approve_timeout => {},1807			owner_can_transfer => ensure!(1808				!limits.owner_can_transfer_instaled() ||1809				old_limit || !new_limit,1810				<Error<T>>::OwnerPermissionsCantBeReverted,1811			),1812			owner_can_destroy => ensure!(1813				old_limit || !new_limit,1814				<Error<T>>::OwnerPermissionsCantBeReverted,1815			),1816			transfers_enabled => {},1817		);1818		Ok(new_limit)1819	}18201821	/// Update collection permissions.1822	pub fn update_permissions(1823		user: &T::CrossAccountId,1824		collection: &mut CollectionHandle<T>,1825		new_permission: CollectionPermissions,1826	) -> DispatchResult {1827		collection.check_is_internal()?;1828		collection.check_is_owner_or_admin(user)?;1829		collection.permissions = Self::clamp_permissions(1830			collection.mode.clone(),1831			&collection.permissions,1832			new_permission,1833		)?;18341835		Self::deposit_event(Event::<T>::CollectionPermissionSet(collection.id));1836		<PalletEvm<T>>::deposit_log(1837			erc::CollectionHelpersEvents::CollectionChanged {1838				collection_id: eth::collection_id_to_address(collection.id),1839			}1840			.to_log(T::ContractAddress::get()),1841		);18421843		collection.save()1844	}18451846	/// Merge set fields from `new_permission` to `old_permission`.1847	fn clamp_permissions(1848		_mode: CollectionMode,1849		old_permission: &CollectionPermissions,1850		mut new_permission: CollectionPermissions,1851	) -> Result<CollectionPermissions, DispatchError> {1852		limit_default_clone!(old_permission, new_permission,1853			access => {},1854			mint_mode => {},1855			nesting => { /* todo check for permissive, if only it gets out of benchmarks */ },1856		);1857		Ok(new_permission)1858	}18591860	/// Repair possibly broken properties of a collection.1861	pub fn repair_collection(collection_id: CollectionId) -> DispatchResult {1862		CollectionProperties::<T>::mutate(collection_id, |properties| {1863			properties.recompute_consumed_space();1864		});18651866		Ok(())1867	}1868}18691870/// Indicates unsupported methods by returning [Error::UnsupportedOperation].1871#[macro_export]1872macro_rules! unsupported {1873	($runtime:path) => {1874		Err($crate::Error::<$runtime>::UnsupportedOperation.into())1875	};1876}18771878/// Return weights for various worst-case operations.1879pub trait CommonWeightInfo<CrossAccountId> {1880	/// Weight of item creation.1881	fn create_item(data: &CreateItemData) -> Weight {1882		Self::create_multiple_items(from_ref(data))1883	}18841885	/// Weight of items creation.1886	fn create_multiple_items(amount: &[CreateItemData]) -> Weight;18871888	/// Weight of items creation.1889	fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;18901891	/// The weight of the burning item.1892	fn burn_item() -> Weight;18931894	/// Property setting weight.1895	///1896	/// * `amount`- The number of properties to set.1897	fn set_collection_properties(amount: u32) -> Weight;18981899	/// Collection property deletion weight.1900	///1901	/// * `amount`- The number of properties to set.1902	fn delete_collection_properties(amount: u32) -> Weight;19031904	/// Token property setting weight.1905	///1906	/// * `amount`- The number of properties to set.1907	fn set_token_properties(amount: u32) -> Weight;19081909	/// Token property deletion weight.1910	///1911	/// * `amount`- The number of properties to delete.1912	fn delete_token_properties(amount: u32) -> Weight;19131914	/// Token property permissions set weight.1915	///1916	/// * `amount`- The number of property permissions to set.1917	fn set_token_property_permissions(amount: u32) -> Weight;19181919	/// Transfer price of the token or its parts.1920	fn transfer() -> Weight;19211922	/// The price of setting the permission of the operation from another user.1923	fn approve() -> Weight;19241925	/// The price of setting the permission of the operation from another user for eth mirror.1926	fn approve_from() -> Weight;19271928	/// Transfer price from another user.1929	fn transfer_from() -> Weight;19301931	/// The price of burning a token from another user.1932	fn burn_from() -> Weight;19331934	/// Differs from burn_item in case of Fungible and Refungible, as it should burn1935	/// whole users's balance.1936	///1937	/// This method shouldn't be used directly, as it doesn't count breadth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead1938	fn burn_recursively_self_raw() -> Weight;19391940	/// Cost of iterating over `amount` children while burning, without counting child burning itself.1941	///1942	/// This method shouldn't be used directly, as it doesn't count depth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead1943	fn burn_recursively_breadth_raw(amount: u32) -> Weight;19441945	/// The price of recursive burning a token.1946	///1947	/// `max_selfs` - The maximum burning weight of the token itself.1948	/// `max_breadth` - The maximum number of nested tokens to burn.1949	fn burn_recursively(max_selfs: u32, max_breadth: u32) -> Weight {1950		Self::burn_recursively_self_raw()1951			.saturating_mul(max_selfs.max(1) as u64)1952			.saturating_add(Self::burn_recursively_breadth_raw(max_breadth))1953	}19541955	/// The price of retrieving token owner1956	fn token_owner() -> Weight;19571958	/// The price of setting approval for all1959	fn set_allowance_for_all() -> Weight;19601961	/// The price of repairing an item.1962	fn force_repair_item() -> Weight;1963}19641965/// Weight info extension trait for refungible pallet.1966pub trait RefungibleExtensionsWeightInfo {1967	/// Weight of token repartition.1968	fn repartition() -> Weight;1969}19701971/// Common collection operations.1972///1973/// It wraps methods in Fungible, Nonfungible and Refungible pallets1974/// and adds weight info.1975pub trait CommonCollectionOperations<T: Config> {1976	/// Create token.1977	///1978	/// * `sender` - The user who mint the token and pays for the transaction.1979	/// * `to` - The user who will own the token.1980	/// * `data` - Token data.1981	/// * `nesting_budget` - A budget that can be spent on nesting tokens.1982	fn create_item(1983		&self,1984		sender: T::CrossAccountId,1985		to: T::CrossAccountId,1986		data: CreateItemData,1987		nesting_budget: &dyn Budget,1988	) -> DispatchResultWithPostInfo;19891990	/// Create multiple tokens.1991	///1992	/// * `sender` - The user who mint the token and pays for the transaction.1993	/// * `to` - The user who will own the token.1994	/// * `data` - Token data.1995	/// * `nesting_budget` - A budget that can be spent on nesting tokens.1996	fn create_multiple_items(1997		&self,1998		sender: T::CrossAccountId,1999		to: T::CrossAccountId,2000		data: Vec<CreateItemData>,2001		nesting_budget: &dyn Budget,2002	) -> DispatchResultWithPostInfo;20032004	/// Create multiple tokens.2005	///2006	/// * `sender` - The user who mint the token and pays for the transaction.2007	/// * `to` - The user who will own the token.2008	/// * `data` - Token data.2009	/// * `nesting_budget` - A budget that can be spent on nesting tokens.2010	fn create_multiple_items_ex(2011		&self,2012		sender: T::CrossAccountId,2013		data: CreateItemExData<T::CrossAccountId>,2014		nesting_budget: &dyn Budget,2015	) -> DispatchResultWithPostInfo;20162017	/// Burn token.2018	///2019	/// * `sender` - The user who owns the token.2020	/// * `token` - Token id that will burned.2021	/// * `amount` - The number of parts of the token that will be burned.2022	fn burn_item(2023		&self,2024		sender: T::CrossAccountId,2025		token: TokenId,2026		amount: u128,2027	) -> DispatchResultWithPostInfo;20282029	/// Burn token and all nested tokens recursievly.2030	///2031	/// * `sender` - The user who owns the token.2032	/// * `token` - Token id that will burned.2033	/// * `self_budget` - The budget that can be spent on burning tokens.2034	/// * `breadth_budget` - The budget that can be spent on burning nested tokens.2035	fn burn_item_recursively(2036		&self,2037		sender: T::CrossAccountId,2038		token: TokenId,2039		self_budget: &dyn Budget,2040		breadth_budget: &dyn Budget,2041	) -> DispatchResultWithPostInfo;20422043	/// Set collection properties.2044	///2045	/// * `sender` - Must be either the owner of the collection or its admin.2046	/// * `properties` - Properties to be set.2047	fn set_collection_properties(2048		&self,2049		sender: T::CrossAccountId,2050		properties: Vec<Property>,2051	) -> DispatchResultWithPostInfo;20522053	/// Delete collection properties.2054	///2055	/// * `sender` - Must be either the owner of the collection or its admin.2056	/// * `properties` - The properties to be removed.2057	fn delete_collection_properties(2058		&self,2059		sender: &T::CrossAccountId,2060		property_keys: Vec<PropertyKey>,2061	) -> DispatchResultWithPostInfo;20622063	/// Set token properties.2064	///2065	/// The appropriate [`PropertyPermission`] for the token property2066	/// must be set with [`Self::set_token_property_permissions`].2067	///2068	/// * `sender` - Must be either the owner of the token or its admin.2069	/// * `token_id` - The token for which the properties are being set.2070	/// * `properties` - Properties to be set.2071	/// * `budget` - Budget for setting properties.2072	fn set_token_properties(2073		&self,2074		sender: T::CrossAccountId,2075		token_id: TokenId,2076		properties: Vec<Property>,2077		budget: &dyn Budget,2078	) -> DispatchResultWithPostInfo;20792080	/// Remove token properties.2081	///2082	/// The appropriate [`PropertyPermission`] for the token property2083	/// must be set with [`Self::set_token_property_permissions`].2084	///2085	/// * `sender` - Must be either the owner of the token or its admin.2086	/// * `token_id` - The token for which the properties are being remove.2087	/// * `property_keys` - Keys to remove corresponding properties.2088	/// * `budget` - Budget for removing properties.2089	fn delete_token_properties(2090		&self,2091		sender: T::CrossAccountId,2092		token_id: TokenId,2093		property_keys: Vec<PropertyKey>,2094		budget: &dyn Budget,2095	) -> DispatchResultWithPostInfo;20962097	/// Get token properties raw map.2098	///2099	/// * `token_id` - The token which properties are needed.2100	fn get_token_properties_raw(&self, token_id: TokenId) -> Option<TokenProperties>;21012102	/// Set token properties raw map.2103	///2104	/// * `token_id` - The token for which the properties are being set.2105	/// * `map` - The raw map containing the token's properties.2106	fn set_token_properties_raw(&self, token_id: TokenId, map: TokenProperties);21072108	/// Set token property permissions.2109	///2110	/// * `sender` - Must be either the owner of the token or its admin.2111	/// * `token_id` - The token for which the properties are being set.2112	/// * `property_permissions` - Property permissions to be set.2113	/// * `budget` - Budget for setting properties.2114	fn set_token_property_permissions(2115		&self,2116		sender: &T::CrossAccountId,2117		property_permissions: Vec<PropertyKeyPermission>,2118	) -> DispatchResultWithPostInfo;21192120	/// Transfer amount of token pieces.2121	///2122	/// * `sender` - Donor user.2123	/// * `to` - Recepient user.2124	/// * `token` - The token of which parts are being sent.2125	/// * `amount` - The number of parts of the token that will be transferred.2126	/// * `budget` - The maximum budget that can be spent on the transfer.2127	fn transfer(2128		&self,2129		sender: T::CrossAccountId,2130		to: T::CrossAccountId,2131		token: TokenId,2132		amount: u128,2133		budget: &dyn Budget,2134	) -> DispatchResultWithPostInfo;21352136	/// Grant access to another account to transfer parts of the token owned by the calling user via [Self::transfer_from].2137	///2138	/// * `sender` - The user who grants access to the token.2139	/// * `spender` - The user to whom the rights are granted.2140	/// * `token` - The token to which access is granted.2141	/// * `amount` - The amount of pieces that another user can dispose of.2142	fn approve(2143		&self,2144		sender: T::CrossAccountId,2145		spender: T::CrossAccountId,2146		token: TokenId,2147		amount: u128,2148	) -> DispatchResultWithPostInfo;21492150	/// Grant access to another account to transfer parts of the token owned by the calling user's eth mirror via [Self::transfer_from].2151	///2152	/// * `sender` - The user who grants access to the token.2153	/// * `from` - Spender's eth mirror.2154	/// * `to` - The user to whom the rights are granted.2155	/// * `token` - The token to which access is granted.2156	/// * `amount` - The amount of pieces that another user can dispose of.2157	fn approve_from(2158		&self,2159		sender: T::CrossAccountId,2160		from: T::CrossAccountId,2161		to: T::CrossAccountId,2162		token: TokenId,2163		amount: u128,2164	) -> DispatchResultWithPostInfo;21652166	/// Send parts of a token owned by another user.2167	///2168	/// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2169	///2170	/// * `sender` - The user who must have access to the token (see [`Self::approve`]).2171	/// * `from` - The user who owns the token.2172	/// * `to` - Recepient user.2173	/// * `token` - The token of which parts are being sent.2174	/// * `amount` - The number of parts of the token that will be transferred.2175	/// * `budget` - The maximum budget that can be spent on the transfer.2176	fn transfer_from(2177		&self,2178		sender: T::CrossAccountId,2179		from: T::CrossAccountId,2180		to: T::CrossAccountId,2181		token: TokenId,2182		amount: u128,2183		budget: &dyn Budget,2184	) -> DispatchResultWithPostInfo;21852186	/// Burn parts of a token owned by another user.2187	///2188	/// Before calling this method, you must grant rights to the calling user via [`Self::approve`].2189	///2190	/// * `sender` - The user who must have access to the token (see [`Self::approve`]).2191	/// * `from` - The user who owns the token.2192	/// * `token` - The token of which parts are being sent.2193	/// * `amount` - The number of parts of the token that will be transferred.2194	/// * `budget` - The maximum budget that can be spent on the burn.2195	fn burn_from(2196		&self,2197		sender: T::CrossAccountId,2198		from: T::CrossAccountId,2199		token: TokenId,2200		amount: u128,2201		budget: &dyn Budget,2202	) -> DispatchResultWithPostInfo;22032204	/// Check permission to nest token.2205	///2206	/// * `sender` - The user who initiated the check.2207	/// * `from` - The token that is checked for embedding.2208	/// * `under` - Token under which to check.2209	/// * `budget` - The maximum budget that can be spent on the check.2210	fn check_nesting(2211		&self,2212		sender: T::CrossAccountId,2213		from: (CollectionId, TokenId),2214		under: TokenId,2215		budget: &dyn Budget,2216	) -> DispatchResult;22172218	/// Nest one token into another.2219	///2220	/// * `under` - Token holder.2221	/// * `to_nest` - Nested token.2222	fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId));22232224	/// Unnest token.2225	///2226	/// * `under` - Token holder.2227	/// * `to_nest` - Token to unnest.2228	fn unnest(&self, under: TokenId, to_nest: (CollectionId, TokenId));22292230	/// Get all user tokens.2231	///2232	/// * `account` - Account for which you need to get tokens.2233	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;22342235	/// Get all the tokens in the collection.2236	fn collection_tokens(&self) -> Vec<TokenId>;22372238	/// Check if the token exists.2239	///2240	/// * `token` - Id token to check.2241	fn token_exists(&self, token: TokenId) -> bool;22422243	/// Get the id of the last minted token.2244	fn last_token_id(&self) -> TokenId;22452246	/// Get the owner of the token.2247	///2248	/// * `token` - The token for which you need to find out the owner.2249	fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError>;22502251	/// Checks if the `maybe_owner` is the indirect owner of the `token`.2252	///2253	/// * `token` - Id token to check.2254	/// * `maybe_owner` - The account to check.2255	/// * `nesting_budget` - A budget that can be spent on nesting tokens.2256	fn check_token_indirect_owner(2257		&self,2258		token: TokenId,2259		maybe_owner: &T::CrossAccountId,2260		nesting_budget: &dyn Budget,2261	) -> Result<bool, DispatchError>;22622263	/// Returns 10 tokens owners in no particular order.2264	///2265	/// * `token` - The token for which you need to find out the owners.2266	fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId>;22672268	/// Get the value of the token property by key.2269	///2270	/// * `token` - Token with the property to get.2271	/// * `key` - Property name.2272	fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue>;22732274	/// Get a set of token properties by key vector.2275	///2276	/// * `token` - Token with the property to get.2277	/// * `keys` - Vector of property keys. If this parameter is [None](sp_std::result::Result),2278	/// then all properties are returned.2279	fn token_properties(&self, token: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property>;22802281	/// Amount of unique collection tokens2282	fn total_supply(&self) -> u32;22832284	/// Amount of different tokens account has.2285	///2286	/// * `account` - The account for which need to get the balance.2287	fn account_balance(&self, account: T::CrossAccountId) -> u32;22882289	/// Amount of specific token account have.2290	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;22912292	/// Amount of token pieces2293	fn total_pieces(&self, token: TokenId) -> Option<u128>;22942295	/// Get the number of parts of the token that a trusted user can manage.2296	///2297	/// * `sender` - Trusted user.2298	/// * `spender` - Owner of the token.2299	/// * `token` - The token for which to get the value.2300	fn allowance(2301		&self,2302		sender: T::CrossAccountId,2303		spender: T::CrossAccountId,2304		token: TokenId,2305	) -> u128;23062307	/// Get extension for RFT collection.2308	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>>;23092310	/// The `operator` is allowed to transfer all tokens of the `owner` on their behalf.2311	/// * `owner` - Token owner2312	/// * `operator` - Operator2313	/// * `approve` - Should operator status be granted or revoked?2314	fn set_allowance_for_all(2315		&self,2316		owner: T::CrossAccountId,2317		operator: T::CrossAccountId,2318		approve: bool,2319	) -> DispatchResultWithPostInfo;23202321	/// Tells whether the given `owner` approves the `operator`.2322	fn allowance_for_all(&self, owner: T::CrossAccountId, operator: T::CrossAccountId) -> bool;23232324	/// Repairs a possibly broken item.2325	fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo;2326}23272328/// Extension for RFT collection.2329pub trait RefungibleExtensions<T>2330where2331	T: Config,2332{2333	/// Change the number of parts of the token.2334	///2335	/// When the value changes down, this function is equivalent to burning parts of the token.2336	///2337	/// * `sender` - The user calling the repartition operation. Must be the owner of the token.2338	/// * `token` - The token for which you want to change the number of parts.2339	/// * `amount` - The new value of the parts of the token.2340	fn repartition(2341		&self,2342		sender: &T::CrossAccountId,2343		token: TokenId,2344		amount: u128,2345	) -> DispatchResultWithPostInfo;2346}23472348/// Merge [`DispatchResult`] with [`Weight`] into [`DispatchResultWithPostInfo`].2349///2350/// Used for [`CommonCollectionOperations`] implementations and flexible enough to do so.2351pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {2352	let post_info = PostDispatchInfo {2353		actual_weight: Some(weight),2354		pays_fee: Pays::Yes,2355	};2356	match res {2357		Ok(()) => Ok(post_info),2358		Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),2359	}2360}23612362impl<T: Config> From<PropertiesError> for Error<T> {2363	fn from(error: PropertiesError) -> Self {2364		match error {2365			PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,2366			PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,2367			PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,2368			PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,2369			PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,2370		}2371	}2372}23732374/// A marker structure that enables the writer implementation2375/// to provide the interface to write properties to **newly created** tokens.2376pub struct NewTokenPropertyWriter;23772378/// A marker structure that enables the writer implementation2379/// to provide the interface to write properties to **already existing** tokens.2380pub struct ExistingTokenPropertyWriter;23812382/// The type-safe interface for writing properties (setting or deleting) to tokens.2383/// It has two distinct implementations for newly created tokens and existing ones.2384///2385/// This type utilizes the lazy evaluation to avoid repeating the computation2386/// of several performance-heavy or PoV-heavy tasks,2387/// such as checking the indirect ownership or reading the token property permissions.2388pub struct PropertyWriter<2389	'a,2390	T,2391	Handle,2392	WriterVariant,2393	FIsAdmin,2394	FPropertyPermissions,2395	FCheckTokenExist,2396	FGetProperties,2397> where2398	T: Config,2399	FIsAdmin: FnOnce() -> bool,2400	FPropertyPermissions: FnOnce() -> PropertiesPermissionMap,2401{2402	collection: &'a Handle,2403	is_collection_admin: LazyValue<bool, FIsAdmin>,2404	property_permissions: LazyValue<PropertiesPermissionMap, FPropertyPermissions>,2405	check_token_exist: FCheckTokenExist,2406	get_properties: FGetProperties,2407	_phantom: PhantomData<(T, WriterVariant)>,2408}24092410impl<'a, T, Handle, FIsAdmin, FPropertyPermissions, FCheckTokenExist, FGetProperties>2411	PropertyWriter<2412		'a,2413		T,2414		Handle,2415		NewTokenPropertyWriter,2416		FIsAdmin,2417		FPropertyPermissions,2418		FCheckTokenExist,2419		FGetProperties,2420	> where2421	T: Config,2422	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2423	FIsAdmin: FnOnce() -> bool,2424	FPropertyPermissions: FnOnce() -> PropertiesPermissionMap,2425	FCheckTokenExist: Copy + FnOnce(TokenId) -> bool,2426	FGetProperties: Copy + FnOnce(TokenId) -> TokenProperties,2427{2428	/// A function to write properties to a **newly created** token.2429	pub fn write_token_properties(2430		&mut self,2431		mint_target_is_sender: bool,2432		token_id: TokenId,2433		properties_updates: impl Iterator<Item = Property>,2434		log: evm_coder::ethereum::Log,2435	) -> DispatchResult {2436		self.internal_write_token_properties(2437			token_id,2438			properties_updates.map(|p| (p.key, Some(p.value))),2439			|_| Ok(mint_target_is_sender),2440			log,2441		)2442	}2443}24442445impl<'a, T, Handle, FIsAdmin, FPropertyPermissions, FCheckTokenExist, FGetProperties>2446	PropertyWriter<2447		'a,2448		T,2449		Handle,2450		ExistingTokenPropertyWriter,2451		FIsAdmin,2452		FPropertyPermissions,2453		FCheckTokenExist,2454		FGetProperties,2455	> where2456	T: Config,2457	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2458	FIsAdmin: FnOnce() -> bool,2459	FPropertyPermissions: FnOnce() -> PropertiesPermissionMap,2460	FCheckTokenExist: Copy + FnOnce(TokenId) -> bool,2461	FGetProperties: Copy + FnOnce(TokenId) -> TokenProperties,2462{2463	/// A function to write properties to an **already existing** token.2464	pub fn write_token_properties(2465		&mut self,2466		sender: &T::CrossAccountId,2467		token_id: TokenId,2468		properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,2469		nesting_budget: &dyn Budget,2470		log: evm_coder::ethereum::Log,2471	) -> DispatchResult {2472		self.internal_write_token_properties(2473			token_id,2474			properties_updates,2475			|collection| collection.check_token_indirect_owner(token_id, sender, nesting_budget),2476			log,2477		)2478	}2479}24802481impl<2482		'a,2483		T,2484		Handle,2485		WriterVariant,2486		FIsAdmin,2487		FPropertyPermissions,2488		FCheckTokenExist,2489		FGetProperties,2490	>2491	PropertyWriter<2492		'a,2493		T,2494		Handle,2495		WriterVariant,2496		FIsAdmin,2497		FPropertyPermissions,2498		FCheckTokenExist,2499		FGetProperties,2500	> where2501	T: Config,2502	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2503	FIsAdmin: FnOnce() -> bool,2504	FPropertyPermissions: FnOnce() -> PropertiesPermissionMap,2505	FCheckTokenExist: Copy + FnOnce(TokenId) -> bool,2506	FGetProperties: Copy + FnOnce(TokenId) -> TokenProperties,2507{2508	fn internal_write_token_properties<FCheckTokenOwner>(2509		&mut self,2510		token_id: TokenId,2511		properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,2512		check_token_owner: FCheckTokenOwner,2513		log: evm_coder::ethereum::Log,2514	) -> DispatchResult2515	where2516		FCheckTokenOwner: FnOnce(&Handle) -> Result<bool, DispatchError>,2517	{2518		let get_properties = self.get_properties;2519		let mut stored_properties = LazyValue::new(move || get_properties(token_id));25202521		let mut is_token_owner = LazyValue::new(|| check_token_owner(self.collection));25222523		let check_token_exist = self.check_token_exist;2524		let mut is_token_exist = LazyValue::new(move || check_token_exist(token_id));25252526		for (key, value) in properties_updates {2527			let permission = self2528				.property_permissions2529				.value()2530				.get(&key)2531				.cloned()2532				.unwrap_or_else(PropertyPermission::none);25332534			match permission {2535				PropertyPermission { mutable: false, .. }2536					if stored_properties.value().get(&key).is_some() =>2537				{2538					return Err(<Error<T>>::NoPermission.into());2539				}25402541				PropertyPermission {2542					collection_admin,2543					token_owner,2544					..2545				} => check_token_permissions::<T, _, _, _>(2546					collection_admin,2547					token_owner,2548					&mut self.is_collection_admin,2549					&mut is_token_owner,2550					&mut is_token_exist,2551				)?,2552			}25532554			match value {2555				Some(value) => {2556					stored_properties2557						.value_mut()2558						.try_set(key.clone(), value)2559						.map_err(<Error<T>>::from)?;25602561					<Pallet<T>>::deposit_event(Event::TokenPropertySet(2562						self.collection.id,2563						token_id,2564						key,2565					));2566				}2567				None => {2568					stored_properties2569						.value_mut()2570						.remove(&key)2571						.map_err(<Error<T>>::from)?;25722573					<Pallet<T>>::deposit_event(Event::TokenPropertyDeleted(2574						self.collection.id,2575						token_id,2576						key,2577					));2578				}2579			}2580		}25812582		let properties_changed = stored_properties.has_value();2583		if properties_changed {2584			<PalletEvm<T>>::deposit_log(log);25852586			self.collection2587				.set_token_properties_raw(token_id, stored_properties.into_inner());2588		}25892590		Ok(())2591	}2592}25932594/// Create a [`PropertyWriter`] for newly created tokens.2595pub fn property_writer_for_new_token<'a, T, Handle>(2596	collection: &'a Handle,2597	sender: &'a T::CrossAccountId,2598) -> PropertyWriter<2599	'a,2600	T,2601	Handle,2602	NewTokenPropertyWriter,2603	impl FnOnce() -> bool + 'a,2604	impl FnOnce() -> PropertiesPermissionMap + 'a,2605	impl Copy + FnOnce(TokenId) -> bool + 'a,2606	impl Copy + FnOnce(TokenId) -> TokenProperties + 'a,2607>2608where2609	T: Config,2610	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2611{2612	PropertyWriter {2613		collection,2614		is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2615		property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),2616		check_token_exist: |token_id| {2617			debug_assert!(collection.token_exists(token_id));2618			true2619		},2620		get_properties: |token_id| {2621			debug_assert!(collection.get_token_properties_raw(token_id).is_none());2622			TokenProperties::new()2623		},2624		_phantom: PhantomData,2625	}2626}26272628#[cfg(feature = "runtime-benchmarks")]2629/// Create a `PropertyWriter` with preloaded `is_collection_admin` and `property_permissions.2630/// Also:2631/// * it will return `true` for the token ownership check.2632/// * it will return empty stored properties without reading them from the storage.2633pub fn collection_info_loaded_property_writer<T, Handle>(2634	collection: &Handle,2635	is_collection_admin: bool,2636	property_permissions: PropertiesPermissionMap,2637) -> PropertyWriter<2638	T,2639	Handle,2640	NewTokenPropertyWriter,2641	impl FnOnce() -> bool,2642	impl FnOnce() -> PropertiesPermissionMap,2643	impl Copy + FnOnce(TokenId) -> bool,2644	impl Copy + FnOnce(TokenId) -> TokenProperties,2645>2646where2647	T: Config,2648	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2649{2650	PropertyWriter {2651		collection,2652		is_collection_admin: LazyValue::new(move || is_collection_admin),2653		property_permissions: LazyValue::new(move || property_permissions),2654		check_token_exist: |_token_id| true,2655		get_properties: |_token_id| TokenProperties::new(),2656		_phantom: PhantomData,2657	}2658}26592660/// Create a [`PropertyWriter`] for already existing tokens.2661pub fn property_writer_for_existing_token<'a, T, Handle>(2662	collection: &'a Handle,2663	sender: &'a T::CrossAccountId,2664) -> PropertyWriter<2665	'a,2666	T,2667	Handle,2668	ExistingTokenPropertyWriter,2669	impl FnOnce() -> bool + 'a,2670	impl FnOnce() -> PropertiesPermissionMap + 'a,2671	impl Copy + FnOnce(TokenId) -> bool + 'a,2672	impl Copy + FnOnce(TokenId) -> TokenProperties + 'a,2673>2674where2675	T: Config,2676	Handle: CommonCollectionOperations<T> + Deref<Target = CollectionHandle<T>>,2677{2678	PropertyWriter {2679		collection,2680		is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2681		property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),2682		check_token_exist: |token_id| collection.token_exists(token_id),2683		get_properties: |token_id| {2684			collection2685				.get_token_properties_raw(token_id)2686				.unwrap_or_default()2687		},2688		_phantom: PhantomData,2689	}2690}26912692/// Computes the weight delta for newly created tokens with properties.2693/// * `properties_nums` - The properties num of each created token.2694/// * `init_token_properties` - The function to obtain the weight from a token's properties num.2695pub fn init_token_properties_delta<T: Config, I: Fn(u32) -> Weight>(2696	properties_nums: impl Iterator<Item = u32>,2697	init_token_properties: I,2698) -> Weight {2699	let mut delta = properties_nums2700		.filter_map(|properties_num| {2701			if properties_num > 0 {2702				Some(init_token_properties(properties_num))2703			} else {2704				None2705			}2706		})2707		.fold(Weight::zero(), |a, b| a.saturating_add(b));27082709	// If at least once the `init_token_properties` was called,2710	// it means at least one newly created token has properties.2711	// Becuase of that, some common collection data also was loaded and we need to add this weight.2712	// However, these common data was loaded only once which is guaranteed by the `PropertyWriter`.2713	if !delta.is_zero() {2714		delta = delta.saturating_add(<SelfWeightOf<T>>::init_token_properties_common())2715	}27162717	delta2718}27192720#[cfg(any(feature = "tests", test))]2721#[allow(missing_docs)]2722pub mod tests {2723	use crate::{DispatchResult, DispatchError, LazyValue, Config};27242725	const fn to_bool(u: u8) -> bool {2726		u != 02727	}27282729	#[derive(Debug)]2730	pub struct TestCase {2731		pub collection_admin: bool,2732		pub is_collection_admin: bool,2733		pub token_owner: bool,2734		pub is_token_owner: bool,2735		pub no_permission: bool,2736	}27372738	impl TestCase {2739		const fn new(2740			collection_admin: u8,2741			is_collection_admin: u8,2742			token_owner: u8,2743			is_token_owner: u8,2744			no_permission: u8,2745		) -> Self {2746			Self {2747				collection_admin: to_bool(collection_admin),2748				is_collection_admin: to_bool(is_collection_admin),2749				token_owner: to_bool(token_owner),2750				is_token_owner: to_bool(is_token_owner),2751				no_permission: to_bool(no_permission),2752			}2753		}2754	}27552756	#[rustfmt::skip]2757	pub const TABLE: [TestCase; 16] = [2758		//                    ┌╴collection_admin2759		//                    │  ┌╴is_collection_admin2760		//                    │  │   ┌╴token_owner2761		//                    │  │   │  ┌╴is_token_ownership2762		//                    │  │   │  │   ┌╴no_permission2763		/*  0*/ TestCase::new(0, 0,  0, 0,  1),2764		/*  1*/ TestCase::new(0, 0,  0, 1,  1),2765		/*  2*/ TestCase::new(0, 0,  1, 0,  1),2766		/*  3*/ TestCase::new(0, 0,  1, 1,  0),2767		/*  4*/ TestCase::new(0, 1,  0, 0,  1),2768		/*  5*/ TestCase::new(0, 1,  0, 1,  1),2769		/*  6*/ TestCase::new(0, 1,  1, 0,  1),2770		/*  7*/ TestCase::new(0, 1,  1, 1,  0),2771		/*  8*/ TestCase::new(1, 0,  0, 0,  1),2772		/*  9*/ TestCase::new(1, 0,  0, 1,  1),2773		/* 10*/ TestCase::new(1, 0,  1, 0,  1),2774		/* 11*/ TestCase::new(1, 0,  1, 1,  0),2775		/* 12*/ TestCase::new(1, 1,  0, 0,  0),2776		/* 13*/ TestCase::new(1, 1,  0, 1,  0),2777		/* 14*/ TestCase::new(1, 1,  1, 0,  0),2778		/* 15*/ TestCase::new(1, 1,  1, 1,  0),2779	];27802781	pub fn check_token_permissions<T, FCA, FTO, FTE>(2782		collection_admin_permitted: bool,2783		token_owner_permitted: bool,2784		is_collection_admin: &mut LazyValue<bool, FCA>,2785		check_token_ownership: &mut LazyValue<Result<bool, DispatchError>, FTO>,2786		check_token_existence: &mut LazyValue<bool, FTE>,2787	) -> DispatchResult2788	where2789		T: Config,2790		FCA: FnOnce() -> bool,2791		FTO: FnOnce() -> Result<bool, DispatchError>,2792		FTE: FnOnce() -> bool,2793	{2794		crate::check_token_permissions::<T, FCA, FTO, FTE>(2795			collection_admin_permitted,2796			token_owner_permitted,2797			is_collection_admin,2798			check_token_ownership,2799			check_token_existence,2800		)2801	}2802}