1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from '@unique/test-utils/util.js';1920describe('Number of tokens per address (NFT)', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({url: import.meta.url});27 [alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);28 });29 });3031 itSub.skip('Collection limits allow greater number than chain limits, chain limits are enforced', async ({helper}) => {32 const collection = await helper.nft.mintCollection(alice, {});33 await collection.setLimits(alice, {accountTokenOwnershipLimit: 20});3435 for(let i = 0; i < 10; i++){36 await expect(collection.mintToken(alice)).to.be.not.rejected;37 }38 await expect(collection.mintToken(alice)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);39 for(let i = 1; i < 11; i++) {40 await expect(collection.burnToken(alice, i)).to.be.not.rejected;41 }42 await collection.burn(alice);43 });4445 itSub('Collection limits allow lower number than chain limits, collection limits are enforced', async ({helper}) => {46 const collection = await helper.nft.mintCollection(alice, {});47 await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});4849 await collection.mintToken(alice);50 await expect(collection.mintToken(alice)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);5152 await collection.burnToken(alice, 1);53 await expect(collection.burn(alice)).to.be.not.rejected;54 });5556 itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {57 const collection = await helper.nft.mintCollection(alice, {});58 await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});5960 61 const tkn1 = await collection.mintToken(alice);62 await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});6364 65 const tkn2 = await collection.mintToken(alice);66 await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);6768 69 70 await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});71 await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});7273 74 const tkn3 = await collection.mintToken(alice);75 await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);76 });77});7879describe('Number of tokens per address (ReFungible)', () => {80 let alice: IKeyringPair;81 let bob: IKeyringPair;8283 before(async function() {84 await usingPlaygrounds(async (helper, privateKey) => {85 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);8687 const donor = await privateKey({url: import.meta.url});88 [alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);89 });90 });9192 itSub.skip('Collection limits allow greater number than chain limits, chain limits are enforced', async ({helper}) => {93 const collection = await helper.rft.mintCollection(alice, {});94 await collection.setLimits(alice, {accountTokenOwnershipLimit: 20});9596 for(let i = 0; i < 10; i++){97 await expect(collection.mintToken(alice, 10n)).to.be.not.rejected;98 }99 await expect(collection.mintToken(alice, 10n)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);100 for(let i = 1; i < 11; i++) {101 await expect(collection.burnToken(alice, i, 10n)).to.be.not.rejected;102 }103 await collection.burn(alice);104 });105106 itSub('Collection limits allow lower number than chain limits, collection limits are enforced', async ({helper}) => {107 const collection = await helper.rft.mintCollection(alice, {});108 await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});109110 await collection.mintToken(alice);111 await expect(collection.mintToken(alice)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);112113 await collection.burnToken(alice, 1);114 await expect(collection.burn(alice)).to.be.not.rejected;115 });116117 itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {118 const collection = await helper.rft.mintCollection(alice, {});119 await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});120121 122 const tkn1 = await collection.mintToken(alice);123 await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});124125 126 const tkn2 = await collection.mintToken(alice);127 await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);128129 130 131 await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});132 await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});133134 135 const tkn3 = await collection.mintToken(alice);136 await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);137 });138});139140141describe.skip('Sponsor timeout (NFT) (only for special chain limits test)', () => {142 143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332});333334describe('Collection zero limits (NFT)', () => {335 let alice: IKeyringPair;336 let bob: IKeyringPair;337 let charlie: IKeyringPair;338339 before(async () => {340 await usingPlaygrounds(async (helper, privateKey) => {341 const donor = await privateKey({url: import.meta.url});342 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);343 });344 });345346 itSub.skip('Limits have 0 in tokens per address field, the chain limits are applied', async ({helper}) => {347 const collection = await helper.nft.mintCollection(alice, {});348 await collection.setLimits(alice, {accountTokenOwnershipLimit: 0});349350 for(let i = 0; i < 10; i++){351 await collection.mintToken(alice);352 }353 await expect(collection.mintToken(alice)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);354 });355356 itSub('Limits have 0 in sponsor timeout, no limits are applied', async ({helper}) => {357 const collection = await helper.nft.mintCollection(alice, {});358 await collection.setLimits(alice, {sponsorTransferTimeout: 0});359 const token = await collection.mintToken(alice);360361 await collection.setSponsor(alice, alice.address);362 await collection.confirmSponsorship(alice);363364 await token.transfer(alice, {Substrate: bob.address});365 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);366367 368 await helper.wait.newBlocks(1);369 await token.transfer(bob, {Substrate: charlie.address});370 const aliceBalanceAfterSponsoredTransaction1 = await helper.balance.getSubstrate(alice.address);371 expect(aliceBalanceAfterSponsoredTransaction1 < aliceBalanceBefore).to.be.true;372 });373});374375describe('Collection zero limits (Fungible)', () => {376 let alice: IKeyringPair;377 let bob: IKeyringPair;378 let charlie: IKeyringPair;379380 before(async () => {381 await usingPlaygrounds(async (helper, privateKey) => {382 const donor = await privateKey({url: import.meta.url});383 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);384 });385 });386387 itSub('Limits have 0 in sponsor timeout, no limits are applied', async ({helper}) => {388 const collection = await helper.ft.mintCollection(alice, {});389 await collection.setLimits(alice, {sponsorTransferTimeout: 0});390 await collection.mint(alice, 3n);391392 await collection.setSponsor(alice, alice.address);393 await collection.confirmSponsorship(alice);394395 await collection.transfer(alice, {Substrate: bob.address}, 2n);396 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);397398 399 await helper.wait.newBlocks(1);400 await collection.transfer(bob, {Substrate: charlie.address});401 const aliceBalanceAfterSponsoredTransaction1 = await helper.balance.getSubstrate(alice.address);402 expect(aliceBalanceAfterSponsoredTransaction1 < aliceBalanceBefore).to.be.true;403 });404});405406describe('Collection zero limits (ReFungible)', () => {407 let alice: IKeyringPair;408 let bob: IKeyringPair;409 let charlie: IKeyringPair;410411 before(async function() {412 await usingPlaygrounds(async (helper, privateKey) => {413 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);414415 const donor = await privateKey({url: import.meta.url});416 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);417 });418 });419420 itSub.skip('Limits have 0 in tokens per address field, the chain limits are applied', async ({helper}) => {421 const collection = await helper.rft.mintCollection(alice, {});422 await collection.setLimits(alice, {accountTokenOwnershipLimit: 0});423 for(let i = 0; i < 10; i++){424 await collection.mintToken(alice);425 }426 await expect(collection.mintToken(alice)).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);427 });428429 itSub('Limits have 0 in sponsor timeout, no limits are applied', async ({helper}) => {430 const collection = await helper.rft.mintCollection(alice, {});431 await collection.setLimits(alice, {sponsorTransferTimeout: 0});432 const token = await collection.mintToken(alice, 3n);433434 await collection.setSponsor(alice, alice.address);435 await collection.confirmSponsorship(alice);436437 await token.transfer(alice, {Substrate: bob.address}, 2n);438 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);439440 441 await helper.wait.newBlocks(1);442 await token.transfer(bob, {Substrate: charlie.address});443 const aliceBalanceAfterSponsoredTransaction1 = await helper.balance.getSubstrate(alice.address);444 expect(aliceBalanceAfterSponsoredTransaction1 < aliceBalanceBefore).to.be.true;445 });446});447448describe('Effective collection limits (NFT)', () => {449 let alice: IKeyringPair;450451 before(async () => {452 await usingPlaygrounds(async (helper, privateKey) => {453 const donor = await privateKey({url: import.meta.url});454 [alice] = await helper.arrange.createAccounts([10n], donor);455 });456 });457458 itSub('Effective collection limits', async ({helper}) => {459 const collection = await helper.nft.mintCollection(alice, {});460 await collection.setLimits(alice, {ownerCanTransfer: true});461462 {463 464 const collectionInfo = await collection.getData();465 const limits = collectionInfo?.raw.limits;466 expect(limits).to.be.any;467468 expect(limits.accountTokenOwnershipLimit).to.be.null;469 expect(limits.sponsoredDataSize).to.be.null;470 expect(limits.sponsoredDataRateLimit).to.be.null;471 expect(limits.tokenLimit).to.be.null;472 expect(limits.sponsorTransferTimeout).to.be.null;473 expect(limits.sponsorApproveTimeout).to.be.null;474 expect(limits.ownerCanTransfer).to.be.true;475 expect(limits.ownerCanDestroy).to.be.null;476 expect(limits.transfersEnabled).to.be.null;477 }478479 { 480 const limits = await helper.collection.getEffectiveLimits(999999);481 expect(limits).to.be.null;482 }483484 { 485 const limits = await collection.getEffectiveLimits();486487 expect(limits.accountTokenOwnershipLimit).to.be.eq(100000000);488 expect(limits.sponsoredDataSize).to.be.eq(2048);489 expect(limits.sponsoredDataRateLimit).to.be.deep.eq({sponsoringDisabled: null});490 expect(limits.tokenLimit).to.be.eq(4294967295);491 expect(limits.sponsorTransferTimeout).to.be.eq(5);492 expect(limits.sponsorApproveTimeout).to.be.eq(5);493 expect(limits.ownerCanTransfer).to.be.true;494 expect(limits.ownerCanDestroy).to.be.true;495 expect(limits.transfersEnabled).to.be.true;496 }497498 {499 500 await collection.setLimits(alice, {501 accountTokenOwnershipLimit: 99_999,502 sponsoredDataSize: 1024,503 tokenLimit: 123,504 transfersEnabled: false,505 });506507 const limits = await collection.getEffectiveLimits();508509 expect(limits.accountTokenOwnershipLimit).to.be.eq(99999);510 expect(limits.sponsoredDataSize).to.be.eq(1024);511 expect(limits.sponsoredDataRateLimit).to.be.deep.eq({sponsoringDisabled: null});512 expect(limits.tokenLimit).to.be.eq(123);513 expect(limits.sponsorTransferTimeout).to.be.eq(5);514 expect(limits.sponsorApproveTimeout).to.be.eq(5);515 expect(limits.ownerCanTransfer).to.be.true;516 expect(limits.ownerCanDestroy).to.be.true;517 expect(limits.transfersEnabled).to.be.false;518 }519 });520});