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

difftreelog

refactor use modify_token_properties

Daniel Shiposha2022-07-04parent: #85380b5.patch.diff
in: master

4 files changed

modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -229,7 +229,7 @@
 				self,
 				&sender,
 				token_id,
-				properties,
+				properties.into_iter(),
 				false,
 				nesting_budget,
 			),
@@ -251,7 +251,7 @@
 				self,
 				&sender,
 				token_id,
-				property_keys,
+				property_keys.into_iter(),
 				nesting_budget,
 			),
 			weight,
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -37,7 +37,7 @@
 
 use crate::{
 	AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,
-	SelfWeightOf, weights::WeightInfo, TokenProperties, property_guard::*,
+	SelfWeightOf, weights::WeightInfo, TokenProperties,
 };
 
 #[solidity_interface(name = "TokenProperties")]
@@ -82,21 +82,18 @@
 			.map_err(|_| "key too long")?;
 		let value = value.try_into().map_err(|_| "value too long")?;
 
-		let is_token_create = false;
 		let nesting_budget = self
 			.recorder
 			.weight_calls_budget(<StructureWeight<T>>::find_parent());
 
-		let mut guard = PropertyGuard::new(PropertyGuardData {
-			sender: &caller,
-			collection: self,
-			token_id: TokenId(token_id),
-			is_token_create,
-			nesting_budget: &nesting_budget,
-		});
-
-		<Pallet<T>>::set_token_property(Property { key, value }, &mut guard)
-			.map_err(dispatch_to_evm::<T>)
+		<Pallet<T>>::set_token_property(
+			self,
+			&caller,
+			TokenId(token_id),
+			Property { key, value },
+			&nesting_budget,
+		)
+		.map_err(dispatch_to_evm::<T>)
 	}
 
 	fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {
@@ -106,20 +103,12 @@
 			.try_into()
 			.map_err(|_| "key too long")?;
 
-		let is_token_create = false;
 		let nesting_budget = self
 			.recorder
 			.weight_calls_budget(<StructureWeight<T>>::find_parent());
 
-		let mut guard = PropertyGuard::new(PropertyGuardData {
-			sender: &caller,
-			collection: self,
-			token_id: TokenId(token_id),
-			is_token_create,
-			nesting_budget: &nesting_budget,
-		});
-
-		<Pallet<T>>::delete_token_property(key, &mut guard).map_err(dispatch_to_evm::<T>)
+		<Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)
+			.map_err(dispatch_to_evm::<T>)
 	}
 
 	/// Throws error if key not found
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
28use up_data_structs::{28use up_data_structs::{
29 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,29 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
30 mapping::TokenAddressMapping, budget::Budget, Property, PropertyPermission, PropertyKey,30 mapping::TokenAddressMapping, budget::Budget, Property, PropertyPermission, PropertyKey,
31 PropertyKeyPermission, Properties, PropertyScope, TrySetProperty, TokenChild, AuxPropertyValue,31 PropertyValue, PropertyKeyPermission, Properties, PropertyScope, TrySetProperty, TokenChild,
32 AuxPropertyValue,
32};33};
33use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};34use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
52pub mod erc;53pub mod erc;
53pub mod weights;54pub mod weights;
54
55mod property_guard;
56
57use property_guard::*;
5855
59pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;56pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;
60pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;57pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
89 NonfungibleItemsHaveNoAmount,86 NonfungibleItemsHaveNoAmount,
90 /// Unable to burn NFT with children87 /// Unable to burn NFT with children
91 CantBurnNftWithChildren,88 CantBurnNftWithChildren,
89 /// Unable to create an empty property
90 UnableToCreateEmptyProperty,
92 }91 }
9392
94 #[pallet::config]93 #[pallet::config]
488 })487 })
489 }488 }
490
491 pub fn set_token_property(
492 property: Property,
493 guard: &mut PropertyGuard<'_, T>,
494 ) -> DispatchResult {
495 Self::check_token_change_permission(&property.key, guard)?;
496
497 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token_id), |properties| {
498 let property = property.clone();
499 properties.try_set(property.key, property.value)
500 })
501 .map_err(<CommonError<T>>::from)?;
502
503 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
504 guard.collection.id,
505 guard.token_id,
506 property.key,
507 ));
508
509 Ok(())
510 }
511489
512 #[transactional]490 #[transactional]
513 pub fn set_token_properties(491 fn modify_token_properties(
514 collection: &NonfungibleHandle<T>,492 collection: &NonfungibleHandle<T>,
515 sender: &T::CrossAccountId,493 sender: &T::CrossAccountId,
516 token_id: TokenId,494 token_id: TokenId,
517 properties: Vec<Property>,495 properties: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
518 is_token_create: bool,496 is_token_create: bool,
519 nesting_budget: &dyn Budget,497 nesting_budget: &dyn Budget,
520 ) -> DispatchResult {498 ) -> DispatchResult {
521 let mut guard = PropertyGuard::new(PropertyGuardData {499 let mut collection_admin_result = None;
522 sender,
523 collection,
524 token_id,
525 is_token_create,
526 nesting_budget,
527 });
528
529 for property in properties {
530 Self::set_token_property(property, &mut guard)?;
531 }
532
533 Ok(())
534 }
535
536 pub fn delete_token_property(
537 property_key: PropertyKey,
538 guard: &mut PropertyGuard<'_, T>,500 let mut token_owner_result = None;
539 ) -> DispatchResult {501
502 let mut check_collection_admin = || {
503 *collection_admin_result
540 Self::check_token_change_permission(&property_key, guard)?;504 .get_or_insert_with(|| collection.check_is_owner_or_admin(sender))
541505 };
506
507 let mut check_token_owner = || {
508 *token_owner_result.get_or_insert_with(|| {
542 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token_id), |properties| {509 let is_owned = <PalletStructure<T>>::check_indirectly_owned(
510 sender.clone(),
511 collection.id,
512 token_id,
513 None,
514 nesting_budget,
515 )?;
516
517 if is_owned {
518 Ok(())
519 } else {
543 properties.remove(&property_key)520 Err(<CommonError<T>>::NoPermission.into())
544 })521 }
522 })
545 .map_err(<CommonError<T>>::from)?;523 };
546
547 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
548 guard.collection.id,
549 guard.token_id,
550 property_key,
551 ));
552
553 Ok(())
554 }
555524
556 fn check_token_change_permission(525 for (key, value) in properties {
557 property_key: &PropertyKey,
558 guard: &mut PropertyGuard<'_, T>,
559 ) -> DispatchResult {
560 let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)526 let permission = <PalletCommon<T>>::property_permissions(collection.id)
561 .get(property_key)527 .get(&key)
562 .cloned()528 .cloned()
563 .unwrap_or_else(PropertyPermission::none);529 .unwrap_or_else(PropertyPermission::none);
564530
565 let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token_id))531 let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
566 .get(property_key)532 .get(&key)
567 .is_some();533 .is_some();
568534
569 match permission {535 match permission {
570 PropertyPermission { mutable: false, .. } if is_property_exists => {536 PropertyPermission { mutable: false, .. } if is_property_exists => {
571 Err(<CommonError<T>>::NoPermission.into())537 return Err(<CommonError<T>>::NoPermission.into());
572 }538 }
573539
574 PropertyPermission {540 PropertyPermission {
577 ..543 ..
578 } => {544 } => {
579 //TODO: investigate threats during public minting.545 //TODO: investigate threats during public minting.
580 if guard.is_token_create && (collection_admin || token_owner) {546 if is_token_create && (collection_admin || token_owner) {
547 if value.is_some() {
581 return Ok(());548 return Ok(());
582 }549 } else {
550 return Err(<Error<T>>::UnableToCreateEmptyProperty.into());
551 }
552 }
583553
584 let mut check_result = Err(<CommonError<T>>::NoPermission.into());554 let mut check_result = Err(<CommonError<T>>::NoPermission.into());
585555
586 if collection_admin {556 if collection_admin {
587 check_result = guard.check_collection_admin();557 check_result = check_collection_admin();
588 }558 }
589559
590 if token_owner {560 if token_owner {
591 check_result.or_else(|_| guard.check_token_owner())561 check_result = check_result.or_else(|_| check_token_owner())
592 } else {562 }
593 check_result563
594 }564 check_result?;
595 }565 }
596 }566 }
567
568 match value {
569 Some(value) => {
570 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
571 properties.try_set(key.clone(), value)
572 })
573 .map_err(<CommonError<T>>::from)?;
574
575 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
576 collection.id,
577 token_id,
578 key,
579 ));
580 }
581 None => {
582 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
583 properties.remove(&key)
584 })
585 .map_err(<CommonError<T>>::from)?;
586
587 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
588 collection.id,
589 token_id,
590 key,
591 ));
592 }
593 }
597 }594 }
595
596 Ok(())
597 }
598
599 #[transactional]
600 pub fn set_token_properties(
601 collection: &NonfungibleHandle<T>,
602 sender: &T::CrossAccountId,
603 token_id: TokenId,
604 properties: impl Iterator<Item = Property>,
605 is_token_create: bool,
606 nesting_budget: &dyn Budget,
607 ) -> DispatchResult {
608 Self::modify_token_properties(
609 collection,
610 sender,
611 token_id,
612 properties.map(|p| (p.key, Some(p.value))),
613 is_token_create,
614 nesting_budget,
615 )
616 }
617
618 pub fn set_token_property(
619 collection: &NonfungibleHandle<T>,
620 sender: &T::CrossAccountId,
621 token_id: TokenId,
622 property: Property,
623 nesting_budget: &dyn Budget,
624 ) -> DispatchResult {
625 let is_token_create = false;
626
627 Self::set_token_properties(
628 collection,
629 sender,
630 token_id,
631 [property].into_iter(),
632 is_token_create,
633 nesting_budget,
634 )
635 }
598636
599 #[transactional]637 #[transactional]
600 pub fn delete_token_properties(638 pub fn delete_token_properties(
601 collection: &NonfungibleHandle<T>,639 collection: &NonfungibleHandle<T>,
602 sender: &T::CrossAccountId,640 sender: &T::CrossAccountId,
603 token_id: TokenId,641 token_id: TokenId,
604 property_keys: Vec<PropertyKey>,642 property_keys: impl Iterator<Item = PropertyKey>,
605 nesting_budget: &dyn Budget,643 nesting_budget: &dyn Budget,
606 ) -> DispatchResult {644 ) -> DispatchResult {
607 let is_token_create = false;645 let is_token_create = false;
608646
609 let mut guard = PropertyGuard::new(PropertyGuardData {647 Self::modify_token_properties(
648 collection,
610 sender,649 sender,
611 collection,
612 token_id,650 token_id,
651 property_keys.into_iter().map(|key| (key, None)),
613 is_token_create,652 is_token_create,
614 nesting_budget,653 nesting_budget,
615 });654 )
616
617 for key in property_keys {
618 Self::delete_token_property(key, &mut guard)?;
619 }
620
621 Ok(())
622 }655 }
656
657 pub fn delete_token_property(
658 collection: &NonfungibleHandle<T>,
659 sender: &T::CrossAccountId,
660 token_id: TokenId,
661 property_key: PropertyKey,
662 nesting_budget: &dyn Budget,
663 ) -> DispatchResult {
664 Self::delete_token_properties(
665 collection,
666 sender,
667 token_id,
668 [property_key].into_iter(),
669 nesting_budget,
670 )
671 }
623672
624 pub fn set_collection_properties(673 pub fn set_collection_properties(
625 collection: &NonfungibleHandle<T>,674 collection: &NonfungibleHandle<T>,
829 collection,878 collection,
830 sender,879 sender,
831 TokenId(token),880 TokenId(token),
832 data.properties.clone().into_inner(),881 data.properties.clone().into_iter(),
833 true,882 true,
834 nesting_budget,883 nesting_budget,
835 ) {884 ) {
deletedpallets/nonfungible/src/property_guard.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/property_guard.rs
+++ /dev/null
@@ -1,59 +0,0 @@
-use super::*;
-
-pub struct PropertyGuard<'a, T: Config> {
-	pub sender: &'a T::CrossAccountId,
-	pub collection: &'a NonfungibleHandle<T>,
-	pub token_id: TokenId,
-	pub is_token_create: bool,
-	nesting_budget: &'a dyn Budget,
-
-	collection_admin_result: Option<DispatchResult>,
-	token_owner_result: Option<DispatchResult>,
-}
-
-pub struct PropertyGuardData<'a, T: Config> {
-	pub sender: &'a T::CrossAccountId,
-	pub collection: &'a NonfungibleHandle<T>,
-	pub token_id: TokenId,
-	pub is_token_create: bool,
-	pub nesting_budget: &'a dyn Budget,
-}
-
-impl<'a, T: Config> PropertyGuard<'a, T> {
-	pub fn new(data: PropertyGuardData<'a, T>) -> Self {
-		Self {
-			sender: data.sender,
-			collection: data.collection,
-			token_id: data.token_id,
-			is_token_create: data.is_token_create,
-			nesting_budget: data.nesting_budget,
-
-			collection_admin_result: None,
-			token_owner_result: None,
-		}
-	}
-
-	pub fn check_collection_admin(&mut self) -> DispatchResult {
-		*self
-			.collection_admin_result
-			.get_or_insert_with(|| self.collection.check_is_owner_or_admin(self.sender))
-	}
-
-	pub fn check_token_owner(&mut self) -> DispatchResult {
-		*self.token_owner_result.get_or_insert_with(|| {
-			let is_owned = <PalletStructure<T>>::check_indirectly_owned(
-				self.sender.clone(),
-				self.collection.id,
-				self.token_id,
-				None,
-				self.nesting_budget,
-			)?;
-
-			if is_owned {
-				Ok(())
-			} else {
-				Err(<CommonError<T>>::NoPermission.into())
-			}
-		})
-	}
-}