diff --git a/pallets/balances-adapter/src/common.rs b/pallets/balances-adapter/src/common.rs index 0dd4fd98bb..efda782c0f 100644 --- a/pallets/balances-adapter/src/common.rs +++ b/pallets/balances-adapter/src/common.rs @@ -172,18 +172,16 @@ impl CommonCollectionOperations for NativeFungibleHandle { fail!(>::UnsupportedOperation); } - fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties { - // No token properties are defined on fungibles - up_data_structs::TokenProperties::new() - } - - fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) { + fn get_token_properties_raw( + &self, + _token_id: TokenId, + ) -> Option { // No token properties are defined on fungibles + None } - fn properties_exist(&self, _token: TokenId) -> bool { + fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) { // No token properties are defined on fungibles - false } fn set_token_property_permissions( diff --git a/pallets/common/src/lib.rs b/pallets/common/src/lib.rs index 53ec3d8452..fbbd29048a 100644 --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -2098,18 +2098,13 @@ pub trait CommonCollectionOperations { /// Get token properties raw map. /// /// * `token_id` - The token which properties are needed. - fn get_token_properties_map(&self, token_id: TokenId) -> TokenProperties; + fn get_token_properties_raw(&self, token_id: TokenId) -> Option; /// Set token properties raw map. /// /// * `token_id` - The token for which the properties are being set. /// * `map` - The raw map containing the token's properties. - fn set_token_properties_map(&self, token_id: TokenId, map: TokenProperties); - - /// Whether the given token has properties. - /// - /// * `token_id` - The token in question. - fn properties_exist(&self, token: TokenId) -> bool; + fn set_token_properties_raw(&self, token_id: TokenId, map: TokenProperties); /// Set token property permissions. /// @@ -2590,7 +2585,7 @@ impl< >::deposit_log(log); self.collection - .set_token_properties_map(token_id, stored_properties.into_inner()); + .set_token_properties_raw(token_id, stored_properties.into_inner()); } Ok(()) @@ -2624,7 +2619,7 @@ where true }, get_properties: |token_id| { - debug_assert!(!collection.properties_exist(token_id)); + debug_assert!(collection.get_token_properties_raw(token_id).is_none()); TokenProperties::new() }, _phantom: PhantomData, @@ -2686,7 +2681,11 @@ where is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)), property_permissions: LazyValue::new(|| >::property_permissions(collection.id)), check_token_exist: |token_id| collection.token_exists(token_id), - get_properties: |token_id| collection.get_token_properties_map(token_id), + get_properties: |token_id| { + collection + .get_token_properties_raw(token_id) + .unwrap_or_default() + }, _phantom: PhantomData, } } diff --git a/pallets/fungible/src/common.rs b/pallets/fungible/src/common.rs index d6cf683914..e459d52de6 100644 --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -364,18 +364,16 @@ impl CommonCollectionOperations for FungibleHandle { fail!(>::SettingPropertiesNotAllowed) } - fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties { - // No token properties are defined on fungibles - up_data_structs::TokenProperties::new() - } - - fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) { + fn get_token_properties_raw( + &self, + _token_id: TokenId, + ) -> Option { // No token properties are defined on fungibles + None } - fn properties_exist(&self, _token: TokenId) -> bool { + fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) { // No token properties are defined on fungibles - false } fn check_nesting( diff --git a/pallets/nonfungible/src/common.rs b/pallets/nonfungible/src/common.rs index c50f79ce02..4854cdf9bd 100644 --- a/pallets/nonfungible/src/common.rs +++ b/pallets/nonfungible/src/common.rs @@ -265,12 +265,15 @@ impl CommonCollectionOperations for NonfungibleHandle { ) } - fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties { + fn get_token_properties_raw( + &self, + token_id: TokenId, + ) -> Option { >::get((self.id, token_id)) } - fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) { - >::set((self.id, token_id), map) + fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) { + >::insert((self.id, token_id), map) } fn set_token_property_permissions( @@ -287,10 +290,6 @@ impl CommonCollectionOperations for NonfungibleHandle { ) } - fn properties_exist(&self, token: TokenId) -> bool { - >::contains_key((self.id, token)) - } - fn burn_item( &self, sender: T::CrossAccountId, @@ -482,13 +481,15 @@ impl CommonCollectionOperations for NonfungibleHandle { } fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option { - >::token_properties((self.id, token_id)) + >::token_properties((self.id, token_id))? .get(key) .cloned() } fn token_properties(&self, token_id: TokenId, keys: Option>) -> Vec { - let properties = >::token_properties((self.id, token_id)); + let Some(properties) = >::token_properties((self.id, token_id)) else { + return vec![]; + }; keys.map(|keys| { keys.into_iter() diff --git a/pallets/nonfungible/src/erc.rs b/pallets/nonfungible/src/erc.rs index dc46fe926f..41ed9792d3 100644 --- a/pallets/nonfungible/src/erc.rs +++ b/pallets/nonfungible/src/erc.rs @@ -272,7 +272,8 @@ impl NonfungibleHandle { .try_into() .map_err(|_| "key too long")?; - let props = >::get((self.id, token_id)); + let props = + >::get((self.id, token_id)).ok_or("Token properties not found")?; let prop = props.get(&key).ok_or("key not found")?; Ok(prop.to_vec().into()) diff --git a/pallets/nonfungible/src/lib.rs b/pallets/nonfungible/src/lib.rs index 019ee2fa71..02ad2165f0 100644 --- a/pallets/nonfungible/src/lib.rs +++ b/pallets/nonfungible/src/lib.rs @@ -102,8 +102,8 @@ use frame_support::{ use up_data_structs::{ AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData, mapping::TokenAddressMapping, budget::Budget, Property, PropertyKey, PropertyValue, - PropertyKeyPermission, PropertyScope, TrySetProperty, TokenChild, AuxPropertyValue, - PropertiesPermissionMap, TokenProperties as TokenPropertiesT, + PropertyKeyPermission, PropertyScope, TokenChild, AuxPropertyValue, PropertiesPermissionMap, + TokenProperties as TokenPropertiesT, }; use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm}; use pallet_common::{ @@ -201,7 +201,7 @@ pub mod pallet { pub type TokenProperties = StorageNMap< Key = (Key, Key), Value = TokenPropertiesT, - QueryKind = ValueQuery, + QueryKind = OptionQuery, >; /// Custom data of a token that is serialized to bytes, @@ -342,38 +342,6 @@ impl Pallet { >::contains_key((collection.id, token)) } - /// Set the token property with the scope. - /// - /// - `property`: Contains key-value pair. - pub fn set_scoped_token_property( - collection_id: CollectionId, - token_id: TokenId, - scope: PropertyScope, - property: Property, - ) -> DispatchResult { - TokenProperties::::try_mutate((collection_id, token_id), |properties| { - properties.try_scoped_set(scope, property.key, property.value) - }) - .map_err(>::from)?; - - Ok(()) - } - - /// Batch operation to set multiple properties with the same scope. - pub fn set_scoped_token_properties( - collection_id: CollectionId, - token_id: TokenId, - scope: PropertyScope, - properties: impl Iterator, - ) -> DispatchResult { - TokenProperties::::try_mutate((collection_id, token_id), |stored_properties| { - stored_properties.try_scoped_set_from_iter(scope, properties) - }) - .map_err(>::from)?; - - Ok(()) - } - /// Add or edit auxiliary data for the property. /// /// - `f`: function that adds or edits auxiliary data. @@ -1394,7 +1362,9 @@ impl Pallet { pub fn repair_item(collection: &NonfungibleHandle, token: TokenId) -> DispatchResult { >::mutate((collection.id, token), |properties| { - properties.recompute_consumed_space(); + if let Some(properties) = properties { + properties.recompute_consumed_space(); + } }); Ok(()) diff --git a/pallets/refungible/src/common.rs b/pallets/refungible/src/common.rs index 41f9dbf4d4..0d64f8deb8 100644 --- a/pallets/refungible/src/common.rs +++ b/pallets/refungible/src/common.rs @@ -435,16 +435,15 @@ impl CommonCollectionOperations for RefungibleHandle { ) } - fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties { + fn get_token_properties_raw( + &self, + token_id: TokenId, + ) -> Option { >::get((self.id, token_id)) } - fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) { - >::set((self.id, token_id), map) - } - - fn properties_exist(&self, token: TokenId) -> bool { - >::contains_key((self.id, token)) + fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) { + >::insert((self.id, token_id), map) } fn check_nesting( @@ -514,13 +513,15 @@ impl CommonCollectionOperations for RefungibleHandle { } fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option { - >::token_properties((self.id, token_id)) + >::token_properties((self.id, token_id))? .get(key) .cloned() } fn token_properties(&self, token_id: TokenId, keys: Option>) -> Vec { - let properties = >::token_properties((self.id, token_id)); + let Some(properties) = >::token_properties((self.id, token_id)) else { + return vec![]; + }; keys.map(|keys| { keys.into_iter() diff --git a/pallets/refungible/src/erc.rs b/pallets/refungible/src/erc.rs index 3a706cf8ef..05ac3c6c62 100644 --- a/pallets/refungible/src/erc.rs +++ b/pallets/refungible/src/erc.rs @@ -283,7 +283,8 @@ impl RefungibleHandle { .try_into() .map_err(|_| "key too long")?; - let props = >::get((self.id, token_id)); + let props = + >::get((self.id, token_id)).ok_or("Token properties not found")?; let prop = props.get(&key).ok_or("key not found")?; Ok(prop.to_vec().into()) diff --git a/pallets/refungible/src/lib.rs b/pallets/refungible/src/lib.rs index 5482eb65b7..e9f4f9575e 100644 --- a/pallets/refungible/src/lib.rs +++ b/pallets/refungible/src/lib.rs @@ -106,8 +106,8 @@ use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap}; use up_data_structs::{ AccessMode, budget::Budget, CollectionId, CreateCollectionData, mapping::TokenAddressMapping, MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyScope, - PropertyValue, TokenId, TrySetProperty, PropertiesPermissionMap, - CreateRefungibleExMultipleOwners, TokenOwnerError, TokenProperties as TokenPropertiesT, + PropertyValue, TokenId, PropertiesPermissionMap, CreateRefungibleExMultipleOwners, + TokenOwnerError, TokenProperties as TokenPropertiesT, }; pub use pallet::*; @@ -175,7 +175,7 @@ pub mod pallet { pub type TokenProperties = StorageNMap< Key = (Key, Key), Value = TokenPropertiesT, - QueryKind = ValueQuery, + QueryKind = OptionQuery, >; /// Total amount of pieces for token @@ -293,34 +293,6 @@ impl Pallet { pub fn token_exists(collection: &RefungibleHandle, token: TokenId) -> bool { >::contains_key((collection.id, token)) } - - pub fn set_scoped_token_property( - collection_id: CollectionId, - token_id: TokenId, - scope: PropertyScope, - property: Property, - ) -> DispatchResult { - TokenProperties::::try_mutate((collection_id, token_id), |properties| { - properties.try_scoped_set(scope, property.key, property.value) - }) - .map_err(>::from)?; - - Ok(()) - } - - pub fn set_scoped_token_properties( - collection_id: CollectionId, - token_id: TokenId, - scope: PropertyScope, - properties: impl Iterator, - ) -> DispatchResult { - TokenProperties::::try_mutate((collection_id, token_id), |stored_properties| { - stored_properties.try_scoped_set_from_iter(scope, properties) - }) - .map_err(>::from)?; - - Ok(()) - } } // unchecked calls skips any permission checks @@ -1426,7 +1398,9 @@ impl Pallet { pub fn repair_item(collection: &RefungibleHandle, token: TokenId) -> DispatchResult { >::mutate((collection.id, token), |properties| { - properties.recompute_consumed_space(); + if let Some(properties) = properties { + properties.recompute_consumed_space(); + } }); Ok(())