1#![recursion_limit = "256"]
28#![cfg_attr(not(feature = "std"), no_std)]
30
31#[cfg(feature = "runtime-benchmarks")]
32mod benchmarking;
33pub mod migration;
34#[cfg(test)]
35pub mod mock;
36#[cfg(test)]
37mod tests;
38
39mod common_functions;
40mod features;
45mod impl_nonfungibles;
46mod types;
47
48pub mod macros;
49pub mod weights;
50
51extern crate alloc;
52
53use alloc::{boxed::Box, vec, vec::Vec};
54use codec::{Decode, Encode};
55use frame_support::traits::{
56 tokens::Locker, BalanceStatus::Reserved, Currency, EnsureOriginWithArg, Incrementable,
57 ReservableCurrency,
58};
59use frame_system::Config as SystemConfig;
60use sp_runtime::{
61 traits::{BlockNumberProvider, IdentifyAccount, Saturating, StaticLookup, Verify, Zero},
62 Debug,
63};
64
65pub use pallet::*;
66pub use types::*;
67pub use weights::WeightInfo;
68
69pub const LOG_TARGET: &'static str = "runtime::nfts";
71
72type AccountIdLookupOf<T> = <<T as SystemConfig>::Lookup as StaticLookup>::Source;
74
75#[frame_support::pallet]
76pub mod pallet {
77 use super::*;
78 use frame_support::{pallet_prelude::*, traits::ExistenceRequirement};
79 use frame_system::{ensure_signed, pallet_prelude::OriginFor};
80
81 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
83
84 #[pallet::pallet]
85 #[pallet::storage_version(STORAGE_VERSION)]
86 pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
87
88 #[cfg(feature = "runtime-benchmarks")]
89 pub trait BenchmarkHelper<CollectionId, ItemId, Public, AccountId, Signature> {
90 fn collection(i: u16) -> CollectionId;
91 fn item(i: u16) -> ItemId;
92 fn signer() -> (Public, AccountId);
93 fn sign(signer: &Public, message: &[u8]) -> Signature;
94 }
95 #[cfg(feature = "runtime-benchmarks")]
96 impl<CollectionId, ItemId>
97 BenchmarkHelper<
98 CollectionId,
99 ItemId,
100 sp_runtime::MultiSigner,
101 sp_runtime::AccountId32,
102 sp_runtime::MultiSignature,
103 > for ()
104 where
105 CollectionId: From<u16>,
106 ItemId: From<u16>,
107 {
108 fn collection(i: u16) -> CollectionId {
109 i.into()
110 }
111 fn item(i: u16) -> ItemId {
112 i.into()
113 }
114 fn signer() -> (sp_runtime::MultiSigner, sp_runtime::AccountId32) {
115 let public = sp_io::crypto::sr25519_generate(0.into(), None);
116 let account = sp_runtime::MultiSigner::Sr25519(public).into_account();
117 (public.into(), account)
118 }
119 fn sign(signer: &sp_runtime::MultiSigner, message: &[u8]) -> sp_runtime::MultiSignature {
120 sp_runtime::MultiSignature::Sr25519(
121 sp_io::crypto::sr25519_sign(0.into(), &signer.clone().try_into().unwrap(), message)
122 .unwrap(),
123 )
124 }
125 }
126
127 #[pallet::config]
128 pub trait Config<I: 'static = ()>: frame_system::Config {
130 #[allow(deprecated)]
132 type RuntimeEvent: From<Event<Self, I>>
133 + IsType<<Self as frame_system::Config>::RuntimeEvent>;
134
135 type CollectionId: Member + Parameter + MaxEncodedLen + Copy + Incrementable;
145
146 type ItemId: Member + Parameter + MaxEncodedLen + Copy;
148
149 type Currency: ReservableCurrency<Self::AccountId>;
151
152 type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
155
156 type CreateOrigin: EnsureOriginWithArg<
159 Self::RuntimeOrigin,
160 Self::CollectionId,
161 Success = Self::AccountId,
162 >;
163
164 type Locker: Locker<Self::CollectionId, Self::ItemId>;
166
167 #[pallet::constant]
169 type CollectionDeposit: Get<DepositBalanceOf<Self, I>>;
170
171 #[pallet::constant]
173 type ItemDeposit: Get<DepositBalanceOf<Self, I>>;
174
175 #[pallet::constant]
177 type MetadataDepositBase: Get<DepositBalanceOf<Self, I>>;
178
179 #[pallet::constant]
181 type AttributeDepositBase: Get<DepositBalanceOf<Self, I>>;
182
183 #[pallet::constant]
186 type DepositPerByte: Get<DepositBalanceOf<Self, I>>;
187
188 #[pallet::constant]
190 type StringLimit: Get<u32>;
191
192 #[pallet::constant]
194 type KeyLimit: Get<u32>;
195
196 #[pallet::constant]
198 type ValueLimit: Get<u32>;
199
200 #[pallet::constant]
202 type ApprovalsLimit: Get<u32>;
203
204 #[pallet::constant]
206 type ItemAttributesApprovalsLimit: Get<u32>;
207
208 #[pallet::constant]
210 type MaxTips: Get<u32>;
211
212 #[pallet::constant]
214 type MaxDeadlineDuration: Get<BlockNumberFor<Self, I>>;
215
216 #[pallet::constant]
218 type MaxAttributesPerCall: Get<u32>;
219
220 #[pallet::constant]
222 type Features: Get<PalletFeatures>;
223
224 type OffchainSignature: Verify<Signer = Self::OffchainPublic> + Parameter;
228
229 type OffchainPublic: IdentifyAccount<AccountId = Self::AccountId>;
233
234 #[cfg(feature = "runtime-benchmarks")]
235 type Helper: BenchmarkHelper<
237 Self::CollectionId,
238 Self::ItemId,
239 Self::OffchainPublic,
240 Self::AccountId,
241 Self::OffchainSignature,
242 >;
243
244 type WeightInfo: WeightInfo;
246
247 type BlockNumberProvider: BlockNumberProvider;
249 }
250
251 #[pallet::storage]
253 pub type Collection<T: Config<I>, I: 'static = ()> = StorageMap<
254 _,
255 Blake2_128Concat,
256 T::CollectionId,
257 CollectionDetails<T::AccountId, DepositBalanceOf<T, I>>,
258 >;
259
260 #[pallet::storage]
262 pub type OwnershipAcceptance<T: Config<I>, I: 'static = ()> =
263 StorageMap<_, Blake2_128Concat, T::AccountId, T::CollectionId>;
264
265 #[pallet::storage]
268 pub type Account<T: Config<I>, I: 'static = ()> = StorageNMap<
269 _,
270 (
271 NMapKey<Blake2_128Concat, T::AccountId>, NMapKey<Blake2_128Concat, T::CollectionId>,
273 NMapKey<Blake2_128Concat, T::ItemId>,
274 ),
275 (),
276 OptionQuery,
277 >;
278
279 #[pallet::storage]
282 pub type CollectionAccount<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
283 _,
284 Blake2_128Concat,
285 T::AccountId,
286 Blake2_128Concat,
287 T::CollectionId,
288 (),
289 OptionQuery,
290 >;
291
292 #[pallet::storage]
294 pub type CollectionRoleOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
296 _,
297 Blake2_128Concat,
298 T::CollectionId,
299 Blake2_128Concat,
300 T::AccountId,
301 CollectionRoles,
302 OptionQuery,
303 >;
304
305 #[pallet::storage]
307 pub type Item<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
308 _,
309 Blake2_128Concat,
310 T::CollectionId,
311 Blake2_128Concat,
312 T::ItemId,
313 ItemDetails<T::AccountId, ItemDepositOf<T, I>, ApprovalsOf<T, I>>,
314 OptionQuery,
315 >;
316
317 #[pallet::storage]
319 pub type CollectionMetadataOf<T: Config<I>, I: 'static = ()> = StorageMap<
320 _,
321 Blake2_128Concat,
322 T::CollectionId,
323 CollectionMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
324 OptionQuery,
325 >;
326
327 #[pallet::storage]
329 pub type ItemMetadataOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
330 _,
331 Blake2_128Concat,
332 T::CollectionId,
333 Blake2_128Concat,
334 T::ItemId,
335 ItemMetadata<ItemMetadataDepositOf<T, I>, T::StringLimit>,
336 OptionQuery,
337 >;
338
339 #[pallet::storage]
341 pub type Attribute<T: Config<I>, I: 'static = ()> = StorageNMap<
342 _,
343 (
344 NMapKey<Blake2_128Concat, T::CollectionId>,
345 NMapKey<Blake2_128Concat, Option<T::ItemId>>,
346 NMapKey<Blake2_128Concat, AttributeNamespace<T::AccountId>>,
347 NMapKey<Blake2_128Concat, BoundedVec<u8, T::KeyLimit>>,
348 ),
349 (BoundedVec<u8, T::ValueLimit>, AttributeDepositOf<T, I>),
350 OptionQuery,
351 >;
352
353 #[pallet::storage]
355 pub type ItemPriceOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
356 _,
357 Blake2_128Concat,
358 T::CollectionId,
359 Blake2_128Concat,
360 T::ItemId,
361 (ItemPrice<T, I>, Option<T::AccountId>),
362 OptionQuery,
363 >;
364
365 #[pallet::storage]
367 pub type ItemAttributesApprovalsOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
368 _,
369 Blake2_128Concat,
370 T::CollectionId,
371 Blake2_128Concat,
372 T::ItemId,
373 ItemAttributesApprovals<T, I>,
374 ValueQuery,
375 >;
376
377 #[pallet::storage]
380 pub type NextCollectionId<T: Config<I>, I: 'static = ()> =
381 StorageValue<_, T::CollectionId, OptionQuery>;
382
383 #[pallet::storage]
385 pub type PendingSwapOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
386 _,
387 Blake2_128Concat,
388 T::CollectionId,
389 Blake2_128Concat,
390 T::ItemId,
391 PendingSwap<
392 T::CollectionId,
393 T::ItemId,
394 PriceWithDirection<ItemPrice<T, I>>,
395 BlockNumberFor<T, I>,
396 >,
397 OptionQuery,
398 >;
399
400 #[pallet::storage]
402 pub type CollectionConfigOf<T: Config<I>, I: 'static = ()> =
403 StorageMap<_, Blake2_128Concat, T::CollectionId, CollectionConfigFor<T, I>, OptionQuery>;
404
405 #[pallet::storage]
407 pub type ItemConfigOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
408 _,
409 Blake2_128Concat,
410 T::CollectionId,
411 Blake2_128Concat,
412 T::ItemId,
413 ItemConfig,
414 OptionQuery,
415 >;
416
417 #[pallet::event]
418 #[pallet::generate_deposit(pub(super) fn deposit_event)]
419 pub enum Event<T: Config<I>, I: 'static = ()> {
420 Created { collection: T::CollectionId, creator: T::AccountId, owner: T::AccountId },
422 ForceCreated { collection: T::CollectionId, owner: T::AccountId },
424 Destroyed { collection: T::CollectionId },
426 Issued { collection: T::CollectionId, item: T::ItemId, owner: T::AccountId },
428 Transferred {
430 collection: T::CollectionId,
431 item: T::ItemId,
432 from: T::AccountId,
433 to: T::AccountId,
434 },
435 Burned { collection: T::CollectionId, item: T::ItemId, owner: T::AccountId },
437 ItemTransferLocked { collection: T::CollectionId, item: T::ItemId },
439 ItemTransferUnlocked { collection: T::CollectionId, item: T::ItemId },
441 ItemPropertiesLocked {
443 collection: T::CollectionId,
444 item: T::ItemId,
445 lock_metadata: bool,
446 lock_attributes: bool,
447 },
448 CollectionLocked { collection: T::CollectionId },
450 OwnerChanged { collection: T::CollectionId, new_owner: T::AccountId },
452 TeamChanged {
454 collection: T::CollectionId,
455 issuer: Option<T::AccountId>,
456 admin: Option<T::AccountId>,
457 freezer: Option<T::AccountId>,
458 },
459 TransferApproved {
462 collection: T::CollectionId,
463 item: T::ItemId,
464 owner: T::AccountId,
465 delegate: T::AccountId,
466 deadline: Option<BlockNumberFor<T, I>>,
467 },
468 ApprovalCancelled {
471 collection: T::CollectionId,
472 item: T::ItemId,
473 owner: T::AccountId,
474 delegate: T::AccountId,
475 },
476 AllApprovalsCancelled { collection: T::CollectionId, item: T::ItemId, owner: T::AccountId },
478 CollectionConfigChanged { collection: T::CollectionId },
480 CollectionMetadataSet { collection: T::CollectionId, data: BoundedVec<u8, T::StringLimit> },
482 CollectionMetadataCleared { collection: T::CollectionId },
484 ItemMetadataSet {
486 collection: T::CollectionId,
487 item: T::ItemId,
488 data: BoundedVec<u8, T::StringLimit>,
489 },
490 ItemMetadataCleared { collection: T::CollectionId, item: T::ItemId },
492 Redeposited { collection: T::CollectionId, successful_items: Vec<T::ItemId> },
494 AttributeSet {
496 collection: T::CollectionId,
497 maybe_item: Option<T::ItemId>,
498 key: BoundedVec<u8, T::KeyLimit>,
499 value: BoundedVec<u8, T::ValueLimit>,
500 namespace: AttributeNamespace<T::AccountId>,
501 },
502 AttributeCleared {
504 collection: T::CollectionId,
505 maybe_item: Option<T::ItemId>,
506 key: BoundedVec<u8, T::KeyLimit>,
507 namespace: AttributeNamespace<T::AccountId>,
508 },
509 ItemAttributesApprovalAdded {
511 collection: T::CollectionId,
512 item: T::ItemId,
513 delegate: T::AccountId,
514 },
515 ItemAttributesApprovalRemoved {
517 collection: T::CollectionId,
518 item: T::ItemId,
519 delegate: T::AccountId,
520 },
521 OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option<T::CollectionId> },
523 CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 },
525 CollectionMintSettingsUpdated { collection: T::CollectionId },
527 NextCollectionIdIncremented { next_id: Option<T::CollectionId> },
529 ItemPriceSet {
531 collection: T::CollectionId,
532 item: T::ItemId,
533 price: ItemPrice<T, I>,
534 whitelisted_buyer: Option<T::AccountId>,
535 },
536 ItemPriceRemoved { collection: T::CollectionId, item: T::ItemId },
538 ItemBought {
540 collection: T::CollectionId,
541 item: T::ItemId,
542 price: ItemPrice<T, I>,
543 seller: T::AccountId,
544 buyer: T::AccountId,
545 },
546 TipSent {
548 collection: T::CollectionId,
549 item: T::ItemId,
550 sender: T::AccountId,
551 receiver: T::AccountId,
552 amount: DepositBalanceOf<T, I>,
553 },
554 SwapCreated {
556 offered_collection: T::CollectionId,
557 offered_item: T::ItemId,
558 desired_collection: T::CollectionId,
559 desired_item: Option<T::ItemId>,
560 price: Option<PriceWithDirection<ItemPrice<T, I>>>,
561 deadline: BlockNumberFor<T, I>,
562 },
563 SwapCancelled {
565 offered_collection: T::CollectionId,
566 offered_item: T::ItemId,
567 desired_collection: T::CollectionId,
568 desired_item: Option<T::ItemId>,
569 price: Option<PriceWithDirection<ItemPrice<T, I>>>,
570 deadline: BlockNumberFor<T, I>,
571 },
572 SwapClaimed {
574 sent_collection: T::CollectionId,
575 sent_item: T::ItemId,
576 sent_item_owner: T::AccountId,
577 received_collection: T::CollectionId,
578 received_item: T::ItemId,
579 received_item_owner: T::AccountId,
580 price: Option<PriceWithDirection<ItemPrice<T, I>>>,
581 deadline: BlockNumberFor<T, I>,
582 },
583 PreSignedAttributesSet {
585 collection: T::CollectionId,
586 item: T::ItemId,
587 namespace: AttributeNamespace<T::AccountId>,
588 },
589 PalletAttributeSet {
592 collection: T::CollectionId,
593 item: Option<T::ItemId>,
594 attribute: PalletAttributes<T::CollectionId>,
595 value: BoundedVec<u8, T::ValueLimit>,
596 },
597 }
598
599 #[pallet::error]
600 pub enum Error<T, I = ()> {
601 NoPermission,
603 UnknownCollection,
605 AlreadyExists,
607 ApprovalExpired,
609 WrongOwner,
611 BadWitness,
613 CollectionIdInUse,
615 ItemsNonTransferable,
617 NotDelegate,
619 WrongDelegate,
621 Unapproved,
623 Unaccepted,
625 ItemLocked,
627 LockedItemAttributes,
629 LockedCollectionAttributes,
631 LockedItemMetadata,
633 LockedCollectionMetadata,
635 MaxSupplyReached,
637 MaxSupplyLocked,
639 MaxSupplyTooSmall,
641 UnknownItem,
643 UnknownSwap,
645 MetadataNotFound,
647 AttributeNotFound,
649 NotForSale,
651 BidTooLow,
653 ReachedApprovalLimit,
655 DeadlineExpired,
657 WrongDuration,
659 MethodDisabled,
661 WrongSetting,
663 InconsistentItemConfig,
665 NoConfig,
667 RolesNotCleared,
669 MintNotStarted,
671 MintEnded,
673 AlreadyClaimed,
675 IncorrectData,
677 WrongOrigin,
679 WrongSignature,
681 IncorrectMetadata,
683 MaxAttributesLimitReached,
685 WrongNamespace,
687 CollectionNotEmpty,
689 WitnessRequired,
691 IncompleteDepositTransfer,
693 }
694
695 #[pallet::call]
696 impl<T: Config<I>, I: 'static> Pallet<T, I> {
697 #[pallet::call_index(0)]
713 #[pallet::weight(T::WeightInfo::create())]
714 pub fn create(
715 origin: OriginFor<T>,
716 admin: AccountIdLookupOf<T>,
717 config: CollectionConfigFor<T, I>,
718 ) -> DispatchResult {
719 let collection = NextCollectionId::<T, I>::get()
720 .or(T::CollectionId::initial_value())
721 .ok_or(Error::<T, I>::UnknownCollection)?;
722
723 let owner = T::CreateOrigin::ensure_origin(origin, &collection)?;
724 let admin = T::Lookup::lookup(admin)?;
725
726 ensure!(
728 !config.has_disabled_setting(CollectionSetting::DepositRequired),
729 Error::<T, I>::WrongSetting
730 );
731
732 Self::do_create_collection(
733 collection,
734 owner.clone(),
735 admin.clone(),
736 config,
737 T::CollectionDeposit::get(),
738 Event::Created { collection, creator: owner, owner: admin },
739 )?;
740
741 Self::set_next_collection_id(collection);
742 Ok(())
743 }
744
745 #[pallet::call_index(1)]
761 #[pallet::weight(T::WeightInfo::force_create())]
762 pub fn force_create(
763 origin: OriginFor<T>,
764 owner: AccountIdLookupOf<T>,
765 config: CollectionConfigFor<T, I>,
766 ) -> DispatchResult {
767 T::ForceOrigin::ensure_origin(origin)?;
768 let owner = T::Lookup::lookup(owner)?;
769
770 let collection = NextCollectionId::<T, I>::get()
771 .or(T::CollectionId::initial_value())
772 .ok_or(Error::<T, I>::UnknownCollection)?;
773
774 Self::do_create_collection(
775 collection,
776 owner.clone(),
777 owner.clone(),
778 config,
779 Zero::zero(),
780 Event::ForceCreated { collection, owner },
781 )?;
782
783 Self::set_next_collection_id(collection);
784 Ok(())
785 }
786
787 #[pallet::call_index(2)]
805 #[pallet::weight(T::WeightInfo::destroy(
806 witness.item_metadatas,
807 witness.item_configs,
808 witness.attributes,
809 ))]
810 pub fn destroy(
811 origin: OriginFor<T>,
812 collection: T::CollectionId,
813 witness: DestroyWitness,
814 ) -> DispatchResultWithPostInfo {
815 let maybe_check_owner = T::ForceOrigin::try_origin(origin)
816 .map(|_| None)
817 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
818 let details = Self::do_destroy_collection(collection, witness, maybe_check_owner)?;
819
820 Ok(Some(T::WeightInfo::destroy(
821 details.item_metadatas,
822 details.item_configs,
823 details.attributes,
824 ))
825 .into())
826 }
827
828 #[pallet::call_index(3)]
845 #[pallet::weight(T::WeightInfo::mint())]
846 pub fn mint(
847 origin: OriginFor<T>,
848 collection: T::CollectionId,
849 item: T::ItemId,
850 mint_to: AccountIdLookupOf<T>,
851 witness_data: Option<MintWitness<T::ItemId, DepositBalanceOf<T, I>>>,
852 ) -> DispatchResult {
853 let caller = ensure_signed(origin)?;
854 let mint_to = T::Lookup::lookup(mint_to)?;
855 let item_config =
856 ItemConfig { settings: Self::get_default_item_settings(&collection)? };
857
858 Self::do_mint(
859 collection,
860 item,
861 Some(caller.clone()),
862 mint_to.clone(),
863 item_config,
864 |collection_details, collection_config| {
865 let mint_settings = collection_config.mint_settings;
866 let now = T::BlockNumberProvider::current_block_number();
867
868 if let Some(start_block) = mint_settings.start_block {
869 ensure!(start_block <= now, Error::<T, I>::MintNotStarted);
870 }
871 if let Some(end_block) = mint_settings.end_block {
872 ensure!(end_block >= now, Error::<T, I>::MintEnded);
873 }
874
875 match mint_settings.mint_type {
876 MintType::Issuer => {
877 ensure!(
878 Self::has_role(&collection, &caller, CollectionRole::Issuer),
879 Error::<T, I>::NoPermission
880 );
881 },
882 MintType::HolderOf(collection_id) => {
883 let MintWitness { owned_item, .. } =
884 witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
885 let owned_item = owned_item.ok_or(Error::<T, I>::BadWitness)?;
886
887 let owns_item = Account::<T, I>::contains_key((
888 &caller,
889 &collection_id,
890 &owned_item,
891 ));
892 ensure!(owns_item, Error::<T, I>::BadWitness);
893
894 let pallet_attribute =
895 PalletAttributes::<T::CollectionId>::UsedToClaim(collection);
896
897 let key = (
898 &collection_id,
899 Some(owned_item),
900 AttributeNamespace::Pallet,
901 &Self::construct_attribute_key(pallet_attribute.encode())?,
902 );
903 let already_claimed = Attribute::<T, I>::contains_key(key.clone());
904 ensure!(!already_claimed, Error::<T, I>::AlreadyClaimed);
905
906 let attribute_value = Self::construct_attribute_value(vec![])?;
907 Attribute::<T, I>::insert(
908 key,
909 (
910 attribute_value.clone(),
911 AttributeDeposit { account: None, amount: Zero::zero() },
912 ),
913 );
914 Self::deposit_event(Event::PalletAttributeSet {
915 collection: collection_id,
916 item: Some(owned_item),
917 attribute: pallet_attribute,
918 value: attribute_value,
919 });
920 },
921 _ => {},
922 }
923
924 if let Some(price) = mint_settings.price {
925 let MintWitness { mint_price, .. } =
926 witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
927 let mint_price = mint_price.ok_or(Error::<T, I>::BadWitness)?;
928 ensure!(mint_price >= price, Error::<T, I>::BadWitness);
929 T::Currency::transfer(
930 &caller,
931 &collection_details.owner,
932 price,
933 ExistenceRequirement::KeepAlive,
934 )?;
935 }
936
937 Ok(())
938 },
939 )
940 }
941
942 #[pallet::call_index(4)]
956 #[pallet::weight(T::WeightInfo::force_mint())]
957 pub fn force_mint(
958 origin: OriginFor<T>,
959 collection: T::CollectionId,
960 item: T::ItemId,
961 mint_to: AccountIdLookupOf<T>,
962 item_config: ItemConfig,
963 ) -> DispatchResult {
964 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
965 .map(|_| None)
966 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
967 let mint_to = T::Lookup::lookup(mint_to)?;
968
969 if let Some(check_origin) = maybe_check_origin {
970 ensure!(
971 Self::has_role(&collection, &check_origin, CollectionRole::Issuer),
972 Error::<T, I>::NoPermission
973 );
974 }
975 Self::do_mint(collection, item, None, mint_to, item_config, |_, _| Ok(()))
976 }
977
978 #[pallet::call_index(5)]
990 #[pallet::weight(T::WeightInfo::burn())]
991 pub fn burn(
992 origin: OriginFor<T>,
993 collection: T::CollectionId,
994 item: T::ItemId,
995 ) -> DispatchResult {
996 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
997 .map(|_| None)
998 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
999
1000 Self::do_burn(collection, item, |details| {
1001 if let Some(check_origin) = maybe_check_origin {
1002 ensure!(details.owner == check_origin, Error::<T, I>::NoPermission);
1003 }
1004 Ok(())
1005 })
1006 }
1007
1008 #[pallet::call_index(6)]
1023 #[pallet::weight(T::WeightInfo::transfer())]
1024 pub fn transfer(
1025 origin: OriginFor<T>,
1026 collection: T::CollectionId,
1027 item: T::ItemId,
1028 dest: AccountIdLookupOf<T>,
1029 ) -> DispatchResult {
1030 let origin = ensure_signed(origin)?;
1031 let dest = T::Lookup::lookup(dest)?;
1032
1033 Self::do_transfer(collection, item, dest, |_, details| {
1034 if details.owner != origin {
1035 let deadline =
1036 details.approvals.get(&origin).ok_or(Error::<T, I>::NoPermission)?;
1037 if let Some(d) = deadline {
1038 let block_number = T::BlockNumberProvider::current_block_number();
1039 ensure!(block_number <= *d, Error::<T, I>::ApprovalExpired);
1040 }
1041 }
1042 Ok(())
1043 })
1044 }
1045
1046 #[pallet::call_index(7)]
1064 #[pallet::weight(T::WeightInfo::redeposit(items.len() as u32))]
1065 pub fn redeposit(
1066 origin: OriginFor<T>,
1067 collection: T::CollectionId,
1068 items: Vec<T::ItemId>,
1069 ) -> DispatchResult {
1070 let origin = ensure_signed(origin)?;
1071
1072 let collection_details =
1073 Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
1074 ensure!(collection_details.owner == origin, Error::<T, I>::NoPermission);
1075
1076 let config = Self::get_collection_config(&collection)?;
1077 let deposit = match config.is_setting_enabled(CollectionSetting::DepositRequired) {
1078 true => T::ItemDeposit::get(),
1079 false => Zero::zero(),
1080 };
1081
1082 let mut successful = Vec::with_capacity(items.len());
1083 for item in items.into_iter() {
1084 let mut details = match Item::<T, I>::get(&collection, &item) {
1085 Some(x) => x,
1086 None => continue,
1087 };
1088 let old = details.deposit.amount;
1089 if old > deposit {
1090 T::Currency::unreserve(&details.deposit.account, old - deposit);
1091 } else if deposit > old {
1092 if T::Currency::reserve(&details.deposit.account, deposit - old).is_err() {
1093 continue;
1096 }
1097 } else {
1098 continue;
1099 }
1100 details.deposit.amount = deposit;
1101 Item::<T, I>::insert(&collection, &item, &details);
1102 successful.push(item);
1103 }
1104
1105 Self::deposit_event(Event::<T, I>::Redeposited {
1106 collection,
1107 successful_items: successful,
1108 });
1109
1110 Ok(())
1111 }
1112
1113 #[pallet::call_index(8)]
1124 #[pallet::weight(T::WeightInfo::lock_item_transfer())]
1125 pub fn lock_item_transfer(
1126 origin: OriginFor<T>,
1127 collection: T::CollectionId,
1128 item: T::ItemId,
1129 ) -> DispatchResult {
1130 let origin = ensure_signed(origin)?;
1131 Self::do_lock_item_transfer(origin, collection, item)
1132 }
1133
1134 #[pallet::call_index(9)]
1145 #[pallet::weight(T::WeightInfo::unlock_item_transfer())]
1146 pub fn unlock_item_transfer(
1147 origin: OriginFor<T>,
1148 collection: T::CollectionId,
1149 item: T::ItemId,
1150 ) -> DispatchResult {
1151 let origin = ensure_signed(origin)?;
1152 Self::do_unlock_item_transfer(origin, collection, item)
1153 }
1154
1155 #[pallet::call_index(10)]
1168 #[pallet::weight(T::WeightInfo::lock_collection())]
1169 pub fn lock_collection(
1170 origin: OriginFor<T>,
1171 collection: T::CollectionId,
1172 lock_settings: CollectionSettings,
1173 ) -> DispatchResult {
1174 let origin = ensure_signed(origin)?;
1175 Self::do_lock_collection(origin, collection, lock_settings)
1176 }
1177
1178 #[pallet::call_index(11)]
1194 #[pallet::weight(T::WeightInfo::transfer_ownership())]
1195 pub fn transfer_ownership(
1196 origin: OriginFor<T>,
1197 collection: T::CollectionId,
1198 new_owner: AccountIdLookupOf<T>,
1199 ) -> DispatchResult {
1200 let origin = ensure_signed(origin)?;
1201 let new_owner = T::Lookup::lookup(new_owner)?;
1202 Self::do_transfer_ownership(origin, collection, new_owner)
1203 }
1204
1205 #[pallet::call_index(12)]
1222 #[pallet::weight(T::WeightInfo::set_team())]
1223 pub fn set_team(
1224 origin: OriginFor<T>,
1225 collection: T::CollectionId,
1226 issuer: Option<AccountIdLookupOf<T>>,
1227 admin: Option<AccountIdLookupOf<T>>,
1228 freezer: Option<AccountIdLookupOf<T>>,
1229 ) -> DispatchResult {
1230 let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1231 .map(|_| None)
1232 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1233 let issuer = issuer.map(T::Lookup::lookup).transpose()?;
1234 let admin = admin.map(T::Lookup::lookup).transpose()?;
1235 let freezer = freezer.map(T::Lookup::lookup).transpose()?;
1236 Self::do_set_team(maybe_check_owner, collection, issuer, admin, freezer)
1237 }
1238
1239 #[pallet::call_index(13)]
1254 #[pallet::weight(T::WeightInfo::force_collection_owner())]
1255 pub fn force_collection_owner(
1256 origin: OriginFor<T>,
1257 collection: T::CollectionId,
1258 owner: AccountIdLookupOf<T>,
1259 ) -> DispatchResult {
1260 T::ForceOrigin::ensure_origin(origin)?;
1261 let new_owner = T::Lookup::lookup(owner)?;
1262 Self::do_force_collection_owner(collection, new_owner)
1263 }
1264
1265 #[pallet::call_index(14)]
1276 #[pallet::weight(T::WeightInfo::force_collection_config())]
1277 pub fn force_collection_config(
1278 origin: OriginFor<T>,
1279 collection: T::CollectionId,
1280 config: CollectionConfigFor<T, I>,
1281 ) -> DispatchResult {
1282 T::ForceOrigin::ensure_origin(origin)?;
1283 Self::do_force_collection_config(collection, config)
1284 }
1285
1286 #[pallet::call_index(15)]
1301 #[pallet::weight(T::WeightInfo::approve_transfer())]
1302 pub fn approve_transfer(
1303 origin: OriginFor<T>,
1304 collection: T::CollectionId,
1305 item: T::ItemId,
1306 delegate: AccountIdLookupOf<T>,
1307 maybe_deadline: Option<BlockNumberFor<T, I>>,
1308 ) -> DispatchResult {
1309 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1310 .map(|_| None)
1311 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1312 let delegate = T::Lookup::lookup(delegate)?;
1313 Self::do_approve_transfer(
1314 maybe_check_origin,
1315 collection,
1316 item,
1317 delegate,
1318 maybe_deadline,
1319 )
1320 }
1321
1322 #[pallet::call_index(16)]
1337 #[pallet::weight(T::WeightInfo::cancel_approval())]
1338 pub fn cancel_approval(
1339 origin: OriginFor<T>,
1340 collection: T::CollectionId,
1341 item: T::ItemId,
1342 delegate: AccountIdLookupOf<T>,
1343 ) -> DispatchResult {
1344 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1345 .map(|_| None)
1346 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1347 let delegate = T::Lookup::lookup(delegate)?;
1348 Self::do_cancel_approval(maybe_check_origin, collection, item, delegate)
1349 }
1350
1351 #[pallet::call_index(17)]
1365 #[pallet::weight(T::WeightInfo::clear_all_transfer_approvals())]
1366 pub fn clear_all_transfer_approvals(
1367 origin: OriginFor<T>,
1368 collection: T::CollectionId,
1369 item: T::ItemId,
1370 ) -> DispatchResult {
1371 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1372 .map(|_| None)
1373 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1374 Self::do_clear_all_transfer_approvals(maybe_check_origin, collection, item)
1375 }
1376
1377 #[pallet::call_index(18)]
1395 #[pallet::weight(T::WeightInfo::lock_item_properties())]
1396 pub fn lock_item_properties(
1397 origin: OriginFor<T>,
1398 collection: T::CollectionId,
1399 item: T::ItemId,
1400 lock_metadata: bool,
1401 lock_attributes: bool,
1402 ) -> DispatchResult {
1403 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1404 .map(|_| None)
1405 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1406 Self::do_lock_item_properties(
1407 maybe_check_origin,
1408 collection,
1409 item,
1410 lock_metadata,
1411 lock_attributes,
1412 )
1413 }
1414
1415 #[pallet::call_index(19)]
1438 #[pallet::weight(T::WeightInfo::set_attribute())]
1439 pub fn set_attribute(
1440 origin: OriginFor<T>,
1441 collection: T::CollectionId,
1442 maybe_item: Option<T::ItemId>,
1443 namespace: AttributeNamespace<T::AccountId>,
1444 key: BoundedVec<u8, T::KeyLimit>,
1445 value: BoundedVec<u8, T::ValueLimit>,
1446 ) -> DispatchResult {
1447 let origin = ensure_signed(origin)?;
1448 let depositor = match namespace {
1449 AttributeNamespace::CollectionOwner => {
1450 Self::collection_owner(collection).ok_or(Error::<T, I>::UnknownCollection)?
1451 },
1452 _ => origin.clone(),
1453 };
1454 Self::do_set_attribute(origin, collection, maybe_item, namespace, key, value, depositor)
1455 }
1456
1457 #[pallet::call_index(20)]
1475 #[pallet::weight(T::WeightInfo::force_set_attribute())]
1476 pub fn force_set_attribute(
1477 origin: OriginFor<T>,
1478 set_as: Option<T::AccountId>,
1479 collection: T::CollectionId,
1480 maybe_item: Option<T::ItemId>,
1481 namespace: AttributeNamespace<T::AccountId>,
1482 key: BoundedVec<u8, T::KeyLimit>,
1483 value: BoundedVec<u8, T::ValueLimit>,
1484 ) -> DispatchResult {
1485 T::ForceOrigin::ensure_origin(origin)?;
1486 Self::do_force_set_attribute(set_as, collection, maybe_item, namespace, key, value)
1487 }
1488
1489 #[pallet::call_index(21)]
1505 #[pallet::weight(T::WeightInfo::clear_attribute())]
1506 pub fn clear_attribute(
1507 origin: OriginFor<T>,
1508 collection: T::CollectionId,
1509 maybe_item: Option<T::ItemId>,
1510 namespace: AttributeNamespace<T::AccountId>,
1511 key: BoundedVec<u8, T::KeyLimit>,
1512 ) -> DispatchResult {
1513 let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1514 .map(|_| None)
1515 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1516 Self::do_clear_attribute(maybe_check_owner, collection, maybe_item, namespace, key)
1517 }
1518
1519 #[pallet::call_index(22)]
1529 #[pallet::weight(T::WeightInfo::approve_item_attributes())]
1530 pub fn approve_item_attributes(
1531 origin: OriginFor<T>,
1532 collection: T::CollectionId,
1533 item: T::ItemId,
1534 delegate: AccountIdLookupOf<T>,
1535 ) -> DispatchResult {
1536 let origin = ensure_signed(origin)?;
1537 let delegate = T::Lookup::lookup(delegate)?;
1538 Self::do_approve_item_attributes(origin, collection, item, delegate)
1539 }
1540
1541 #[pallet::call_index(23)]
1552 #[pallet::weight(T::WeightInfo::cancel_item_attributes_approval(
1553 witness.account_attributes
1554 ))]
1555 pub fn cancel_item_attributes_approval(
1556 origin: OriginFor<T>,
1557 collection: T::CollectionId,
1558 item: T::ItemId,
1559 delegate: AccountIdLookupOf<T>,
1560 witness: CancelAttributesApprovalWitness,
1561 ) -> DispatchResult {
1562 let origin = ensure_signed(origin)?;
1563 let delegate = T::Lookup::lookup(delegate)?;
1564 Self::do_cancel_item_attributes_approval(origin, collection, item, delegate, witness)
1565 }
1566
1567 #[pallet::call_index(24)]
1584 #[pallet::weight(T::WeightInfo::set_metadata())]
1585 pub fn set_metadata(
1586 origin: OriginFor<T>,
1587 collection: T::CollectionId,
1588 item: T::ItemId,
1589 data: BoundedVec<u8, T::StringLimit>,
1590 ) -> DispatchResult {
1591 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1592 .map(|_| None)
1593 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1594 Self::do_set_item_metadata(maybe_check_origin, collection, item, data, None)
1595 }
1596
1597 #[pallet::call_index(25)]
1611 #[pallet::weight(T::WeightInfo::clear_metadata())]
1612 pub fn clear_metadata(
1613 origin: OriginFor<T>,
1614 collection: T::CollectionId,
1615 item: T::ItemId,
1616 ) -> DispatchResult {
1617 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1618 .map(|_| None)
1619 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1620 Self::do_clear_item_metadata(maybe_check_origin, collection, item)
1621 }
1622
1623 #[pallet::call_index(26)]
1639 #[pallet::weight(T::WeightInfo::set_collection_metadata())]
1640 pub fn set_collection_metadata(
1641 origin: OriginFor<T>,
1642 collection: T::CollectionId,
1643 data: BoundedVec<u8, T::StringLimit>,
1644 ) -> DispatchResult {
1645 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1646 .map(|_| None)
1647 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1648 Self::do_set_collection_metadata(maybe_check_origin, collection, data)
1649 }
1650
1651 #[pallet::call_index(27)]
1664 #[pallet::weight(T::WeightInfo::clear_collection_metadata())]
1665 pub fn clear_collection_metadata(
1666 origin: OriginFor<T>,
1667 collection: T::CollectionId,
1668 ) -> DispatchResult {
1669 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1670 .map(|_| None)
1671 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1672 Self::do_clear_collection_metadata(maybe_check_origin, collection)
1673 }
1674
1675 #[pallet::call_index(28)]
1686 #[pallet::weight(T::WeightInfo::set_accept_ownership())]
1687 pub fn set_accept_ownership(
1688 origin: OriginFor<T>,
1689 maybe_collection: Option<T::CollectionId>,
1690 ) -> DispatchResult {
1691 let who = ensure_signed(origin)?;
1692 Self::do_set_accept_ownership(who, maybe_collection)
1693 }
1694
1695 #[pallet::call_index(29)]
1705 #[pallet::weight(T::WeightInfo::set_collection_max_supply())]
1706 pub fn set_collection_max_supply(
1707 origin: OriginFor<T>,
1708 collection: T::CollectionId,
1709 max_supply: u32,
1710 ) -> DispatchResult {
1711 let maybe_check_owner = T::ForceOrigin::try_origin(origin)
1712 .map(|_| None)
1713 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1714 Self::do_set_collection_max_supply(maybe_check_owner, collection, max_supply)
1715 }
1716
1717 #[pallet::call_index(30)]
1727 #[pallet::weight(T::WeightInfo::update_mint_settings())]
1728 pub fn update_mint_settings(
1729 origin: OriginFor<T>,
1730 collection: T::CollectionId,
1731 mint_settings: MintSettings<BalanceOf<T, I>, BlockNumberFor<T, I>, T::CollectionId>,
1732 ) -> DispatchResult {
1733 let maybe_check_origin = T::ForceOrigin::try_origin(origin)
1734 .map(|_| None)
1735 .or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
1736 Self::do_update_mint_settings(maybe_check_origin, collection, mint_settings)
1737 }
1738
1739 #[pallet::call_index(31)]
1751 #[pallet::weight(T::WeightInfo::set_price())]
1752 pub fn set_price(
1753 origin: OriginFor<T>,
1754 collection: T::CollectionId,
1755 item: T::ItemId,
1756 price: Option<ItemPrice<T, I>>,
1757 whitelisted_buyer: Option<AccountIdLookupOf<T>>,
1758 ) -> DispatchResult {
1759 let origin = ensure_signed(origin)?;
1760 let whitelisted_buyer = whitelisted_buyer.map(T::Lookup::lookup).transpose()?;
1761 Self::do_set_price(collection, item, origin, price, whitelisted_buyer)
1762 }
1763
1764 #[pallet::call_index(32)]
1774 #[pallet::weight(T::WeightInfo::buy_item())]
1775 pub fn buy_item(
1776 origin: OriginFor<T>,
1777 collection: T::CollectionId,
1778 item: T::ItemId,
1779 bid_price: ItemPrice<T, I>,
1780 ) -> DispatchResult {
1781 let origin = ensure_signed(origin)?;
1782 Self::do_buy_item(collection, item, origin, bid_price)
1783 }
1784
1785 #[pallet::call_index(33)]
1793 #[pallet::weight(T::WeightInfo::pay_tips(tips.len() as u32))]
1794 pub fn pay_tips(
1795 origin: OriginFor<T>,
1796 tips: BoundedVec<ItemTipOf<T, I>, T::MaxTips>,
1797 ) -> DispatchResult {
1798 let origin = ensure_signed(origin)?;
1799 Self::do_pay_tips(origin, tips)
1800 }
1801
1802 #[pallet::call_index(34)]
1819 #[pallet::weight(T::WeightInfo::create_swap())]
1820 pub fn create_swap(
1821 origin: OriginFor<T>,
1822 offered_collection: T::CollectionId,
1823 offered_item: T::ItemId,
1824 desired_collection: T::CollectionId,
1825 maybe_desired_item: Option<T::ItemId>,
1826 maybe_price: Option<PriceWithDirection<ItemPrice<T, I>>>,
1827 duration: BlockNumberFor<T, I>,
1828 ) -> DispatchResult {
1829 let origin = ensure_signed(origin)?;
1830 Self::do_create_swap(
1831 origin,
1832 offered_collection,
1833 offered_item,
1834 desired_collection,
1835 maybe_desired_item,
1836 maybe_price,
1837 duration,
1838 )
1839 }
1840
1841 #[pallet::call_index(35)]
1851 #[pallet::weight(T::WeightInfo::cancel_swap())]
1852 pub fn cancel_swap(
1853 origin: OriginFor<T>,
1854 offered_collection: T::CollectionId,
1855 offered_item: T::ItemId,
1856 ) -> DispatchResult {
1857 let origin = ensure_signed(origin)?;
1858 Self::do_cancel_swap(origin, offered_collection, offered_item)
1859 }
1860
1861 #[pallet::call_index(36)]
1874 #[pallet::weight(T::WeightInfo::claim_swap())]
1875 pub fn claim_swap(
1876 origin: OriginFor<T>,
1877 send_collection: T::CollectionId,
1878 send_item: T::ItemId,
1879 receive_collection: T::CollectionId,
1880 receive_item: T::ItemId,
1881 witness_price: Option<PriceWithDirection<ItemPrice<T, I>>>,
1882 ) -> DispatchResult {
1883 let origin = ensure_signed(origin)?;
1884 Self::do_claim_swap(
1885 origin,
1886 send_collection,
1887 send_item,
1888 receive_collection,
1889 receive_item,
1890 witness_price,
1891 )
1892 }
1893
1894 #[pallet::call_index(37)]
1908 #[pallet::weight(T::WeightInfo::mint_pre_signed(mint_data.attributes.len() as u32))]
1909 pub fn mint_pre_signed(
1910 origin: OriginFor<T>,
1911 mint_data: Box<PreSignedMintOf<T, I>>,
1912 signature: T::OffchainSignature,
1913 signer: T::AccountId,
1914 ) -> DispatchResult {
1915 let origin = ensure_signed(origin)?;
1916 Self::validate_signature(&Encode::encode(&mint_data), &signature, &signer)?;
1917 Self::do_mint_pre_signed(origin, *mint_data, signer)
1918 }
1919
1920 #[pallet::call_index(38)]
1934 #[pallet::weight(T::WeightInfo::set_attributes_pre_signed(data.attributes.len() as u32))]
1935 pub fn set_attributes_pre_signed(
1936 origin: OriginFor<T>,
1937 data: PreSignedAttributesOf<T, I>,
1938 signature: T::OffchainSignature,
1939 signer: T::AccountId,
1940 ) -> DispatchResult {
1941 let origin = ensure_signed(origin)?;
1942 Self::validate_signature(&Encode::encode(&data), &signature, &signer)?;
1943 Self::do_set_attributes_pre_signed(origin, data, signer)
1944 }
1945 }
1946}
1947
1948sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);