diff --git a/pallets/nfts/src/benchmarking.rs b/pallets/nfts/src/benchmarking.rs index 8fa87557c..bc25602c5 100644 --- a/pallets/nfts/src/benchmarking.rs +++ b/pallets/nfts/src/benchmarking.rs @@ -17,8 +17,6 @@ //! Nfts pallet benchmarking. -#![cfg(feature = "runtime-benchmarks")] - use enumflags2::{BitFlag, BitFlags}; use frame_benchmarking::v1::{ account, benchmarks_instance_pallet, whitelist_account, whitelisted_caller, BenchmarkError, @@ -74,8 +72,8 @@ fn mint_item, I: 'static>( whitelist_account!(caller); } let caller_lookup = T::Lookup::unlookup(caller.clone()); - let item_exists = Item::::contains_key(&collection, &item); - let item_config = ItemConfigOf::::get(&collection, &item); + let item_exists = Item::::contains_key(collection, item); + let item_config = ItemConfigOf::::get(collection, item); if item_exists { return (item, caller, caller_lookup) } else if let Some(item_config) = item_config { @@ -682,7 +680,7 @@ benchmarks_instance_pallet! { } pay_tips { - let n in 0 .. T::MaxTips::get() as u32; + let n in 0 .. T::MaxTips::get(); let amount = BalanceOf::::from(100u32); let caller: T::AccountId = whitelisted_caller(); let collection = T::Helper::collection(0); @@ -788,7 +786,7 @@ benchmarks_instance_pallet! { } mint_pre_signed { - let n in 0 .. T::MaxAttributesPerCall::get() as u32; + let n in 0 .. T::MaxAttributesPerCall::get(); let (caller_public, caller) = T::Helper::signer(); T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let caller_lookup = T::Lookup::unlookup(caller.clone()); @@ -823,14 +821,14 @@ benchmarks_instance_pallet! { let target: T::AccountId = account("target", 0, SEED); T::Currency::make_free_balance_be(&target, DepositBalanceOf::::max_value()); frame_system::Pallet::::set_block_number(One::one()); - }: _(SystemOrigin::Signed(target.clone()), Box::new(mint_data), signature.into(), caller) + }: _(SystemOrigin::Signed(target.clone()), Box::new(mint_data), signature, caller) verify { let metadata: BoundedVec<_, _> = metadata.try_into().unwrap(); assert_last_event::(Event::ItemMetadataSet { collection, item, data: metadata }.into()); } set_attributes_pre_signed { - let n in 0 .. T::MaxAttributesPerCall::get() as u32; + let n in 0 .. T::MaxAttributesPerCall::get(); let (collection, _, _) = create_collection::(); let item_owner: T::AccountId = account("item_owner", 0, SEED); @@ -866,7 +864,7 @@ benchmarks_instance_pallet! { let signature = T::Helper::sign(&signer_public, &message); frame_system::Pallet::::set_block_number(One::one()); - }: _(SystemOrigin::Signed(item_owner.clone()), pre_signed_data, signature.into(), signer.clone()) + }: _(SystemOrigin::Signed(item_owner.clone()), pre_signed_data, signature, signer.clone()) verify { assert_last_event::( Event::PreSignedAttributesSet { diff --git a/pallets/nfts/src/common_functions.rs b/pallets/nfts/src/common_functions.rs index f51de1922..f9224046f 100644 --- a/pallets/nfts/src/common_functions.rs +++ b/pallets/nfts/src/common_functions.rs @@ -45,7 +45,7 @@ impl, I: 'static> Pallet { signature: &T::OffchainSignature, signer: &T::AccountId, ) -> DispatchResult { - if signature.verify(&**data, &signer) { + if signature.verify(&**data, signer) { return Ok(()) } @@ -58,7 +58,7 @@ impl, I: 'static> Pallet { wrapped.extend(data); wrapped.extend(suffix); - ensure!(signature.verify(&*wrapped, &signer), Error::::WrongSignature); + ensure!(signature.verify(&*wrapped, signer), Error::::WrongSignature); Ok(()) } @@ -69,6 +69,7 @@ impl, I: 'static> Pallet { Self::deposit_event(Event::NextCollectionIdIncremented { next_id }); } + #[allow(missing_docs)] #[cfg(any(test, feature = "runtime-benchmarks"))] pub fn set_next_id(id: T::CollectionId) { NextCollectionId::::set(Some(id)); diff --git a/pallets/nfts/src/features/approvals.rs b/pallets/nfts/src/features/approvals.rs index ad5d93c2e..5ffaeea72 100644 --- a/pallets/nfts/src/features/approvals.rs +++ b/pallets/nfts/src/features/approvals.rs @@ -53,8 +53,7 @@ impl, I: 'static> Pallet { Self::is_pallet_feature_enabled(PalletFeature::Approvals), Error::::MethodDisabled ); - let mut details = - Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let mut details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; let collection_config = Self::get_collection_config(&collection)?; ensure!( @@ -73,7 +72,7 @@ impl, I: 'static> Pallet { .approvals .try_insert(delegate.clone(), deadline) .map_err(|_| Error::::ReachedApprovalLimit)?; - Item::::insert(&collection, &item, &details); + Item::::insert(collection, item, &details); Self::deposit_event(Event::TransferApproved { collection, @@ -106,8 +105,7 @@ impl, I: 'static> Pallet { item: T::ItemId, delegate: T::AccountId, ) -> DispatchResult { - let mut details = - Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let mut details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; let maybe_deadline = details.approvals.get(&delegate).ok_or(Error::::NotDelegate)?; @@ -125,7 +123,7 @@ impl, I: 'static> Pallet { } details.approvals.remove(&delegate); - Item::::insert(&collection, &item, &details); + Item::::insert(collection, item, &details); Self::deposit_event(Event::ApprovalCancelled { collection, @@ -156,14 +154,14 @@ impl, I: 'static> Pallet { item: T::ItemId, ) -> DispatchResult { let mut details = - Item::::get(&collection, &item).ok_or(Error::::UnknownCollection)?; + Item::::get(collection, item).ok_or(Error::::UnknownCollection)?; if let Some(check_origin) = maybe_check_origin { ensure!(check_origin == details.owner, Error::::NoPermission); } details.approvals.clear(); - Item::::insert(&collection, &item, &details); + Item::::insert(collection, item, &details); Self::deposit_event(Event::AllApprovalsCancelled { collection, diff --git a/pallets/nfts/src/features/atomic_swap.rs b/pallets/nfts/src/features/atomic_swap.rs index 31c93fba8..6c15f15ae 100644 --- a/pallets/nfts/src/features/atomic_swap.rs +++ b/pallets/nfts/src/features/atomic_swap.rs @@ -62,17 +62,17 @@ impl, I: 'static> Pallet { ); ensure!(duration <= T::MaxDeadlineDuration::get(), Error::::WrongDuration); - let item = Item::::get(&offered_collection_id, &offered_item_id) + let item = Item::::get(offered_collection_id, offered_item_id) .ok_or(Error::::UnknownItem)?; ensure!(item.owner == caller, Error::::NoPermission); match maybe_desired_item_id { Some(desired_item_id) => ensure!( - Item::::contains_key(&desired_collection_id, &desired_item_id), + Item::::contains_key(desired_collection_id, desired_item_id), Error::::UnknownItem ), None => ensure!( - Collection::::contains_key(&desired_collection_id), + Collection::::contains_key(desired_collection_id), Error::::UnknownCollection ), }; @@ -81,8 +81,8 @@ impl, I: 'static> Pallet { let deadline = duration.saturating_add(now); PendingSwapOf::::insert( - &offered_collection_id, - &offered_item_id, + offered_collection_id, + offered_item_id, PendingSwap { desired_collection: desired_collection_id, desired_item: maybe_desired_item_id, @@ -118,17 +118,17 @@ impl, I: 'static> Pallet { offered_collection_id: T::CollectionId, offered_item_id: T::ItemId, ) -> DispatchResult { - let swap = PendingSwapOf::::get(&offered_collection_id, &offered_item_id) + let swap = PendingSwapOf::::get(offered_collection_id, offered_item_id) .ok_or(Error::::UnknownSwap)?; let now = frame_system::Pallet::::block_number(); if swap.deadline > now { - let item = Item::::get(&offered_collection_id, &offered_item_id) + let item = Item::::get(offered_collection_id, offered_item_id) .ok_or(Error::::UnknownItem)?; ensure!(item.owner == caller, Error::::NoPermission); } - PendingSwapOf::::remove(&offered_collection_id, &offered_item_id); + PendingSwapOf::::remove(offered_collection_id, offered_item_id); Self::deposit_event(Event::SwapCancelled { offered_collection: offered_collection_id, @@ -172,11 +172,11 @@ impl, I: 'static> Pallet { Error::::MethodDisabled ); - let send_item = Item::::get(&send_collection_id, &send_item_id) + let send_item = Item::::get(send_collection_id, send_item_id) .ok_or(Error::::UnknownItem)?; - let receive_item = Item::::get(&receive_collection_id, &receive_item_id) + let receive_item = Item::::get(receive_collection_id, receive_item_id) .ok_or(Error::::UnknownItem)?; - let swap = PendingSwapOf::::get(&receive_collection_id, &receive_item_id) + let swap = PendingSwapOf::::get(receive_collection_id, receive_item_id) .ok_or(Error::::UnknownSwap)?; ensure!(send_item.owner == caller, Error::::NoPermission); diff --git a/pallets/nfts/src/features/attributes.rs b/pallets/nfts/src/features/attributes.rs index ab0cdc68b..b7ba769ae 100644 --- a/pallets/nfts/src/features/attributes.rs +++ b/pallets/nfts/src/features/attributes.rs @@ -69,8 +69,8 @@ impl, I: 'static> Pallet { let collection_config = Self::get_collection_config(&collection)?; // for the `CollectionOwner` namespace we need to check if the collection/item is not locked - match namespace { - AttributeNamespace::CollectionOwner => match maybe_item { + if namespace == AttributeNamespace::CollectionOwner { + match maybe_item { None => { ensure!( collection_config.is_setting_enabled(CollectionSetting::UnlockedAttributes), @@ -82,12 +82,11 @@ impl, I: 'static> Pallet { .map(|c| c.has_disabled_setting(ItemSetting::UnlockedAttributes))?; ensure!(!maybe_is_locked, Error::::LockedItemAttributes); }, - }, - _ => (), + } } let mut collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; let attribute = Attribute::::get((collection, maybe_item, &namespace, &key)); let attribute_exists = attribute.is_some(); @@ -183,7 +182,7 @@ impl, I: 'static> Pallet { value: BoundedVec, ) -> DispatchResult { let mut collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; let attribute = Attribute::::get((collection, maybe_item, &namespace, &key)); if let Some((_, deposit)) = attribute { @@ -229,8 +228,7 @@ impl, I: 'static> Pallet { let now = frame_system::Pallet::::block_number(); ensure!(deadline >= now, Error::::DeadlineExpired); - let item_details = - Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let item_details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; ensure!(item_details.owner == origin, Error::::NoPermission); // Only the CollectionOwner and Account() namespaces could be updated in this way. @@ -239,7 +237,7 @@ impl, I: 'static> Pallet { AttributeNamespace::CollectionOwner => {}, AttributeNamespace::Account(account) => { ensure!(account == &signer, Error::::NoPermission); - let approvals = ItemAttributesApprovalsOf::::get(&collection, &item); + let approvals = ItemAttributesApprovalsOf::::get(collection, item); if !approvals.contains(account) { Self::do_approve_item_attributes( origin.clone(), @@ -274,7 +272,7 @@ impl, I: 'static> Pallet { /// `depositor` account. The deposit associated with the attribute, if any, will be unreserved. /// /// - `maybe_check_origin`: An optional account that acts as an additional security check when - /// clearing the attribute. This can be `None` if no additional check is required. + /// clearing the attribute. This can be `None` if no additional check is required. /// - `collection`: The identifier of the collection to which the item belongs, or the /// collection itself if clearing a collection attribute. /// - `maybe_item`: The identifier of the item to which the attribute belongs, or `None` if @@ -298,14 +296,14 @@ impl, I: 'static> Pallet { // the same as the `deposit.account` (e.g. the deposit was paid by different account) if deposit.account != maybe_check_origin { ensure!( - Self::is_valid_namespace(&check_origin, &namespace, &collection, &maybe_item)?, + Self::is_valid_namespace(check_origin, &namespace, &collection, &maybe_item)?, Error::::NoPermission ); } // can't clear `CollectionOwner` type attributes if the collection/item is locked - match namespace { - AttributeNamespace::CollectionOwner => match maybe_item { + if namespace == AttributeNamespace::CollectionOwner { + match maybe_item { None => { let collection_config = Self::get_collection_config(&collection)?; ensure!( @@ -327,18 +325,17 @@ impl, I: 'static> Pallet { // e.g. in off-chain mints, the attribute's depositor will be the item's // owner, that's why we need to do this extra check. ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } }, - }, - _ => (), + } }; } let mut collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; collection_details.attributes.saturating_dec(); @@ -381,7 +378,7 @@ impl, I: 'static> Pallet { Error::::MethodDisabled ); - let details = Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; ensure!(check_origin == details.owner, Error::::NoPermission); ItemAttributesApprovalsOf::::try_mutate(collection, item, |approvals| { @@ -422,7 +419,7 @@ impl, I: 'static> Pallet { Error::::MethodDisabled ); - let details = Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; ensure!(check_origin == details.owner, Error::::NoPermission); ItemAttributesApprovalsOf::::try_mutate(collection, item, |approvals| { @@ -463,17 +460,17 @@ impl, I: 'static> Pallet { let mut result = false; match namespace { AttributeNamespace::CollectionOwner => - result = Self::has_role(&collection, &origin, CollectionRole::Admin), + result = Self::has_role(collection, origin, CollectionRole::Admin), AttributeNamespace::ItemOwner => if let Some(item) = maybe_item { let item_details = - Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + Item::::get(collection, item).ok_or(Error::::UnknownItem)?; result = origin == &item_details.owner }, AttributeNamespace::Account(account_id) => if let Some(item) = maybe_item { - let approvals = ItemAttributesApprovalsOf::::get(&collection, &item); - result = account_id == origin && approvals.contains(&origin) + let approvals = ItemAttributesApprovalsOf::::get(collection, item); + result = account_id == origin && approvals.contains(origin) }, _ => (), }; diff --git a/pallets/nfts/src/features/buy_sell.rs b/pallets/nfts/src/features/buy_sell.rs index 8cf86f79a..476053ee0 100644 --- a/pallets/nfts/src/features/buy_sell.rs +++ b/pallets/nfts/src/features/buy_sell.rs @@ -82,7 +82,7 @@ impl, I: 'static> Pallet { Error::::MethodDisabled ); - let details = Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; ensure!(details.owner == sender, Error::::NoPermission); let collection_config = Self::get_collection_config(&collection)?; @@ -98,7 +98,7 @@ impl, I: 'static> Pallet { ); if let Some(ref price) = price { - ItemPriceOf::::insert(&collection, &item, (price, whitelisted_buyer.clone())); + ItemPriceOf::::insert(collection, item, (price, whitelisted_buyer.clone())); Self::deposit_event(Event::ItemPriceSet { collection, item, @@ -106,7 +106,7 @@ impl, I: 'static> Pallet { whitelisted_buyer, }); } else { - ItemPriceOf::::remove(&collection, &item); + ItemPriceOf::::remove(collection, item); Self::deposit_event(Event::ItemPriceRemoved { collection, item }); } @@ -137,11 +137,11 @@ impl, I: 'static> Pallet { Error::::MethodDisabled ); - let details = Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; ensure!(details.owner != buyer, Error::::NoPermission); let price_info = - ItemPriceOf::::get(&collection, &item).ok_or(Error::::NotForSale)?; + ItemPriceOf::::get(collection, item).ok_or(Error::::NotForSale)?; ensure!(bid_price >= price_info.0, Error::::BidTooLow); diff --git a/pallets/nfts/src/features/create_delete_collection.rs b/pallets/nfts/src/features/create_delete_collection.rs index 348ec6b92..60286d701 100644 --- a/pallets/nfts/src/features/create_delete_collection.rs +++ b/pallets/nfts/src/features/create_delete_collection.rs @@ -65,8 +65,8 @@ impl, I: 'static> Pallet { ), ); - CollectionConfigOf::::insert(&collection, config); - CollectionAccount::::insert(&owner, &collection, ()); + CollectionConfigOf::::insert(collection, config); + CollectionAccount::::insert(&owner, collection, ()); Self::deposit_event(event); @@ -120,13 +120,13 @@ impl, I: 'static> Pallet { Error::::BadWitness ); - for (_, metadata) in ItemMetadataOf::::drain_prefix(&collection) { + for (_, metadata) in ItemMetadataOf::::drain_prefix(collection) { if let Some(depositor) = metadata.deposit.account { T::Currency::unreserve(&depositor, metadata.deposit.amount); } } - CollectionMetadataOf::::remove(&collection); + CollectionMetadataOf::::remove(collection); Self::clear_roles(&collection)?; for (_, (_, deposit)) in Attribute::::drain_prefix((&collection,)) { @@ -137,10 +137,10 @@ impl, I: 'static> Pallet { } } - CollectionAccount::::remove(&collection_details.owner, &collection); + CollectionAccount::::remove(&collection_details.owner, collection); T::Currency::unreserve(&collection_details.owner, collection_details.owner_deposit); - CollectionConfigOf::::remove(&collection); - let _ = ItemConfigOf::::clear_prefix(&collection, witness.item_configs, None); + CollectionConfigOf::::remove(collection); + let _ = ItemConfigOf::::clear_prefix(collection, witness.item_configs, None); Self::deposit_event(Event::Destroyed { collection }); diff --git a/pallets/nfts/src/features/create_delete_item.rs b/pallets/nfts/src/features/create_delete_item.rs index e9843b2e7..7a69de9ea 100644 --- a/pallets/nfts/src/features/create_delete_item.rs +++ b/pallets/nfts/src/features/create_delete_item.rs @@ -55,55 +55,51 @@ impl, I: 'static> Pallet { ) -> DispatchResult { ensure!(!Item::::contains_key(collection, item), Error::::AlreadyExists); - Collection::::try_mutate( - &collection, - |maybe_collection_details| -> DispatchResult { - let collection_details = - maybe_collection_details.as_mut().ok_or(Error::::UnknownCollection)?; + Collection::::try_mutate(collection, |maybe_collection_details| -> DispatchResult { + let collection_details = + maybe_collection_details.as_mut().ok_or(Error::::UnknownCollection)?; - let collection_config = Self::get_collection_config(&collection)?; - with_details_and_config(collection_details, &collection_config)?; + let collection_config = Self::get_collection_config(&collection)?; + with_details_and_config(collection_details, &collection_config)?; - if let Some(max_supply) = collection_config.max_supply { - ensure!(collection_details.items < max_supply, Error::::MaxSupplyReached); - } + if let Some(max_supply) = collection_config.max_supply { + ensure!(collection_details.items < max_supply, Error::::MaxSupplyReached); + } - collection_details.items.saturating_inc(); + collection_details.items.saturating_inc(); - let collection_config = Self::get_collection_config(&collection)?; - let deposit_amount = match collection_config - .is_setting_enabled(CollectionSetting::DepositRequired) - { + let collection_config = Self::get_collection_config(&collection)?; + let deposit_amount = + match collection_config.is_setting_enabled(CollectionSetting::DepositRequired) { true => T::ItemDeposit::get(), false => Zero::zero(), }; - let deposit_account = match maybe_depositor { - None => collection_details.owner.clone(), - Some(depositor) => depositor, - }; + let deposit_account = match maybe_depositor { + None => collection_details.owner.clone(), + Some(depositor) => depositor, + }; - let item_owner = mint_to.clone(); - Account::::insert((&item_owner, &collection, &item), ()); + let item_owner = mint_to.clone(); + Account::::insert((&item_owner, &collection, &item), ()); - if let Ok(existing_config) = ItemConfigOf::::try_get(&collection, &item) { - ensure!(existing_config == item_config, Error::::InconsistentItemConfig); - } else { - ItemConfigOf::::insert(&collection, &item, item_config); - collection_details.item_configs.saturating_inc(); - } + if let Ok(existing_config) = ItemConfigOf::::try_get(collection, item) { + ensure!(existing_config == item_config, Error::::InconsistentItemConfig); + } else { + ItemConfigOf::::insert(collection, item, item_config); + collection_details.item_configs.saturating_inc(); + } - T::Currency::reserve(&deposit_account, deposit_amount)?; + T::Currency::reserve(&deposit_account, deposit_amount)?; - let deposit = ItemDeposit { account: deposit_account, amount: deposit_amount }; - let details = ItemDetails { - owner: item_owner, - approvals: ApprovalsOf::::default(), - deposit, - }; - Item::::insert(&collection, &item, details); - Ok(()) - }, - )?; + let deposit = ItemDeposit { account: deposit_account, amount: deposit_amount }; + let details = ItemDetails { + owner: item_owner, + approvals: ApprovalsOf::::default(), + deposit, + }; + Item::::insert(collection, item, details); + Ok(()) + })?; Self::deposit_event(Event::Issued { collection, item, owner: mint_to }); Ok(()) @@ -221,12 +217,12 @@ impl, I: 'static> Pallet { // then we keep the config record and don't remove it let remove_config = !item_config.has_disabled_settings(); let owner = Collection::::try_mutate( - &collection, + collection, |maybe_collection_details| -> Result { let collection_details = maybe_collection_details.as_mut().ok_or(Error::::UnknownCollection)?; - let details = Item::::get(&collection, &item) - .ok_or(Error::::UnknownCollection)?; + let details = + Item::::get(collection, item).ok_or(Error::::UnknownCollection)?; with_details(&details)?; // Return the deposit. @@ -239,7 +235,7 @@ impl, I: 'static> Pallet { // Clear the metadata if it's not locked. if item_config.is_setting_enabled(ItemSetting::UnlockedMetadata) { - if let Some(metadata) = ItemMetadataOf::::take(&collection, &item) { + if let Some(metadata) = ItemMetadataOf::::take(collection, item) { let depositor_account = metadata.deposit.account.unwrap_or(collection_details.owner.clone()); @@ -258,14 +254,14 @@ impl, I: 'static> Pallet { }, )?; - Item::::remove(&collection, &item); + Item::::remove(collection, item); Account::::remove((&owner, &collection, &item)); - ItemPriceOf::::remove(&collection, &item); - PendingSwapOf::::remove(&collection, &item); - ItemAttributesApprovalsOf::::remove(&collection, &item); + ItemPriceOf::::remove(collection, item); + PendingSwapOf::::remove(collection, item); + ItemAttributesApprovalsOf::::remove(collection, item); if remove_config { - ItemConfigOf::::remove(&collection, &item); + ItemConfigOf::::remove(collection, item); } Self::deposit_event(Event::Burned { collection, item, owner }); diff --git a/pallets/nfts/src/features/lock.rs b/pallets/nfts/src/features/lock.rs index 4649f4a01..a013d0149 100644 --- a/pallets/nfts/src/features/lock.rs +++ b/pallets/nfts/src/features/lock.rs @@ -29,7 +29,7 @@ impl, I: 'static> Pallet { /// settings on the collection. The only setting that can't be disabled is `DepositRequired`. /// /// Note: it's possible only to lock the setting, but not to unlock it after. - + /// /// - `origin`: The origin of the transaction, representing the account attempting to lock the /// collection. /// - `collection`: The identifier of the collection to be locked. @@ -80,7 +80,7 @@ impl, I: 'static> Pallet { if !config.has_disabled_setting(ItemSetting::Transferable) { config.disable_setting(ItemSetting::Transferable); } - ItemConfigOf::::insert(&collection, &item, config); + ItemConfigOf::::insert(collection, item, config); Self::deposit_event(Event::::ItemTransferLocked { collection, item }); Ok(()) @@ -110,7 +110,7 @@ impl, I: 'static> Pallet { if config.has_disabled_setting(ItemSetting::Transferable) { config.enable_setting(ItemSetting::Transferable); } - ItemConfigOf::::insert(&collection, &item, config); + ItemConfigOf::::insert(collection, item, config); Self::deposit_event(Event::::ItemTransferUnlocked { collection, item }); Ok(()) @@ -140,7 +140,7 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } diff --git a/pallets/nfts/src/features/metadata.rs b/pallets/nfts/src/features/metadata.rs index b3d16b12c..8c630832d 100644 --- a/pallets/nfts/src/features/metadata.rs +++ b/pallets/nfts/src/features/metadata.rs @@ -18,6 +18,7 @@ //! This module contains helper methods to configure the metadata of collections and items. use alloc::vec::Vec; +use core::cmp::Ordering; use frame_support::pallet_prelude::*; @@ -50,14 +51,14 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } let is_root = maybe_check_origin.is_none(); let mut collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; let item_config = Self::get_item_config(&collection, &item)?; ensure!( @@ -106,7 +107,7 @@ impl, I: 'static> Pallet { data: data.clone(), }); - Collection::::insert(&collection, &collection_details); + Collection::::insert(collection, &collection_details); Self::deposit_event(Event::ItemMetadataSet { collection, item, data }); Ok(()) }) @@ -132,7 +133,7 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } @@ -141,7 +142,7 @@ impl, I: 'static> Pallet { let metadata = ItemMetadataOf::::take(collection, item) .ok_or(Error::::MetadataNotFound)?; let mut collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; let depositor_account = metadata.deposit.account.unwrap_or(collection_details.owner.clone()); @@ -159,7 +160,7 @@ impl, I: 'static> Pallet { collection_details.owner_deposit.saturating_reduce(metadata.deposit.amount); } - Collection::::insert(&collection, &collection_details); + Collection::::insert(collection, &collection_details); Self::deposit_event(Event::ItemMetadataCleared { collection, item }); Ok(()) @@ -185,7 +186,7 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } @@ -198,7 +199,7 @@ impl, I: 'static> Pallet { ); let mut details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; CollectionMetadataOf::::try_mutate_exists(collection, |metadata| { let old_deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit); @@ -210,14 +211,18 @@ impl, I: 'static> Pallet { .saturating_mul(((data.len()) as u32).into()) .saturating_add(T::MetadataDepositBase::get()); } - if deposit > old_deposit { - T::Currency::reserve(&details.owner, deposit - old_deposit)?; - } else if deposit < old_deposit { - T::Currency::unreserve(&details.owner, old_deposit - deposit); + match deposit.cmp(&old_deposit) { + Ordering::Greater => { + T::Currency::reserve(&details.owner, deposit - old_deposit)?; + }, + Ordering::Less => { + T::Currency::unreserve(&details.owner, old_deposit - deposit); + }, + _ => {}, } details.owner_deposit.saturating_accrue(deposit); - Collection::::insert(&collection, details); + Collection::::insert(collection, details); *metadata = Some(CollectionMetadata { deposit, data: data.clone() }); @@ -245,13 +250,13 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Admin), + Self::has_role(&collection, check_origin, CollectionRole::Admin), Error::::NoPermission ); } let mut details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; let collection_config = Self::get_collection_config(&collection)?; ensure!( @@ -264,7 +269,7 @@ impl, I: 'static> Pallet { let deposit = metadata.take().ok_or(Error::::UnknownCollection)?.deposit; T::Currency::unreserve(&details.owner, deposit); details.owner_deposit.saturating_reduce(deposit); - Collection::::insert(&collection, details); + Collection::::insert(collection, details); Self::deposit_event(Event::CollectionMetadataCleared { collection }); Ok(()) }) diff --git a/pallets/nfts/src/features/roles.rs b/pallets/nfts/src/features/roles.rs index 053eaf0b0..8ddec3380 100644 --- a/pallets/nfts/src/features/roles.rs +++ b/pallets/nfts/src/features/roles.rs @@ -81,7 +81,7 @@ impl, I: 'static> Pallet { // Insert new records. for (account, roles) in account_to_role { - CollectionRoleOf::::insert(&collection, &account, roles); + CollectionRoleOf::::insert(collection, &account, roles); } Self::deposit_event(Event::TeamChanged { collection, issuer, admin, freezer }); @@ -98,7 +98,7 @@ impl, I: 'static> Pallet { /// may need to be adjusted. pub(crate) fn clear_roles(collection_id: &T::CollectionId) -> Result<(), DispatchError> { let res = CollectionRoleOf::::clear_prefix( - &collection_id, + collection_id, CollectionRoles::max_roles() as u32, None, ); @@ -118,7 +118,7 @@ impl, I: 'static> Pallet { account_id: &T::AccountId, role: CollectionRole, ) -> bool { - CollectionRoleOf::::get(&collection_id, &account_id) + CollectionRoleOf::::get(collection_id, account_id) .map_or(false, |roles| roles.has_role(role)) } @@ -132,9 +132,13 @@ impl, I: 'static> Pallet { collection_id: &T::CollectionId, role: CollectionRole, ) -> Option { - CollectionRoleOf::::iter_prefix(&collection_id).into_iter().find_map( - |(account, roles)| if roles.has_role(role) { Some(account.clone()) } else { None }, - ) + CollectionRoleOf::::iter_prefix(collection_id).find_map(|(account, roles)| { + if roles.has_role(role) { + Some(account.clone()) + } else { + None + } + }) } /// Groups provided roles by account, given one account could have multiple roles. diff --git a/pallets/nfts/src/features/settings.rs b/pallets/nfts/src/features/settings.rs index 9c7ac7ca2..22495bc95 100644 --- a/pallets/nfts/src/features/settings.rs +++ b/pallets/nfts/src/features/settings.rs @@ -33,8 +33,8 @@ impl, I: 'static> Pallet { collection: T::CollectionId, config: CollectionConfigFor, ) -> DispatchResult { - ensure!(Collection::::contains_key(&collection), Error::::UnknownCollection); - CollectionConfigOf::::insert(&collection, config); + ensure!(Collection::::contains_key(collection), Error::::UnknownCollection); + CollectionConfigOf::::insert(collection, config); Self::deposit_event(Event::CollectionConfigChanged { collection }); Ok(()) } @@ -67,7 +67,7 @@ impl, I: 'static> Pallet { ); let details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; if let Some(check_owner) = &maybe_check_owner { ensure!(check_owner == &details.owner, Error::::NoPermission); } @@ -105,7 +105,7 @@ impl, I: 'static> Pallet { ) -> DispatchResult { if let Some(check_origin) = &maybe_check_origin { ensure!( - Self::has_role(&collection, &check_origin, CollectionRole::Issuer), + Self::has_role(&collection, check_origin, CollectionRole::Issuer), Error::::NoPermission ); } @@ -129,7 +129,7 @@ impl, I: 'static> Pallet { collection_id: &T::CollectionId, ) -> Result, DispatchError> { let config = - CollectionConfigOf::::get(&collection_id).ok_or(Error::::NoConfig)?; + CollectionConfigOf::::get(collection_id).ok_or(Error::::NoConfig)?; Ok(config) } @@ -145,8 +145,8 @@ impl, I: 'static> Pallet { collection_id: &T::CollectionId, item_id: &T::ItemId, ) -> Result { - let config = ItemConfigOf::::get(&collection_id, &item_id) - .ok_or(Error::::UnknownItem)?; + let config = + ItemConfigOf::::get(collection_id, item_id).ok_or(Error::::UnknownItem)?; Ok(config) } @@ -174,6 +174,6 @@ impl, I: 'static> Pallet { /// otherwise it returns `false`. pub(crate) fn is_pallet_feature_enabled(feature: PalletFeature) -> bool { let features = T::Features::get(); - return features.is_enabled(feature) + features.is_enabled(feature) } } diff --git a/pallets/nfts/src/features/transfer.rs b/pallets/nfts/src/features/transfer.rs index b7223a7c3..e0ae770ab 100644 --- a/pallets/nfts/src/features/transfer.rs +++ b/pallets/nfts/src/features/transfer.rs @@ -55,7 +55,7 @@ impl, I: 'static> Pallet { ) -> DispatchResult { // Retrieve collection details. let collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; // Ensure the item is not locked. ensure!(!T::Locker::is_locked(collection, item), Error::::ItemLocked); @@ -81,8 +81,7 @@ impl, I: 'static> Pallet { ); // Retrieve the item details. - let mut details = - Item::::get(&collection, &item).ok_or(Error::::UnknownItem)?; + let mut details = Item::::get(collection, item).ok_or(Error::::UnknownItem)?; // Perform the transfer with custom details using the provided closure. with_details(&collection_details, &mut details)?; @@ -99,9 +98,9 @@ impl, I: 'static> Pallet { details.approvals.clear(); // Update item details. - Item::::insert(&collection, &item, &details); - ItemPriceOf::::remove(&collection, &item); - PendingSwapOf::::remove(&collection, &item); + Item::::insert(collection, item, &details); + ItemPriceOf::::remove(collection, item); + PendingSwapOf::::remove(collection, item); // Emit `Transferred` event. Self::deposit_event(Event::Transferred { @@ -149,8 +148,8 @@ impl, I: 'static> Pallet { )?; // Update account ownership information. - CollectionAccount::::remove(&details.owner, &collection); - CollectionAccount::::insert(&new_owner, &collection, ()); + CollectionAccount::::remove(&details.owner, collection); + CollectionAccount::::insert(&new_owner, collection, ()); details.owner = new_owner.clone(); OwnershipAcceptance::::remove(&new_owner); @@ -224,8 +223,8 @@ impl, I: 'static> Pallet { )?; // Update collection accounts and set the new owner. - CollectionAccount::::remove(&details.owner, &collection); - CollectionAccount::::insert(&owner, &collection, ()); + CollectionAccount::::remove(&details.owner, collection); + CollectionAccount::::insert(&owner, collection, ()); details.owner = owner.clone(); // Emit `OwnerChanged` event. diff --git a/pallets/nfts/src/impl_nonfungibles.rs b/pallets/nfts/src/impl_nonfungibles.rs index 362cccd93..b014e3ed4 100644 --- a/pallets/nfts/src/impl_nonfungibles.rs +++ b/pallets/nfts/src/impl_nonfungibles.rs @@ -120,20 +120,15 @@ impl, I: 'static> Inspect<::AccountId> for Palle /// Default implementation is that all items are transferable. fn can_transfer(collection: &Self::CollectionId, item: &Self::ItemId) -> bool { use PalletAttributes::TransferDisabled; - match Self::has_system_attribute(&collection, &item, TransferDisabled) { + match Self::has_system_attribute(collection, item, TransferDisabled) { Ok(transfer_disabled) if transfer_disabled => return false, _ => (), } - match ( - CollectionConfigOf::::get(collection), - ItemConfigOf::::get(collection, item), - ) { - (Some(cc), Some(ic)) - if cc.is_setting_enabled(CollectionSetting::TransferableItems) && - ic.is_setting_enabled(ItemSetting::Transferable) => - true, - _ => false, - } + matches!( + (CollectionConfigOf::::get(collection), ItemConfigOf::::get(collection, item)), + (Some(cc), Some(ic)) if cc.is_setting_enabled(CollectionSetting::TransferableItems) + && ic.is_setting_enabled(ItemSetting::Transferable) + ) } } @@ -421,7 +416,7 @@ impl, I: 'static> Transfer for Pallet { fn disable_transfer(collection: &Self::CollectionId, item: &Self::ItemId) -> DispatchResult { let transfer_disabled = - Self::has_system_attribute(&collection, &item, PalletAttributes::TransferDisabled)?; + Self::has_system_attribute(collection, item, PalletAttributes::TransferDisabled)?; // Can't lock the item twice if transfer_disabled { return Err(Error::::ItemLocked.into()) diff --git a/pallets/nfts/src/lib.rs b/pallets/nfts/src/lib.rs index 89bfb9637..90d2a6224 100644 --- a/pallets/nfts/src/lib.rs +++ b/pallets/nfts/src/lib.rs @@ -30,6 +30,7 @@ #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +#[allow(missing_docs)] pub mod migration; #[cfg(test)] pub mod mock; @@ -45,12 +46,14 @@ mod features; mod impl_nonfungibles; mod types; +#[allow(missing_docs)] pub mod macros; pub mod weights; extern crate alloc; use alloc::{boxed::Box, vec, vec::Vec}; +use core::cmp::Ordering; use codec::{Decode, Encode}; use frame_support::traits::{ @@ -67,7 +70,7 @@ pub use types::*; pub use weights::WeightInfo; /// The log target of this pallet. -pub const LOG_TARGET: &'static str = "runtime::nfts"; +pub const LOG_TARGET: &str = "runtime::nfts"; /// A type alias for the account ID type used in the dispatchable functions of this pallet. type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; @@ -87,6 +90,7 @@ pub mod pallet { pub struct Pallet(PhantomData<(T, I)>); #[cfg(feature = "runtime-benchmarks")] + #[allow(missing_docs)] pub trait BenchmarkHelper { fn collection(i: u16) -> CollectionId; fn item(i: u16) -> ItemId; @@ -416,6 +420,7 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[allow(missing_docs)] pub enum Event, I: 'static = ()> { /// A `collection` was created. Created { collection: T::CollectionId, creator: T::AccountId, owner: T::AccountId }, @@ -1068,7 +1073,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let collection_details = - Collection::::get(&collection).ok_or(Error::::UnknownCollection)?; + Collection::::get(collection).ok_or(Error::::UnknownCollection)?; ensure!(collection_details.owner == origin, Error::::NoPermission); let config = Self::get_collection_config(&collection)?; @@ -1079,24 +1084,26 @@ pub mod pallet { let mut successful = Vec::with_capacity(items.len()); for item in items.into_iter() { - let mut details = match Item::::get(&collection, &item) { + let mut details = match Item::::get(collection, item) { Some(x) => x, None => continue, }; let old = details.deposit.amount; - if old > deposit { - T::Currency::unreserve(&details.deposit.account, old - deposit); - } else if deposit > old { - if T::Currency::reserve(&details.deposit.account, deposit - old).is_err() { - // NOTE: No alterations made to collection_details in this iteration so far, - // so this is OK to do. - continue - } - } else { - continue + match old.cmp(&deposit) { + Ordering::Greater => { + T::Currency::unreserve(&details.deposit.account, old - deposit); + }, + Ordering::Less => { + if T::Currency::reserve(&details.deposit.account, deposit - old).is_err() { + // NOTE: No alterations made to collection_details in this iteration so + // far, so this is OK to do. + continue + } + }, + _ => continue, } details.deposit.amount = deposit; - Item::::insert(&collection, &item, &details); + Item::::insert(collection, item, &details); successful.push(item); } diff --git a/pallets/nfts/src/migration.rs b/pallets/nfts/src/migration.rs index af611bf16..7d59b481f 100644 --- a/pallets/nfts/src/migration.rs +++ b/pallets/nfts/src/migration.rs @@ -22,12 +22,14 @@ use sp_runtime::TryRuntimeError; use super::*; +#[allow(missing_docs)] pub mod v1 { use frame_support::{pallet_prelude::*, weights::Weight}; use super::*; #[derive(Decode)] + #[allow(missing_docs)] pub struct OldCollectionDetails { pub owner: AccountId, pub owner_deposit: DepositBalance, @@ -71,7 +73,7 @@ pub mod v1 { OldCollectionDetails>, _, >(|key, old_value| { - let item_configs = ItemConfigOf::::iter_prefix(&key).count() as u32; + let item_configs = ItemConfigOf::::iter_prefix(key).count() as u32; configs_iterated += item_configs as u64; translated.saturating_inc(); Some(old_value.migrate_to_v1(item_configs))