From 87988d2bdf5f7f2441236ce316e94dd6c9bce160 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 09 Jun 2022 18:33:47 +0000 Subject: [PATCH] feat(rmrk): make resources lazy --- --- a/pallets/proxy-rmrk-core/src/lib.rs +++ b/pallets/proxy-rmrk-core/src/lib.rs @@ -158,6 +158,7 @@ NftTypeEncodeError, RmrkPropertyKeyIsTooLong, RmrkPropertyValueIsTooLong, + UnableToDecodeRmrkData, /* RMRK compatible events */ CollectionNotEmpty, @@ -378,20 +379,7 @@ Self::rmrk_property(RoyaltyInfo, &royalty_info)?, Self::rmrk_property(Metadata, &metadata)?, Self::rmrk_property(Equipped, &false)?, - Self::rmrk_property( - ResourceCollection, - &Self::init_collection( - sender.clone(), - CreateCollectionData { - ..Default::default() - }, - [Self::rmrk_property( - CollectionType, - &misc::CollectionType::Resource, - )?] - .into_iter(), - )?, - )?, // todo possibly add limits to the collection if rmrk warrants them + Self::rmrk_property(ResourceCollection, &None::)?, Self::rmrk_property(ResourcePriorities, &>::new())?, ] .into_iter(), @@ -702,9 +690,11 @@ >::find_topmost_owner(collection_id, nft_id, &budget) .map_err(|_| >::ResourceDoesntExist)?; - let resource_collection_id: CollectionId = + let resource_collection_id: Option = Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection) .map_err(|_| >::ResourceDoesntExist)?; + + let resource_collection_id = resource_collection_id.ok_or(>::ResourceDoesntExist)?; let is_pending: bool = Self::get_nft_property_decoded( resource_collection_id, @@ -761,10 +751,12 @@ ensure!(cross_sender == nft_owner, >::NoPermission); - let resource_collection_id: CollectionId = + let resource_collection_id: Option = Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection) .map_err(|_| >::ResourceDoesntExist)?; + let resource_collection_id = resource_collection_id.ok_or(>::ResourceDoesntExist)?; + let is_pending: bool = Self::get_nft_property_decoded( resource_collection_id, resource_id, @@ -1065,7 +1057,7 @@ pub fn decode_property(vec: PropertyValue) -> Result { vec.decode() - .map_err(|_| >::RmrkPropertyValueIsTooLong.into()) + .map_err(|_| >::UnableToDecodeRmrkData.into()) } pub fn rebind(vec: &BoundedVec) -> Result, DispatchError> @@ -1181,8 +1173,37 @@ let pending = sender != nft_owner; - let resource_collection_id: CollectionId = + let resource_collection_id: Option = Self::get_nft_property_decoded(collection_id, token_id, ResourceCollection)?; + + let resource_collection_id = match resource_collection_id { + Some(id) => id, + None => { + let resource_collection_id = Self::init_collection( + sender.clone(), + CreateCollectionData { + ..Default::default() + }, + [ + Self::rmrk_property( + CollectionType, + &misc::CollectionType::Resource, + )? + ] + .into_iter(), + )?; + + >::set_scoped_token_property( + collection_id, + token_id, + PropertyScope::Rmrk, + Self::rmrk_property(ResourceCollection, &Some(resource_collection_id))? + )?; + + resource_collection_id + } + }; + let resource_collection = Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?; @@ -1218,8 +1239,11 @@ Self::get_typed_nft_collection(collection_id, misc::CollectionType::Regular)?; ensure!(collection.owner == sender, Error::::NoPermission); - let resource_collection_id: CollectionId = + let resource_collection_id: Option = Self::get_nft_property_decoded(collection_id, nft_id, ResourceCollection)?; + + let resource_collection_id = resource_collection_id.ok_or(Error::::ResourceDoesntExist)?; + let resource_collection = Self::get_typed_nft_collection(resource_collection_id, misc::CollectionType::Resource)?; ensure!( --- a/runtime/common/src/runtime_apis.rs +++ b/runtime/common/src/runtime_apis.rs @@ -339,7 +339,13 @@ let nft_id = TokenId(nft_id); if RmrkCore::ensure_nft_type(collection_id, nft_id, NftType::Regular).is_err() { return Ok(Vec::new()); } - let res_collection_id: CollectionId = RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::ResourceCollection)?; + let res_collection_id: Option = RmrkCore::get_nft_property_decoded(collection_id, nft_id, RmrkProperty::ResourceCollection)?; + + let res_collection_id = match res_collection_id { + Some(id) => id, + None => return Ok(Vec::new()) + }; + let resource_collection = RmrkCore::get_typed_nft_collection(res_collection_id, CollectionType::Resource)?; let resources = resource_collection -- gitstuff