git.delta.rocks / unique-network / refs/commits / dcd5f668cefb

difftreelog

fix(rmrk) sending nft

Daniel Shiposha2022-06-05parent: #7e808da.patch.diff
in: master

5 files changed

modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -25,7 +25,7 @@
 	Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations,
 };
 use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle, TokenData};
-use pallet_structure::Pallet as PalletStructure;
+use pallet_structure::{Pallet as PalletStructure, Error as StructureError};
 use pallet_evm::account::CrossAccountId;
 use core::convert::AsRef;
 
@@ -128,8 +128,10 @@
 		NoAvailableNftId,
 		CollectionUnknown,
 		NoPermission,
+		NonTransferable,
 		CollectionFullOrLocked,
 		ResourceDoesntExist,
+		CannotSendToDescendentOrSelf,
 	}
 
 	#[pallet::call]
@@ -207,7 +209,7 @@
 			);
 
 			<PalletNft<T>>::destroy_collection(collection, &cross_sender)
-				.map_err(Self::map_common_err_to_proxy)?;
+				.map_err(Self::map_unique_err_to_proxy)?;
 
 			Self::deposit_event(Event::CollectionDestroyed {
 				issuer: sender,
@@ -283,6 +285,7 @@
 			recipient: Option<T::AccountId>,
 			royalty_amount: Option<Permill>,
 			metadata: RmrkString,
+			transferable: bool,
 		) -> DispatchResult {
 			let sender = ensure_signed(origin)?;
 			let sender = T::CrossAccountId::from_sub(sender);
@@ -304,6 +307,8 @@
 				&collection,
 				[
 					Self::rmrk_property(TokenType, &NftType::Regular)?,
+					Self::rmrk_property(Transferable, &transferable)?,
+					Self::rmrk_property(PendingNftAccept, &false)?,
 					Self::rmrk_property(RoyaltyInfo, &royalty_info)?,
 					Self::rmrk_property(Metadata, &metadata)?,
 					Self::rmrk_property(Equipped, &false)?,
@@ -327,7 +332,7 @@
 			)
 			.map_err(|err| match err {
 				DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),
-				err => Self::map_common_err_to_proxy(err),
+				err => Self::map_unique_err_to_proxy(err),
 			})?;
 
 			Self::deposit_event(Event::NftMinted {
@@ -373,7 +378,7 @@
 			new_owner: RmrkAccountIdOrCollectionNftTuple<T::AccountId>,
 		) -> DispatchResult {
 			let sender = ensure_signed(origin.clone())?;
-			let cross_sender = T::CrossAccountId::from_sub(sender.clone());
+			let cross_sender = T::CrossAccountId::from_sub(sender);
 
 			let collection_id = Self::unique_collection_id(rmrk_collection_id)?;
 			let nft_id = rmrk_nft_id.into();
@@ -388,8 +393,14 @@
 				misc::CollectionType::Regular,
 			)?;
 
-			let budget = budget::Value::new(NESTING_BUDGET);
+			if !Self::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Transferable)? {
+				return Err(<Error<T>>::NonTransferable.into());
+			}
 
+			if Self::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::PendingNftAccept)? {
+				return Err(<Error<T>>::NoPermission.into());
+			}
+
 			let target_owner;
 
 			match new_owner {
@@ -399,43 +410,45 @@
 				RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(target_collection_id, target_nft_id) => {
 					let target_collection_id = Self::unique_collection_id(target_collection_id)?;
 
-					target_owner = T::CrossTokenAddressMapping::token_to_address(
-						target_collection_id,
-						target_nft_id.into(),
-					);
+					let target_nft_budget = budget::Value::new(NESTING_BUDGET);
 
-					let spender = <PalletStructure<T>>::get_indirect_owner(
+					let target_nft_owner = <PalletStructure<T>>::get_checked_indirect_owner(
 						target_collection_id,
 						target_nft_id.into(),
 						Some((collection_id, nft_id)),
-						&budget,
-					)?;
+						&target_nft_budget,
+					).map_err(Self::map_unique_err_to_proxy)?;
 
-					let is_approval_required = cross_sender != spender;
+					let is_approval_required = cross_sender != target_nft_owner;
 
 					if is_approval_required {
-						// FIXME
-						// <PalletNft<T>>::set_allowance(
-						// 	&collection,
-						// 	&cross_sender,
-						// 	nft_id,
-						// 	Some(&spender),
-						// 	&budget
-						// ).map_err(Self::map_common_err_to_proxy)?;
+						target_owner = target_nft_owner;
 
-						return Ok(());
+						<PalletNft<T>>::set_scoped_token_property(
+							collection.id,
+							nft_id,
+							PropertyScope::Rmrk,
+							Self::rmrk_property(PendingNftAccept, &is_approval_required)?,
+						)?;
+					} else {
+						target_owner = T::CrossTokenAddressMapping::token_to_address(
+							target_collection_id,
+							target_nft_id.into(),
+						);
 					}
 				}
 			}
 
