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

difftreelog

Add extrinsic: delete token property

Daniel Shiposha2022-05-04parent: #d66bc31.patch.diff
in: master

12 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -294,6 +294,8 @@
 
 		TokenPropertySet(CollectionId, TokenId, Property),
 
+		TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),
+
 		PropertyPermissionSet(CollectionId, PropertyKeyPermission),
 	}
 
@@ -738,10 +740,9 @@
 	) -> DispatchResult {
 		collection.check_is_owner_or_admin(sender)?;
 
-		CollectionProperties::<T>::try_mutate(
-			collection.id,
-			|properties| properties.try_set_property(property.clone())
-		)?;
+		CollectionProperties::<T>::try_mutate(collection.id, |properties| {
+			properties.try_set_property(property.clone())
+		})?;
 
 		Self::deposit_event(Event::CollectionPropertySet(collection.id, property));
 
@@ -763,13 +764,16 @@
 	pub fn set_property_permission(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		property_permission: PropertyKeyPermission
+		property_permission: PropertyKeyPermission,
 	) -> DispatchResult {
 		collection.check_is_owner_or_admin(sender)?;
 
 		let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);
 		let current_permission = all_permissions.get(&property_permission.key);
-		if matches![current_permission, Some(PropertyPermission::AdminConst | PropertyPermission::ItemOwnerConst)] {
+		if matches![
+			current_permission,
+			Some(PropertyPermission::AdminConst | PropertyPermission::ItemOwnerConst)
+		] {
 			return Err(<Error<T>>::NoPermission.into());
 		}
 
@@ -779,7 +783,10 @@
 		})
 		.map_err(|_| PropertiesError::PropertyLimitReached)?;
 
-		Self::deposit_event(Event::PropertyPermissionSet(collection.id, property_permission));
+		Self::deposit_event(Event::PropertyPermissionSet(
+			collection.id,
+			property_permission,
+		));
 
 		Ok(())
 	}
