git.delta.rocks / unique-network / refs/commits / 0ceea0af66a9

difftreelog

fix account token ownership limit when transfer (#1080)

Daniel Shiposha2024-06-25parent: #347f218.patch.diff
in: master
* fix: account token ownership limit when mint

* fix: limit tests

* fix: account token ownership limit rft mint

* Revert "fix: account token ownership limit rft mint"

This reverts commit 054110e015fd237fed77be1ef21bba6cea13fbea.

* Revert "fix: limit tests"

This reverts commit 75699574656bb0a34caddf3c9abdbffef79ba85d.

* Revert "fix: account token ownership limit when mint"

This reverts commit b89a3c86d3bf0888a636bc7da1949affa12d867d.

* fix: account token ownership limit when transfer

* test: account token ownership transfer tests

---------

3 files changed

modifiedjs-packages/tests/limits.test.tsdiffbeforeafterboth
--- a/js-packages/tests/limits.test.ts
+++ b/js-packages/tests/limits.test.ts
@@ -19,11 +19,12 @@
 
 describe('Number of tokens per address (NFT)', () => {
   let alice: IKeyringPair;
+  let bob: IKeyringPair;
 
   before(async () => {
     await usingPlaygrounds(async (helper, privateKey) => {
       const donor = await privateKey({url: import.meta.url});
-      [alice] = await helper.arrange.createAccounts([10n], donor);
+      [alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);
     });
   });
 
@@ -51,17 +52,40 @@
     await collection.burnToken(alice, 1);
     await expect(collection.burn(alice)).to.be.not.rejected;
   });
+
+  itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {
+    const collection = await helper.nft.mintCollection(alice, {});
+    await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});
+
+    // Limit 1 - can transfer #1 token
+    const tkn1 = await collection.mintToken(alice);
+    await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});
+
+    // Limit 1 - cannot transfer #2 token
+    const tkn2 = await collection.mintToken(alice);
+    await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
+
+    // Increase limit to 2
+    // Now can transfer token #2
+    await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});
+    await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});
+
+    // But cannot transfer token #3
+    const tkn3 = await collection.mintToken(alice);
+    await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
+  });
 });
 
 describe('Number of tokens per address (ReFungible)', () => {
   let alice: IKeyringPair;
+  let bob: IKeyringPair;
 
   before(async function() {
     await usingPlaygrounds(async (helper, privateKey) => {
       requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);
 
       const donor = await privateKey({url: import.meta.url});
-      [alice] = await helper.arrange.createAccounts([10n], donor);
+      [alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);
     });
   });
 
@@ -89,6 +113,28 @@
     await collection.burnToken(alice, 1);
     await expect(collection.burn(alice)).to.be.not.rejected;
   });
+
+  itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {
+    const collection = await helper.rft.mintCollection(alice, {});
+    await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});
+
+    // Limit 1 - can transfer #1 token
+    const tkn1 = await collection.mintToken(alice);
+    await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});
+
+    // Limit 1 - cannot transfer #2 token
+    const tkn2 = await collection.mintToken(alice);
+    await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
+
+    // Increase limit to 2
+    // Now can transfer token #2
+    await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});
+    await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});
+
+    // But cannot transfer token #3
+    const tkn3 = await collection.mintToken(alice);
+    await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
+  });
 });
 
 // todo:playgrounds skipped ~ postponed
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
746 .ok_or(ArithmeticError::Overflow)?;746 .ok_or(ArithmeticError::Overflow)?;
747747
748 ensure!(748 ensure!(
749 balance_to < collection.limits.account_token_ownership_limit(),749 balance_to <= collection.limits.account_token_ownership_limit(),
750 <CommonError<T>>::AccountTokenLimitExceeded,750 <CommonError<T>>::AccountTokenLimitExceeded,
751 );751 );
752752
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -679,7 +679,7 @@
 				.checked_add(1)
 				.ok_or(ArithmeticError::Overflow)?;
 			ensure!(
-				account_balance_to < collection.limits.account_token_ownership_limit(),
+				account_balance_to <= collection.limits.account_token_ownership_limit(),
 				<CommonError<T>>::AccountTokenLimitExceeded,
 			);