+			let src_nft_budget = budget::Value::new(NESTING_BUDGET);
+
 			<PalletNft<T>>::transfer_from(
 				&collection,
 				&cross_sender,
 				&from,
 				&target_owner,
 				nft_id,
-				&budget
-			).map_err(Self::map_common_err_to_proxy)?;
+				&src_nft_budget
+			).map_err(Self::map_unique_err_to_proxy)?;
 
 			Ok(())
 		}
@@ -719,7 +732,7 @@
 		let collection = Self::get_typed_nft_collection(collection_id, collection_type)?;
 
 		<PalletNft<T>>::burn(&collection, &sender, token_id)
-			.map_err(Self::map_common_err_to_proxy)?;
+			.map_err(Self::map_unique_err_to_proxy)?;
 
 		Ok(())
 	}
@@ -762,7 +775,7 @@
 		)
 		.map_err(|err| match err {
 			DispatchError::Arithmetic(_) => <Error<T>>::NoAvailableNftId.into(),
-			err => Self::map_common_err_to_proxy(err),
+			err => Self::map_unique_err_to_proxy(err),
 		})?;
 
 		Ok(resource_id.0)
@@ -794,7 +807,7 @@
 		let sender = T::CrossAccountId::from_sub(sender);
 		if topmost_owner == sender {
 			<PalletNft<T>>::burn(&resource_collection, &sender, resource_id)
-				.map_err(Self::map_common_err_to_proxy)?;
+				.map_err(Self::map_unique_err_to_proxy)?;
 		} else {
 			<PalletNft<T>>::set_scoped_token_property(
 				resource_collection_id,
@@ -828,7 +841,7 @@
 	) -> DispatchResult {
 		collection
 			.check_is_owner(account)
-			.map_err(Self::map_common_err_to_proxy)
+			.map_err(Self::map_unique_err_to_proxy)
 	}
 
 	pub fn last_collection_idx() -> RmrkCollectionId {
@@ -1064,14 +1077,16 @@
 		Ok(properties)
 	}
 
-	fn map_common_err_to_proxy(err: DispatchError) -> DispatchError {
-		map_common_err_to_proxy! {
+	fn map_unique_err_to_proxy(err: DispatchError) -> DispatchError {
+		map_unique_err_to_proxy! {
 			match err {
-				NoPermission => NoPermission,
-				CollectionTokenLimitExceeded => CollectionFullOrLocked,
-				PublicMintingNotAllowed => NoPermission,
-				TokenNotFound => NoAvailableNftId,
-				ApprovedValueTooLow => NoPermission
+				CommonError::NoPermission => NoPermission,
+				CommonError::CollectionTokenLimitExceeded => CollectionFullOrLocked,
+				CommonError::PublicMintingNotAllowed => NoPermission,
+				CommonError::TokenNotFound => NoAvailableNftId,
+				CommonError::ApprovedValueTooLow => NoPermission,
+				StructureError::TokenNotFound => NoAvailableNftId,
+				StructureError::OuroborosDetected => CannotSendToDescendentOrSelf
 			}
 		}
 	}
modifiedpallets/proxy-rmrk-core/src/misc.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/misc.rs
+++ b/pallets/proxy-rmrk-core/src/misc.rs
@@ -2,10 +2,10 @@
 use codec::{Encode, Decode, Error};
 
 #[macro_export]
-macro_rules! map_common_err_to_proxy {
-    (match $err:ident { $($common_err:ident => $proxy_err:ident),+ }) => {
+macro_rules! map_unique_err_to_proxy {
+    (match $err:ident { $($unique_err_ty:ident :: $unique_err:ident => $proxy_err:ident),+ }) => {
         $(
-            if $err == <CommonError<T>>::$common_err.into() {
+            if $err == <$unique_err_ty<T>>::$unique_err.into() {
                 return <Error<T>>::$proxy_err.into()
             } else
         )+ {
modifiedpallets/proxy-rmrk-core/src/property.rsdiffbeforeafterboth
after · pallets/proxy-rmrk-core/src/property.rs
1use super::*;2use core::convert::AsRef;34pub enum RmrkProperty<'r> {5	Metadata,6	CollectionType,7	TokenType,8	Transferable,9	RoyaltyInfo,10	Equipped,11	ResourceCollection,12	ResourcePriorities,13	ResourceType,14	PendingNftAccept,15	PendingResourceAccept,16	PendingResourceRemoval,17	Parts,18	Base,19	Src,20	Slot,21	License,22	Thumb,23	EquippedNft,24	BaseType,25	ExternalPartId,26	EquippableList,27	ZIndex,28	ThemeName,29	ThemeInherit,30	UserProperty(&'r [u8]),31}3233impl<'r> RmrkProperty<'r> {34	pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {35		fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {36			container.as_ref()37		}3839		macro_rules! key {40            ($($component:expr),+) => {41                PropertyKey::try_from([$(key!(@ &$component)),+].concat())42                    .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)43            };4445            (@ $key:expr) => {46                get_bytes($key)47            };48        }4950		match self {51			Self::Metadata => key!("metadata"),52			Self::CollectionType => key!("collection-type"),53			Self::TokenType => key!("token-type"),54			Self::Transferable => key!("transferable"),55			Self::RoyaltyInfo => key!("royalty-info"),56			Self::Equipped => key!("equipped"),57			Self::ResourceCollection => key!("resource-collection"),58			Self::ResourcePriorities => key!("resource-priorities"),59			Self::ResourceType => key!("resource-type"),60			Self::PendingNftAccept => key!("pending-nft-accept"),61			Self::PendingResourceAccept => key!("pending-resource-accept"),62			Self::PendingResourceRemoval => key!("pending-resource-removal"),63			Self::Parts => key!("parts"),64			Self::Base => key!("base"),65			Self::Src => key!("src"),66			Self::Slot => key!("slot"),67			Self::License => key!("license"),68			Self::Thumb => key!("thumb"),69			Self::EquippedNft => key!("equipped-nft"),70			Self::BaseType => key!("base-type"),71			Self::ExternalPartId => key!("ext-part-id"),72			Self::EquippableList => key!("equippable-list"),73			Self::ZIndex => key!("z-index"),74			Self::ThemeName => key!("theme-name"),75			Self::ThemeInherit => key!("theme-inherit"),76			Self::UserProperty(name) => key!("userprop-", name),77		}78	}79}
modifiedpallets/structure/src/lib.rsdiffbeforeafterboth
--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -149,7 +149,7 @@
 		})
 	}
 
-	pub fn get_indirect_owner(
+	pub fn get_checked_indirect_owner(
 		collection: CollectionId,
 		token: TokenId,
 		for_nest: Option<(CollectionId, TokenId)>,
@@ -203,7 +203,7 @@
 			None => user,
 		};
 
-		Self::get_indirect_owner(
+		Self::get_checked_indirect_owner(
 			collection,
 			token,
 			for_nest,
modifiedruntime/common/src/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -188,19 +188,17 @@
                         None => return Ok(None)
                     };
 
-                    let allowance = pallet_nonfungible::Allowance::<Runtime>::get((collection_id, nft_id));
-
                     Ok(Some(RmrkInstanceInfo {
                         owner: owner,
                         royalty: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::RoyaltyInfo)?,
                         metadata: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Metadata)?,
                         equipped: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::Equipped)?,
-                        pending: allowance.is_some(),
+                        pending: RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::PendingNftAccept)?,
                     }))
                 }
 
                 fn account_tokens(account_id: AccountId, collection_id: RmrkCollectionId) -> Result<Vec<RmrkNftId>, DispatchError> {
-                    use pallet_proxy_rmrk_core::misc::CollectionType;
+                    use pallet_proxy_rmrk_core::{RmrkProperty, misc::CollectionType};
                     use pallet_common::CommonCollectionOperations;
 
                     let cross_account_id = CrossAccountId::from_sub(account_id);
@@ -210,15 +208,26 @@
                         Err(_) => return Ok(Vec::new()),
                     };
 