@@ -787,7 +794,7 @@
 	pub fn set_property_permissions(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		property_permissions: Vec<PropertyKeyPermission>
+		property_permissions: Vec<PropertyKeyPermission>,
 	) -> DispatchResult {
 		for prop_pemission in property_permissions {
 			Self::set_property_permission(collection, sender, prop_pemission)?;
@@ -950,6 +957,7 @@
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
+	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
@@ -996,6 +1004,12 @@
 		token_id: TokenId,
 		property: Vec<Property>,
 	) -> DispatchResultWithPostInfo;
+	fn delete_token_properties(
+		&self,
+		sender: T::CrossAccountId,
+		token_id: TokenId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo;
 	fn set_property_permissions(
 		&self,
 		sender: &T::CrossAccountId,
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -21,7 +21,7 @@
 use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
 use sp_runtime::ArithmeticError;
 use sp_std::{vec::Vec, vec};
-use up_data_structs::{CustomDataLimit, Property, PropertyKeyPermission,};
+use up_data_structs::{CustomDataLimit, Property, PropertyKey, PropertyKeyPermission};
 
 use crate::{
 	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,
@@ -58,6 +58,10 @@
 		<SelfWeightOf<T>>::set_token_properties(amount)
 	}
 
+	fn delete_token_properties(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::delete_token_properties(amount)
+	}
+
 	fn set_property_permissions(amount: u32) -> Weight {
 		<SelfWeightOf<T>>::set_property_permissions(amount)
 	}
@@ -262,6 +266,15 @@
 		fail!(<Error<T>>::PropertiesNotAllowed)
 	}
 
+	fn delete_token_properties(
+		&self,
+		_sender: T::CrossAccountId,
+		_token_id: TokenId,
+		_property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo {
+		fail!(<Error<T>>::PropertiesNotAllowed)
+	}
+
 	fn set_variable_metadata(
 		&self,
 		_sender: T::CrossAccountId,
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -37,6 +37,7 @@
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
+	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
@@ -73,17 +74,22 @@
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 
-	fn set_collection_properties(amount: u32) -> Weight {
+	fn set_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
+	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_token_properties(amount: u32) -> Weight {
+	fn delete_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_property_permissions(amount: u32) -> Weight {
+	fn set_property_permissions(_amount: u32) -> Weight {
 		// Error
 		0
 	}
@@ -146,17 +152,22 @@
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 
-	fn set_collection_properties(amount: u32) -> Weight {
+	fn set_collection_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_token_properties(amount: u32) -> Weight {
+	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_property_permissions(amount: u32) -> Weight {
+	fn delete_token_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
+	fn set_property_permissions(_amount: u32) -> Weight {
 		// Error
 		0
 	}
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -18,7 +18,8 @@
 
 use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
 use up_data_structs::{
-	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property, PropertyKeyPermission,
+	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,
+	PropertyKey, PropertyKeyPermission,
 };
 use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
 use sp_runtime::DispatchError;
@@ -58,6 +59,10 @@
 		<SelfWeightOf<T>>::set_token_properties(amount)
 	}
 
+	fn delete_token_properties(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::delete_token_properties(amount)
+	}
+
 	fn set_property_permissions(amount: u32) -> Weight {
 		<SelfWeightOf<T>>::set_property_permissions(amount)
 	}
@@ -162,7 +167,7 @@
 
 		with_weight(
 			<Pallet<T>>::set_collection_properties(self, &sender, properties),
-			weight
+			weight,
 		)
 	}
 
@@ -176,7 +181,21 @@
 
 		with_weight(
 			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties),
-			weight
+			weight,
+		)
+	}
+
+	fn delete_token_properties(
+		&self,
+		sender: T::CrossAccountId,
+		token_id: TokenId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo {
+		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);
+
+		with_weight(
+			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),
+			weight,
 		)
 	}
 
@@ -185,11 +204,12 @@
 		sender: &T::CrossAccountId,
 		property_permissions: Vec<PropertyKeyPermission>,
 	) -> DispatchResultWithPostInfo {
-		let weight = <CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);
+		let weight =
+			<CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);
 
 		with_weight(
 			<Pallet<T>>::set_property_permissions(self, sender, property_permissions),
-			weight
+			weight,
 		)
 	}
 
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -21,7 +21,7 @@
 use up_data_structs::{
 	AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
 	mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
-	PropertyKeyPermission,
+	PropertyKey, PropertyKeyPermission,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_common::{
@@ -261,8 +261,63 @@
 		token_id: TokenId,
 		property: Property,
 	) -> DispatchResult {
+		Self::check_token_change_permission(collection, sender, token_id, &property.key)?;
+
+		<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+			properties.try_set_property(property.clone())
+		})?;
+
+		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
+			collection.id,
+			token_id,
+			property,
+		));
+
+		Ok(())
+	}
+
+	pub fn set_token_properties(
+		collection: &NonfungibleHandle<T>,
+		sender: &T::CrossAccountId,
+		token_id: TokenId,
+		properties: Vec<Property>,
+	) -> DispatchResult {
+		for property in properties {
+			Self::set_token_property(collection, sender, token_id, property)?;
+		}
+
+		Ok(())
+	}
+
+	pub fn delete_token_property(
+		collection: &NonfungibleHandle<T>,
+		sender: &T::CrossAccountId,
+		token_id: TokenId,
+		property_key: PropertyKey,
+	) -> DispatchResult {
+		Self::check_token_change_permission(collection, sender, token_id, &property_key)?;
+
+		<TokenProperties<T>>::mutate((collection.id, token_id), |properties| {
+			properties.remove_property(&property_key);
+		});
+
+		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
+			collection.id,
+			token_id,
+			property_key,
+		));
+
+		Ok(())
+	}
+
+	fn check_token_change_permission(
+		collection: &NonfungibleHandle<T>,
+		sender: &T::CrossAccountId,
+		token_id: TokenId,
+		property_key: &PropertyKey,
+	) -> DispatchResult {
 		let permission = <PalletCommon<T>>::property_permission(collection.id)
-			.get(&property.key)
+			.get(property_key)
 			.map(|p| p.clone())
 			.unwrap_or(PropertyPermission::None);
 
@@ -275,43 +330,29 @@
 		};
 
 		let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
