difftreelog
feat set/get tokenPropertyPermissions for NFT
in: master
5 files changed
pallets/nonfungible/src/erc.rsdiffbeforeafterboth34 CollectionPropertiesVec,34 CollectionPropertiesVec,35};35};36use pallet_evm_coder_substrate::dispatch_to_evm;36use pallet_evm_coder_substrate::dispatch_to_evm;37use sp_std::vec::Vec;37use sp_std::{vec::Vec, vec};38use pallet_common::{38use pallet_common::{39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},70 token_owner: bool,70 token_owner: bool,71 ) -> Result<()> {71 ) -> Result<()> {72 let caller = T::CrossAccountId::from_eth(caller);72 let caller = T::CrossAccountId::from_eth(caller);73 <Pallet<T>>::set_property_permissions(73 <Pallet<T>>::set_token_property_permissions(74 self,74 self,75 &caller,75 &caller,76 [PropertyKeyPermission {76 vec![PropertyKeyPermission {77 key: <Vec<u8>>::from(key)77 key: <Vec<u8>>::from(key)78 .try_into()78 .try_into()79 .map_err(|_| "too long key")?,79 .map_err(|_| "too long key")?,80 permission: PropertyPermission {80 permission: PropertyPermission {81 mutable: is_mutable,81 mutable: is_mutable,82 collection_admin,82 collection_admin,83 token_owner,83 token_owner,84 },84 },85 }]85 }],86 .into(),87 )86 )88 .map_err(dispatch_to_evm::<T>)87 .map_err(dispatch_to_evm::<T>)89 }88 }137 });136 });138 }137 }139138140 <Pallet<T>>::set_property_permissions(self, &caller, perms).map_err(dispatch_to_evm::<T>)139 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)140 .map_err(dispatch_to_evm::<T>)141 }141 }142142pallets/nonfungible/src/lib.rsdiffbeforeafterboth824 )824 )825 }825 }826827 /// Set property permissions for the collection.828 ///829 /// Sender should be the owner or admin of the collection.830 pub fn set_property_permissions(831 collection: &CollectionHandle<T>,832 sender: &T::CrossAccountId,833 permission: Vec<PropertyKeyPermission>,834 ) -> DispatchResult {835 <PalletCommon<T>>::set_token_property_permissions(collection, sender, permission)836 }837826838 pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {827 pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {839 <PalletCommon<T>>::property_permissions(collection_id)828 <PalletCommon<T>>::property_permissions(collection_id)pallets/refungible/src/erc.rsdiffbeforeafterboth33use pallet_common::{33use pallet_common::{34 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,34 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,35 erc::{CommonEvmHandler, CollectionCall, static_property::key},35 erc::{CommonEvmHandler, CollectionCall, static_property::key},36 eth::EthCrossAccount,36 eth::{EthCrossAccount, EthTokenPermissions},37 Error as CommonError,37 Error as CommonError,38};38};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};63 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.63 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.64 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.64 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.65 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]65 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]66 #[solidity(hide)]66 fn set_token_property_permission(67 fn set_token_property_permission(67 &mut self,68 &mut self,68 caller: caller,69 caller: caller,89 .map_err(dispatch_to_evm::<T>)90 .map_err(dispatch_to_evm::<T>)90 }91 }9293 /// @notice Set permissions for token property.94 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.95 /// @param permissions Permissions for keys.96 fn set_token_property_permissions(97 &mut self,98 caller: caller,99 permissions: Vec<(string, Vec<(EthTokenPermissions, bool)>)>,100 ) -> Result<()> {101 let caller = T::CrossAccountId::from_eth(caller);102 const PERMISSIONS_FIELDS_COUNT: usize = 3;103104 let mut perms = <Vec<_>>::new();105106 for (key, pp) in permissions {107 if pp.len() > PERMISSIONS_FIELDS_COUNT {108 return Err(alloc::format!(109 "Actual number of fields {} for {}, which exceeds the maximum value of {}",110 pp.len(),111 stringify!(EthTokenPermissions),112 PERMISSIONS_FIELDS_COUNT113 )114 .as_str()115 .into());116 }117118 let mut token_permission = PropertyPermission {119 mutable: false,120 collection_admin: false,121 token_owner: false,122 };123124 for (perm, value) in pp {125 match perm {126 EthTokenPermissions::Mutable => token_permission.mutable = value,127 EthTokenPermissions::TokenOwner => token_permission.token_owner = value,128 EthTokenPermissions::CollectionAdmin => {129 token_permission.collection_admin = value130 }131 }132 }133134 perms.push(PropertyKeyPermission {135 key: <Vec<u8>>::from(key)136 .try_into()137 .map_err(|_| "too long key")?,138 permission: token_permission,139 });140 }141142 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)143 .map_err(dispatch_to_evm::<T>)144 }145146 fn token_property_permissions(147 &self,148 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {149 let mut res = <Vec<_>>::new();150 for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {151 let key = string::from_utf8(key.into_inner()).unwrap();152 let pp = [153 (EthTokenPermissions::Mutable, pp.mutable),154 (EthTokenPermissions::TokenOwner, pp.token_owner),155 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),156 ]157 .into();158 res.push((key, pp));159 }160 Ok(res)161 }9116292 /// @notice Set token property value.163 /// @notice Set token property value.93 /// @dev Throws error if `msg.sender` has no permission to edit the property.164 /// @dev Throws error if `msg.sender` has no permission to edit the property.pallets/refungible/src/lib.rsdiffbeforeafterboth113 AccessMode, budget::Budget, CollectionId, CollectionFlags, CollectionPropertiesVec,113 AccessMode, budget::Budget, CollectionId, CollectionFlags, CollectionPropertiesVec,114 CreateCollectionData, CustomDataLimit, mapping::TokenAddressMapping, MAX_ITEMS_PER_BATCH,114 CreateCollectionData, CustomDataLimit, mapping::TokenAddressMapping, MAX_ITEMS_PER_BATCH,115 MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyPermission,115 MAX_REFUNGIBLE_PIECES, Property, PropertyKey, PropertyKeyPermission, PropertyPermission,116 PropertyScope, PropertyValue, TokenId, TrySetProperty,116 PropertyScope, PropertyValue, TokenId, TrySetProperty, PropertiesPermissionMap,117};117};118118119pub use pallet::*;119pub use pallet::*;1378 <PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)1378 <PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)1379 }1379 }13801381 pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {1382 <PalletCommon<T>>::property_permissions(collection_id)1383 }138013841381 pub fn set_scoped_token_property_permissions(1385 pub fn set_scoped_token_property_permissions(1382 collection: &RefungibleHandle<T>,1386 collection: &RefungibleHandle<T>,tests/src/eth/tokenProperties.test.tsdiffbeforeafterboth32 });32 });33 });33 });343435 itEth.only('Can be reconfigured', async({helper}) => {35 itEth('Can be reconfigured', async({helper}) => {36 const caller = await helper.eth.createAccountWithBalance(donor);36 const caller = await helper.eth.createAccountWithBalance(donor);37 for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {37 for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {38 const collection = await helper.nft.mintCollection(alice);38 const collection = await helper.nft.mintCollection(alice);