git.delta.rocks / unique-network / refs/commits / 117d4ce3143f

difftreelog

Add change_property_permissions

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

10 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -36,7 +36,7 @@
 	CUSTOM_DATA_LIMIT, CollectionLimits, CustomDataLimit, CreateCollectionData, SponsorshipState,
 	CreateItemExData, SponsoringRateLimit, budget::Budget, COLLECTION_FIELD_LIMIT, CollectionField,
 	PhantomType, Property, Properties, PropertiesPermissionMap, PropertyKey, PropertyPermission,
-	PropertiesError,
+	PropertiesError, PropertyKeyPermission,
 };
 pub use pallet::*;
 use sp_core::H160;
@@ -293,6 +293,8 @@
 		CollectionPropertySet(CollectionId, Property),
 
 		TokenPropertySet(CollectionId, TokenId, Property),
+
+		PropertyPermissionSet(CollectionId, PropertyKeyPermission),
 	}
 
 	#[pallet::error]
@@ -741,7 +743,7 @@
 			|properties| properties.try_change_property(property.clone())
 		)?;
 
-		<Pallet<T>>::deposit_event(Event::CollectionPropertySet(collection.id, property));
+		Self::deposit_event(Event::CollectionPropertySet(collection.id, property));
 
 		Ok(())
 	}
@@ -761,19 +763,39 @@
 	pub fn change_property_permission(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
-		property_key: PropertyKey,
-		permission: PropertyPermission,
+		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)] {
+			return Err(<Error<T>>::NoPermission.into());
+		}
+
 		CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {
-			permissions.try_insert(property_key, permission)
+			let property_permission = property_permission.clone();
+			permissions.try_insert(property_permission.key, property_permission.permission)
 		})
 		.map_err(|_| PropertiesError::PropertyLimitReached)?;
 
+		Self::deposit_event(Event::PropertyPermissionSet(collection.id, property_permission));
+
 		Ok(())
 	}
 
+	pub fn change_property_permissions(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		property_permissions: Vec<PropertyKeyPermission>
+	) -> DispatchResult {
+		for prop_pemission in property_permissions {
+			Self::change_property_permission(collection, sender, prop_pemission)?;
+		}
+
+		Ok(())
+	}
+
 	fn set_field_raw(
 		collection_id: CollectionId,
 		field: CollectionField,
@@ -928,6 +950,7 @@
 	fn burn_item() -> Weight;
 	fn change_collection_properties(amount: u32) -> Weight;
 	fn change_token_properties(amount: u32) -> Weight;
+	fn change_property_permissions(amount: u32) -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
 	fn transfer_from() -> Weight;
@@ -962,20 +985,22 @@
 		token: TokenId,
 		amount: u128,
 	) -> DispatchResultWithPostInfo;
-
 	fn change_collection_properties(
 		&self,
 		sender: T::CrossAccountId,
 		properties: Vec<Property>,
 	) -> DispatchResultWithPostInfo;
-
 	fn change_token_properties(
 		&self,
 		sender: T::CrossAccountId,
 		token_id: TokenId,
 		property: Vec<Property>,
 	) -> DispatchResultWithPostInfo;
-
+	fn change_property_permissions(
+		&self,
+		sender: &T::CrossAccountId,
+		property_permissions: Vec<PropertyKeyPermission>,
+	) -> DispatchResultWithPostInfo;
 	fn transfer(
 		&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};
+use up_data_structs::{CustomDataLimit, Property, PropertyKeyPermission,};
 
 use crate::{
 	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,
@@ -58,6 +58,10 @@
 		<SelfWeightOf<T>>::change_token_properties(amount)
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::change_property_permissions(amount)
+	}
+
 	fn transfer() -> Weight {
 		<SelfWeightOf<T>>::transfer()
 	}
@@ -250,6 +254,14 @@
 		fail!(<Error<T>>::PropertiesNotAllowed)
 	}
 
+	fn change_property_permissions(
+		&self,
+		_sender: &T::CrossAccountId,
+		_property_permissions: Vec<PropertyKeyPermission>,
+	) -> DispatchResultWithPostInfo {
+		fail!(<Error<T>>::PropertiesNotAllowed)
+	}
+
 	fn set_variable_metadata(
 		&self,
 		_sender: T::CrossAccountId,
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
37 fn burn_item() -> Weight;37 fn burn_item() -> Weight;
38 fn change_collection_properties(amount: u32) -> Weight;38 fn change_collection_properties(amount: u32) -> Weight;
39 fn change_token_properties(amount: u32) -> Weight;39 fn change_token_properties(amount: u32) -> Weight;
40 fn change_property_permissions(amount: u32) -> Weight;
40 fn transfer() -> Weight;41 fn transfer() -> Weight;
41 fn approve() -> Weight;42 fn approve() -> Weight;
42 fn transfer_from() -> Weight;43 fn transfer_from() -> Weight;
82 083 0
83 }84 }
85
86 fn change_property_permissions(amount: u32) -> Weight {
87 // Error
88 0
89 }
8490
85 // Storage: Fungible Balance (r:2 w:2)91 // Storage: Fungible Balance (r:2 w:2)
86 fn transfer() -> Weight {92 fn transfer() -> Weight {
150 0156 0
151 }157 }
158
159 fn change_property_permissions(amount: u32) -> Weight {
160 // Error
161 0
162 }
152163
153 // Storage: Fungible Balance (r:2 w:2)164 // Storage: Fungible Balance (r:2 w:2)
154 fn transfer() -> Weight {165 fn transfer() -> Weight {
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -18,7 +18,7 @@
 
 use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
 use up_data_structs::{
-	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,
+	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property, PropertyKeyPermission,
 };
 use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
 use sp_runtime::DispatchError;
@@ -58,6 +58,10 @@
 		<SelfWeightOf<T>>::change_token_properties(amount)
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::change_property_permissions(amount)
+	}
+
 	fn transfer() -> Weight {
 		<SelfWeightOf<T>>::transfer()
 	}
@@ -176,6 +180,19 @@
 		)
 	}
 