-			.get_property(&property.key)
+			.get_property(property_key)
 			.is_some();
 
 		match (permission, is_property_exists) {
-			(PropertyPermission::AdminConst, false) => {
-				collection.check_is_owner_or_admin(sender)?
-			}
-			(PropertyPermission::Admin, _) => collection.check_is_owner_or_admin(sender)?,
-			(PropertyPermission::ItemOwnerConst, false) => check_token_owner()?,
-			(PropertyPermission::ItemOwner, _) => check_token_owner()?,
+			(PropertyPermission::AdminConst, false) => collection.check_is_owner_or_admin(sender),
+			(PropertyPermission::Admin, _) => collection.check_is_owner_or_admin(sender),
+			(PropertyPermission::ItemOwnerConst, false) => check_token_owner(),
+			(PropertyPermission::ItemOwner, _) => check_token_owner(),
 			(PropertyPermission::ItemOwnerOrAdmin, _) => {
-				check_token_owner().or(collection.check_is_owner_or_admin(sender))?;
+				check_token_owner().or(collection.check_is_owner_or_admin(sender))
 			}
-			_ => return Err(<CommonError<T>>::NoPermission.into()),
+			_ => Err(<CommonError<T>>::NoPermission.into()),
 		}
-
-		<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
-			properties.try_set_property(property.clone())
-		})?;
-
-		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
-			collection.id,
-			token_id,
-			property,
-		));
-
-		Ok(())
 	}
 
