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

difftreelog

fix use OptionQuery for TokenProperties

Daniel Shiposha2023-10-02parent: #5ef5e6b.patch.diff
in: master

9 files changed

modifiedpallets/balances-adapter/src/common.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/common.rs
+++ b/pallets/balances-adapter/src/common.rs
@@ -172,18 +172,16 @@
 		fail!(<pallet_common::Error<T>>::UnsupportedOperation);
 	}
 
-	fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties {
+	fn get_token_properties_raw(
+		&self,
+		_token_id: TokenId,
+	) -> Option<up_data_structs::TokenProperties> {
 		// No token properties are defined on fungibles
-		up_data_structs::TokenProperties::new()
+		None
 	}
 
-	fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
-		// No token properties are defined on fungibles
-	}
-
-	fn properties_exist(&self, _token: TokenId) -> bool {
+	fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
 		// No token properties are defined on fungibles
-		false
 	}
 
 	fn set_token_property_permissions(
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
2098 /// Get token properties raw map.2098 /// Get token properties raw map.
2099 ///2099 ///
2100 /// * `token_id` - The token which properties are needed.2100 /// * `token_id` - The token which properties are needed.
2101 fn get_token_properties_map(&self, token_id: TokenId) -> TokenProperties;2101 fn get_token_properties_raw(&self, token_id: TokenId) -> Option<TokenProperties>;
21022102
2103 /// Set token properties raw map.2103 /// Set token properties raw map.
2104 ///2104 ///
2105 /// * `token_id` - The token for which the properties are being set.2105 /// * `token_id` - The token for which the properties are being set.
2106 /// * `map` - The raw map containing the token's properties.2106 /// * `map` - The raw map containing the token's properties.
2107 fn set_token_properties_map(&self, token_id: TokenId, map: TokenProperties);2107 fn set_token_properties_raw(&self, token_id: TokenId, map: TokenProperties);
2108
2109 /// Whether the given token has properties.
2110 ///
2111 /// * `token_id` - The token in question.
2112 fn properties_exist(&self, token: TokenId) -> bool;
21132108
2114 /// Set token property permissions.2109 /// Set token property permissions.
2115 ///2110 ///
2590 <PalletEvm<T>>::deposit_log(log);2585 <PalletEvm<T>>::deposit_log(log);
25912586
2592 self.collection2587 self.collection
2593 .set_token_properties_map(token_id, stored_properties.into_inner());2588 .set_token_properties_raw(token_id, stored_properties.into_inner());
2594 }2589 }
25952590
2596 Ok(())2591 Ok(())
2624 true2619 true
2625 },2620 },
2626 get_properties: |token_id| {2621 get_properties: |token_id| {
2627 debug_assert!(!collection.properties_exist(token_id));2622 debug_assert!(collection.get_token_properties_raw(token_id).is_none());
2628 TokenProperties::new()2623 TokenProperties::new()
2629 },2624 },
2630 _phantom: PhantomData,2625 _phantom: PhantomData,
2686 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2681 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),
2687 property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),2682 property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),
2688 check_token_exist: |token_id| collection.token_exists(token_id),2683 check_token_exist: |token_id| collection.token_exists(token_id),
2689 get_properties: |token_id| collection.get_token_properties_map(token_id),2684 get_properties: |token_id| {
2685 collection
2686 .get_token_properties_raw(token_id)
2687 .unwrap_or_default()
2688 },
2690 _phantom: PhantomData,2689 _phantom: PhantomData,
2691 }2690 }
2692}2691}
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -364,18 +364,16 @@
 		fail!(<Error<T>>::SettingPropertiesNotAllowed)
 	}
 
-	fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties {
+	fn get_token_properties_raw(
+		&self,
+		_token_id: TokenId,
+	) -> Option<up_data_structs::TokenProperties> {
 		// No token properties are defined on fungibles
-		up_data_structs::TokenProperties::new()
+		None
 	}
 
-	fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
-		// No token properties are defined on fungibles
-	}
-
-	fn properties_exist(&self, _token: TokenId) -> bool {
+	fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
 		// No token properties are defined on fungibles
-		false
 	}
 
 	fn check_nesting(
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -265,12 +265,15 @@
 		)
 	}
 
