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

difftreelog

CORE-386 Fix PR

Trubnikov Sergey2022-06-10parent: #fd95478.patch.diff
in: master

5 files changed

modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -22,7 +22,10 @@
 pub use pallet_evm::{PrecompileOutput, PrecompileResult, PrecompileHandle, account::CrossAccountId};
 use pallet_evm_coder_substrate::dispatch_to_evm;
 use sp_std::vec::Vec;
-use up_data_structs::{Property, SponsoringRateLimit, NestingRule, OwnerRestrictedSet, AccessMode};
+use up_data_structs::{
+	Property, SponsoringRateLimit, NestingRule, OwnerRestrictedSet, AccessMode,
+	CollectionPermissions,
+};
 use alloc::format;
 
 use crate::{Pallet, CollectionHandle, Config, CollectionProperties};
@@ -211,10 +214,20 @@
 	#[solidity(rename_selector = "setNesting")]
 	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {
 		check_is_owner_or_admin(caller, self)?;
-		self.collection.permissions.nesting = Some(match enable {
-			false => NestingRule::Disabled,
-			true => NestingRule::Owner,
-		});
+		let permissions = CollectionPermissions {
+			nesting: Some(match enable {
+				false => NestingRule::Disabled,
+				true => NestingRule::Owner,
+			}),
+			..Default::default()
+		};
+		self.collection.permissions = <Pallet<T>>::clamp_permissions(
+			self.collection.mode.clone(),
+			&self.collection.permissions,
+			permissions,
+		)
+		.map_err(dispatch_to_evm::<T>)?;
+
 		save(self)?;
 		Ok(())
 	}
@@ -237,19 +250,29 @@
 			)));
 		}
 		check_is_owner_or_admin(caller, self)?;
-		self.collection.permissions.nesting = Some(match enable {
-			false => NestingRule::Disabled,
-			true => {
-				let mut bv = OwnerRestrictedSet::new();
-				for i in collections {
-					bv.try_insert(crate::eth::map_eth_to_id(&i).ok_or_else(|| {
-						Error::Revert("Can't convert address into collection id".into())
-					})?)
-					.map_err(|e| Error::Revert(format!("{:?}", e)))?;
+		let permissions = CollectionPermissions {
+			nesting: Some(match enable {
+				false => NestingRule::Disabled,
+				true => {
+					let mut bv = OwnerRestrictedSet::new();
+					for i in collections {
+						bv.try_insert(crate::eth::map_eth_to_id(&i).ok_or_else(|| {
+							Error::Revert("Can't convert address into collection id".into())
+						})?)
+						.map_err(|e| Error::Revert(format!("{:?}", e)))?;
+					}
+					NestingRule::OwnerRestricted(bv)
 				}
-				NestingRule::OwnerRestricted(bv)
-			}
-		});
+			}),
+			..Default::default()
+		};
+		self.collection.permissions = <Pallet<T>>::clamp_permissions(
+			self.collection.mode.clone(),
+			&self.collection.permissions,
+			permissions,
+		)
+		.map_err(dispatch_to_evm::<T>)?;
+		
 		save(self)?;
 		Ok(())
 	}