-                    Ok(
-                        collection.account_tokens(cross_account_id)
-                            .into_iter()
-                            .map(|token| token.0)
-                            .collect()
-                    )
+                    let tokens = collection.account_tokens(cross_account_id)
+                        .into_iter()
+                        .filter(|token| {
+                            let is_pending = RmrkCore::get_nft_property_decoded(
+                                collection_id,
+                                *token,
+                                RmrkProperty::PendingNftAccept
+                            ).unwrap_or(true);
+
+                            !is_pending
+                        })
+                        .map(|token| token.0)
+                        .collect();
+
+                    Ok(tokens)
                 }
 
                 fn nft_children(collection_id: RmrkCollectionId, nft_id: RmrkNftId) -> Result<Vec<RmrkNftChild>, DispatchError> {
+                    use pallet_proxy_rmrk_core::RmrkProperty;
+
                     let collection_id = match RmrkCore::unique_collection_id(collection_id) {
                         Ok(id) => id,
                         Err(_) => return Ok(Vec::new())
@@ -229,6 +238,16 @@
                     Ok(
                         pallet_nonfungible::TokenChildren::<Runtime>::iter_prefix((collection_id, nft_id))
                             .filter_map(|((child_collection, child_token), _)| {
+                                let is_pending = RmrkCore::get_nft_property_decoded(
+                                    child_collection,
+                                    child_token,
+                                    RmrkProperty::PendingNftAccept
+                                ).ok()?;
+
+                                if is_pending {
+                                    return None;
+                                }
+
                                 let rmrk_child_collection = RmrkCore::rmrk_collection_id(
                                     child_collection
                                 ).ok()?;
@@ -398,7 +417,7 @@
                         Ok(c) => c,
                         Err(_) => return Ok(Vec::new()),
                     };
-                    
+
                     let parts = collection.collection_tokens()
                         .into_iter()
                         .filter_map(|token_id| {