git.delta.rocks / unique-network / refs/commits / 08e7d2834a5c

difftreelog

feat set/get tokenPropertyPermissions for NFT

Trubnikov Sergey2022-12-14parent: #bf0e967.patch.diff
in: master

5 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -34,7 +34,7 @@
 	CollectionPropertiesVec,
 };
 use pallet_evm_coder_substrate::dispatch_to_evm;
-use sp_std::vec::Vec;
+use sp_std::{vec::Vec, vec};
 use pallet_common::{
 	CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,
 	erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},
@@ -70,10 +70,10 @@
 		token_owner: bool,
 	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
-		<Pallet<T>>::set_property_permissions(
+		<Pallet<T>>::set_token_property_permissions(
 			self,
 			&caller,
-			[PropertyKeyPermission {
+			vec![PropertyKeyPermission {
 				key: <Vec<u8>>::from(key)
 					.try_into()
 					.map_err(|_| "too long key")?,
@@ -82,8 +82,7 @@
 					collection_admin,
 					token_owner,
 				},
-			}]
-			.into(),
+			}],
 		)
 		.map_err(dispatch_to_evm::<T>)
 	}
@@ -137,7 +136,8 @@
 			});
 		}
 
-		<Pallet<T>>::set_property_permissions(self, &caller, perms).map_err(dispatch_to_evm::<T>)
+		<Pallet<T>>::set_token_property_permissions(self, &caller, perms)
+			.map_err(dispatch_to_evm::<T>)
 	}
 
 	fn token_property_permissions(
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -824,17 +824,6 @@
 		)
 	}
 
-	/// Set property permissions for the collection.
-	///
-	/// Sender should be the owner or admin of the collection.
-	pub fn set_property_permissions(
-		collection: &CollectionHandle<T>,
-		sender: &T::CrossAccountId,
-		permission: Vec<PropertyKeyPermission>,
-	) -> DispatchResult {
-		<PalletCommon<T>>::set_token_property_permissions(collection, sender, permission)
-	}
-
 	pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {
 		<PalletCommon<T>>::property_permissions(collection_id)
 	}
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
33use pallet_common::{33use pallet_common::{
34 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,34 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,
35 erc::{CommonEvmHandler, CollectionCall, static_property::key},35 erc::{CommonEvmHandler, CollectionCall, static_property::key},
36 eth::EthCrossAccount,36 eth::{EthCrossAccount, EthTokenPermissions},
37 Error as CommonError,37 Error as CommonError,
38};38};
39use pallet_evm::{account::CrossAccountId, PrecompileHandle};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};
63 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.63 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.
64 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.64 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.
65 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]65 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]
66 #[solidity(hide)]
66 fn set_token_property_permission(67 fn set_token_property_permission(
67 &mut self,68 &mut self,
68 caller: caller,69 caller: caller,
89 .map_err(dispatch_to_evm::<T>)90 .map_err(dispatch_to_evm::<T>)
90 }91 }
92
93 /// @notice Set permissions for token property.
94 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.
95 /// @param permissions Permissions for keys.
96 fn set_token_property_permissions(
97 &mut self,
98 caller: caller,
99 permissions: Vec<(string, Vec<(EthTokenPermissions, bool)>)>,
100 ) -> Result<()> {
101 let caller = T::CrossAccountId::from_eth(caller);
102 const PERMISSIONS_FIELDS_COUNT: usize = 3;
103
104 let mut perms = <Vec<_>>::new();
105
106 for (key, pp) in permissions {
107 if pp.len() > PERMISSIONS_FIELDS_COUNT {
108 return Err(alloc::format!(
109 "Actual number of fields {} for {}, which exceeds the maximum value of {}",
110 pp.len(),
111 stringify!(EthTokenPermissions),
112 PERMISSIONS_FIELDS_COUNT
113 )
114 .as_str()
115 .into());
116 }
117
118 let mut token_permission = PropertyPermission {
119 mutable: false,
120 collection_admin: false,
121 token_owner: false,
122 };
123
124 for (perm, value) in pp {
125 match perm {
126 EthTokenPermissions::Mutable => token_permission.mutable = value,
127 EthTokenPermissions::TokenOwner => token_permission.token_owner = value,
128 EthTokenPermissions::CollectionAdmin => {
129 token_permission.collection_admin = value
130 }
131 }
132 }
133
134 perms.push(PropertyKeyPermission {
135 key: <Vec<u8>>::from(key)
136 .try_into()
137 .map_err(|_| "too long key")?,
138 permission: token_permission,
139 });
140 }
141
142 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)
143 .map_err(dispatch_to_evm::<T>)
144 }
145
146 fn token_property_permissions(
147 &self,
148 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {
149 let mut res = <Vec<_>>::new();
150 for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {
151 let key = string::from_utf8(key.into_inner()).unwrap();
152 let pp = [
153 (EthTokenPermissions::Mutable, pp.mutable),
154 (EthTokenPermissions::TokenOwner, pp.token_owner),
155 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),
156 ]
157 .into();
158 res.push((key, pp));
159 }
160 Ok(res)
161 }
91162
92 /// @notice Set token property value.163 /// @notice Set token property value.
93 /// @dev Throws error if `msg.sender` has no permission to edit the property.164 /// @dev Throws error if `msg.sender` has no permission to edit the property.
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -113,7 +113,7 @@
 	AccessMode, budget::Budget, CollectionId, CollectionFlags, CollectionPropertiesVec,
 	CreateCollectionData, CustomDataLimit, mapping::TokenAddressMapping, MAX_ITEMS_PER_BATCH,
 	MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyPermission,
-	PropertyScope, PropertyValue, TokenId, TrySetProperty,
+	PropertyScope, PropertyValue, TokenId, TrySetProperty, PropertiesPermissionMap,
 };
 
 pub use pallet::*;
@@ -1378,6 +1378,10 @@
 		<PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)
 	}
 
+	pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {
+		<PalletCommon<T>>::property_permissions(collection_id)
+	}
+
 	pub fn set_scoped_token_property_permissions(
 		collection: &RefungibleHandle<T>,
 		sender: &T::CrossAccountId,
modifiedtests/src/eth/tokenProperties.test.tsdiffbeforeafterboth
--- a/tests/src/eth/tokenProperties.test.ts
+++ b/tests/src/eth/tokenProperties.test.ts
@@ -32,7 +32,7 @@
     });
   });
 
-  itEth.only('Can be reconfigured', async({helper}) => {
+  itEth('Can be reconfigured', async({helper}) => {
     const caller = await helper.eth.createAccountWithBalance(donor);
     for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {
       const collection = await helper.nft.mintCollection(alice);