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

difftreelog

fix bulk properties set

Daniel Shiposha2023-01-12parent: #46515fa.patch.diff
in: master

6 files changed

modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -125,7 +125,7 @@
 			.map(eth::Property::try_into)
 			.collect::<Result<Vec<_>>>()?;
 
-		<Pallet<T>>::set_collection_properties(self, &caller, properties)
+		<Pallet<T>>::set_collection_properties(self, &caller, properties.into_iter())
 			.map_err(dispatch_to_evm::<T>)
 	}
 
@@ -158,7 +158,8 @@
 			})
 			.collect::<Result<Vec<_>>>()?;
 
-		<Pallet<T>>::delete_collection_properties(self, &caller, keys).map_err(dispatch_to_evm::<T>)
+		<Pallet<T>>::delete_collection_properties(self, &caller, keys.into_iter())
+			.map_err(dispatch_to_evm::<T>)
 	}
 
 	/// Get collection property.
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1198,6 +1198,51 @@
 		Ok(())
 	}
 
+	fn modify_collection_properties(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
+	) -> DispatchResult {
+		collection.check_is_owner_or_admin(sender)?;
+
+		let mut stored_properties = <CollectionProperties<T>>::get(collection.id);
+
+		for (key, value) in properties_updates {
+			match value {
+				Some(value) => {
+					stored_properties
+						.try_set(key.clone(), value)
+						.map_err(<Error<T>>::from)?;
+
+					Self::deposit_event(Event::CollectionPropertySet(collection.id, key));
+					<PalletEvm<T>>::deposit_log(
+						erc::CollectionHelpersEvents::CollectionChanged {
+							collection_id: eth::collection_id_to_address(collection.id),
+						}
+						.to_log(T::ContractAddress::get()),
+					);
+				}
+				None => {
+					stored_properties.remove(&key).map_err(<Error<T>>::from)?;
+
+					Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, key));
+					<PalletEvm<T>>::deposit_log(
+						erc::CollectionHelpersEvents::CollectionChanged {
+							collection_id: eth::collection_id_to_address(collection.id),
+						}
+						.to_log(T::ContractAddress::get()),
+					);
+				}
+			}
+		}
+
+		<CollectionProperties<T>>::mutate(collection.id, |properties| {
+			*properties = stored_properties;
+		});
+
+		Ok(())
+	}
+
 	/// Set collection property.
 	///
 	/// * `collection` - Collection handler.
@@ -1208,23 +1253,7 @@
 		sender: &T::CrossAccountId,
 		property: Property,
 	) -> DispatchResult {
-		collection.check_is_owner_or_admin(sender)?;
-
-		CollectionProperties::<T>::try_mutate(collection.id, |properties| {
-			let property = property.clone();
-			properties.try_set(property.key, property.value)
-		})
-		.map_err(<Error<T>>::from)?;
-
-		Self::deposit_event(Event::CollectionPropertySet(collection.id, property.key));
-		<PalletEvm<T>>::deposit_log(
-			erc::CollectionHelpersEvents::CollectionChanged {
-				collection_id: eth::collection_id_to_address(collection.id),
-			}
-			.to_log(T::ContractAddress::get()),
-		);
-
-		Ok(())
+		Self::set_collection_properties(collection, sender, [property].into_iter())
 	}
 
 	/// Set a scoped collection property, where the scope is a special prefix
@@ -1274,13 +1303,13 @@
 	pub fn set_collection_properties(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		properties: Vec<Property>,
+		properties: impl Iterator<Item = Property>,
 	) -> DispatchResult {
-		for property in properties {
-			Self::set_collection_property(collection, sender, property)?;
-		}
-
-		Ok(())
+		Self::modify_collection_properties(
+			collection,
+			sender,
+			properties.map(|property| (property.key, Some(property.value))),
+		)
 	}
 
 	/// Delete collection property.