modifiedtests/src/setCollectionLimits.test.tsdiffbeforeafterboth
before · tests/src/setCollectionLimits.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits18import {ApiPromise} from '@polkadot/api';19import {IKeyringPair} from '@polkadot/types/types';20import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';22import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';23import {24  createCollectionExpectSuccess, getCreatedCollectionCount,25  getCreateItemResult,26  setCollectionLimitsExpectFailure,27  setCollectionLimitsExpectSuccess,28  addCollectionAdminExpectSuccess,29  queryCollectionExpectSuccess,30} from './util/helpers';3132chai.use(chaiAsPromised);33const expect = chai.expect;3435let alice: IKeyringPair;36let bob: IKeyringPair;37let collectionIdForTesting: number;3839const accountTokenOwnershipLimit = 0;40const sponsoredDataSize = 0;41const sponsorTransferTimeout = 1;42const tokenLimit = 10;4344describe('setCollectionLimits positive', () => {45  let tx;46  before(async () => {47    await usingApi(async (api, privateKeyWrapper) => {48      alice = privateKeyWrapper('//Alice');49      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});50    });51  });52  it('execute setCollectionLimits with predefined params ', async () => {53    await usingApi(async (api: ApiPromise) => {54      tx = api.tx.unique.setCollectionLimits(55        collectionIdForTesting,56        {57          accountTokenOwnershipLimit: accountTokenOwnershipLimit,58          sponsoredDataSize: sponsoredDataSize,59          tokenLimit: tokenLimit,60          sponsorTransferTimeout,61          ownerCanTransfer: true,62          ownerCanDestroy: true,63        },64      );65      const events = await submitTransactionAsync(alice, tx);66      const result = getCreateItemResult(events);6768      // get collection limits defined previously69      const collectionInfo = await queryCollectionExpectSuccess(api, collectionIdForTesting);7071      // tslint:disable-next-line:no-unused-expression72      expect(result.success).to.be.true;73      expect(collectionInfo.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.equal(accountTokenOwnershipLimit);74      expect(collectionInfo.limits.sponsoredDataSize.unwrap().toNumber()).to.be.equal(sponsoredDataSize);75      expect(collectionInfo.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);76      expect(collectionInfo.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.equal(sponsorTransferTimeout);77      expect(collectionInfo.limits.ownerCanTransfer.unwrap().toJSON()).to.be.true;78      expect(collectionInfo.limits.ownerCanDestroy.unwrap().toJSON()).to.be.true;79    });80  });8182  it('Set the same token limit twice', async () => {83    await usingApi(async (api: ApiPromise) => {8485      const collectionLimits = {86        accountTokenOwnershipLimit: accountTokenOwnershipLimit,87        sponsoredMintSize: sponsoredDataSize,88        tokenLimit: tokenLimit,89        sponsorTransferTimeout,90        ownerCanTransfer: true,91        ownerCanDestroy: true,92      };9394      // The first time95      const tx1 = api.tx.unique.setCollectionLimits(96        collectionIdForTesting,97        collectionLimits,98      );99      const events1 = await submitTransactionAsync(alice, tx1);100      const result1 = getCreateItemResult(events1);101      expect(result1.success).to.be.true;102      const collectionInfo1 = await queryCollectionExpectSuccess(api, collectionIdForTesting);103      expect(collectionInfo1.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);104105      // The second time106      const tx2 = api.tx.unique.setCollectionLimits(107        collectionIdForTesting,108        collectionLimits,109      );110      const events2 = await submitTransactionAsync(alice, tx2);111      const result2 = getCreateItemResult(events2);112      expect(result2.success).to.be.true;113      const collectionInfo2 = await queryCollectionExpectSuccess(api, collectionIdForTesting);114      expect(collectionInfo2.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);115    });116  });117118});119120describe('setCollectionLimits negative', () => {121  let tx;122  before(async () => {123    await usingApi(async (api, privateKeyWrapper) => {124      alice = privateKeyWrapper('//Alice');125      bob = privateKeyWrapper('//Bob');126      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});127    });128  });129  it('execute setCollectionLimits for not exists collection', async () => {130    await usingApi(async (api: ApiPromise) => {131      const collectionCount = await getCreatedCollectionCount(api);132      const nonExistedCollectionId = collectionCount + 1;133      tx = api.tx.unique.setCollectionLimits(134        nonExistedCollectionId,135        {136          accountTokenOwnershipLimit,137          sponsoredDataSize,138          // sponsoredMintSize,139          tokenLimit,140        },141      );142      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;143    });144  });145  it('execute setCollectionLimits from user who is not owner of this collection', async () => {146    await usingApi(async (api: ApiPromise) => {147      tx = api.tx.unique.setCollectionLimits(148        collectionIdForTesting,149        {150          accountTokenOwnershipLimit,151          sponsoredDataSize,152          // sponsoredMintSize,153          tokenLimit,154        },155      );156      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;157    });158  });159  it('execute setCollectionLimits from admin collection', async () => {160    await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);161    await usingApi(async (api: ApiPromise) => {162      tx = api.tx.unique.setCollectionLimits(163        collectionIdForTesting,164        {165          accountTokenOwnershipLimit,166          sponsoredDataSize,167          // sponsoredMintSize,168          tokenLimit,169        },170      );171      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;172    });173  });174175  it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {176    const collectionId = await createCollectionExpectSuccess();177    await setCollectionLimitsExpectSuccess(alice, collectionId, {178      accountTokenOwnershipLimit: accountTokenOwnershipLimit,179      sponsoredMintSize: sponsoredDataSize,180      tokenLimit: tokenLimit,181      sponsorTransferTimeout,182      ownerCanTransfer: false,183      ownerCanDestroy: true,184    });185    await setCollectionLimitsExpectFailure(alice, collectionId, {186      accountTokenOwnershipLimit: accountTokenOwnershipLimit,187      sponsoredMintSize: sponsoredDataSize,188      tokenLimit: tokenLimit,189      sponsorTransferTimeout,190      ownerCanTransfer: true,191      ownerCanDestroy: true,192    });193  });194195  it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {196    const collectionId = await createCollectionExpectSuccess();197    await setCollectionLimitsExpectSuccess(alice, collectionId, {198      accountTokenOwnershipLimit: accountTokenOwnershipLimit,199      sponsoredMintSize: sponsoredDataSize,200      tokenLimit: tokenLimit,201      sponsorTransferTimeout,202      ownerCanTransfer: true,203      ownerCanDestroy: false,204    });205    await setCollectionLimitsExpectFailure(alice, collectionId, {206      accountTokenOwnershipLimit: accountTokenOwnershipLimit,207      sponsoredMintSize: sponsoredDataSize,208      tokenLimit: tokenLimit,209      sponsorTransferTimeout,210      ownerCanTransfer: true,211      ownerCanDestroy: true,212    });213  });214215  it('Setting the higher token limit fails', async () => {216    await usingApi(async () => {217218      const collectionId = await createCollectionExpectSuccess();219      const collectionLimits = {220        accountTokenOwnershipLimit: accountTokenOwnershipLimit,221        sponsoredMintSize: sponsoredDataSize,222        tokenLimit: tokenLimit,223        sponsorTransferTimeout,224        ownerCanTransfer: true,225        ownerCanDestroy: true,226      };227228      // The first time229      await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);230231      // The second time - higher token limit232      collectionLimits.tokenLimit += 1;233      await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);234    });235  });236237});
modifiedtests/src/setCollectionSponsor.test.tsdiffbeforeafterboth
--- a/tests/src/setCollectionSponsor.test.ts
+++ b/tests/src/setCollectionSponsor.test.ts
@@ -65,6 +65,11 @@
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await setCollectionSponsorExpectSuccess(collectionId, charlie.address);
   });
+  it('Collection admin add sponsor', async () => {
+    const collectionId = await createCollectionExpectSuccess();
+    await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);
+    await setCollectionSponsorExpectSuccess(collectionId, charlie.address, '//Bob');
+  });
 });
 
 describe('(!negative test!) integration test: ext. setCollectionSponsor():', () => {
@@ -93,10 +98,5 @@
     const collectionId = await createCollectionExpectSuccess();
     await destroyCollectionExpectSuccess(collectionId);
     await setCollectionSponsorExpectFailure(collectionId, bob.address);
-  });
-  it('(!negative test!) Collection admin add sponsor', async () => {
-    const collectionId = await createCollectionExpectSuccess();
-    await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);
-    await setCollectionSponsorExpectFailure(collectionId, charlie.address, '//Bob');
   });
 });
