referrerpolicy=no-referrer-when-downgrade

pallet_uniques/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Unique (Items) Module
19//!
20//! A simple, secure module for dealing with non-fungible items.
21//!
22//! ## Related Modules
23//!
24//! * [`System`](../frame_system/index.html)
25//! * [`Support`](../frame_support/index.html)
26
27#![recursion_limit = "256"]
28// Ensure we're `no_std` when compiling for Wasm.
29#![cfg_attr(not(feature = "std"), no_std)]
30
31#[cfg(feature = "runtime-benchmarks")]
32mod benchmarking;
33#[cfg(test)]
34pub mod mock;
35#[cfg(test)]
36mod tests;
37
38mod functions;
39mod impl_nonfungibles;
40mod types;
41
42pub mod asset_ops;
43pub mod migration;
44pub mod weights;
45
46extern crate alloc;
47
48use alloc::vec::Vec;
49use codec::{Decode, DecodeWithMemTracking, Encode};
50use frame_support::traits::{
51	tokens::Locker, BalanceStatus::Reserved, Currency, EnsureOriginWithArg, ReservableCurrency,
52};
53use frame_system::Config as SystemConfig;
54use sp_runtime::{
55	traits::{Saturating, StaticLookup, Zero},
56	ArithmeticError, Debug,
57};
58
59pub use pallet::*;
60pub use types::*;
61pub use weights::WeightInfo;
62
63/// The log target for this pallet.
64const LOG_TARGET: &str = "runtime::uniques";
65
66/// A type alias for the account ID type used in the dispatchable functions of this pallet.
67type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
68
69#[frame_support::pallet]
70pub mod pallet {
71	use super::*;
72	use frame_support::pallet_prelude::*;
73	use frame_system::pallet_prelude::*;
74
75	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
76
77	#[pallet::pallet]
78	#[pallet::storage_version(STORAGE_VERSION)]
79	pub struct Pallet<T, I = ()>(_);
80
81	#[cfg(feature = "runtime-benchmarks")]
82	pub trait BenchmarkHelper<CollectionId, ItemId> {
83		fn collection(i: u16) -> CollectionId;
84		fn item(i: u16) -> ItemId;
85	}
86	#[cfg(feature = "runtime-benchmarks")]
87	impl<CollectionId: From<u16>, ItemId: From<u16>> BenchmarkHelper<CollectionId, ItemId> for () {
88		fn collection(i: u16) -> CollectionId {
89			i.into()
90		}
91		fn item(i: u16) -> ItemId {
92			i.into()
93		}
94	}
95
96	#[pallet::config]
97	/// The module configuration trait.
98	pub trait Config<I: 'static = ()>: frame_system::Config {
99		/// The overarching event type.
100		#[allow(deprecated)]
101		type RuntimeEvent: From<Event<Self, I>>
102			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
103
104		/// Identifier for the collection of item.
105		type CollectionId: Member + Parameter + MaxEncodedLen;
106
107		/// The type used to identify a unique item within a collection.
108		type ItemId: Member + Parameter + MaxEncodedLen + Copy;
109
110		/// The currency mechanism, used for paying for reserves.
111		type Currency: ReservableCurrency<Self::AccountId>;
112
113		/// The origin which may forcibly create or destroy an item or otherwise alter privileged
114		/// attributes.
115		type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
116
117		/// Standard collection creation is only allowed if the origin attempting it and the
118		/// collection are in this set.
119		type CreateOrigin: EnsureOriginWithArg<
120			Self::RuntimeOrigin,
121			Self::CollectionId,
122			Success = Self::AccountId,
123		>;
124
125		/// Locker trait to enable Locking mechanism downstream.
126		type Locker: Locker<Self::CollectionId, Self::ItemId>;
127
128		/// The basic amount of funds that must be reserved for collection.
129		#[pallet::constant]
130		type CollectionDeposit: Get<DepositBalanceOf<Self, I>>;
131
132		/// The basic amount of funds that must be reserved for an item.
133		#[pallet::constant]
134		type ItemDeposit: Get<DepositBalanceOf<Self, I>>;
135
136		/// The basic amount of funds that must be reserved when adding metadata to your item.
137		#[pallet::constant]
138		type MetadataDepositBase: Get<DepositBalanceOf<Self, I>>;
139
140		/// The basic amount of funds that must be reserved when adding an attribute to an item.
141		#[pallet::constant]
142		type AttributeDepositBase: Get<DepositBalanceOf<Self, I>>;
143
144		/// The additional funds that must be reserved for the number of bytes store in metadata,
145		/// either "normal" metadata or attribute metadata.
146		#[pallet::constant]
147		type DepositPerByte: Get<DepositBalanceOf<Self, I>>;
148
149		/// The maximum length of data stored on-chain.
150		#[pallet::constant]
151		type StringLimit: Get<u32>;
152
153		/// The maximum length of an attribute key.
154		#[pallet::constant]
155		type KeyLimit: Get<u32>;
156
157		/// The maximum length of an attribute value.
158		#[pallet::constant]
159		type ValueLimit: Get<u32>;
160
161		#[cfg(feature = "runtime-benchmarks")]
162		/// A set of helper functions for benchmarking.
163		type Helper: BenchmarkHelper<Self::CollectionId, Self::ItemId>;
164
165		/// Weight information for extrinsics in this pallet.
166		type WeightInfo: WeightInfo;
167	}
168
169	#[pallet::storage]
170	#[pallet::storage_prefix = "Class"]
171	/// Details of a collection.
172	pub type Collection<T: Config<I>, I: 'static = ()> = StorageMap<
173		_,
174		Blake2_128Concat,
175		T::CollectionId,
176		CollectionDetails<T::AccountId, DepositBalanceOf<T, I>>,
177	>;
178
179	#[pallet::storage]
180	/// The collection, if any, of which an account is willing to take ownership.
181	pub type OwnershipAcceptance<T: Config<I>, I: 'static = ()> =
182		StorageMap<_, Blake2_128Concat, T::AccountId, T::CollectionId>;
183
184	#[pallet::storage]
185	/// The items held by any given account; set out this way so that items owned by a single
186	/// account can be enumerated.
187	pub type Account<T: Config<I>, I: 'static = ()> = StorageNMap<
188		_,
189		(
190			NMapKey<Blake2_128Concat, T::AccountId>, // owner
191			NMapKey<Blake2_128Concat, T::CollectionId>,
192			NMapKey<Blake2_128Concat, T::ItemId>,
193		),
194		(),
195		OptionQuery,
196	>;
197
198	#[pallet::storage]
199	#[pallet::storage_prefix = "ClassAccount"]
200	/// The collections owned by any given account; set out this way so that collections owned by
201	/// a single account can be enumerated.
202	pub type CollectionAccount<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
203		_,
204		Blake2_128Concat,
205		T::AccountId,
206		Blake2_128Concat,
207		T::CollectionId,
208		(),
209		OptionQuery,
210	>;
211
212	#[pallet::storage]
213	#[pallet::storage_prefix = "Asset"]
214	/// The items in existence and their ownership details.
215	pub type Item<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
216		_,
217		Blake2_128Concat,
218		T::CollectionId,
219		Blake2_128Concat,
220		T::ItemId,
221		ItemDetails<T::AccountId, DepositBalanceOf<T, I>>,
222		OptionQuery,
223	>;
224
225	#[pallet::storage]
226	#[pallet::storage_prefix = "ClassMetadataOf"]
227	/// Metadata of a collection.
228	pub type CollectionMetadataOf<T: Config<I>, I: 'static = ()> = StorageMap<
229		_,
230		Blake2_128Concat,
231		T::CollectionId,
232		CollectionMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
233		OptionQuery,
234	>;
235
236	#[pallet::storage]
237	#[pallet::storage_prefix = "InstanceMetadataOf"]
238	/// Metadata of an item.
239	pub type ItemMetadataOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
240		_,
241		Blake2_128Concat,
242		T::CollectionId,
243		Blake2_128Concat,
244		T::ItemId,
245		ItemMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
246		OptionQuery,
247	>;
248
249	#[pallet::storage]
250	/// Attributes of a collection.
251	pub type Attribute<T: Config<I>, I: 'static = ()> = StorageNMap<
252		_,
253		(
254			NMapKey<Blake2_128Concat, T::CollectionId>,
255			NMapKey<Blake2_128Concat, Option<T::ItemId>>,
256			NMapKey<Blake2_128Concat, BoundedVec<u8, T::KeyLimit>>,
257		),
258		(BoundedVec<u8, T::ValueLimit>, DepositBalanceOf<T, I>),
259		OptionQuery,
260	>;
261
262	#[pallet::storage]
263	/// Price of an asset instance.
264	pub type ItemPriceOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
265		_,
266		Blake2_128Concat,
267		T::CollectionId,
268		Blake2_128Concat,
269		T::ItemId,
270		(ItemPrice<T, I>, Option<T::AccountId>),
271		OptionQuery,
272	>;
273
274	#[pallet::storage]
275	/// Keeps track of the number of items a collection might have.
276	pub type CollectionMaxSupply<T: Config<I>, I: 'static = ()> =
277		StorageMap<_, Blake2_128Concat, T::CollectionId, u32, OptionQuery>;
278
279	#[pallet::event]
280	#[pallet::generate_deposit(pub(super) fn deposit_event)]
281	pub enum Event<T: Config<I>, I: 'static = ()> {
282		/// A `collection` was created.
283		Created { collection: T::CollectionId, creator: T::AccountId, owner: T::AccountId },
284		/// A `collection` was force-created.
285		ForceCreated { collection: T::CollectionId, owner: T::AccountId },
286		/// A `collection` was destroyed.
287		Destroyed { collection: T::CollectionId },
288		/// An `item` was issued.
289		Issued { collection: T::CollectionId, item: T::ItemId, owner: T::AccountId },
290		/// An `item` was transferred.
291		Transferred {
292			collection: T::CollectionId,
293			item: T::ItemId,
294			from: T::AccountId,
295			to: T::AccountId,
296		},
297		/// An `item` was destroyed.
298		Burned { collection: T::CollectionId, item: T::ItemId, owner: T::AccountId },
299		/// Some `item` was frozen.
300		Frozen { collection: T::CollectionId, item: T::ItemId },
301		/// Some `item` was thawed.
302		Thawed { collection: T::CollectionId, item: T::ItemId },
303		/// Some `collection` was frozen.
304		CollectionFrozen { collection: T::CollectionId },
305		/// Some `collection` was thawed.
306		CollectionThawed { collection: T::CollectionId },
307		/// The owner changed.
308		OwnerChanged { collection: T::CollectionId, new_owner: T::AccountId },
309		/// The management team changed.
310		TeamChanged {
311			collection: T::CollectionId,
312			issuer: T::AccountId,
313			admin: T::AccountId,
314			freezer: T::AccountId,
315		},
316		/// An `item` of a `collection` has been approved by the `owner` for transfer by
317		/// a `delegate`.
318		ApprovedTransfer {
319			collection: T::CollectionId,
320			item: T::ItemId,
321			owner: T::AccountId,
322			delegate: T::AccountId,
323		},
324		/// An approval for a `delegate` account to transfer the `item` of an item
325		/// `collection` was cancelled by its `owner`.
326		ApprovalCancelled {
327			collection: T::CollectionId,
328			item: T::ItemId,
329			owner: T::AccountId,
330			delegate: T::AccountId,
331		},
332		/// A `collection` has had its attributes changed by the `Force` origin.
333		ItemStatusChanged { collection: T::CollectionId },
334		/// New metadata has been set for a `collection`.
335		CollectionMetadataSet {
336			collection: T::CollectionId,
337			data: BoundedVec<u8, T::StringLimit>,
338			is_frozen: bool,
339		},
340		/// Metadata has been cleared for a `collection`.
341		CollectionMetadataCleared { collection: T::CollectionId },
342		/// New metadata has been set for an item.
343		MetadataSet {
344			collection: T::CollectionId,
345			item: T::ItemId,
346			data: BoundedVec<u8, T::StringLimit>,
347			is_frozen: bool,
348		},
349		/// Metadata has been cleared for an item.
350		MetadataCleared { collection: T::CollectionId, item: T::ItemId },
351		/// Metadata has been cleared for an item.
352		Redeposited { collection: T::CollectionId, successful_items: Vec<T::ItemId> },
353		/// New attribute metadata has been set for a `collection` or `item`.
354		AttributeSet {
355			collection: T::CollectionId,
356			maybe_item: Option<T::ItemId>,
357			key: BoundedVec<u8, T::KeyLimit>,
358			value: BoundedVec<u8, T::ValueLimit>,
359		},
360		/// Attribute metadata has been cleared for a `collection` or `item`.
361		AttributeCleared {
362			collection: T::CollectionId,
363			maybe_item: Option<T::ItemId>,
364			key: BoundedVec<u8, T::KeyLimit>,
365		},
366		/// Ownership acceptance has changed for an account.
367		OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option<T::CollectionId> },
368		/// Max supply has been set for a collection.
369		CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 },
370		/// The price was set for the instance.
371		ItemPriceSet {
372			collection: T::CollectionId,
373			item: T::ItemId,
374			price: ItemPrice<T, I>,
375			whitelisted_buyer: Option<T::AccountId>,
376		},
377		/// The price for the instance was removed.
378		ItemPriceRemoved { collection: T::CollectionId, item: T::ItemId },
379		/// An item was bought.
380		ItemBought {
381			collection: T::CollectionId,
382			item: T::ItemId,
383			price: ItemPrice<T, I>,
384			seller: T::AccountId,
385			buyer: T::AccountId,
386		},
387	}
388
389	#[pallet::error]
390	pub enum Error<T, I = ()> {
391		/// The signing account has no permission to do the operation.
392		NoPermission,
393		/// The given item ID is unknown.
394		UnknownCollection,
395		/// The item ID has already been used for an item.
396		AlreadyExists,
397		/// The owner turned out to be different to what was expected.
398		WrongOwner,
399		/// Invalid witness data given.
400		BadWitness,
401		/// The item ID is already taken.
402		InUse,
403		/// The item or collection is frozen.
404		Frozen,
405		/// The delegate turned out to be different to what was expected.
406		WrongDelegate,
407		/// There is no delegate approved.
408		NoDelegate,
409		/// No approval exists that would allow the transfer.
410		Unapproved,
411		/// The named owner has not signed ownership of the collection is acceptable.
412		Unaccepted,
413		/// The item is locked.
414		Locked,
415		/// All items have been minted.
416		MaxSupplyReached,
417		/// The max supply has already been set.
418		MaxSupplyAlreadySet,
419		/// The provided max supply is less to the amount of items a collection already has.
420		MaxSupplyTooSmall,
421		/// The given item ID is unknown.
422		UnknownItem,
423		/// Item is not for sale.
424		NotForSale,
425		/// The provided bid is too low.
426		BidTooLow,
427		/// No metadata is found.
428		NoMetadata,
429		/// Wrong metadata key/value bytes supplied.
430		WrongMetadata,
431		/// An attribute is not found.
432		AttributeNotFound,
433		/// Wrong attribute key/value bytes supplied.
434		WrongAttribute,
435	}
436
437	impl<T: Config<I>, I: 'static> Pallet<T, I> {
438		/// Get the owner of the item, if the item exists.
439		pub fn owner(collection: T::CollectionId, item: T::ItemId) -> Option<T::AccountId> {
440			Item::<T, I>::get(collection, item).map(|i| i.owner)
441		}
442
443		/// Get the owner of the item, if the item exists.
444		pub fn collection_owner(collection: T::CollectionId) -> Option<T::AccountId> {
445			Collection::<T, I>::get(collection).map(|i| i.owner)
446		}
447	}
448
449	#[pallet::call]
450	impl<T: Config<I>, I: 'static> Pallet<T, I> {
451		/// Issue a new collection of non-fungible items from a public origin.
452		///
453		/// This new collection has no items initially and its owner is the origin.
454		///
455		/// The origin must conform to the configured `CreateOrigin` and have sufficient funds free.
456		///
457		/// `ItemDeposit` funds of sender are reserved.
458		///
459		/// Parameters:
460		/// - `collection`: The identifier of the new collection. This must not be currently in use.
461		/// - `admin`: The admin of this collection. The admin is the initial address of each
462		/// member of the collection's admin team.
463		///
464		/// Emits `Created` event when successful.
465		///
466		/// Weight: `O(1)`
467		#[pallet::call_index(0)]
468		#[pallet::weight(T::WeightInfo::create())]
469		pub fn create(
470			origin: OriginFor<T>,
471			collection: T::CollectionId,
472			admin: AccountIdLookupOf<T>,
473		) -> DispatchResult {
474			let owner = T::CreateOrigin::ensure_origin(origin, &collection)?;
475			let admin = T::Lookup::lookup(admin)?;
476
477			Self::do_create_collection(
478				collection.clone(),
479				owner.clone(),
480				admin.clone(),
481				T::CollectionDeposit::get(),
482				false,
483				Event::Created { collection, creator: owner, owner: admin },
484			)
485		}
486
487		/// Issue a new collection of non-fungible items from a privileged origin.
488		///
489		/// This new collection has no items initially.
490		///
491		/// The origin must conform to `ForceOrigin`.
492		///
493		/// Unlike `create`, no funds are reserved.
494		///
495		/// - `collection`: The identifier of the new item. This must not be currently in use.
496		/// - `owner`: The owner of this collection of items. The owner has full superuser
497		///   permissions
498		/// over this item, but may later change and configure the permissions using
499		/// `transfer_ownership` and `set_team`.
500		///
501		/// Emits `ForceCreated` event when successful.
502		///
503		/// Weight: `O(1)`
504		#[pallet::call_index(1)]
505		#[pallet::weight(T::WeightInfo::force_create())]
506		pub fn force_create(
507			origin: OriginFor<T>,
508			collection: T::CollectionId,
509			owner: AccountIdLookupOf<T>,
510			free_holding: bool,
511		) -> DispatchResult {
512			T::ForceOrigin::ensure_origin(origin)?;
513			let owner = T::Lookup::lookup(owner)?;
514
515			Self::do_create_collection(
516				collection.clone(),
517				owner.clone(),
518				owner.clone(),
519				Zero::zero(),
520				free_holding,
521				Event::ForceCreated { collection, owner },
522			)
523		}
524
525		/// Destroy a collection of fungible items.
526		///
527		/// The origin must conform to `ForceOrigin` or must be `Signed` and the sender must be the
528		/// owner of the `collection`.
529		///
530		/// - `collection`: The identifier of the collection to be destroyed.
531		/// - `witness`: Information on the items minted in the collection. This must be
532		/// correct.
533		///
534		/// Emits `Destroyed` event when successful.
535		///
536		/// Weight: `O(n + m)` where:
537		/// - `n = witness.items`
538		/// - `m = witness.item_metadatas`
539		/// - `a = witness.attributes`
540		#[pallet::call_index(2)]
541		#[pallet::weight(T::WeightInfo::destroy(
542			witness.items,
543 			witness.item_metadatas,
544			witness.attributes,
545 		))]
546		pub fn destroy(
547			origin: OriginFor<T>,
548			collection: T::CollectionId,
549			witness: DestroyWitness,
550		) -> DispatchResultWithPostInfo {
551			let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
552				Ok(_) => None,
553				Err(origin) => Some(ensure_signed(origin)?),
554			};
555			let details = Self::do_destroy_collection(collection, witness, maybe_check_owner)?;
556
557			Ok(Some(T::WeightInfo::destroy(
558				details.items,
559				details.item_metadatas,
560				details.attributes,
561			))
562			.into())
563		}
564
565		/// Mint an item of a particular collection.
566		///
567		/// The origin must be Signed and the sender must be the Issuer of the `collection`.
568		///
569		/// - `collection`: The collection of the item to be minted.
570		/// - `item`: The item value of the item to be minted.
571		/// - `beneficiary`: The initial owner of the minted item.
572		///
573		/// Emits `Issued` event when successful.
574		///
575		/// Weight: `O(1)`
576		#[pallet::call_index(3)]
577		#[pallet::weight(T::WeightInfo::mint())]
578		pub fn mint(
579			origin: OriginFor<T>,
580			collection: T::CollectionId,
581			item: T::ItemId,
582			owner: AccountIdLookupOf<T>,
583		) -> DispatchResult {
584			let origin = ensure_signed(origin)?;
585			let owner = T::Lookup::lookup(owner)?;
586
587			Self::do_mint(collection, item, owner, |collection_details| {
588				ensure!(collection_details.issuer == origin, Error::<T, I>::NoPermission);
589				Ok(())
590			})
591		}
592
593		/// Destroy a single item.
594		///
595		/// Origin must be Signed and the signing account must be either:
596		/// - the Admin of the `collection`;
597		/// - the Owner of the `item`;
598		///
599		/// - `collection`: The collection of the item to be burned.
600		/// - `item`: The item of the item to be burned.
601		/// - `check_owner`: If `Some` then the operation will fail with `WrongOwner` unless the
602		///   item is owned by this value.
603		///
604		/// Emits `Burned` with the actual amount burned.
605		///
606		/// Weight: `O(1)`
607		/// Modes: `check_owner.is_some()`.
608		#[pallet::call_index(4)]
609		#[pallet::weight(T::WeightInfo::burn())]
610		pub fn burn(
611			origin: OriginFor<T>,
612			collection: T::CollectionId,
613			item: T::ItemId,
614			check_owner: Option<AccountIdLookupOf<T>>,
615		) -> DispatchResult {
616			let origin = ensure_signed(origin)?;
617			let check_owner = check_owner.map(T::Lookup::lookup).transpose()?;
618
619			Self::do_burn(collection, item, |collection_details, details| {
620				let is_permitted = collection_details.admin == origin || details.owner == origin;
621				ensure!(is_permitted, Error::<T, I>::NoPermission);
622				ensure!(
623					check_owner.map_or(true, |o| o == details.owner),
624					Error::<T, I>::WrongOwner
625				);
626				Ok(())
627			})
628		}
629
630		/// Move an item from the sender account to another.
631		///
632		/// This resets the approved account of the item.
633		///
634		/// Origin must be Signed and the signing account must be either:
635		/// - the Admin of the `collection`;
636		/// - the Owner of the `item`;
637		/// - the approved delegate for the `item` (in this case, the approval is reset).
638		///
639		/// Arguments:
640		/// - `collection`: The collection of the item to be transferred.
641		/// - `item`: The item of the item to be transferred.
642		/// - `dest`: The account to receive ownership of the item.
643		///
644		/// Emits `Transferred`.
645		///
646		/// Weight: `O(1)`
647		#[pallet::call_index(5)]
648		#[pallet::weight(T::WeightInfo::transfer())]
649		pub fn transfer(
650			origin: OriginFor<T>,
651			collection: T::CollectionId,
652			item: T::ItemId,
653			dest: AccountIdLookupOf<T>,
654		) -> DispatchResult {
655			let origin = ensure_signed(origin)?;
656			let dest = T::Lookup::lookup(dest)?;
657
658			Self::do_transfer(collection, item, dest, |collection_details, details| {
659				if details.owner != origin && collection_details.admin != origin {
660					let approved = details.approved.take().map_or(false, |i| i == origin);
661					ensure!(approved, Error::<T, I>::NoPermission);
662				}
663				Ok(())
664			})
665		}
666
667		/// Reevaluate the deposits on some items.
668		///
669		/// Origin must be Signed and the sender should be the Owner of the `collection`.
670		///
671		/// - `collection`: The collection to be frozen.
672		/// - `items`: The items of the collection whose deposits will be reevaluated.
673		///
674		/// NOTE: This exists as a best-effort function. Any items which are unknown or
675		/// in the case that the owner account does not have reservable funds to pay for a
676		/// deposit increase are ignored. Generally the owner isn't going to call this on items
677		/// whose existing deposit is less than the refreshed deposit as it would only cost them,
678		/// so it's of little consequence.
679		///
680		/// It will still return an error in the case that the collection is unknown of the signer
681		/// is not permitted to call it.
682		///
683		/// Weight: `O(items.len())`
684		#[pallet::call_index(6)]
685		#[pallet::weight(T::WeightInfo::redeposit(items.len() as u32))]
686		pub fn redeposit(
687			origin: OriginFor<T>,
688			collection: T::CollectionId,
689			items: Vec<T::ItemId>,
690		) -> DispatchResult {
691			let origin = ensure_signed(origin)?;
692
693			let mut collection_details =
694				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
695			ensure!(collection_details.owner == origin, Error::<T, I>::NoPermission);
696			let deposit = match collection_details.free_holding {
697				true => Zero::zero(),
698				false => T::ItemDeposit::get(),
699			};
700
701			let mut successful = Vec::with_capacity(items.len());
702			for item in items.into_iter() {
703				let mut details = match Item::<T, I>::get(&collection, &item) {
704					Some(x) => x,
705					None => continue,
706				};
707				let old = details.deposit;
708				if old > deposit {
709					T::Currency::unreserve(&collection_details.owner, old - deposit);
710				} else if deposit > old {
711					if T::Currency::reserve(&collection_details.owner, deposit - old).is_err() {
712						// NOTE: No alterations made to collection_details in this iteration so far,
713						// so this is OK to do.
714						continue;
715					}
716				} else {
717					continue;
718				}
719				collection_details.total_deposit.saturating_accrue(deposit);
720				collection_details.total_deposit.saturating_reduce(old);
721				details.deposit = deposit;
722				Item::<T, I>::insert(&collection, &item, &details);
723				successful.push(item);
724			}
725			Collection::<T, I>::insert(&collection, &collection_details);
726
727			Self::deposit_event(Event::<T, I>::Redeposited {
728				collection,
729				successful_items: successful,
730			});
731
732			Ok(())
733		}
734
735		/// Disallow further unprivileged transfer of an item.
736		///
737		/// Origin must be Signed and the sender should be the Freezer of the `collection`.
738		///
739		/// - `collection`: The collection of the item to be frozen.
740		/// - `item`: The item of the item to be frozen.
741		///
742		/// Emits `Frozen`.
743		///
744		/// Weight: `O(1)`
745		#[pallet::call_index(7)]
746		#[pallet::weight(T::WeightInfo::freeze())]
747		pub fn freeze(
748			origin: OriginFor<T>,
749			collection: T::CollectionId,
750			item: T::ItemId,
751		) -> DispatchResult {
752			let origin = ensure_signed(origin)?;
753
754			let mut details =
755				Item::<T, I>::get(&collection, &item).ok_or(Error::<T, I>::UnknownCollection)?;
756			let collection_details =
757				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
758			ensure!(collection_details.freezer == origin, Error::<T, I>::NoPermission);
759
760			details.is_frozen = true;
761			Item::<T, I>::insert(&collection, &item, &details);
762
763			Self::deposit_event(Event::<T, I>::Frozen { collection, item });
764			Ok(())
765		}
766
767		/// Re-allow unprivileged transfer of an item.
768		///
769		/// Origin must be Signed and the sender should be the Freezer of the `collection`.
770		///
771		/// - `collection`: The collection of the item to be thawed.
772		/// - `item`: The item of the item to be thawed.
773		///
774		/// Emits `Thawed`.
775		///
776		/// Weight: `O(1)`
777		#[pallet::call_index(8)]
778		#[pallet::weight(T::WeightInfo::thaw())]
779		pub fn thaw(
780			origin: OriginFor<T>,
781			collection: T::CollectionId,
782			item: T::ItemId,
783		) -> DispatchResult {
784			let origin = ensure_signed(origin)?;
785
786			let mut details =
787				Item::<T, I>::get(&collection, &item).ok_or(Error::<T, I>::UnknownCollection)?;
788			let collection_details =
789				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
790			ensure!(collection_details.admin == origin, Error::<T, I>::NoPermission);
791
792			details.is_frozen = false;
793			Item::<T, I>::insert(&collection, &item, &details);
794
795			Self::deposit_event(Event::<T, I>::Thawed { collection, item });
796			Ok(())
797		}
798
799		/// Disallow further unprivileged transfers for a whole collection.
800		///
801		/// Origin must be Signed and the sender should be the Freezer of the `collection`.
802		///
803		/// - `collection`: The collection to be frozen.
804		///
805		/// Emits `CollectionFrozen`.
806		///
807		/// Weight: `O(1)`
808		#[pallet::call_index(9)]
809		#[pallet::weight(T::WeightInfo::freeze_collection())]
810		pub fn freeze_collection(
811			origin: OriginFor<T>,
812			collection: T::CollectionId,
813		) -> DispatchResult {
814			let origin = ensure_signed(origin)?;
815
816			Collection::<T, I>::try_mutate(collection.clone(), |maybe_details| {
817				let details = maybe_details.as_mut().ok_or(Error::<T, I>::UnknownCollection)?;
818				ensure!(origin == details.freezer, Error::<T, I>::NoPermission);
819
820				details.is_frozen = true;
821
822				Self::deposit_event(Event::<T, I>::CollectionFrozen { collection });
823				Ok(())
824			})
825		}
826
827		/// Re-allow unprivileged transfers for a whole collection.
828		///
829		/// Origin must be Signed and the sender should be the Admin of the `collection`.
830		///
831		/// - `collection`: The collection to be thawed.
832		///
833		/// Emits `CollectionThawed`.
834		///
835		/// Weight: `O(1)`
836		#[pallet::call_index(10)]
837		#[pallet::weight(T::WeightInfo::thaw_collection())]
838		pub fn thaw_collection(
839			origin: OriginFor<T>,
840			collection: T::CollectionId,
841		) -> DispatchResult {
842			let origin = ensure_signed(origin)?;
843
844			Collection::<T, I>::try_mutate(collection.clone(), |maybe_details| {
845				let details = maybe_details.as_mut().ok_or(Error::<T, I>::UnknownCollection)?;
846				ensure!(origin == details.admin, Error::<T, I>::NoPermission);
847
848				details.is_frozen = false;
849
850				Self::deposit_event(Event::<T, I>::CollectionThawed { collection });
851				Ok(())
852			})
853		}
854
855		/// Change the Owner of a collection.
856		///
857		/// Origin must be Signed and the sender should be the Owner of the `collection`.
858		///
859		/// - `collection`: The collection whose owner should be changed.
860		/// - `owner`: The new Owner of this collection. They must have called
861		///   `set_accept_ownership` with `collection` in order for this operation to succeed.
862		///
863		/// Emits `OwnerChanged`.
864		///
865		/// Weight: `O(1)`
866		#[pallet::call_index(11)]
867		#[pallet::weight(T::WeightInfo::transfer_ownership())]
868		pub fn transfer_ownership(
869			origin: OriginFor<T>,
870			collection: T::CollectionId,
871			new_owner: AccountIdLookupOf<T>,
872		) -> DispatchResult {
873			let origin = ensure_signed(origin)?;
874			let new_owner = T::Lookup::lookup(new_owner)?;
875
876			let acceptable_collection = OwnershipAcceptance::<T, I>::get(&new_owner);
877			ensure!(acceptable_collection.as_ref() == Some(&collection), Error::<T, I>::Unaccepted);
878
879			Collection::<T, I>::try_mutate(collection.clone(), |maybe_details| {
880				let details = maybe_details.as_mut().ok_or(Error::<T, I>::UnknownCollection)?;
881				ensure!(origin == details.owner, Error::<T, I>::NoPermission);
882				if details.owner == new_owner {
883					return Ok(());
884				}
885
886				// Move the deposit to the new owner.
887				T::Currency::repatriate_reserved(
888					&details.owner,
889					&new_owner,
890					details.total_deposit,
891					Reserved,
892				)?;
893
894				CollectionAccount::<T, I>::remove(&details.owner, &collection);
895				CollectionAccount::<T, I>::insert(&new_owner, &collection, ());
896
897				details.owner = new_owner.clone();
898				OwnershipAcceptance::<T, I>::remove(&new_owner);
899				frame_system::Pallet::<T>::dec_consumers(&new_owner);
900
901				Self::deposit_event(Event::OwnerChanged { collection, new_owner });
902				Ok(())
903			})
904		}
905
906		/// Change the Issuer, Admin and Freezer of a collection.
907		///
908		/// Origin must be Signed and the sender should be the Owner of the `collection`.
909		///
910		/// - `collection`: The collection whose team should be changed.
911		/// - `issuer`: The new Issuer of this collection.
912		/// - `admin`: The new Admin of this collection.
913		/// - `freezer`: The new Freezer of this collection.
914		///
915		/// Emits `TeamChanged`.
916		///
917		/// Weight: `O(1)`
918		#[pallet::call_index(12)]
919		#[pallet::weight(T::WeightInfo::set_team())]
920		pub fn set_team(
921			origin: OriginFor<T>,
922			collection: T::CollectionId,
923			issuer: AccountIdLookupOf<T>,
924			admin: AccountIdLookupOf<T>,
925			freezer: AccountIdLookupOf<T>,
926		) -> DispatchResult {
927			let origin = ensure_signed(origin)?;
928			let issuer = T::Lookup::lookup(issuer)?;
929			let admin = T::Lookup::lookup(admin)?;
930			let freezer = T::Lookup::lookup(freezer)?;
931
932			Collection::<T, I>::try_mutate(collection.clone(), |maybe_details| {
933				let details = maybe_details.as_mut().ok_or(Error::<T, I>::UnknownCollection)?;
934				ensure!(origin == details.owner, Error::<T, I>::NoPermission);
935
936				details.issuer = issuer.clone();
937				details.admin = admin.clone();
938				details.freezer = freezer.clone();
939
940				Self::deposit_event(Event::TeamChanged { collection, issuer, admin, freezer });
941				Ok(())
942			})
943		}
944
945		/// Approve an item to be transferred by a delegated third-party account.
946		///
947		/// The origin must conform to `ForceOrigin` or must be `Signed` and the sender must be
948		/// either the owner of the `item` or the admin of the collection.
949		///
950		/// - `collection`: The collection of the item to be approved for delegated transfer.
951		/// - `item`: The item of the item to be approved for delegated transfer.
952		/// - `delegate`: The account to delegate permission to transfer the item.
953		///
954		/// Important NOTE: The `approved` account gets reset after each transfer.
955		///
956		/// Emits `ApprovedTransfer` on success.
957		///
958		/// Weight: `O(1)`
959		#[pallet::call_index(13)]
960		#[pallet::weight(T::WeightInfo::approve_transfer())]
961		pub fn approve_transfer(
962			origin: OriginFor<T>,
963			collection: T::CollectionId,
964			item: T::ItemId,
965			delegate: AccountIdLookupOf<T>,
966		) -> DispatchResult {
967			let maybe_check: Option<T::AccountId> = T::ForceOrigin::try_origin(origin)
968				.map(|_| None)
969				.or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
970
971			let delegate = T::Lookup::lookup(delegate)?;
972
973			let collection_details =
974				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
975			let mut details =
976				Item::<T, I>::get(&collection, &item).ok_or(Error::<T, I>::UnknownCollection)?;
977
978			if let Some(check) = maybe_check {
979				let permitted = check == collection_details.admin || check == details.owner;
980				ensure!(permitted, Error::<T, I>::NoPermission);
981			}
982
983			details.approved = Some(delegate);
984			Item::<T, I>::insert(&collection, &item, &details);
985
986			let delegate = details.approved.expect("set as Some above; qed");
987			Self::deposit_event(Event::ApprovedTransfer {
988				collection,
989				item,
990				owner: details.owner,
991				delegate,
992			});
993
994			Ok(())
995		}
996
997		/// Cancel the prior approval for the transfer of an item by a delegate.
998		///
999		/// Origin must be either:
1000		/// - the `Force` origin;
1001		/// - `Signed` with the signer being the Admin of the `collection`;
1002		/// - `Signed` with the signer being the Owner of the `item`;
1003		///
1004		/// Arguments:
1005		/// - `collection`: The collection of the item of whose approval will be cancelled.
1006		/// - `item`: The item of the item of whose approval will be cancelled.
1007		/// - `maybe_check_delegate`: If `Some` will ensure that the given account is the one to
1008		///   which permission of transfer is delegated.
1009		///
1010		/// Emits `ApprovalCancelled` on success.
1011		///
1012		/// Weight: `O(1)`
1013		#[pallet::call_index(14)]
1014		#[pallet::weight(T::WeightInfo::cancel_approval())]
1015		pub fn cancel_approval(
1016			origin: OriginFor<T>,
1017			collection: T::CollectionId,
1018			item: T::ItemId,
1019			maybe_check_delegate: Option<AccountIdLookupOf<T>>,
1020		) -> DispatchResult {
1021			let maybe_check: Option<T::AccountId> = T::ForceOrigin::try_origin(origin)
1022				.map(|_| None)
1023				.or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1024
1025			let collection_details =
1026				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1027			let mut details =
1028				Item::<T, I>::get(&collection, &item).ok_or(Error::<T, I>::UnknownCollection)?;
1029			if let Some(check) = maybe_check {
1030				let permitted = check == collection_details.admin || check == details.owner;
1031				ensure!(permitted, Error::<T, I>::NoPermission);
1032			}
1033			let maybe_check_delegate = maybe_check_delegate.map(T::Lookup::lookup).transpose()?;
1034			let old = details.approved.take().ok_or(Error::<T, I>::NoDelegate)?;
1035			if let Some(check_delegate) = maybe_check_delegate {
1036				ensure!(check_delegate == old, Error::<T, I>::WrongDelegate);
1037			}
1038
1039			Item::<T, I>::insert(&collection, &item, &details);
1040			Self::deposit_event(Event::ApprovalCancelled {
1041				collection,
1042				item,
1043				owner: details.owner,
1044				delegate: old,
1045			});
1046
1047			Ok(())
1048		}
1049
1050		/// Alter the attributes of a given item.
1051		///
1052		/// Origin must be `ForceOrigin`.
1053		///
1054		/// - `collection`: The identifier of the item.
1055		/// - `owner`: The new Owner of this item.
1056		/// - `issuer`: The new Issuer of this item.
1057		/// - `admin`: The new Admin of this item.
1058		/// - `freezer`: The new Freezer of this item.
1059		/// - `free_holding`: Whether a deposit is taken for holding an item of this collection.
1060		/// - `is_frozen`: Whether this collection is frozen except for permissioned/admin
1061		/// instructions.
1062		///
1063		/// Emits `ItemStatusChanged` with the identity of the item.
1064		///
1065		/// Weight: `O(1)`
1066		#[pallet::call_index(15)]
1067		#[pallet::weight(T::WeightInfo::force_item_status())]
1068		pub fn force_item_status(
1069			origin: OriginFor<T>,
1070			collection: T::CollectionId,
1071			owner: AccountIdLookupOf<T>,
1072			issuer: AccountIdLookupOf<T>,
1073			admin: AccountIdLookupOf<T>,
1074			freezer: AccountIdLookupOf<T>,
1075			free_holding: bool,
1076			is_frozen: bool,
1077		) -> DispatchResult {
1078			T::ForceOrigin::ensure_origin(origin)?;
1079
1080			Collection::<T, I>::try_mutate(collection.clone(), |maybe_item| {
1081				let mut item = maybe_item.take().ok_or(Error::<T, I>::UnknownCollection)?;
1082				let old_owner = item.owner;
1083				let new_owner = T::Lookup::lookup(owner)?;
1084				item.owner = new_owner.clone();
1085				item.issuer = T::Lookup::lookup(issuer)?;
1086				item.admin = T::Lookup::lookup(admin)?;
1087				item.freezer = T::Lookup::lookup(freezer)?;
1088				item.free_holding = free_holding;
1089				item.is_frozen = is_frozen;
1090				*maybe_item = Some(item);
1091				CollectionAccount::<T, I>::remove(&old_owner, &collection);
1092				CollectionAccount::<T, I>::insert(&new_owner, &collection, ());
1093
1094				Self::deposit_event(Event::ItemStatusChanged { collection });
1095				Ok(())
1096			})
1097		}
1098
1099		/// Set an attribute for a collection or item.
1100		///
1101		/// Origin must be either `ForceOrigin` or Signed and the sender should be the Owner of the
1102		/// `collection`.
1103		///
1104		/// If the origin is Signed, then funds of signer are reserved according to the formula:
1105		/// `MetadataDepositBase + DepositPerByte * (key.len + value.len)` taking into
1106		/// account any already reserved funds.
1107		///
1108		/// - `collection`: The identifier of the collection whose item's metadata to set.
1109		/// - `maybe_item`: The identifier of the item whose metadata to set.
1110		/// - `key`: The key of the attribute.
1111		/// - `value`: The value to which to set the attribute.
1112		///
1113		/// Emits `AttributeSet`.
1114		///
1115		/// Weight: `O(1)`
1116		#[pallet::call_index(16)]
1117		#[pallet::weight(T::WeightInfo::set_attribute())]
1118		pub fn set_attribute(
1119			origin: OriginFor<T>,
1120			collection: T::CollectionId,
1121			maybe_item: Option<T::ItemId>,
1122			key: BoundedVec<u8, T::KeyLimit>,
1123			value: BoundedVec<u8, T::ValueLimit>,
1124		) -> DispatchResult {
1125			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1126				.map(|_| None)
1127				.or_else(|origin| ensure_signed(origin).map(Some))?;
1128
1129			let mut collection_details =
1130				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1131			if let Some(check_owner) = &maybe_check_owner {
1132				ensure!(check_owner == &collection_details.owner, Error::<T, I>::NoPermission);
1133			}
1134			let maybe_is_frozen = match maybe_item {
1135				None => CollectionMetadataOf::<T, I>::get(collection.clone()).map(|v| v.is_frozen),
1136				Some(item) => {
1137					ItemMetadataOf::<T, I>::get(collection.clone(), item).map(|v| v.is_frozen)
1138				},
1139			};
1140			ensure!(!maybe_is_frozen.unwrap_or(false), Error::<T, I>::Frozen);
1141
1142			let attribute = Attribute::<T, I>::get((collection.clone(), maybe_item, &key));
1143			if attribute.is_none() {
1144				collection_details.attributes.saturating_inc();
1145			}
1146			let old_deposit = attribute.map_or(Zero::zero(), |m| m.1);
1147			collection_details.total_deposit.saturating_reduce(old_deposit);
1148			let mut deposit = Zero::zero();
1149			if !collection_details.free_holding && maybe_check_owner.is_some() {
1150				deposit = T::DepositPerByte::get()
1151					.saturating_mul(((key.len() + value.len()) as u32).into())
1152					.saturating_add(T::AttributeDepositBase::get());
1153			}
1154			collection_details.total_deposit.saturating_accrue(deposit);
1155			if deposit > old_deposit {
1156				T::Currency::reserve(&collection_details.owner, deposit - old_deposit)?;
1157			} else if deposit < old_deposit {
1158				T::Currency::unreserve(&collection_details.owner, old_deposit - deposit);
1159			}
1160
1161			Attribute::<T, I>::insert((&collection, maybe_item, &key), (&value, deposit));
1162			Collection::<T, I>::insert(collection.clone(), &collection_details);
1163			Self::deposit_event(Event::AttributeSet { collection, maybe_item, key, value });
1164			Ok(())
1165		}
1166
1167		/// Clear an attribute for a collection or item.
1168		///
1169		/// Origin must be either `ForceOrigin` or Signed and the sender should be the Owner of the
1170		/// `collection`.
1171		///
1172		/// Any deposit is freed for the collection's owner.
1173		///
1174		/// - `collection`: The identifier of the collection whose item's metadata to clear.
1175		/// - `maybe_item`: The identifier of the item whose metadata to clear.
1176		/// - `key`: The key of the attribute.
1177		///
1178		/// Emits `AttributeCleared`.
1179		///
1180		/// Weight: `O(1)`
1181		#[pallet::call_index(17)]
1182		#[pallet::weight(T::WeightInfo::clear_attribute())]
1183		pub fn clear_attribute(
1184			origin: OriginFor<T>,
1185			collection: T::CollectionId,
1186			maybe_item: Option<T::ItemId>,
1187			key: BoundedVec<u8, T::KeyLimit>,
1188		) -> DispatchResult {
1189			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1190				.map(|_| None)
1191				.or_else(|origin| ensure_signed(origin).map(Some))?;
1192
1193			let mut collection_details =
1194				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1195			if let Some(check_owner) = &maybe_check_owner {
1196				ensure!(check_owner == &collection_details.owner, Error::<T, I>::NoPermission);
1197			}
1198			let maybe_is_frozen = match maybe_item {
1199				None => CollectionMetadataOf::<T, I>::get(collection.clone()).map(|v| v.is_frozen),
1200				Some(item) => {
1201					ItemMetadataOf::<T, I>::get(collection.clone(), item).map(|v| v.is_frozen)
1202				},
1203			};
1204			ensure!(!maybe_is_frozen.unwrap_or(false), Error::<T, I>::Frozen);
1205
1206			if let Some((_, deposit)) =
1207				Attribute::<T, I>::take((collection.clone(), maybe_item, &key))
1208			{
1209				collection_details.attributes.saturating_dec();
1210				collection_details.total_deposit.saturating_reduce(deposit);
1211				T::Currency::unreserve(&collection_details.owner, deposit);
1212				Collection::<T, I>::insert(collection.clone(), &collection_details);
1213				Self::deposit_event(Event::AttributeCleared { collection, maybe_item, key });
1214			}
1215			Ok(())
1216		}
1217
1218		/// Set the metadata for an item.
1219		///
1220		/// Origin must be either `ForceOrigin` or Signed and the sender should be the Owner of the
1221		/// `collection`.
1222		///
1223		/// If the origin is Signed, then funds of signer are reserved according to the formula:
1224		/// `MetadataDepositBase + DepositPerByte * data.len` taking into
1225		/// account any already reserved funds.
1226		///
1227		/// - `collection`: The identifier of the collection whose item's metadata to set.
1228		/// - `item`: The identifier of the item whose metadata to set.
1229		/// - `data`: The general information of this item. Limited in length by `StringLimit`.
1230		/// - `is_frozen`: Whether the metadata should be frozen against further changes.
1231		///
1232		/// Emits `MetadataSet`.
1233		///
1234		/// Weight: `O(1)`
1235		#[pallet::call_index(18)]
1236		#[pallet::weight(T::WeightInfo::set_metadata())]
1237		pub fn set_metadata(
1238			origin: OriginFor<T>,
1239			collection: T::CollectionId,
1240			item: T::ItemId,
1241			data: BoundedVec<u8, T::StringLimit>,
1242			is_frozen: bool,
1243		) -> DispatchResult {
1244			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1245				.map(|_| None)
1246				.or_else(|origin| ensure_signed(origin).map(Some))?;
1247
1248			let mut collection_details =
1249				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1250
1251			if let Some(check_owner) = &maybe_check_owner {
1252				ensure!(check_owner == &collection_details.owner, Error::<T, I>::NoPermission);
1253			}
1254
1255			ItemMetadataOf::<T, I>::try_mutate_exists(collection.clone(), item, |metadata| {
1256				let was_frozen = metadata.as_ref().map_or(false, |m| m.is_frozen);
1257				ensure!(maybe_check_owner.is_none() || !was_frozen, Error::<T, I>::Frozen);
1258
1259				if metadata.is_none() {
1260					collection_details.item_metadatas.saturating_inc();
1261				}
1262				let old_deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
1263				collection_details.total_deposit.saturating_reduce(old_deposit);
1264				let mut deposit = Zero::zero();
1265				if !collection_details.free_holding && maybe_check_owner.is_some() {
1266					deposit = T::DepositPerByte::get()
1267						.saturating_mul(((data.len()) as u32).into())
1268						.saturating_add(T::MetadataDepositBase::get());
1269				}
1270				if deposit > old_deposit {
1271					T::Currency::reserve(&collection_details.owner, deposit - old_deposit)?;
1272				} else if deposit < old_deposit {
1273					T::Currency::unreserve(&collection_details.owner, old_deposit - deposit);
1274				}
1275				collection_details.total_deposit.saturating_accrue(deposit);
1276
1277				*metadata = Some(ItemMetadata { deposit, data: data.clone(), is_frozen });
1278
1279				Collection::<T, I>::insert(&collection, &collection_details);
1280				Self::deposit_event(Event::MetadataSet { collection, item, data, is_frozen });
1281				Ok(())
1282			})
1283		}
1284
1285		/// Clear the metadata for an item.
1286		///
1287		/// Origin must be either `ForceOrigin` or Signed and the sender should be the Owner of the
1288		/// `item`.
1289		///
1290		/// Any deposit is freed for the collection's owner.
1291		///
1292		/// - `collection`: The identifier of the collection whose item's metadata to clear.
1293		/// - `item`: The identifier of the item whose metadata to clear.
1294		///
1295		/// Emits `MetadataCleared`.
1296		///
1297		/// Weight: `O(1)`
1298		#[pallet::call_index(19)]
1299		#[pallet::weight(T::WeightInfo::clear_metadata())]
1300		pub fn clear_metadata(
1301			origin: OriginFor<T>,
1302			collection: T::CollectionId,
1303			item: T::ItemId,
1304		) -> DispatchResult {
1305			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1306				.map(|_| None)
1307				.or_else(|origin| ensure_signed(origin).map(Some))?;
1308
1309			let mut collection_details =
1310				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1311			if let Some(check_owner) = &maybe_check_owner {
1312				ensure!(check_owner == &collection_details.owner, Error::<T, I>::NoPermission);
1313			}
1314
1315			ItemMetadataOf::<T, I>::try_mutate_exists(collection.clone(), item, |metadata| {
1316				let was_frozen = metadata.as_ref().map_or(false, |m| m.is_frozen);
1317				ensure!(maybe_check_owner.is_none() || !was_frozen, Error::<T, I>::Frozen);
1318
1319				if metadata.is_some() {
1320					collection_details.item_metadatas.saturating_dec();
1321				}
1322				let deposit = metadata.take().ok_or(Error::<T, I>::UnknownCollection)?.deposit;
1323				T::Currency::unreserve(&collection_details.owner, deposit);
1324				collection_details.total_deposit.saturating_reduce(deposit);
1325
1326				Collection::<T, I>::insert(&collection, &collection_details);
1327				Self::deposit_event(Event::MetadataCleared { collection, item });
1328				Ok(())
1329			})
1330		}
1331
1332		/// Set the metadata for a collection.
1333		///
1334		/// Origin must be either `ForceOrigin` or `Signed` and the sender should be the Owner of
1335		/// the `collection`.
1336		///
1337		/// If the origin is `Signed`, then funds of signer are reserved according to the formula:
1338		/// `MetadataDepositBase + DepositPerByte * data.len` taking into
1339		/// account any already reserved funds.
1340		///
1341		/// - `collection`: The identifier of the item whose metadata to update.
1342		/// - `data`: The general information of this item. Limited in length by `StringLimit`.
1343		/// - `is_frozen`: Whether the metadata should be frozen against further changes.
1344		///
1345		/// Emits `CollectionMetadataSet`.
1346		///
1347		/// Weight: `O(1)`
1348		#[pallet::call_index(20)]
1349		#[pallet::weight(T::WeightInfo::set_collection_metadata())]
1350		pub fn set_collection_metadata(
1351			origin: OriginFor<T>,
1352			collection: T::CollectionId,
1353			data: BoundedVec<u8, T::StringLimit>,
1354			is_frozen: bool,
1355		) -> DispatchResult {
1356			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1357				.map(|_| None)
1358				.or_else(|origin| ensure_signed(origin).map(Some))?;
1359
1360			let mut details =
1361				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1362			if let Some(check_owner) = &maybe_check_owner {
1363				ensure!(check_owner == &details.owner, Error::<T, I>::NoPermission);
1364			}
1365
1366			CollectionMetadataOf::<T, I>::try_mutate_exists(collection.clone(), |metadata| {
1367				let was_frozen = metadata.as_ref().map_or(false, |m| m.is_frozen);
1368				ensure!(maybe_check_owner.is_none() || !was_frozen, Error::<T, I>::Frozen);
1369
1370				let old_deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
1371				details.total_deposit.saturating_reduce(old_deposit);
1372				let mut deposit = Zero::zero();
1373				if maybe_check_owner.is_some() && !details.free_holding {
1374					deposit = T::DepositPerByte::get()
1375						.saturating_mul(((data.len()) as u32).into())
1376						.saturating_add(T::MetadataDepositBase::get());
1377				}
1378				if deposit > old_deposit {
1379					T::Currency::reserve(&details.owner, deposit - old_deposit)?;
1380				} else if deposit < old_deposit {
1381					T::Currency::unreserve(&details.owner, old_deposit - deposit);
1382				}
1383				details.total_deposit.saturating_accrue(deposit);
1384
1385				Collection::<T, I>::insert(&collection, details);
1386
1387				*metadata = Some(CollectionMetadata { deposit, data: data.clone(), is_frozen });
1388
1389				Self::deposit_event(Event::CollectionMetadataSet { collection, data, is_frozen });
1390				Ok(())
1391			})
1392		}
1393
1394		/// Clear the metadata for a collection.
1395		///
1396		/// Origin must be either `ForceOrigin` or `Signed` and the sender should be the Owner of
1397		/// the `collection`.
1398		///
1399		/// Any deposit is freed for the collection's owner.
1400		///
1401		/// - `collection`: The identifier of the collection whose metadata to clear.
1402		///
1403		/// Emits `CollectionMetadataCleared`.
1404		///
1405		/// Weight: `O(1)`
1406		#[pallet::call_index(21)]
1407		#[pallet::weight(T::WeightInfo::clear_collection_metadata())]
1408		pub fn clear_collection_metadata(
1409			origin: OriginFor<T>,
1410			collection: T::CollectionId,
1411		) -> DispatchResult {
1412			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1413				.map(|_| None)
1414				.or_else(|origin| ensure_signed(origin).map(Some))?;
1415
1416			let mut details =
1417				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1418			if let Some(check_owner) = &maybe_check_owner {
1419				ensure!(check_owner == &details.owner, Error::<T, I>::NoPermission);
1420			}
1421
1422			CollectionMetadataOf::<T, I>::try_mutate_exists(collection.clone(), |metadata| {
1423				let was_frozen = metadata.as_ref().map_or(false, |m| m.is_frozen);
1424				ensure!(maybe_check_owner.is_none() || !was_frozen, Error::<T, I>::Frozen);
1425
1426				let deposit = metadata.take().ok_or(Error::<T, I>::UnknownCollection)?.deposit;
1427				T::Currency::unreserve(&details.owner, deposit);
1428				details.total_deposit.saturating_reduce(deposit);
1429				Collection::<T, I>::insert(&collection, details);
1430				Self::deposit_event(Event::CollectionMetadataCleared { collection });
1431				Ok(())
1432			})
1433		}
1434
1435		/// Set (or reset) the acceptance of ownership for a particular account.
1436		///
1437		/// Origin must be `Signed` and if `maybe_collection` is `Some`, then the signer must have a
1438		/// provider reference.
1439		///
1440		/// - `maybe_collection`: The identifier of the collection whose ownership the signer is
1441		///   willing to accept, or if `None`, an indication that the signer is willing to accept no
1442		///   ownership transferal.
1443		///
1444		/// Emits `OwnershipAcceptanceChanged`.
1445		#[pallet::call_index(22)]
1446		#[pallet::weight(T::WeightInfo::set_accept_ownership())]
1447		pub fn set_accept_ownership(
1448			origin: OriginFor<T>,
1449			maybe_collection: Option<T::CollectionId>,
1450		) -> DispatchResult {
1451			let who = ensure_signed(origin)?;
1452			let exists = OwnershipAcceptance::<T, I>::contains_key(&who);
1453			match (exists, maybe_collection.is_some()) {
1454				(false, true) => {
1455					frame_system::Pallet::<T>::inc_consumers(&who)?;
1456				},
1457				(true, false) => {
1458					frame_system::Pallet::<T>::dec_consumers(&who);
1459				},
1460				_ => {},
1461			}
1462			if let Some(collection) = maybe_collection.as_ref() {
1463				OwnershipAcceptance::<T, I>::insert(&who, collection);
1464			} else {
1465				OwnershipAcceptance::<T, I>::remove(&who);
1466			}
1467			Self::deposit_event(Event::OwnershipAcceptanceChanged { who, maybe_collection });
1468			Ok(())
1469		}
1470
1471		/// Set the maximum amount of items a collection could have.
1472		///
1473		/// Origin must be either `ForceOrigin` or `Signed` and the sender should be the Owner of
1474		/// the `collection`.
1475		///
1476		/// Note: This function can only succeed once per collection.
1477		///
1478		/// - `collection`: The identifier of the collection to change.
1479		/// - `max_supply`: The maximum amount of items a collection could have.
1480		///
1481		/// Emits `CollectionMaxSupplySet` event when successful.
1482		#[pallet::call_index(23)]
1483		#[pallet::weight(T::WeightInfo::set_collection_max_supply())]
1484		pub fn set_collection_max_supply(
1485			origin: OriginFor<T>,
1486			collection: T::CollectionId,
1487			max_supply: u32,
1488		) -> DispatchResult {
1489			let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1490				.map(|_| None)
1491				.or_else(|origin| ensure_signed(origin).map(Some))?;
1492
1493			ensure!(
1494				!CollectionMaxSupply::<T, I>::contains_key(&collection),
1495				Error::<T, I>::MaxSupplyAlreadySet
1496			);
1497
1498			let details =
1499				Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1500			if let Some(check_owner) = &maybe_check_owner {
1501				ensure!(check_owner == &details.owner, Error::<T, I>::NoPermission);
1502			}
1503
1504			ensure!(details.items <= max_supply, Error::<T, I>::MaxSupplyTooSmall);
1505
1506			CollectionMaxSupply::<T, I>::insert(&collection, max_supply);
1507			Self::deposit_event(Event::CollectionMaxSupplySet { collection, max_supply });
1508			Ok(())
1509		}
1510
1511		/// Set (or reset) the price for an item.
1512		///
1513		/// Origin must be Signed and must be the owner of the asset `item`.
1514		///
1515		/// - `collection`: The collection of the item.
1516		/// - `item`: The item to set the price for.
1517		/// - `price`: The price for the item. Pass `None`, to reset the price.
1518		/// - `buyer`: Restricts the buy operation to a specific account.
1519		///
1520		/// Emits `ItemPriceSet` on success if the price is not `None`.
1521		/// Emits `ItemPriceRemoved` on success if the price is `None`.
1522		#[pallet::call_index(24)]
1523		#[pallet::weight(T::WeightInfo::set_price())]
1524		pub fn set_price(
1525			origin: OriginFor<T>,
1526			collection: T::CollectionId,
1527			item: T::ItemId,
1528			price: Option<ItemPrice<T, I>>,
1529			whitelisted_buyer: Option<AccountIdLookupOf<T>>,
1530		) -> DispatchResult {
1531			let origin = ensure_signed(origin)?;
1532			let whitelisted_buyer = whitelisted_buyer.map(T::Lookup::lookup).transpose()?;
1533			Self::do_set_price(collection, item, origin, price, whitelisted_buyer)
1534		}
1535
1536		/// Allows to buy an item if it's up for sale.
1537		///
1538		/// Origin must be Signed and must not be the owner of the `item`.
1539		///
1540		/// - `collection`: The collection of the item.
1541		/// - `item`: The item the sender wants to buy.
1542		/// - `bid_price`: The price the sender is willing to pay.
1543		///
1544		/// Emits `ItemBought` on success.
1545		#[pallet::call_index(25)]
1546		#[pallet::weight(T::WeightInfo::buy_item())]
1547		pub fn buy_item(
1548			origin: OriginFor<T>,
1549			collection: T::CollectionId,
1550			item: T::ItemId,
1551			bid_price: ItemPrice<T, I>,
1552		) -> DispatchResult {
1553			let origin = ensure_signed(origin)?;
1554			Self::do_buy_item(collection, item, origin, bid_price)
1555		}
1556	}
1557}