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
678 collection.check_allowlist(spender)?;678 collection.check_allowlist(spender)?;
679 }679 }
680
681 if collection.ignores_token_restrictions(spender) {
682 return Ok(Self::compute_allowance_decrease(
683 collection, from, spender, amount,
684 ));
685 }
686
680 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {687 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
681 // TODO: should collection owner be allowed to perform this transfer?
682 ensure!(688 ensure!(
683 <PalletStructure<T>>::check_indirectly_owned(689 <PalletStructure<T>>::check_indirectly_owned(
684 spender.clone(),690 spender.clone(),
692 return Ok(None);698 return Ok(None);
693 }699 }
700
694 let allowance = <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount);701 let allowance = Self::compute_allowance_decrease(collection, from, spender, amount);
695 if allowance.is_none() {
696 ensure!(702 ensure!(allowance.is_some(), <CommonError<T>>::ApprovedValueTooLow);
697 collection.ignores_allowance(spender),
698 <CommonError<T>>::ApprovedValueTooLow
699 );
700 }
701703
702 Ok(allowance)704 Ok(allowance)
703 }705 }
706
707 /// Returns `Some(amount)` if the `spender` have allowance to spend this amount.
708 /// Otherwise, it returns `None`.
709 fn compute_allowance_decrease(
710 collection: &FungibleHandle<T>,
711 from: &T::CrossAccountId,
712 spender: &T::CrossAccountId,
713 amount: u128,
714 ) -> Option<u128> {
715 <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount)
716 }
704717
705 /// Transfer fungible tokens from one account to another.718 /// Transfer fungible tokens from one account to another.
706 /// Same as the [`transfer`][`Pallet::transfer`] but spender doesn't needs to be an owner of the token pieces.719 /// Same as the [`transfer`][`Pallet::transfer`] but spender doesn't needs to be an owner of the token 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
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -1178,8 +1178,10 @@
 			collection.check_allowlist(spender)?;
 		}
 
-		if collection.limits.owner_can_transfer() && collection.is_owner_or_admin(spender) {
-			return Ok(None);
+		if collection.ignores_token_restrictions(spender) {
+			return Ok(Self::compute_allowance_decrease(
+				collection, token, from, &spender, amount,
+			));
 		}
 
 		if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
@@ -1196,21 +1198,30 @@
 			);
 			return Ok(None);
 		}
-		let allowance =
-			<Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);
 
+		let allowance = Self::compute_allowance_decrease(collection, token, from, &spender, amount);
+		if allowance.is_some() {
+			return Ok(allowance);
+		}
+
 		// Allowance (if any) would be reduced if spender is also wallet operator
 		if <CollectionAllowance<T>>::get((collection.id, from, spender)) {
 			return Ok(allowance);
 		}
 
-		if allowance.is_none() {
-			ensure!(
-				collection.ignores_allowance(spender),
-				<CommonError<T>>::ApprovedValueTooLow
-			);
-		}
-		Ok(allowance)
+		Err(<CommonError<T>>::ApprovedValueTooLow.into())
+	}
+
+	/// Returns `Some(amount)` if the `spender` have allowance to spend this amount.
+	/// Otherwise, it returns `None`.
+	fn compute_allowance_decrease(
+		collection: &RefungibleHandle<T>,
+		token: TokenId,
+		from: &T::CrossAccountId,
+		spender: &T::CrossAccountId,
+		amount: u128,
+	) -> Option<u128> {
+		<Allowance<T>>::get((collection.id, token, from, spender)).checked_sub(amount)
 	}
 
 	/// Transfer RFT token pieces from one account to another.