git.delta.rocks / unique-network / refs/commits / 4bce10c77fc9

difftreelog

Fix collection limits

Greg Zaitsev2021-02-26parent: #4b68421.patch.diff
in: master

3 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
383 /// Collection limit bounds per collection exceeded383 /// Collection limit bounds per collection exceeded
384 CollectionLimitBoundsExceeded,384 CollectionLimitBoundsExceeded,
385 /// Tried to enable permissions which are only permitted to be disabled385 /// Tried to enable permissions which are only permitted to be disabled
386 OwnerPermissionsCantBeReturned,386 OwnerPermissionsCantBeReverted,
387 /// Schema data size limit bound exceeded387 /// Schema data size limit bound exceeded
388 SchemaDataLimitExceeded,388 SchemaDataLimitExceeded,
389 /// Maximum refungibility exceeded389 /// Maximum refungibility exceeded
626 let sender = ensure_signed(origin)?;626 let sender = ensure_signed(origin)?;
627 Self::check_owner_permissions(collection_id, sender)?;627 Self::check_owner_permissions(collection_id, sender)?;
628628
629 let mut target_collection = <Collection<T>>::get(collection_id);629 let target_collection = <Collection<T>>::get(collection_id);
630 if !target_collection.limits.owner_can_destroy {630 if !target_collection.limits.owner_can_destroy {
631 fail!(Error::<T>::NoPermission);631 fail!(Error::<T>::NoPermission);
632 }632 }
1532 pub fn set_collection_limits(1532 pub fn set_collection_limits(
1533 origin,1533 origin,
1534 collection_id: u32,1534 collection_id: u32,
1535 limits: CollectionLimits,1535 new_limits: CollectionLimits,
1536 ) -> DispatchResult {1536 ) -> DispatchResult {
1537 let sender = ensure_signed(origin)?;1537 let sender = ensure_signed(origin)?;
1538 Self::check_owner_permissions(collection_id, sender.clone())?;1538 Self::check_owner_permissions(collection_id, sender.clone())?;
1539 let mut target_collection = <Collection<T>>::get(collection_id);1539 let mut target_collection = <Collection<T>>::get(collection_id);
1540 let old_limits = target_collection.limits;
1540 let chain_limits = ChainLimit::get();1541 let chain_limits = ChainLimit::get();
1541 let climits = target_collection.limits;
15421542
1543 // collection bounds1543 // collection bounds
1544 ensure!(limits.sponsor_transfer_timeout <= MAX_SPONSOR_TIMEOUT &&1544 ensure!(new_limits.sponsor_transfer_timeout <= MAX_SPONSOR_TIMEOUT &&
1545 limits.account_token_ownership_limit <= MAX_TOKEN_OWNERSHIP, 1545 new_limits.account_token_ownership_limit <= MAX_TOKEN_OWNERSHIP &&
1546 new_limits.sponsored_data_size <= chain_limits.custom_data_limit &&
1547 new_limits.sponsored_mint_size <= chain_limits.custom_data_limit,
1546 Error::<T>::CollectionLimitBoundsExceeded);1548 Error::<T>::CollectionLimitBoundsExceeded);
15471549
1548 // token_limit check prev1550 // token_limit check prev
1549 ensure!(climits.token_limit > limits.token_limit && 1551 ensure!(old_limits.token_limit >= new_limits.token_limit, Error::<T>::CollectionTokenLimitExceeded);
1550 limits.token_limit <= chain_limits.account_token_ownership_limit, 1552 ensure!(new_limits.token_limit > 0, Error::<T>::CollectionTokenLimitExceeded);
1551 Error::<T>::AccountTokenLimitExceeded);
15521553
1553 ensure!(1554 ensure!(
1554 (climits.owner_can_transfer || !limits.owner_can_transfer) &&1555 (old_limits.owner_can_transfer || !new_limits.owner_can_transfer) &&
1555 (climits.owner_can_destroy || !limits.owner_can_destroy),1556 (old_limits.owner_can_destroy || !new_limits.owner_can_destroy),
1556 Error::<T>::OwnerPermissionsCantBeReturned,1557 Error::<T>::OwnerPermissionsCantBeReverted,
1557 );1558 );
15581559
1559 target_collection.limits = limits;1560 target_collection.limits = new_limits;
1560 <Collection<T>>::insert(collection_id, target_collection);1561 <Collection<T>>::insert(collection_id, target_collection);
15611562
1562 Ok(())1563 Ok(())
modifiedtests/src/setCollectionLimits.test.tsdiffbeforeafterboth
--- a/tests/src/setCollectionLimits.test.ts
+++ b/tests/src/setCollectionLimits.test.ts
@@ -28,21 +28,8 @@
 const accountTokenOwnershipLimit = 0;
 const sponsoredDataSize = 0;
 const sponsoredMintSize = 0;
-const tokenLimit = 0;
-
-describe('hooks', () => {
-  before(async () => {
-    await usingApi(async () => {
-      const keyring = new Keyring({ type: 'sr25519' });
-      alice = keyring.addFromUri('//Alice');
-    });
-  });
-  it('choose or create collection for testing', async () => {
-    await usingApi(async () => {
-      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
-    });
-  });
-});
+const sponsorTimeout = 1;
+const tokenLimit = 1;
 
 describe('setCollectionLimits positive', () => {
   let tx;
@@ -50,6 +37,7 @@
     await usingApi(async () => {
       const keyring = new Keyring({ type: 'sr25519' });
       alice = keyring.addFromUri('//Alice');
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
     });
   });
   it('execute setCollectionLimits with predefined params ', async () => {
@@ -57,25 +45,28 @@
       tx = api.tx.nft.setCollectionLimits(
         collectionIdForTesting,
         {
-          accountTokenOwnershipLimit,
-          sponsoredDataSize,
-          sponsoredMintSize,
-          tokenLimit,
+          AccountTokenOwnershipLimit: accountTokenOwnershipLimit,
+          SponsoredMintSize: sponsoredDataSize,
+          TokenLimit: tokenLimit,
+          SponsorTimeout: sponsorTimeout,
+          OwnerCanTransfer: true,
+          OwnerCanDestroy: true
         },
       );
       const events = await submitTransactionAsync(alice, tx);
       const result = getCreateItemResult(events);
+
+      // get collection limits defined previously
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
+
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
-    });
-  });
-  it('get collection limits defined in previous test', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
       expect(collectionInfo.Limits.AccountTokenOwnershipLimit.toNumber()).to.be.equal(accountTokenOwnershipLimit);
-      expect(collectionInfo.Limits.SponsoredMintSize.toNumber()).to.be.equal(sponsoredMintSize);
+      expect(collectionInfo.Limits.SponsoredMintSize.toNumber()).to.be.equal(sponsoredDataSize);
       expect(collectionInfo.Limits.TokenLimit.toNumber()).to.be.equal(tokenLimit);
-      expect(collectionInfo.Limits.SponsorTimeout.toNumber()).to.be.equal(sponsoredDataSize);
+      expect(collectionInfo.Limits.SponsorTimeout.toNumber()).to.be.equal(sponsorTimeout);
+      expect(collectionInfo.Limits.OwnerCanTransfer.valueOf()).to.be.true;
+      expect(collectionInfo.Limits.OwnerCanDestroy.valueOf()).to.be.true;
     });
   });
 });