-	fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties {
+	fn get_token_properties_raw(
+		&self,
+		token_id: TokenId,
+	) -> Option<up_data_structs::TokenProperties> {
 		<TokenProperties<T>>::get((self.id, token_id))
 	}
 
-	fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
-		<TokenProperties<T>>::set((self.id, token_id), map)
+	fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
+		<TokenProperties<T>>::insert((self.id, token_id), map)
 	}
 
 	fn set_token_property_permissions(
@@ -287,10 +290,6 @@
 		)
 	}
 
-	fn properties_exist(&self, token: TokenId) -> bool {
-		<TokenProperties<T>>::contains_key((self.id, token))
-	}
-
 	fn burn_item(
 		&self,
 		sender: T::CrossAccountId,
@@ -482,13 +481,15 @@
 	}
 
 	fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {
-		<Pallet<T>>::token_properties((self.id, token_id))
+		<Pallet<T>>::token_properties((self.id, token_id))?
 			.get(key)
 			.cloned()
 	}
 
 	fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {
-		let properties = <Pallet<T>>::token_properties((self.id, token_id));
+		let Some(properties) = <Pallet<T>>::token_properties((self.id, token_id)) else {
+			return vec![];
+		};
 
 		keys.map(|keys| {
 			keys.into_iter()
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -272,7 +272,8 @@
 			.try_into()
 			.map_err(|_| "key too long")?;
 
-		let props = <TokenProperties<T>>::get((self.id, token_id));
+		let props =
+			<TokenProperties<T>>::get((self.id, token_id)).ok_or("Token properties not found")?;
 		let prop = props.get(&key).ok_or("key not found")?;
 
 		Ok(prop.to_vec().into())
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -102,8 +102,8 @@
 use up_data_structs::{
 	AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
 	mapping::TokenAddressMapping, budget::Budget, Property, PropertyKey, PropertyValue,
-	PropertyKeyPermission, PropertyScope, TrySetProperty, TokenChild, AuxPropertyValue,
-	PropertiesPermissionMap, TokenProperties as TokenPropertiesT,
+	PropertyKeyPermission, PropertyScope, TokenChild, AuxPropertyValue, PropertiesPermissionMap,
+	TokenProperties as TokenPropertiesT,
 };
 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
 use pallet_common::{
@@ -201,7 +201,7 @@
 	pub type TokenProperties<T: Config> = StorageNMap<
 		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
 		Value = TokenPropertiesT,
-		QueryKind = ValueQuery,
+		QueryKind = OptionQuery,
 	>;
 
 	/// Custom data of a token that is serialized to bytes,
@@ -340,40 +340,8 @@
 	/// - `token`: Token ID.
 	pub fn token_exists(collection: &NonfungibleHandle<T>, token: TokenId) -> bool {
 		<TokenData<T>>::contains_key((collection.id, token))
-	}
-
-	/// Set the token property with the scope.
-	///
-	/// - `property`: Contains key-value pair.
-	pub fn set_scoped_token_property(
-		collection_id: CollectionId,
-		token_id: TokenId,
-		scope: PropertyScope,
-		property: Property,
-	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {
-			properties.try_scoped_set(scope, property.key, property.value)
-		})
-		.map_err(<CommonError<T>>::from)?;
-
-		Ok(())
 	}
 
-	/// Batch operation to set multiple properties with the same scope.
-	pub fn set_scoped_token_properties(
-		collection_id: CollectionId,
-		token_id: TokenId,
-		scope: PropertyScope,
-		properties: impl Iterator<Item = Property>,
-	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
-			stored_properties.try_scoped_set_from_iter(scope, properties)
-		})
-		.map_err(<CommonError<T>>::from)?;
-
-		Ok(())
-	}
-
 	/// Add or edit auxiliary data for the property.
 	///
 	/// - `f`: function that adds or edits auxiliary data.
@@ -1394,7 +1362,9 @@
 
 	pub fn repair_item(collection: &NonfungibleHandle<T>, token: TokenId) -> DispatchResult {
 		<TokenProperties<T>>::mutate((collection.id, token), |properties| {
-			properties.recompute_consumed_space();
+			if let Some(properties) = properties {
+				properties.recompute_consumed_space();
+			}
 		});
 
 		Ok(())
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -435,16 +435,15 @@
 		)
 	}
 
