difftreelog
fix PR
in: master
4 files changed
pallets/nonfungible/src/erc.rsdiffbeforeafterboth90 /// @notice Set permissions for token property.90 /// @notice Set permissions for token property.91 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.91 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.92 /// @param permissions Permissions for keys.92 /// @param permissions Permissions for keys.93 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]93 fn set_token_property_permissions(94 fn set_token_property_permissions(94 &mut self,95 &mut self,95 caller: caller,96 caller: caller,98 let caller = T::CrossAccountId::from_eth(caller);99 let caller = T::CrossAccountId::from_eth(caller);99 const PERMISSIONS_FIELDS_COUNT: usize = 3;100 const PERMISSIONS_FIELDS_COUNT: usize = 3;100101101 let mut perms = <Vec<_>>::new();102 let mut perms = Vec::new();102103103 for (key, pp) in permissions {104 for (key, pp) in permissions {104 if pp.len() > PERMISSIONS_FIELDS_COUNT {105 if pp.len() > PERMISSIONS_FIELDS_COUNT {112 .into());113 .into());113 }114 }114115115 let mut token_permission = PropertyPermission {116 let mut token_permission = PropertyPermission::default();116 mutable: false,117 collection_admin: false,118 token_owner: false,119 };120117121 for (perm, value) in pp {118 for (perm, value) in pp {122 match perm {119 match perm {129 }126 }130127131 perms.push(PropertyKeyPermission {128 perms.push(PropertyKeyPermission {132 key: <Vec<u8>>::from(key)129 key: key.into_bytes().try_into().map_err(|_| "too long key")?,133 .try_into()134 .map_err(|_| "too long key")?,135 permission: token_permission,130 permission: token_permission,144 fn token_property_permissions(139 fn token_property_permissions(145 &self,140 &self,146 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {141 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {147 let mut res = <Vec<_>>::new();142 let perms = <Pallet<T>>::token_property_permission(self.id);143 Ok(perms144 .into_iter()148 for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {145 .map(|(key, pp)| {149 let key = string::from_utf8(key.into_inner()).unwrap();146 let key = string::from_utf8(key.into_inner()).expect("Stored key must be valid");150 let pp = vec![147 let pp = vec![151 (EthTokenPermissions::Mutable, pp.mutable),148 (EthTokenPermissions::Mutable, pp.mutable),152 (EthTokenPermissions::TokenOwner, pp.token_owner),149 (EthTokenPermissions::TokenOwner, pp.token_owner),153 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),150 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),154 ];151 ];155 res.push((key, pp));152 (key, pp)156 }153 })157 Ok(res)154 .collect())158 }155 }159156160 /// @notice Set token property value.157 /// @notice Set token property value.pallets/refungible/src/erc.rsdiffbeforeafterboth93 /// @notice Set permissions for token property.93 /// @notice Set permissions for token property.94 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.94 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.95 /// @param permissions Permissions for keys.95 /// @param permissions Permissions for keys.96 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]96 fn set_token_property_permissions(97 fn set_token_property_permissions(97 &mut self,98 &mut self,98 caller: caller,99 caller: caller,101 let caller = T::CrossAccountId::from_eth(caller);102 let caller = T::CrossAccountId::from_eth(caller);102 const PERMISSIONS_FIELDS_COUNT: usize = 3;103 const PERMISSIONS_FIELDS_COUNT: usize = 3;103104104 let mut perms = <Vec<_>>::new();105 let mut perms = Vec::new();105106106 for (key, pp) in permissions {107 for (key, pp) in permissions {107 if pp.len() > PERMISSIONS_FIELDS_COUNT {108 if pp.len() > PERMISSIONS_FIELDS_COUNT {132 }133 }133134134 perms.push(PropertyKeyPermission {135 perms.push(PropertyKeyPermission {135 key: <Vec<u8>>::from(key)136 key: key.into_bytes().try_into().map_err(|_| "too long key")?,136 .try_into()137 .map_err(|_| "too long key")?,138 permission: token_permission,137 permission: token_permission,147 fn token_property_permissions(146 fn token_property_permissions(148 &self,147 &self,149 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {148 ) -> Result<Vec<(string, Vec<(EthTokenPermissions, bool)>)>> {150 let mut res = <Vec<_>>::new();149 let perms = <Pallet<T>>::token_property_permission(self.id);150 Ok(perms151 .into_iter()151 for (key, pp) in <Pallet<T>>::token_property_permission(self.id) {152 .map(|(key, pp)| {152 let key = string::from_utf8(key.into_inner()).unwrap();153 let key = string::from_utf8(key.into_inner()).expect("Stored key must be valid");153 let pp = [154 let pp = vec![154 (EthTokenPermissions::Mutable, pp.mutable),155 (EthTokenPermissions::Mutable, pp.mutable),155 (EthTokenPermissions::TokenOwner, pp.token_owner),156 (EthTokenPermissions::TokenOwner, pp.token_owner),156 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),157 (EthTokenPermissions::CollectionAdmin, pp.collection_admin),157 ]158 ];158 .into();159 res.push((key, pp));159 (key, pp)160 }160 })161 Ok(res)161 .collect())162 }162 }163163164 /// @notice Set token property value.164 /// @notice Set token property value.primitives/data-structs/src/lib.rsdiffbeforeafterboth1010pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;1010pub type PropertyValue = BoundedBytes<ConstU32<MAX_PROPERTY_VALUE_LENGTH>>;101110111012/// Property permission.1012/// Property permission.1013#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone)]1013#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen, PartialEq, Clone, Default)]1014#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1014#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]1015pub struct PropertyPermission {1015pub struct PropertyPermission {1016 /// Permission to change the property and property permission.1016 /// Permission to change the property and property permission.tests/src/eth/tokenProperties.test.tsdiffbeforeafterboth68 }68 }69 }));69 }));7071 [72 {mode: 'nft' as const, requiredPallets: []},73 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},74 ].map(testCase =>75 itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as owner`, testCase.requiredPallets, async({helper}) => {76 const owner = await helper.eth.createAccountWithBalance(donor);77 78 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');79 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);8081 await collection.methods.setTokenPropertyPermissions([82 ['testKey_0', [83 [EthTokenPermissions.Mutable, true], 84 [EthTokenPermissions.TokenOwner, true], 85 [EthTokenPermissions.CollectionAdmin, true]],86 ],87 ['testKey_1', [88 [EthTokenPermissions.Mutable, true], 89 [EthTokenPermissions.TokenOwner, false], 90 [EthTokenPermissions.CollectionAdmin, true]],91 ],92 ['testKey_2', [93 [EthTokenPermissions.Mutable, false], 94 [EthTokenPermissions.TokenOwner, true], 95 [EthTokenPermissions.CollectionAdmin, false]],96 ],97 ]).send({from: owner});98 99 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([100 {101 key: 'testKey_0',102 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},103 },104 {105 key: 'testKey_1',106 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},107 },108 {109 key: 'testKey_2',110 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},111 },112 ]);113114 expect(await collection.methods.tokenPropertyPermissions().call({from: owner})).to.be.like([115 ['testKey_0', [116 [EthTokenPermissions.Mutable.toString(), true], 117 [EthTokenPermissions.TokenOwner.toString(), true], 118 [EthTokenPermissions.CollectionAdmin.toString(), true]],119 ],120 ['testKey_1', [121 [EthTokenPermissions.Mutable.toString(), true], 122 [EthTokenPermissions.TokenOwner.toString(), false], 123 [EthTokenPermissions.CollectionAdmin.toString(), true]],124 ],125 ['testKey_2', [126 [EthTokenPermissions.Mutable.toString(), false], 127 [EthTokenPermissions.TokenOwner.toString(), true], 128 [EthTokenPermissions.CollectionAdmin.toString(), false]],129 ],130 ]);131 132 }));133134 [135 {mode: 'nft' as const, requiredPallets: []},136 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},137 ].map(testCase =>138 itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as admin`, testCase.requiredPallets, async({helper}) => {139 const owner = await helper.eth.createAccountWithBalance(donor);140 const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);141 142 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');143 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);144 await collection.methods.addCollectionAdminCross(caller).send({from: owner});145146 await collection.methods.setTokenPropertyPermissions([147 ['testKey_0', [148 [EthTokenPermissions.Mutable, true], 149 [EthTokenPermissions.TokenOwner, true], 150 [EthTokenPermissions.CollectionAdmin, true]],151 ],152 ['testKey_1', [153 [EthTokenPermissions.Mutable, true], 154 [EthTokenPermissions.TokenOwner, false], 155 [EthTokenPermissions.CollectionAdmin, true]],156 ],157 ['testKey_2', [158 [EthTokenPermissions.Mutable, false], 159 [EthTokenPermissions.TokenOwner, true], 160 [EthTokenPermissions.CollectionAdmin, false]],161 ],162 ]).send({from: caller.eth});163 164 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([165 {166 key: 'testKey_0',167 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},168 },169 {170 key: 'testKey_1',171 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},172 },173 {174 key: 'testKey_2',175 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},176 },177 ]);178179 expect(await collection.methods.tokenPropertyPermissions().call({from: caller.eth})).to.be.like([180 ['testKey_0', [181 [EthTokenPermissions.Mutable.toString(), true], 182 [EthTokenPermissions.TokenOwner.toString(), true], 183 [EthTokenPermissions.CollectionAdmin.toString(), true]],184 ],185 ['testKey_1', [186 [EthTokenPermissions.Mutable.toString(), true], 187 [EthTokenPermissions.TokenOwner.toString(), false], 188 [EthTokenPermissions.CollectionAdmin.toString(), true]],189 ],190 ['testKey_2', [191 [EthTokenPermissions.Mutable.toString(), false], 192 [EthTokenPermissions.TokenOwner.toString(), true], 193 [EthTokenPermissions.CollectionAdmin.toString(), false]],194 ],195 ]);196 197 }));7019871 [199 [72 {200 {320 expect(actualProps).to.deep.eq(expectedProps);448 expect(actualProps).to.deep.eq(expectedProps);321 }));449 }));450451 [452 {mode: 'nft' as const, requiredPallets: []},453 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},454 ].map(testCase =>455 itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions as non owner or admin`, testCase.requiredPallets, async({helper}) => {456 const owner = await helper.eth.createAccountWithBalance(donor);457 const caller = await helper.eth.createAccountWithBalance(donor);458 459 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');460 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);461 462 await expect(collection.methods.setTokenPropertyPermissions([463 ['testKey_0', [464 [EthTokenPermissions.Mutable, true], 465 [EthTokenPermissions.TokenOwner, true], 466 [EthTokenPermissions.CollectionAdmin, true]],467 ],468 ]).call({from: caller})).to.be.rejectedWith('NoPermission'); 469 }));470471 [472 {mode: 'nft' as const, requiredPallets: []},473 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},474 ].map(testCase =>475 itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions with invalid character`, testCase.requiredPallets, async({helper}) => {476 const owner = await helper.eth.createAccountWithBalance(donor);477 478 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');479 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);480 481 await expect(collection.methods.setTokenPropertyPermissions([482 // "Space" is invalid character483 ['testKey 0', [484 [EthTokenPermissions.Mutable, true], 485 [EthTokenPermissions.TokenOwner, true], 486 [EthTokenPermissions.CollectionAdmin, true]],487 ],488 ]).call({from: owner})).to.be.rejectedWith('InvalidCharacterInPropertyKey'); 489 }));490 322});491});323492