-	pub fn set_token_properties(
+	pub fn delete_token_properties(
 		collection: &NonfungibleHandle<T>,
 		sender: &T::CrossAccountId,
 		token_id: TokenId,
-		properties: Vec<Property>,
+		property_keys: Vec<PropertyKey>,
 	) -> DispatchResult {
-		for property in properties {
-			Self::set_token_property(collection, sender, token_id, property)?;
+		for key in property_keys {
+			Self::delete_token_property(collection, sender, token_id, key)?;
 		}
 
 		Ok(())
@@ -328,13 +369,9 @@
 	pub fn set_property_permissions(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		property_permissions: Vec<PropertyKeyPermission>
+		property_permissions: Vec<PropertyKeyPermission>,
 	) -> DispatchResult {
-		<PalletCommon<T>>::set_property_permissions(
-			collection,
-			sender,
-			property_permissions,
-		)
+		<PalletCommon<T>>::set_property_permissions(collection, sender, property_permissions)
 	}
 
 	pub fn transfer(
modifiedpallets/nonfungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -38,6 +38,7 @@
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
+	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
@@ -104,6 +105,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn delete_token_properties(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	fn set_property_permissions(amount: u32) -> Weight {
 		// TODO calculate appropriate weight
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
@@ -209,6 +215,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn delete_token_properties(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	fn set_property_permissions(amount: u32) -> Weight {
 		// TODO calculate appropriate weight
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};
21use up_data_structs::{21use up_data_structs::{
22 CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,22 CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,
23 budget::Budget, Property, PropertyKeyPermission,23 budget::Budget, Property, PropertyKey, PropertyKeyPermission,
24};24};
25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
26use sp_runtime::DispatchError;26use sp_runtime::DispatchError;
74 <SelfWeightOf<T>>::set_token_properties(amount)74 <SelfWeightOf<T>>::set_token_properties(amount)
75 }75 }
76
77 fn delete_token_properties(amount: u32) -> Weight {
78 <SelfWeightOf<T>>::delete_token_properties(amount)
79 }
7680
77 fn set_property_permissions(amount: u32) -> Weight {81 fn set_property_permissions(amount: u32) -> Weight {
78 <SelfWeightOf<T>>::set_property_permissions(amount)82 <SelfWeightOf<T>>::set_property_permissions(amount)
281 fail!(<Error<T>>::PropertiesNotAllowed)285 fail!(<Error<T>>::PropertiesNotAllowed)
282 }286 }
287
288 fn delete_token_properties(
289 &self,
290 _sender: T::CrossAccountId,
291 _token_id: TokenId,
292 _property_keys: Vec<PropertyKey>,
293 ) -> DispatchResultWithPostInfo {
294 fail!(<Error<T>>::PropertiesNotAllowed)
295 }
283296
284 fn set_variable_metadata(297 fn set_variable_metadata(
285 &self,298 &self,
modifiedpallets/refungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -40,6 +40,7 @@
 	fn burn_item_fully() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
+	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
 	fn transfer_normal() -> Weight;
 	fn transfer_creating() -> Weight;
@@ -133,17 +134,22 @@
 			.saturating_add(T::DbWeight::get().writes(6 as Weight))
 	}
 
-	fn set_collection_properties(amount: u32) -> Weight {
+	fn set_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
+	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_token_properties(amount: u32) -> Weight {
+	fn delete_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_property_permissions(amount: u32) -> Weight {
+	fn set_property_permissions(_amount: u32) -> Weight {
 		// Error
 		0
 	}
@@ -317,17 +323,22 @@
 			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
 	}
 
-	fn set_collection_properties(amount: u32) -> Weight {
+	fn set_collection_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_token_properties(amount: u32) -> Weight {
+	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
 	}
 
-	fn set_property_permissions(amount: u32) -> Weight {
+	fn delete_token_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
+	fn set_property_permissions(_amount: u32) -> Weight {
 		// Error
 		0
 	}
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -39,7 +39,7 @@
 	MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
 	AccessMode, CreateItemData, CollectionLimits, CollectionId, CollectionMode, TokenId,
 	SchemaVersion, SponsorshipState, MetaUpdatePermission, CreateCollectionData, CustomDataLimit,
-	CreateItemExData, budget, CollectionField, Property, PropertyKeyPermission,
+	CreateItemExData, budget, CollectionField, Property, PropertyKey, PropertyKeyPermission,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_common::{
@@ -723,6 +723,21 @@
 			dispatch_call::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))
 		}
 
+		#[weight = T::CommonWeightInfo::delete_token_properties(properties.len() as u32)]
+		#[transactional]
+		pub fn delete_token_properties(
+			origin,
+			collection_id: CollectionId,
+			token_id: TokenId,
+			properties: Vec<PropertyKey>
+		) -> DispatchResultWithPostInfo {
+			ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
+
+			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+			dispatch_call::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, properties))
+		}
+
 		#[weight = T::CommonWeightInfo::set_property_permissions(property_permissions.len() as u32)]
 		#[transactional]
 		pub fn set_property_permissions(
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -720,6 +720,17 @@
 		Ok(())
 	}
 
+	pub fn remove_property(&mut self, key: &PropertyKey) {
+		let property = self.map.get(key);
+
+		if let Some(value) = property {
+			let value_len = value.len() as u32;
+
+			self.map.remove(key);
+			self.consumed_space -= value_len;
+		}
+	}
+
 	pub fn get_property(&self, key: &PropertyKey) -> Option<&PropertyValue> {
 		self.map.get(key)
 	}
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -16,9 +16,7 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
-use up_data_structs::{
-	CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,
-};
+use up_data_structs::{CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits};
 use sp_std::vec::Vec;
 use codec::Decode;
 use sp_runtime::DispatchError;
modifiedruntime/common/src/weights.rsdiffbeforeafterboth
--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -62,6 +62,10 @@
 		dispatch_weight::<T>() + max_weight_of!(set_token_properties(amount))
 	}
 
+	fn delete_token_properties(amount: u32) -> Weight {
+		dispatch_weight::<T>() + max_weight_of!(delete_token_properties(amount))
+	}
+
 	fn set_property_permissions(amount: u32) -> Weight {
 		dispatch_weight::<T>() + max_weight_of!(set_property_permissions(amount))
 	}