From 117d4ce3143f5cba3c0511c0b36ae0aeec64f981 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Wed, 04 May 2022 10:22:50 +0000 Subject: [PATCH] Add change_property_permissions --- --- 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()) )?; - >::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, sender: &T::CrossAccountId, - property_key: PropertyKey, - permission: PropertyPermission, + property_permission: PropertyKeyPermission ) -> DispatchResult { collection.check_is_owner_or_admin(sender)?; + let all_permissions = CollectionPropertyPermissions::::get(collection.id); + let current_permission = all_permissions.get(&property_permission.key); + if matches![current_permission, Some(PropertyPermission::AdminConst | PropertyPermission::ItemOwnerConst)] { + return Err(>::NoPermission.into()); + } + CollectionPropertyPermissions::::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, + sender: &T::CrossAccountId, + property_permissions: Vec + ) -> 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, ) -> DispatchResultWithPostInfo; - fn change_token_properties( &self, sender: T::CrossAccountId, token_id: TokenId, property: Vec, ) -> DispatchResultWithPostInfo; - + fn change_property_permissions( + &self, + sender: &T::CrossAccountId, + property_permissions: Vec, + ) -> DispatchResultWithPostInfo; fn transfer( &self, sender: T::CrossAccountId, --- 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 @@ >::change_token_properties(amount) } + fn change_property_permissions(amount: u32) -> Weight { + >::change_property_permissions(amount) + } + fn transfer() -> Weight { >::transfer() } @@ -250,6 +254,14 @@ fail!(>::PropertiesNotAllowed) } + fn change_property_permissions( + &self, + _sender: &T::CrossAccountId, + _property_permissions: Vec, + ) -> DispatchResultWithPostInfo { + fail!(>::PropertiesNotAllowed) + } + fn set_variable_metadata( &self, _sender: T::CrossAccountId, --- a/pallets/fungible/src/weights.rs +++ b/pallets/fungible/src/weights.rs @@ -37,6 +37,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; @@ -82,6 +83,11 @@ 0 } + fn change_property_permissions(amount: u32) -> Weight { + // Error + 0 + } + // Storage: Fungible Balance (r:2 w:2) fn transfer() -> Weight { (17_713_000 as Weight) @@ -150,6 +156,11 @@ 0 } + fn change_property_permissions(amount: u32) -> Weight { + // Error + 0 + } + // Storage: Fungible Balance (r:2 w:2) fn transfer() -> Weight { (17_713_000 as Weight) --- 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 @@ >::change_token_properties(amount) } + fn change_property_permissions(amount: u32) -> Weight { + >::change_property_permissions(amount) + } + fn transfer() -> Weight { >::transfer() } @@ -176,6 +180,19 @@ ) } + fn change_property_permissions( + &self, + sender: &T::CrossAccountId, + property_permissions: Vec, + ) -> DispatchResultWithPostInfo { + let weight = >::change_property_permissions(property_permissions.len() as u32); + + with_weight( + >::change_property_permissions(self, sender, property_permissions), + weight + ) + } + fn burn_item( &self, sender: T::CrossAccountId, --- 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 @@ >::change_collection_properties(collection, sender, properties) } + pub fn change_property_permissions( + collection: &CollectionHandle, + sender: &T::CrossAccountId, + property_permissions: Vec + ) -> DispatchResult { + >::change_property_permissions( + collection, + sender, + property_permissions, + ) + } + pub fn transfer( collection: &NonfungibleHandle, from: &T::CrossAccountId, --- 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) --- 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 @@ >::change_token_properties(amount) } + fn change_property_permissions(amount: u32) -> Weight { + >::change_property_permissions(amount) + } + fn transfer() -> Weight { max_weight_of!( transfer_normal(), @@ -269,6 +273,14 @@ fail!(>::PropertiesNotAllowed) } + fn change_property_permissions( + &self, + _sender: &T::CrossAccountId, + _property_permissions: Vec, + ) -> DispatchResultWithPostInfo { + fail!(>::PropertiesNotAllowed) + } + fn set_variable_metadata( &self, sender: T::CrossAccountId, --- 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) --- 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::(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, + ) -> DispatchResultWithPostInfo { + ensure!(!property_permissions.is_empty(), Error::::EmptyArgument); + + let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); + + dispatch_call::(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) -> DispatchResultWithPostInfo { --- a/runtime/common/src/weights.rs +++ b/runtime/common/src/weights.rs @@ -62,6 +62,10 @@ dispatch_weight::() + max_weight_of!(change_token_properties(amount)) } + fn change_property_permissions(amount: u32) -> Weight { + dispatch_weight::() + max_weight_of!(change_property_permissions(amount)) + } + fn transfer() -> Weight { dispatch_weight::() + max_weight_of!(transfer()) } -- gitstuff