git.delta.rocks / unique-network / refs/commits / 1fe93b2e72cb

difftreelog

fix PR

Trubnikov Sergey2022-12-16parent: #ea778c4.patch.diff
in: master

4 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -90,6 +90,7 @@
 	/// @notice Set permissions for token property.
 	/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
 	/// @param permissions Permissions for keys.
+	#[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]
 	fn set_token_property_permissions(
 		&mut self,
 		caller: caller,
@@ -98,7 +99,7 @@
 		let caller = T::CrossAccountId::from_eth(caller);
 		const PERMISSIONS_FIELDS_COUNT: usize = 3;
 
-		let mut perms = <Vec<_>>::new();
+		let mut perms = Vec::new();
 
 		for (key, pp) in permissions {
 			if pp.len() > PERMISSIONS_FIELDS_COUNT {
@@ -112,11 +113,7 @@
 				.into());
 			}
 
-			let mut token_permission = PropertyPermission {
-				mutable: false,
-				collection_admin: false,
-				token_owner: false,
-			};
+			let mut token_permission = PropertyPermission::default();
 
 			for (perm, value) in pp {
 				match perm {
@@ -129,9 +126,7 @@
 			}
 
 			perms.push(PropertyKeyPermission {
-				key: <Vec<u8>>::from(key)
-					.try_into()
-					.map_err(|_| "too long key")?,
+				key: key.into_bytes().try_into().map_err(|_| "too long key")?,
 				permission: token_permission,
 			});
 		}
@@ -144,17 +139,19 @@
 	fn token_property_permissions(
 		&self,
 	) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {
-		let mut res = <Vec<_>>::new();
-		for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {
-			let key = string::from_utf8(key.into_inner()).unwrap();
-			let pp = vec![
-				(EthTokenPermissions::Mutable, pp.mutable),
-				(EthTokenPermissions::TokenOwner, pp.token_owner),
-				(EthTokenPermissions::CollectionAdmin, pp.collection_admin),
-			];
-			res.push((key, pp));
-		}
-		Ok(res)
+		let perms = <Pallet<T>>::token_property_permission(self.id);
+		Ok(perms
+			.into_iter()
+			.map(|(key, pp)| {
+				let key = string::from_utf8(key.into_inner()).expect("Stored key must be valid");
+				let pp = vec![
+					(EthTokenPermissions::Mutable, pp.mutable),
+					(EthTokenPermissions::TokenOwner, pp.token_owner),
+					(EthTokenPermissions::CollectionAdmin, pp.collection_admin),
+				];
+				(key, pp)
+			})
+			.collect())
 	}
 
 	/// @notice Set token property value.
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -93,6 +93,7 @@
 	/// @notice Set permissions for token property.
 	/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
 	/// @param permissions Permissions for keys.
+	#[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]
 	fn set_token_property_permissions(
 		&mut self,
 		caller: caller,
@@ -101,7 +102,7 @@
 		let caller = T::CrossAccountId::from_eth(caller);
 		const PERMISSIONS_FIELDS_COUNT: usize = 3;
 
-		let mut perms = <Vec<_>>::new();
+		let mut perms = Vec::new();
 
 		for (key, pp) in permissions {
 			if pp.len() > PERMISSIONS_FIELDS_COUNT {
@@ -132,9 +133,7 @@
 			}
 
 			perms.push(PropertyKeyPermission {
-				key: <Vec<u8>>::from(key)
-					.try_into()
-					.map_err(|_| "too long key")?,
+				key: key.into_bytes().try_into().map_err(|_| "too long key")?,
 				permission: token_permission,
 			});
 		}
@@ -147,18 +146,19 @@
 	fn token_property_permissions(
 		&self,
 	) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {
-		let mut res = <Vec<_>>::new();
-		for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {
-			let key = string::from_utf8(key.into_inner()).unwrap();
-			let pp = [
-				(EthTokenPermissions::Mutable, pp.mutable),
-				(EthTokenPermissions::TokenOwner, pp.token_owner),
-				(EthTokenPermissions::CollectionAdmin, pp.collection_admin),
-			]
-			.into();
-			res.push((key, pp));
-		}
-		Ok(res)
+		let perms = <Pallet<T>>::token_property_permission(self.id);
+		Ok(perms
+			.into_iter()
+			.map(|(key, pp)| {
+				let key = string::from_utf8(key.into_inner()).expect("Stored key must be valid");
+				let pp = vec![
+					(EthTokenPermissions::Mutable, pp.mutable),
+					(EthTokenPermissions::TokenOwner, pp.token_owner),
+					(EthTokenPermissions::CollectionAdmin, pp.collection_admin),
+				];
+				(key, pp)
+			})
+			.collect())
 	}
 
 	/// @notice Set token property value.
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -1010,7 +1010,7 @@
 pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;
 
 /// Property permission.
-#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]
+#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone, Default)]
 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
 pub struct PropertyPermission {
 	/// Permission to change the property and property permission.
modifiedtests/src/eth/tokenProperties.test.tsdiffbeforeafterboth
68 }68 }
69 }));69 }));
70
71 [
72 {mode: 'nft' as const, requiredPallets: []},
73 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
74 ].map(testCase =>
75 itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as owner`, testCase.requiredPallets, async({helper}) => {
76 const owner = await helper.eth.createAccountWithBalance(donor);
77
78 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
79 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
80
81 await collection.methods.setTokenPropertyPermissions([
82 ['testKey_0', [
83 [EthTokenPermissions.Mutable, true],
84 [EthTokenPermissions.TokenOwner, true],
85 [EthTokenPermissions.CollectionAdmin, true]],
86 ],
87 ['testKey_1', [
88 [EthTokenPermissions.Mutable, true],
89 [EthTokenPermissions.TokenOwner, false],
90 [EthTokenPermissions.CollectionAdmin, true]],
91 ],
92 ['testKey_2', [
93 [EthTokenPermissions.Mutable, false],
94 [EthTokenPermissions.TokenOwner, true],
95 [EthTokenPermissions.CollectionAdmin, false]],
96 ],
97 ]).send({from: owner});
98
99 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([
100 {
101 key: 'testKey_0',
102 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},
103 },
104 {
105 key: 'testKey_1',
106 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},
107 },
108 {
109 key: 'testKey_2',
110 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},
111 },
112 ]);
113
114 expect(await collection.methods.tokenPropertyPermissions().call({from: owner})).to.be.like([
115 ['testKey_0', [
116 [EthTokenPermissions.Mutable.toString(), true],
117 [EthTokenPermissions.TokenOwner.toString(), true],
118 [EthTokenPermissions.CollectionAdmin.toString(), true]],
119 ],
120 ['testKey_1', [
121 [EthTokenPermissions.Mutable.toString(), true],
122 [EthTokenPermissions.TokenOwner.toString(), false],
123 [EthTokenPermissions.CollectionAdmin.toString(), true]],
124 ],
125 ['testKey_2', [
126 [EthTokenPermissions.Mutable.toString(), false],
127 [EthTokenPermissions.TokenOwner.toString(), true],
128 [EthTokenPermissions.CollectionAdmin.toString(), false]],
129 ],
130 ]);
131
132 }));
133
134 [
135 {mode: 'nft' as const, requiredPallets: []},
136 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
137 ].map(testCase =>
138 itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as admin`, testCase.requiredPallets, async({helper}) => {
139 const owner = await helper.eth.createAccountWithBalance(donor);
140 const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);
141
142 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
143 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
144 await collection.methods.addCollectionAdminCross(caller).send({from: owner});
145
146 await collection.methods.setTokenPropertyPermissions([
147 ['testKey_0', [
148 [EthTokenPermissions.Mutable, true],
149 [EthTokenPermissions.TokenOwner, true],
150 [EthTokenPermissions.CollectionAdmin, true]],
151 ],
152 ['testKey_1', [
153 [EthTokenPermissions.Mutable, true],
154 [EthTokenPermissions.TokenOwner, false],
155 [EthTokenPermissions.CollectionAdmin, true]],
156 ],
157 ['testKey_2', [
158 [EthTokenPermissions.Mutable, false],
159 [EthTokenPermissions.TokenOwner, true],
160 [EthTokenPermissions.CollectionAdmin, false]],
161 ],
162 ]).send({from: caller.eth});
163
164 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([
165 {
166 key: 'testKey_0',
167 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},
168 },
169 {
170 key: 'testKey_1',
171 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},
172 },
173 {
174 key: 'testKey_2',
175 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},
176 },
177 ]);
178
179 expect(await collection.methods.tokenPropertyPermissions().call({from: caller.eth})).to.be.like([
180 ['testKey_0', [
181 [EthTokenPermissions.Mutable.toString(), true],
182 [EthTokenPermissions.TokenOwner.toString(), true],
183 [EthTokenPermissions.CollectionAdmin.toString(), true]],
184 ],
185 ['testKey_1', [
186 [EthTokenPermissions.Mutable.toString(), true],
187 [EthTokenPermissions.TokenOwner.toString(), false],
188 [EthTokenPermissions.CollectionAdmin.toString(), true]],
189 ],
190 ['testKey_2', [
191 [EthTokenPermissions.Mutable.toString(), false],
192 [EthTokenPermissions.TokenOwner.toString(), true],
193 [EthTokenPermissions.CollectionAdmin.toString(), false]],
194 ],
195 ]);
196
197 }));
70198
71 [199 [
72 {200 {
320 expect(actualProps).to.deep.eq(expectedProps);448 expect(actualProps).to.deep.eq(expectedProps);
321 }));449 }));
450
451 [
452 {mode: 'nft' as const, requiredPallets: []},
453 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
454 ].map(testCase =>
455 itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions as non owner or admin`, testCase.requiredPallets, async({helper}) => {
456 const owner = await helper.eth.createAccountWithBalance(donor);
457 const caller = await helper.eth.createAccountWithBalance(donor);
458
459 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
460 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
461
462 await expect(collection.methods.setTokenPropertyPermissions([
463 ['testKey_0', [
464 [EthTokenPermissions.Mutable, true],
465 [EthTokenPermissions.TokenOwner, true],
466 [EthTokenPermissions.CollectionAdmin, true]],
467 ],
468 ]).call({from: caller})).to.be.rejectedWith('NoPermission');
469 }));
470
471 [
472 {mode: 'nft' as const, requiredPallets: []},
473 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
474 ].map(testCase =>
475 itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions with invalid character`, testCase.requiredPallets, async({helper}) => {
476 const owner = await helper.eth.createAccountWithBalance(donor);
477
478 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
479 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
480
481 await expect(collection.methods.setTokenPropertyPermissions([
482 // "Space" is invalid character
483 ['testKey 0', [
484 [EthTokenPermissions.Mutable, true],
485 [EthTokenPermissions.TokenOwner, true],
486 [EthTokenPermissions.CollectionAdmin, true]],
487 ],
488 ]).call({from: owner})).to.be.rejectedWith('InvalidCharacterInPropertyKey');
489 }));
490
322});491});
323492