+	fn change_property_permissions(
+		&self,
+		sender: &T::CrossAccountId,
+		property_permissions: Vec<PropertyKeyPermission>,
+	) -> DispatchResultWithPostInfo {
+		let weight = <CommonWeights<T>>::change_property_permissions(property_permissions.len() as u32);
+
+		with_weight(
+			<Pallet<T>>::change_property_permissions(self, sender, property_permissions),
+			weight
+		)
+	}
+
 	fn burn_item(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -21,6 +21,7 @@
 use up_data_structs::{
 	AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
 	mapping::TokenAddressMapping, NestingRule, budget::Budget, Property, PropertyPermission,
+	PropertyKeyPermission,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_common::{
@@ -324,6 +325,18 @@
 		<PalletCommon<T>>::change_collection_properties(collection, sender, properties)
 	}
 
+	pub fn change_property_permissions(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		property_permissions: Vec<PropertyKeyPermission>
+	) -> DispatchResult {
+		<PalletCommon<T>>::change_property_permissions(
+			collection,
+			sender,
+			property_permissions,
+		)
+	}
+
 	pub fn transfer(
 		collection: &NonfungibleHandle<T>,
 		from: &T::CrossAccountId,
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 change_collection_properties(amount: u32) -> Weight;
 	fn change_token_properties(amount: u32) -> Weight;
+	fn change_property_permissions(amount: u32) -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
 	fn transfer_from() -> Weight;
@@ -103,6 +104,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	// Storage: Nonfungible TokenData (r:1 w:1)
 	// Storage: Nonfungible AccountBalance (r:2 w:2)
 	// Storage: Nonfungible Allowance (r:1 w:0)
@@ -203,6 +209,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	// Storage: Nonfungible TokenData (r:1 w:1)
 	// Storage: Nonfungible AccountBalance (r:2 w:2)
 	// Storage: Nonfungible Allowance (r:1 w:0)
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -20,7 +20,7 @@
 use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};
 use up_data_structs::{
 	CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,
-	budget::Budget, Property,
+	budget::Budget, Property, PropertyKeyPermission,
 };
 use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
 use sp_runtime::DispatchError;
@@ -74,6 +74,10 @@
 		<SelfWeightOf<T>>::change_token_properties(amount)
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::change_property_permissions(amount)
+	}
+
 	fn transfer() -> Weight {
 		max_weight_of!(
 			transfer_normal(),
@@ -269,6 +273,14 @@
 		fail!(<Error<T>>::PropertiesNotAllowed)
 	}
 
+	fn change_property_permissions(
+		&self,
+		_sender: &T::CrossAccountId,
+		_property_permissions: Vec<PropertyKeyPermission>,
+	) -> DispatchResultWithPostInfo {
+		fail!(<Error<T>>::PropertiesNotAllowed)
+	}
+
 	fn set_variable_metadata(
 		&self,
 		sender: T::CrossAccountId,
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 change_collection_properties(amount: u32) -> Weight;
 	fn change_token_properties(amount: u32) -> Weight;
+	fn change_property_permissions(amount: u32) -> Weight;
 	fn transfer_normal() -> Weight;
 	fn transfer_creating() -> Weight;
 	fn transfer_removing() -> Weight;
@@ -142,6 +143,11 @@
 		0
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_normal() -> Weight {
 		(19_766_000 as Weight)
@@ -321,6 +327,11 @@
 		0
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	// Storage: Refungible Balance (r:2 w:2)
 	fn transfer_normal() -> Weight {
 		(19_766_000 as Weight)
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,
+	CreateItemExData, budget, CollectionField, Property, PropertyKeyPermission,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_common::{
@@ -723,6 +723,20 @@
 			dispatch_call::<T, _>(collection_id, |d| d.change_token_properties(sender, token_id, properties))
 		}
 
+		#[weight = T::CommonWeightInfo::change_property_permissions(property_permissions.len() as u32)]
+		#[transactional]
+		pub fn change_property_permissions(
+			origin,
+			collection_id: CollectionId,
+			property_permissions: Vec<PropertyKeyPermission>,
+		) -> DispatchResultWithPostInfo {
+			ensure!(!property_permissions.is_empty(), Error::<T>::EmptyArgument);
+
+			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+			dispatch_call::<T, _>(collection_id, |d| d.change_property_permissions(&sender, property_permissions))
+		}
+
 		#[weight = T::CommonWeightInfo::create_multiple_items_ex(&data)]
 		#[transactional]
 		pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {
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!(change_token_properties(amount))
 	}
 
+	fn change_property_permissions(amount: u32) -> Weight {
+		dispatch_weight::<T>() + max_weight_of!(change_property_permissions(amount))
+	}
+
 	fn transfer() -> Weight {
 		dispatch_weight::<T>() + max_weight_of!(transfer())
 	}