-	fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties {
+	fn get_token_properties_raw(
+		&self,
+		token_id: TokenId,
+	) -> Option<up_data_structs::TokenProperties> {
 		<TokenProperties<T>>::get((self.id, token_id))
 	}
 
-	fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
-		<TokenProperties<T>>::set((self.id, token_id), map)
-	}
-
-	fn properties_exist(&self, token: TokenId) -> bool {
-		<TokenProperties<T>>::contains_key((self.id, token))
+	fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
+		<TokenProperties<T>>::insert((self.id, token_id), map)
 	}
 
 	fn check_nesting(
@@ -514,13 +513,15 @@
 	}
 
 	fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {
-		<Pallet<T>>::token_properties((self.id, token_id))
+		<Pallet<T>>::token_properties((self.id, token_id))?
 			.get(key)
 			.cloned()
 	}
 
 	fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {
-		let properties = <Pallet<T>>::token_properties((self.id, token_id));
+		let Some(properties) = <Pallet<T>>::token_properties((self.id, token_id)) else {
+			return vec![];
+		};
 
 		keys.map(|keys| {
 			keys.into_iter()
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -283,7 +283,8 @@
 			.try_into()
 			.map_err(|_| "key too long")?;
 
-		let props = <TokenProperties<T>>::get((self.id, token_id));
+		let props =
+			<TokenProperties<T>>::get((self.id, token_id)).ok_or("Token properties not found")?;
 		let prop = props.get(&key).ok_or("key not found")?;
 
 		Ok(prop.to_vec().into())
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -106,8 +106,8 @@
 use up_data_structs::{
 	AccessMode, budget::Budget, CollectionId, CreateCollectionData, mapping::TokenAddressMapping,
 	MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyScope,
-	PropertyValue, TokenId, TrySetProperty, PropertiesPermissionMap,
-	CreateRefungibleExMultipleOwners, TokenOwnerError, TokenProperties as TokenPropertiesT,
+	PropertyValue, TokenId, PropertiesPermissionMap, CreateRefungibleExMultipleOwners,
+	TokenOwnerError, TokenProperties as TokenPropertiesT,
 };
 
 pub use pallet::*;
@@ -175,7 +175,7 @@
 	pub type TokenProperties<T: Config> = StorageNMap<
 		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
 		Value = TokenPropertiesT,
-		QueryKind = ValueQuery,
+		QueryKind = OptionQuery,
 	>;
 
 	/// Total amount of pieces for token
@@ -292,35 +292,7 @@
 	/// - `token`: Token ID.
 	pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {
 		<TotalSupply<T>>::contains_key((collection.id, token))
-	}
-
-	pub fn set_scoped_token_property(
-		collection_id: CollectionId,
-		token_id: TokenId,
-		scope: PropertyScope,
-		property: Property,
-	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {
-			properties.try_scoped_set(scope, property.key, property.value)
-		})
-		.map_err(<CommonError<T>>::from)?;
-
-		Ok(())
 	}
-
-	pub fn set_scoped_token_properties(
-		collection_id: CollectionId,
-		token_id: TokenId,
-		scope: PropertyScope,
-		properties: impl Iterator<Item = Property>,
-	) -> DispatchResult {
-		TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
-			stored_properties.try_scoped_set_from_iter(scope, properties)
-		})
-		.map_err(<CommonError<T>>::from)?;
-
-		Ok(())
-	}
 }
 
 // unchecked calls skips any permission checks
@@ -1426,7 +1398,9 @@
 
 	pub fn repair_item(collection: &RefungibleHandle<T>, token: TokenId) -> DispatchResult {
 		<TokenProperties<T>>::mutate((collection.id, token), |properties| {
-			properties.recompute_consumed_space();
+			if let Some(properties) = properties {
+				properties.recompute_consumed_space();
+			}
 		});
 
 		Ok(())