git.delta.rocks / unique-network / refs/commits / 017086424d43

difftreelog

fix owner/admin ignores token restrictions

Daniel Shiposha2023-01-16parent: #670ea14.patch.diff
in: master

4 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -390,8 +390,10 @@
 		Ok(())
 	}
 
-	/// Return **true** if `user` was not allowed to have tokens, and he can ignore such restrictions.
-	pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {
+	/// Returns **true** if
+	/// * the `user`is a collection owner or admin
+	/// * the collection limits allow the owner/admins to transfer/burn any collection token
+	pub fn ignores_token_restrictions(&self, user: &T::CrossAccountId) -> bool {
 		self.limits.owner_can_transfer() && self.is_owner_or_admin(user)
 	}
 
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -677,8 +677,14 @@
 			// `from`, `to` checked in [`transfer`]
 			collection.check_allowlist(spender)?;
 		}
+
+		if collection.ignores_token_restrictions(spender) {
+			return Ok(Self::compute_allowance_decrease(
+				collection, from, spender, amount,
+			));
+		}
+
 		if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
-			// TODO: should collection owner be allowed to perform this transfer?
 			ensure!(
 				<PalletStructure<T>>::check_indirectly_owned(
 					spender.clone(),
@@ -690,18 +696,25 @@
 				<CommonError<T>>::ApprovedValueTooLow,
 			);
 			return Ok(None);
-		}
-		let allowance = <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount);
-		if allowance.is_none() {
-			ensure!(
-				collection.ignores_allowance(spender),
-				<CommonError<T>>::ApprovedValueTooLow
-			);
 		}
 
+		let allowance = Self::compute_allowance_decrease(collection, from, spender, amount);
+		ensure!(allowance.is_some(), <CommonError<T>>::ApprovedValueTooLow);
+
 		Ok(allowance)
 	}
 
+	/// Returns `Some(amount)` if the `spender` have allowance to spend this amount.
+	/// Otherwise, it returns `None`.
+	fn compute_allowance_decrease(
+		collection: &FungibleHandle<T>,
+		from: &T::CrossAccountId,
+		spender: &T::CrossAccountId,
+		amount: u128,
+	) -> Option<u128> {
+		<Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount)
+	}
+
 	/// Transfer fungible tokens from one account to another.
 	/// Same as the [`transfer`][`Pallet::transfer`] but spender doesn't needs to be an owner of the token pieces.
 	/// The owner should set allowance for the spender to transfer pieces.
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -1246,7 +1246,7 @@
 			collection.check_allowlist(spender)?;
 		}
 
-		if collection.limits.owner_can_transfer() && collection.is_owner_or_admin(spender) {
+		if collection.ignores_token_restrictions(spender) {
 			return Ok(());
 		}
 
@@ -1269,11 +1269,8 @@
 		if <CollectionAllowance<T>>::get((collection.id, from, spender)) {
 			return Ok(());
 		}
-		ensure!(
-			collection.ignores_allowance(spender),
-			<CommonError<T>>::ApprovedValueTooLow
-		);
-		Ok(())
+
+		Err(<CommonError<T>>::ApprovedValueTooLow.into())
 	}
 
 	/// Transfer NFT token from one account to another.
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
1178 collection.check_allowlist(spender)?;1178 collection.check_allowlist(spender)?;
1179 }1179 }
11801180
1181 if collection.limits.owner_can_transfer() && collection.is_owner_or_admin(spender) {1181 if collection.ignores_token_restrictions(spender) {
1182 return Ok(None);1182 return Ok(Self::compute_allowance_decrease(
1183 collection, token, from, &spender, amount,
1184 ));
1183 }1185 }
11841186
1185 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {1187 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
1197 return Ok(None);1199 return Ok(None);
1198 }1200 }
1201
1199 let allowance =1202 let allowance = Self::compute_allowance_decrease(collection, token, from, &spender, amount);
1200 <Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);1203 if allowance.is_some() {
1204 return Ok(allowance);
1205 }
12011206
1202 // Allowance (if any) would be reduced if spender is also wallet operator1207 // Allowance (if any) would be reduced if spender is also wallet operator
1203 if <CollectionAllowance<T>>::get((collection.id, from, spender)) {1208 if <CollectionAllowance<T>>::get((collection.id, from, spender)) {
1204 return Ok(allowance);1209 return Ok(allowance);
1205 }1210 }
12061211
1207 if allowance.is_none() {
1208 ensure!(
1209 collection.ignores_allowance(spender),
1210 <CommonError<T>>::ApprovedValueTooLow1212 Err(<CommonError<T>>::ApprovedValueTooLow.into())
1211 );
1212 }
1213 Ok(allowance)
1214 }1213 }
1214
1215 /// Returns `Some(amount)` if the `spender` have allowance to spend this amount.
1216 /// Otherwise, it returns `None`.
1217 fn compute_allowance_decrease(
1218 collection: &RefungibleHandle<T>,
1219 token: TokenId,
1220 from: &T::CrossAccountId,
1221 spender: &T::CrossAccountId,
1222 amount: u128,
1223 ) -> Option<u128> {
1224 <Allowance<T>>::get((collection.id, token, from, spender)).checked_sub(amount)
1225 }
12151226
1216 /// Transfer RFT token pieces from one account to another.1227 /// Transfer RFT token pieces from one account to another.
1217 ///1228 ///