modifiedtests/src/setMintPermission.test.tsdiffbeforeafterboth
--- a/tests/src/setMintPermission.test.ts
+++ b/tests/src/setMintPermission.test.ts
@@ -67,6 +67,14 @@
       await setMintPermissionExpectSuccess(alice, collectionId, false);
     });
   });
+
+  it('Collection admin success on set', async () => {
+    await usingApi(async () => {
+      const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
+      await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);
+      await setMintPermissionExpectSuccess(bob, collectionId, true);
+    });
+  });
 });
 
 describe('Negative Integration Test setMintPermission', () => {
@@ -100,14 +108,6 @@
     const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
     await enableAllowListExpectSuccess(alice, collectionId);
     await setMintPermissionExpectFailure(bob, collectionId, true);
-  });
-
-  it('Collection admin fails on set', async () => {
-    await usingApi(async () => {
-      const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
-      await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);
-      await setMintPermissionExpectFailure(bob, collectionId, true);
-    });
   });
 
   it('ensure non-allow-listed non-privileged address can\'t mint tokens', async () => {
modifiedtests/src/setPublicAccessMode.test.tsdiffbeforeafterboth
--- a/tests/src/setPublicAccessMode.test.ts
+++ b/tests/src/setPublicAccessMode.test.ts
@@ -103,22 +103,23 @@
       await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;
     });
   });
-});
 
-describe('Negative Integration Test ext. collection admin setPublicAccessMode(): ', () => {
-  before(async () => {
-    await usingApi(async (api, privateKeyWrapper) => {
-      alice = privateKeyWrapper('//Alice');
-      bob = privateKeyWrapper('//Bob');
-    });
-  });
   it('setPublicAccessMode by collection admin', async () => {
     await usingApi(async (api: ApiPromise) => {
       // tslint:disable-next-line: no-bitwise
       const collectionId = await createCollectionExpectSuccess();
       await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);
       const tx = api.tx.unique.setCollectionPermissions(collectionId, {access: 'AllowList'});
-      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;
+      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.not.rejected;
+    });
+  });
+});
+
+describe('Negative Integration Test ext. collection admin setPublicAccessMode(): ', () => {
+  before(async () => {
+    await usingApi(async (api, privateKeyWrapper) => {
+      alice = privateKeyWrapper('//Alice');
+      bob = privateKeyWrapper('//Bob');
     });
   });
 });