@@ -1293,25 +1322,7 @@
 		sender: &T::CrossAccountId,
 		property_key: PropertyKey,
 	) -> DispatchResult {
-		collection.check_is_owner_or_admin(sender)?;
-
-		CollectionProperties::<T>::try_mutate(collection.id, |properties| {
-			properties.remove(&property_key)
-		})
-		.map_err(<Error<T>>::from)?;
-
-		Self::deposit_event(Event::CollectionPropertyDeleted(
-			collection.id,
-			property_key,
-		));
-		<PalletEvm<T>>::deposit_log(
-			erc::CollectionHelpersEvents::CollectionChanged {
-				collection_id: eth::collection_id_to_address(collection.id),
-			}
-			.to_log(T::ContractAddress::get()),
-		);
-
-		Ok(())
+		Self::delete_collection_properties(collection, sender, [property_key].into_iter())
 	}
 
 	/// Delete collection properties.
@@ -1323,13 +1334,9 @@
 	pub fn delete_collection_properties(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		property_keys: Vec<PropertyKey>,
+		property_keys: impl Iterator<Item = PropertyKey>,
 	) -> DispatchResult {
-		for key in property_keys {
-			Self::delete_collection_property(collection, sender, key)?;
-		}
-
-		Ok(())
+		Self::modify_collection_properties(collection, sender, property_keys.map(|key| (key, None)))
 	}
 
 	/// Set collection propetry permission without any checks.
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -266,7 +266,7 @@
 		sender: &T::CrossAccountId,
 		properties: Vec<Property>,
 	) -> DispatchResult {
-		<PalletCommon<T>>::set_collection_properties(collection, sender, properties)
+		<PalletCommon<T>>::set_collection_properties(collection, sender, properties.into_iter())
 	}
 
 	/// Delete properties of the collection, associated with the provided keys.
@@ -275,7 +275,11 @@
 		sender: &T::CrossAccountId,
 		property_keys: Vec<PropertyKey>,
 	) -> DispatchResult {
-		<PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)
+		<PalletCommon<T>>::delete_collection_properties(
+			collection,
+			sender,
+			property_keys.into_iter(),
+		)
 	}
 
 	/// Checks if collection has tokens. Return `true` if it has.
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -590,7 +590,7 @@
 		collection: &NonfungibleHandle<T>,
 		sender: &T::CrossAccountId,
 		token_id: TokenId,
-		properties: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
+		properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
 		is_token_create: bool,
 		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
@@ -614,15 +614,16 @@
 			})
 		};
 
-		for (key, value) in properties {
-			let permission = <PalletCommon<T>>::property_permissions(collection.id)
+		let mut stored_properties = <TokenProperties<T>>::get((collection.id, token_id));
+		let permissions = <PalletCommon<T>>::property_permissions(collection.id);
+
+		for (key, value) in properties_updates {
+			let permission = permissions
 				.get(&key)
 				.cloned()
 				.unwrap_or_else(PropertyPermission::none);
 
-			let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
-				.get(&key)
-				.is_some();
+			let is_property_exists = stored_properties.get(&key).is_some();
 
 			match permission {
 				PropertyPermission { mutable: false, .. } if is_property_exists => {
@@ -649,10 +650,9 @@
 
 			match value {
 				Some(value) => {
-					<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
-						properties.try_set(key.clone(), value)
-					})
-					.map_err(<CommonError<T>>::from)?;
+					stored_properties
+						.try_set(key.clone(), value)
+						.map_err(<CommonError<T>>::from)?;
 
 					<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
 						collection.id,
@@ -661,10 +661,9 @@
 					));
 				}
 				None => {
-					<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
-						properties.remove(&key)
-					})
-					.map_err(<CommonError<T>>::from)?;
+					stored_properties
+						.remove(&key)
+						.map_err(<CommonError<T>>::from)?;
 
 					<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
 						collection.id,
@@ -683,6 +682,10 @@
 			);
 		}
 
+		<TokenProperties<T>>::mutate((collection.id, token_id), |properties| {
+			*properties = stored_properties;
+		});
+
 		Ok(())
 	}
 
@@ -784,7 +787,7 @@
 		sender: &T::CrossAccountId,
 		properties: Vec<Property>,
 	) -> DispatchResult {
-		<PalletCommon<T>>::set_collection_properties(collection, sender, properties)
+		<PalletCommon<T>>::set_collection_properties(collection, sender, properties.into_iter())
 	}
 
 	/// Remove properties from the collection
