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

difftreelog

fix use OptionQuery for TokenProperties

Daniel Shiposha2023-10-02parent: #5ef5e6b.patch.diff
in: master

9 files changed

modifiedpallets/balances-adapter/src/common.rsdiffbeforeafterboth
172 fail!(<pallet_common::Error<T>>::UnsupportedOperation);172 fail!(<pallet_common::Error<T>>::UnsupportedOperation);
173 }173 }
174174
175 fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties {175 fn get_token_properties_raw(
176 &self,
177 _token_id: TokenId,
178 ) -> Option<up_data_structs::TokenProperties> {
176 // No token properties are defined on fungibles179 // No token properties are defined on fungibles
177 up_data_structs::TokenProperties::new()180 None
178 }181 }
179182
180 fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {183 fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
181 // No token properties are defined on fungibles184 // No token properties are defined on fungibles
182 }185 }
183
184 fn properties_exist(&self, _token: TokenId) -> bool {
185 // No token properties are defined on fungibles
186 false
187 }
188186
189 fn set_token_property_permissions(187 fn set_token_property_permissions(
190 &self,188 &self,
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
2098 /// Get token properties raw map.2098 /// Get token properties raw map.
2099 ///2099 ///
2100 /// * `token_id` - The token which properties are needed.2100 /// * `token_id` - The token which properties are needed.
2101 fn get_token_properties_map(&self, token_id: TokenId) -> TokenProperties;2101 fn get_token_properties_raw(&self, token_id: TokenId) -> Option<TokenProperties>;
21022102
2103 /// Set token properties raw map.2103 /// Set token properties raw map.
2104 ///2104 ///
2105 /// * `token_id` - The token for which the properties are being set.2105 /// * `token_id` - The token for which the properties are being set.
2106 /// * `map` - The raw map containing the token's properties.2106 /// * `map` - The raw map containing the token's properties.
2107 fn set_token_properties_map(&self, token_id: TokenId, map: TokenProperties);2107 fn set_token_properties_raw(&self, token_id: TokenId, map: TokenProperties);
2108
2109 /// Whether the given token has properties.
2110 ///
2111 /// * `token_id` - The token in question.
2112 fn properties_exist(&self, token: TokenId) -> bool;
21132108
2114 /// Set token property permissions.2109 /// Set token property permissions.
2115 ///2110 ///
2590 <PalletEvm<T>>::deposit_log(log);2585 <PalletEvm<T>>::deposit_log(log);
25912586
2592 self.collection2587 self.collection
2593 .set_token_properties_map(token_id, stored_properties.into_inner());2588 .set_token_properties_raw(token_id, stored_properties.into_inner());
2594 }2589 }
25952590
2596 Ok(())2591 Ok(())
2624 true2619 true
2625 },2620 },
2626 get_properties: |token_id| {2621 get_properties: |token_id| {
2627 debug_assert!(!collection.properties_exist(token_id));2622 debug_assert!(collection.get_token_properties_raw(token_id).is_none());
2628 TokenProperties::new()2623 TokenProperties::new()
2629 },2624 },
2630 _phantom: PhantomData,2625 _phantom: PhantomData,
2686 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),2681 is_collection_admin: LazyValue::new(|| collection.is_owner_or_admin(sender)),
2687 property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),2682 property_permissions: LazyValue::new(|| <Pallet<T>>::property_permissions(collection.id)),
2688 check_token_exist: |token_id| collection.token_exists(token_id),2683 check_token_exist: |token_id| collection.token_exists(token_id),
2689 get_properties: |token_id| collection.get_token_properties_map(token_id),2684 get_properties: |token_id| {
2685 collection
2686 .get_token_properties_raw(token_id)
2687 .unwrap_or_default()
2688 },
2690 _phantom: PhantomData,2689 _phantom: PhantomData,
2691 }2690 }
2692}2691}
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
364 fail!(<Error<T>>::SettingPropertiesNotAllowed)364 fail!(<Error<T>>::SettingPropertiesNotAllowed)
365 }365 }
366366
367 fn get_token_properties_map(&self, _token_id: TokenId) -> up_data_structs::TokenProperties {367 fn get_token_properties_raw(
368 &self,
369 _token_id: TokenId,
370 ) -> Option<up_data_structs::TokenProperties> {
368 // No token properties are defined on fungibles371 // No token properties are defined on fungibles
369 up_data_structs::TokenProperties::new()372 None
370 }373 }
371374
372 fn set_token_properties_map(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {375 fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {
373 // No token properties are defined on fungibles376 // No token properties are defined on fungibles
374 }377 }
375
376 fn properties_exist(&self, _token: TokenId) -> bool {
377 // No token properties are defined on fungibles
378 false
379 }
380378
381 fn check_nesting(379 fn check_nesting(
382 &self,380 &self,
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
265 )265 )
266 }266 }
267267
268 fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties {268 fn get_token_properties_raw(
269 &self,
270 token_id: TokenId,
271 ) -> Option<up_data_structs::TokenProperties> {
269 <TokenProperties<T>>::get((self.id, token_id))272 <TokenProperties<T>>::get((self.id, token_id))
270 }273 }
271274
272 fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {275 fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
273 <TokenProperties<T>>::set((self.id, token_id), map)276 <TokenProperties<T>>::insert((self.id, token_id), map)
274 }277 }
275278
276 fn set_token_property_permissions(279 fn set_token_property_permissions(
287 )290 )
288 }291 }
289
290 fn properties_exist(&self, token: TokenId) -> bool {
291 <TokenProperties<T>>::contains_key((self.id, token))
292 }
293292
294 fn burn_item(293 fn burn_item(
295 &self,294 &self,
482 }481 }
483482
484 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {483 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {
485 <Pallet<T>>::token_properties((self.id, token_id))484 <Pallet<T>>::token_properties((self.id, token_id))?
486 .get(key)485 .get(key)
487 .cloned()486 .cloned()
488 }487 }
489488
490 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {489 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {
491 let properties = <Pallet<T>>::token_properties((self.id, token_id));490 let Some(properties) = <Pallet<T>>::token_properties((self.id, token_id)) else {
491 return vec![];
492 };
492493
493 keys.map(|keys| {494 keys.map(|keys| {
494 keys.into_iter()495 keys.into_iter()
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
273 .map_err(|_| "key too long")?;273 .map_err(|_| "key too long")?;
274274
275 let props = <TokenProperties<T>>::get((self.id, token_id));275 let props =
276 <TokenProperties<T>>::get((self.id, token_id)).ok_or("Token properties not found")?;
276 let prop = props.get(&key).ok_or("key not found")?;277 let prop = props.get(&key).ok_or("key not found")?;
277278
278 Ok(prop.to_vec().into())279 Ok(prop.to_vec().into())
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
102use up_data_structs::{102use up_data_structs::{
103 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,103 AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
104 mapping::TokenAddressMapping, budget::Budget, Property, PropertyKey, PropertyValue,104 mapping::TokenAddressMapping, budget::Budget, Property, PropertyKey, PropertyValue,
105 PropertyKeyPermission, PropertyScope, TrySetProperty, TokenChild, AuxPropertyValue,105 PropertyKeyPermission, PropertyScope, TokenChild, AuxPropertyValue, PropertiesPermissionMap,
106 PropertiesPermissionMap, TokenProperties as TokenPropertiesT,106 TokenProperties as TokenPropertiesT,
107};107};
108use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};108use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
201 pub type TokenProperties<T: Config> = StorageNMap<201 pub type TokenProperties<T: Config> = StorageNMap<
202 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),202 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
203 Value = TokenPropertiesT,203 Value = TokenPropertiesT,
204 QueryKind = ValueQuery,204 QueryKind = OptionQuery,
205 >;205 >;
206206
207 /// Custom data of a token that is serialized to bytes,207 /// Custom data of a token that is serialized to bytes,
342 <TokenData<T>>::contains_key((collection.id, token))342 <TokenData<T>>::contains_key((collection.id, token))
343 }343 }
344
345 /// Set the token property with the scope.
346 ///
347 /// - `property`: Contains key-value pair.
348 pub fn set_scoped_token_property(
349 collection_id: CollectionId,
350 token_id: TokenId,
351 scope: PropertyScope,
352 property: Property,
353 ) -> DispatchResult {
354 TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {
355 properties.try_scoped_set(scope, property.key, property.value)
356 })
357 .map_err(<CommonError<T>>::from)?;
358
359 Ok(())
360 }
361
362 /// Batch operation to set multiple properties with the same scope.
363 pub fn set_scoped_token_properties(
364 collection_id: CollectionId,
365 token_id: TokenId,
366 scope: PropertyScope,
367 properties: impl Iterator<Item = Property>,
368 ) -> DispatchResult {
369 TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
370 stored_properties.try_scoped_set_from_iter(scope, properties)
371 })
372 .map_err(<CommonError<T>>::from)?;
373
374 Ok(())
375 }
376344
377 /// Add or edit auxiliary data for the property.345 /// Add or edit auxiliary data for the property.
378 ///346 ///
13941362
1395 pub fn repair_item(collection: &NonfungibleHandle<T>, token: TokenId) -> DispatchResult {1363 pub fn repair_item(collection: &NonfungibleHandle<T>, token: TokenId) -> DispatchResult {
1396 <TokenProperties<T>>::mutate((collection.id, token), |properties| {1364 <TokenProperties<T>>::mutate((collection.id, token), |properties| {
1365 if let Some(properties) = properties {
1397 properties.recompute_consumed_space();1366 properties.recompute_consumed_space();
1398 });1367 }
1368 });
13991369
1400 Ok(())1370 Ok(())
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
435 )435 )
436 }436 }
437437
438 fn get_token_properties_map(&self, token_id: TokenId) -> up_data_structs::TokenProperties {438 fn get_token_properties_raw(
439 &self,
440 token_id: TokenId,
441 ) -> Option<up_data_structs::TokenProperties> {
439 <TokenProperties<T>>::get((self.id, token_id))442 <TokenProperties<T>>::get((self.id, token_id))
440 }443 }
441444
442 fn set_token_properties_map(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {445 fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {
443 <TokenProperties<T>>::set((self.id, token_id), map)446 <TokenProperties<T>>::insert((self.id, token_id), map)
444 }447 }
445
446 fn properties_exist(&self, token: TokenId) -> bool {
447 <TokenProperties<T>>::contains_key((self.id, token))
448 }
449448
450 fn check_nesting(449 fn check_nesting(
451 &self,450 &self,
514 }513 }
515514
516 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {515 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {
517 <Pallet<T>>::token_properties((self.id, token_id))516 <Pallet<T>>::token_properties((self.id, token_id))?
518 .get(key)517 .get(key)
519 .cloned()518 .cloned()
520 }519 }
521520
522 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {521 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {
523 let properties = <Pallet<T>>::token_properties((self.id, token_id));522 let Some(properties) = <Pallet<T>>::token_properties((self.id, token_id)) else {
523 return vec![];
524 };
524525
525 keys.map(|keys| {526 keys.map(|keys| {
526 keys.into_iter()527 keys.into_iter()
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
284 .map_err(|_| "key too long")?;284 .map_err(|_| "key too long")?;
285285
286 let props = <TokenProperties<T>>::get((self.id, token_id));286 let props =
287 <TokenProperties<T>>::get((self.id, token_id)).ok_or("Token properties not found")?;
287 let prop = props.get(&key).ok_or("key not found")?;288 let prop = props.get(&key).ok_or("key not found")?;
288289
289 Ok(prop.to_vec().into())290 Ok(prop.to_vec().into())
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
106use up_data_structs::{106use up_data_structs::{
107 AccessMode, budget::Budget, CollectionId, CreateCollectionData, mapping::TokenAddressMapping,107 AccessMode, budget::Budget, CollectionId, CreateCollectionData, mapping::TokenAddressMapping,
108 MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyScope,108 MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyScope,
109 PropertyValue, TokenId, TrySetProperty, PropertiesPermissionMap,109 PropertyValue, TokenId, PropertiesPermissionMap, CreateRefungibleExMultipleOwners,
110 CreateRefungibleExMultipleOwners, TokenOwnerError, TokenProperties as TokenPropertiesT,110 TokenOwnerError, TokenProperties as TokenPropertiesT,
111};111};
112112
175 pub type TokenProperties<T: Config> = StorageNMap<175 pub type TokenProperties<T: Config> = StorageNMap<
176 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),176 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
177 Value = TokenPropertiesT,177 Value = TokenPropertiesT,
178 QueryKind = ValueQuery,178 QueryKind = OptionQuery,
179 >;179 >;
180180
181 /// Total amount of pieces for token181 /// Total amount of pieces for token
294 <TotalSupply<T>>::contains_key((collection.id, token))294 <TotalSupply<T>>::contains_key((collection.id, token))
295 }295 }
296
297 pub fn set_scoped_token_property(
298 collection_id: CollectionId,
299 token_id: TokenId,
300 scope: PropertyScope,
301 property: Property,
302 ) -> DispatchResult {
303 TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {
304 properties.try_scoped_set(scope, property.key, property.value)
305 })
306 .map_err(<CommonError<T>>::from)?;
307
308 Ok(())
309 }
310
311 pub fn set_scoped_token_properties(
312 collection_id: CollectionId,
313 token_id: TokenId,
314 scope: PropertyScope,
315 properties: impl Iterator<Item = Property>,
316 ) -> DispatchResult {
317 TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {
318 stored_properties.try_scoped_set_from_iter(scope, properties)
319 })
320 .map_err(<CommonError<T>>::from)?;
321
322 Ok(())
323 }
324}296}
325297
326// unchecked calls skips any permission checks298// unchecked calls skips any permission checks
14261398
1427 pub fn repair_item(collection: &RefungibleHandle<T>, token: TokenId) -> DispatchResult {1399 pub fn repair_item(collection: &RefungibleHandle<T>, token: TokenId) -> DispatchResult {
1428 <TokenProperties<T>>::mutate((collection.id, token), |properties| {1400 <TokenProperties<T>>::mutate((collection.id, token), |properties| {
1401 if let Some(properties) = properties {
1429 properties.recompute_consumed_space();1402 properties.recompute_consumed_space();
1430 });1403 }
1404 });
14311405
1432 Ok(())1406 Ok(())