@@ -87,6 +78,7 @@
       const keyring = new Keyring({ type: 'sr25519' });
       alice = keyring.addFromUri('//Alice');
       bob = keyring.addFromUri('//Bob');
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
     });
   });
   it('execute setCollectionLimits for not exists collection', async () => {
@@ -136,13 +128,41 @@
 
   it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {
     const collectionId = await createCollectionExpectSuccess();
-    await setCollectionLimitsExpectSuccess(alice, collectionId, { OwnerCanTransfer: false });
-    await setCollectionLimitsExpectFailure(alice, collectionId, { OwnerCanTransfer: true });
+    await setCollectionLimitsExpectSuccess(alice, collectionId, { 
+      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,
+      SponsoredMintSize: sponsoredDataSize,
+      TokenLimit: tokenLimit,
+      SponsorTimeout: sponsorTimeout,
+      OwnerCanTransfer: false,
+      OwnerCanDestroy: true
+    });
+    await setCollectionLimitsExpectFailure(alice, collectionId, { 
+      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,
+      SponsoredMintSize: sponsoredDataSize,
+      TokenLimit: tokenLimit,
+      SponsorTimeout: sponsorTimeout,
+      OwnerCanTransfer: true,
+      OwnerCanDestroy: true
+    });
   });
 
   it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {
     const collectionId = await createCollectionExpectSuccess();
-    await setCollectionLimitsExpectSuccess(alice, collectionId, { OwnerCanDestroy: false });
-    await setCollectionLimitsExpectFailure(alice, collectionId, { OwnerCanDestroy: true });
+    await setCollectionLimitsExpectSuccess(alice, collectionId, {
+      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,
+      SponsoredMintSize: sponsoredDataSize,
+      TokenLimit: tokenLimit,
+      SponsorTimeout: sponsorTimeout,
+      OwnerCanTransfer: true,
+      OwnerCanDestroy: false
+    });
+    await setCollectionLimitsExpectFailure(alice, collectionId, { 
+      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,
+      SponsoredMintSize: sponsoredDataSize,
+      TokenLimit: tokenLimit,
+      SponsorTimeout: sponsorTimeout,
+      OwnerCanTransfer: true,
+      OwnerCanDestroy: true
+    });
   });
 });
modifiedtests/src/types.tsdiffbeforeafterboth
--- a/tests/src/types.ts
+++ b/tests/src/types.ts
@@ -17,6 +17,8 @@
     SponsoredMintSize: BN;
     TokenLimit: BN;
     SponsorTimeout: BN;
+    OwnerCanTransfer: boolean;
+    OwnerCanDestroy: boolean;
   };
   MintMode: boolean;
   Mode: {