@@ -793,7 +796,11 @@
 		sender: &T::CrossAccountId,
 		property_keys: Vec<PropertyKey>,
 	) -> DispatchResult {
-		<PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)
+		<PalletCommon<T>>::delete_collection_properties(
+			collection,
+			sender,
+			property_keys.into_iter(),
+		)
 	}
 
 	/// Set property permissions for the token.
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
520 collection: &RefungibleHandle<T>,520 collection: &RefungibleHandle<T>,
521 sender: &T::CrossAccountId,521 sender: &T::CrossAccountId,
522 token_id: TokenId,522 token_id: TokenId,
523 properties: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,523 properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
524 is_token_create: bool,524 is_token_create: bool,
525 nesting_budget: &dyn Budget,525 nesting_budget: &dyn Budget,
526 ) -> DispatchResult {526 ) -> DispatchResult {
544 Ok(is_bundle_owner)544 Ok(is_bundle_owner)
545 };545 };
546
547 let mut stored_properties = <TokenProperties<T>>::get((collection.id, token_id));
548 let permissions = <PalletCommon<T>>::property_permissions(collection.id);
546549
547 for (key, value) in properties {550 for (key, value) in properties_updates {
548 let permission = <PalletCommon<T>>::property_permissions(collection.id)551 let permission = permissions
549 .get(&key)552 .get(&key)
550 .cloned()553 .cloned()
551 .unwrap_or_else(PropertyPermission::none);554 .unwrap_or_else(PropertyPermission::none);
552555
553 let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))556 let is_property_exists = stored_properties.get(&key).is_some();
554 .get(&key)
555 .is_some();
556557
578579
579 match value {580 match value {
580 Some(value) => {581 Some(value) => {
581 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {582 stored_properties
582 properties.try_set(key.clone(), value)583 .try_set(key.clone(), value)
583 })
584 .map_err(<CommonError<T>>::from)?;584 .map_err(<CommonError<T>>::from)?;
585585
586 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(586 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
590 ));590 ));
591 }591 }
592 None => {592 None => {
593 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {593 stored_properties
594 properties.remove(&key)594 .remove(&key)
595 })
596 .map_err(<CommonError<T>>::from)?;595 .map_err(<CommonError<T>>::from)?;
597596
598 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(597 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
612 );611 );
613 }612 }
613
614 <TokenProperties<T>>::mutate((collection.id, token_id), |properties| {
615 *properties = stored_properties;
616 });
614617
615 Ok(())618 Ok(())
616 }619 }
1353 sender: &T::CrossAccountId,1356 sender: &T::CrossAccountId,
1354 properties: Vec<Property>,1357 properties: Vec<Property>,
1355 ) -> DispatchResult {1358 ) -> DispatchResult {
1356 <PalletCommon<T>>::set_collection_properties(collection, sender, properties)1359 <PalletCommon<T>>::set_collection_properties(collection, sender, properties.into_iter())
1357 }1360 }
13581361
1359 pub fn delete_collection_properties(1362 pub fn delete_collection_properties(
1360 collection: &RefungibleHandle<T>,1363 collection: &RefungibleHandle<T>,
1361 sender: &T::CrossAccountId,1364 sender: &T::CrossAccountId,
1362 property_keys: Vec<PropertyKey>,1365 property_keys: Vec<PropertyKey>,
1363 ) -> DispatchResult {1366 ) -> DispatchResult {
1364 <PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)1367 <PalletCommon<T>>::delete_collection_properties(
1368 collection,
1369 sender,
1370 property_keys.into_iter(),
1371 )
1365 }1372 }
13661373
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -33,7 +33,6 @@
 };
 use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};
 use pallet_evm_coder_substrate::{dispatch_to_evm, SubstrateRecorder, WithRecorder};
-use sp_std::vec;
 use up_data_structs::{
 	CollectionDescription, CollectionMode, CollectionName, CollectionTokenPrefix,
 	CreateCollectionData,
@@ -316,13 +315,14 @@
 			<PalletCommon<T>>::set_collection_properties(
 				&collection,
 				&caller,
-				vec![up_data_structs::Property {
+				[up_data_structs::Property {
 					key: key::base_uri(),
 					value: base_uri
 						.into_bytes()
 						.try_into()
 						.map_err(|_| "base uri is too large")?,
-				}],
+				}]
+				.into_iter(),
 			)
 			.map_err(dispatch_to_evm::<T>)?;
 		}