difftreelog
fix manage properties of nested tokens
in: master
7 files changed
pallets/common/src/lib.rsdiffbeforeafterboth1311 sender: T::CrossAccountId,1311 sender: T::CrossAccountId,1312 token_id: TokenId,1312 token_id: TokenId,1313 property: Vec<Property>,1313 property: Vec<Property>,1314 budget: &dyn Budget,1314 ) -> DispatchResultWithPostInfo;1315 ) -> DispatchResultWithPostInfo;1315 fn delete_token_properties(1316 fn delete_token_properties(1316 &self,1317 &self,1317 sender: T::CrossAccountId,1318 sender: T::CrossAccountId,1318 token_id: TokenId,1319 token_id: TokenId,1319 property_keys: Vec<PropertyKey>,1320 property_keys: Vec<PropertyKey>,1321 budget: &dyn Budget,1320 ) -> DispatchResultWithPostInfo;1322 ) -> DispatchResultWithPostInfo;1321 fn set_token_property_permissions(1323 fn set_token_property_permissions(1322 &self,1324 &self,pallets/fungible/src/common.rsdiffbeforeafterboth298 _sender: T::CrossAccountId,298 _sender: T::CrossAccountId,299 _token_id: TokenId,299 _token_id: TokenId,300 _property: Vec<Property>,300 _property: Vec<Property>,301 _budget: &dyn Budget,301 ) -> DispatchResultWithPostInfo {302 ) -> DispatchResultWithPostInfo {302 fail!(<Error<T>>::SettingPropertiesNotAllowed)303 fail!(<Error<T>>::SettingPropertiesNotAllowed)303 }304 }315 _sender: T::CrossAccountId,316 _sender: T::CrossAccountId,316 _token_id: TokenId,317 _token_id: TokenId,317 _property_keys: Vec<PropertyKey>,318 _property_keys: Vec<PropertyKey>,319 _budget: &dyn Budget,318 ) -> DispatchResultWithPostInfo {320 ) -> DispatchResultWithPostInfo {319 fail!(<Error<T>>::SettingPropertiesNotAllowed)321 fail!(<Error<T>>::SettingPropertiesNotAllowed)320 }322 }pallets/nonfungible/src/common.rsdiffbeforeafterboth220 sender: T::CrossAccountId,220 sender: T::CrossAccountId,221 token_id: TokenId,221 token_id: TokenId,222 properties: Vec<Property>,222 properties: Vec<Property>,223 budget: &dyn Budget,223 ) -> DispatchResultWithPostInfo {224 ) -> DispatchResultWithPostInfo {224 let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);225 let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);225226226 with_weight(227 with_weight(227 <Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false),228 <Pallet<T>>::set_token_properties(self, &sender, token_id, properties, false, budget),228 weight,229 weight,229 )230 )230 }231 }234 sender: T::CrossAccountId,235 sender: T::CrossAccountId,235 token_id: TokenId,236 token_id: TokenId,236 property_keys: Vec<PropertyKey>,237 property_keys: Vec<PropertyKey>,238 budget: &dyn Budget,237 ) -> DispatchResultWithPostInfo {239 ) -> DispatchResultWithPostInfo {238 let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);240 let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);239241240 with_weight(242 with_weight(241 <Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),243 <Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys, budget),242 weight,244 weight,243 )245 )244 }246 }pallets/nonfungible/src/erc.rsdiffbeforeafterboth38use crate::{38use crate::{39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,40 SelfWeightOf, weights::WeightInfo, TokenProperties,40 SelfWeightOf, weights::WeightInfo, TokenProperties,41 property_guard::PropertyGuard,41};42};424343#[solidity_interface(name = "TokenProperties")]44#[solidity_interface(name = "TokenProperties")]82 .map_err(|_| "key too long")?;83 .map_err(|_| "key too long")?;83 let value = value.try_into().map_err(|_| "value too long")?;84 let value = value.try_into().map_err(|_| "value too long")?;8586 let is_token_create = false;87 let budget = self88 .recorder89 .weight_calls_budget(<StructureWeight<T>>::find_parent());9091 let mut guard = PropertyGuard::new(92 &caller,93 self,94 TokenId(token_id),95 is_token_create,96 &budget,97 );849885 <Pallet<T>>::set_token_property(99 <Pallet<T>>::set_token_property(Property { key, value }, &mut guard)86 self,87 &caller,88 TokenId(token_id),89 Property { key, value },90 false,91 )92 .map_err(dispatch_to_evm::<T>)100 .map_err(dispatch_to_evm::<T>)93 }101 }99 .try_into()107 .try_into()100 .map_err(|_| "key too long")?;108 .map_err(|_| "key too long")?;109110 let is_token_create = false;111 let budget = self112 .recorder113 .weight_calls_budget(<StructureWeight<T>>::find_parent());114115 let mut guard = PropertyGuard::new(116 &caller,117 self,118 TokenId(token_id),119 is_token_create,120 &budget,121 );101122102 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key)123 <Pallet<T>>::delete_token_property(key, &mut guard)103 .map_err(dispatch_to_evm::<T>)124 .map_err(dispatch_to_evm::<T>)104 }125 }105126pallets/nonfungible/src/lib.rsdiffbeforeafterboth52pub mod erc;52pub mod erc;53pub mod weights;53pub mod weights;5455mod property_guard;5657use property_guard::*;545855pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;59pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;56pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;60pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;484 })488 })485 }489 }486490487 pub fn set_token_property(491 pub fn set_token_property(property: Property, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {488 collection: &NonfungibleHandle<T>,489 sender: &T::CrossAccountId,490 token_id: TokenId,491 property: Property,492 is_token_create: bool,493 ) -> DispatchResult {494 Self::check_token_change_permission(492 Self::check_token_change_permission(&property.key, guard)?;495 collection,496 sender,497 token_id,498 &property.key,499 is_token_create,500 )?;501493502 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {494 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {503 let property = property.clone();495 let property = property.clone();504 properties.try_set(property.key, property.value)496 properties.try_set(property.key, property.value)505 })497 })506 .map_err(<CommonError<T>>::from)?;498 .map_err(<CommonError<T>>::from)?;507499508 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(500 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(509 collection.id,501 guard.collection.id,510 token_id,502 guard.token,511 property.key,503 property.key,512 ));504 ));513505521 token_id: TokenId,513 token_id: TokenId,522 properties: Vec<Property>,514 properties: Vec<Property>,523 is_token_create: bool,515 is_token_create: bool,516 nesting_budget: &dyn Budget,524 ) -> DispatchResult {517 ) -> DispatchResult {518 let mut guard = PropertyGuard::new(519 sender,520 collection,521 token_id,522 is_token_create,523 nesting_budget,524 );525525 for property in properties {526 for property in properties {526 Self::set_token_property(collection, sender, token_id, property, is_token_create)?;527 Self::set_token_property(property, &mut guard)?;527 }528 }528529529 Ok(())530 Ok(())530 }531 }531532532 pub fn delete_token_property(533 pub fn delete_token_property(property_key: PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {533 collection: &NonfungibleHandle<T>,534 sender: &T::CrossAccountId,535 token_id: TokenId,536 property_key: PropertyKey,537 ) -> DispatchResult {538 Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;534 Self::check_token_change_permission(&property_key, guard)?;539535540 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {536 <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token), |properties| {541 properties.remove(&property_key)537 properties.remove(&property_key)542 })538 })543 .map_err(<CommonError<T>>::from)?;539 .map_err(<CommonError<T>>::from)?;544540545 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(541 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(546 collection.id,542 guard.collection.id,547 token_id,543 guard.token,548 property_key,544 property_key,549 ));545 ));550546551 Ok(())547 Ok(())552 }548 }553549554 fn check_token_change_permission(550 fn check_token_change_permission(property_key: &PropertyKey, guard: &mut PropertyGuard<'_, T>) -> DispatchResult {555 collection: &NonfungibleHandle<T>,556 sender: &T::CrossAccountId,557 token_id: TokenId,558 property_key: &PropertyKey,559 is_token_create: bool,560 ) -> DispatchResult {561 let permission = <PalletCommon<T>>::property_permissions(collection.id)551 let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)562 .get(property_key)552 .get(property_key)563 .cloned()553 .cloned()564 .unwrap_or_else(PropertyPermission::none);554 .unwrap_or_else(PropertyPermission::none);565566 let token_data = <TokenData<T>>::get((collection.id, token_id))567 .ok_or(<CommonError<T>>::TokenNotFound)?;568569 let check_token_owner = || -> DispatchResult {570 ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);571 Ok(())572 };573555574 let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))556 let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token))575 .get(property_key)557 .get(property_key)576 .is_some();558 .is_some();577559586 ..568 ..587 } => {569 } => {588 //TODO: investigate threats during public minting.570 //TODO: investigate threats during public minting.589 if is_token_create && (collection_admin || token_owner) {571 if guard.is_token_create && (collection_admin || token_owner) {590 return Ok(());572 return Ok(());591 }573 }592574593 let mut check_result = Err(<CommonError<T>>::NoPermission.into());575 let mut check_result = Err(<CommonError<T>>::NoPermission.into());594576595 if collection_admin {577 if collection_admin {596 check_result = collection.check_is_owner_or_admin(sender);578 check_result = guard.check_collection_admin();597 }579 }598580599 if token_owner {581 if token_owner {600 check_result.or_else(|_| check_token_owner())582 check_result.or_else(|_| guard.check_token_owner())601 } else {583 } else {602 check_result584 check_result603 }585 }611 sender: &T::CrossAccountId,593 sender: &T::CrossAccountId,612 token_id: TokenId,594 token_id: TokenId,613 property_keys: Vec<PropertyKey>,595 property_keys: Vec<PropertyKey>,596 nesting_budget: &dyn Budget,614 ) -> DispatchResult {597 ) -> DispatchResult {598 let is_token_create = false;599600 let mut guard = PropertyGuard::new(601 sender,602 collection,603 token_id,604 is_token_create,605 nesting_budget,606 );607615 for key in property_keys {608 for key in property_keys {616 Self::delete_token_property(collection, sender, token_id, key)?;609 Self::delete_token_property(key, &mut guard)?;617 }610 }618611619 Ok(())612 Ok(())829 TokenId(token),822 TokenId(token),830 data.properties.clone().into_inner(),823 data.properties.clone().into_inner(),831 true,824 true,825 nesting_budget,832 ) {826 ) {833 return TransactionOutcome::Rollback(Err(e));827 return TransactionOutcome::Rollback(Err(e));834 }828 }pallets/refungible/src/common.rsdiffbeforeafterboth314 _sender: T::CrossAccountId,314 _sender: T::CrossAccountId,315 _token_id: TokenId,315 _token_id: TokenId,316 _property: Vec<Property>,316 _property: Vec<Property>,317 _budget: &dyn Budget,317 ) -> DispatchResultWithPostInfo {318 ) -> DispatchResultWithPostInfo {318 fail!(<Error<T>>::SettingPropertiesNotAllowed)319 fail!(<Error<T>>::SettingPropertiesNotAllowed)319 }320 }331 _sender: T::CrossAccountId,332 _sender: T::CrossAccountId,332 _token_id: TokenId,333 _token_id: TokenId,333 _property_keys: Vec<PropertyKey>,334 _property_keys: Vec<PropertyKey>,335 _budget: &dyn Budget,334 ) -> DispatchResultWithPostInfo {336 ) -> DispatchResultWithPostInfo {335 fail!(<Error<T>>::SettingPropertiesNotAllowed)337 fail!(<Error<T>>::SettingPropertiesNotAllowed)336 }338 }pallets/unique/src/lib.rsdiffbeforeafterboth654 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);654 ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);655655656 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);656 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);657 let budget = budget::Value::new(NESTING_BUDGET);657658658 dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))659 dispatch_tx::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties, &budget))659 }660 }660661661 #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]662 #[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]669 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);670 ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);670671671 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);672 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);673 let budget = budget::Value::new(NESTING_BUDGET);672674673 dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))675 dispatch_tx::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys, &budget))674 }676 }675677676 #[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]678 #[weight = T::CommonWeightInfo::set_token_property_permissions(property_permissions.len() as u32)]