1import type {IKeyringPair} from '@polkadot/types/types';2import {Pallets} from '@unique/test-utils/util.js';3import {expect, itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';4import {CollectionLimitField, CreateCollectionData} from '@unique/test-utils/eth/types.js';567describe('Can set collection limits', () => {8 let donor: IKeyringPair;910 before(async () => {11 await usingEthPlaygrounds(async (_helper, privateKey) => {12 donor = await privateKey({url: import.meta.url});13 });14 });1516 [17 {case: 'nft' as const},18 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},19 {case: 'ft' as const},20 ].map(testCase =>21 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {22 const owner = await helper.eth.createAccountWithBalance(donor);23 const {collectionId, collectionAddress} = await helper.eth.createCollection(owner, new CreateCollectionData('Limits', 'absolutely anything', 'FLO', testCase.case, 18)).send();24 const limits = {25 accountTokenOwnershipLimit: 1000,26 sponsoredDataSize: 1024,27 sponsoredDataRateLimit: 30,28 tokenLimit: 1000000,29 sponsorTransferTimeout: 6,30 sponsorApproveTimeout: 6,31 ownerCanTransfer: 1,32 ownerCanDestroy: 0,33 transfersEnabled: 0,34 };3536 const expectedLimits = {37 accountTokenOwnershipLimit: 1000,38 sponsoredDataSize: 1024,39 sponsoredDataRateLimit: {blocks: 30},40 tokenLimit: 1000000,41 sponsorTransferTimeout: 6,42 sponsorApproveTimeout: 6,43 ownerCanTransfer: true,44 ownerCanDestroy: false,45 transfersEnabled: false,46 };4748 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);49 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: limits.accountTokenOwnershipLimit}}).send();50 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.SponsoredDataSize, value: {status: true, value: limits.sponsoredDataSize}}).send();51 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.SponsoredDataRateLimit, value: {status: true, value: limits.sponsoredDataRateLimit}}).send();52 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.TokenLimit, value: {status: true, value: limits.tokenLimit}}).send();53 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.SponsorTransferTimeout, value: {status: true, value: limits.sponsorTransferTimeout}}).send();54 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.SponsorApproveTimeout, value: {status: true, value: limits.sponsorApproveTimeout}}).send();55 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.OwnerCanTransfer, value: {status: true, value: limits.ownerCanTransfer}}).send();56 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.OwnerCanDestroy, value: {status: true, value: limits.ownerCanDestroy}}).send();57 await collectionEvm.methods.setCollectionLimit({field: CollectionLimitField.TransferEnabled, value: {status: true, value: limits.transfersEnabled}}).send();5859 60 const data = (await helper.rft.getData(collectionId))!;61 expect(data.raw.limits).to.deep.eq(expectedLimits);62 expect(await helper.collection.getEffectiveLimits(collectionId)).to.deep.eq(expectedLimits);63 64 const limitsEvm = await collectionEvm.methods.collectionLimits().call({from: owner});65 expect(limitsEvm).to.have.length(9);66 expect(limitsEvm[0]).to.deep.eq([CollectionLimitField.AccountTokenOwnership.toString(), [true, limits.accountTokenOwnershipLimit.toString()]]);67 expect(limitsEvm[1]).to.deep.eq([CollectionLimitField.SponsoredDataSize.toString(), [true, limits.sponsoredDataSize.toString()]]);68 expect(limitsEvm[2]).to.deep.eq([CollectionLimitField.SponsoredDataRateLimit.toString(), [true, limits.sponsoredDataRateLimit.toString()]]);69 expect(limitsEvm[3]).to.deep.eq([CollectionLimitField.TokenLimit.toString(), [true, limits.tokenLimit.toString()]]);70 expect(limitsEvm[4]).to.deep.eq([CollectionLimitField.SponsorTransferTimeout.toString(), [true, limits.sponsorTransferTimeout.toString()]]);71 expect(limitsEvm[5]).to.deep.eq([CollectionLimitField.SponsorApproveTimeout.toString(), [true, limits.sponsorApproveTimeout.toString()]]);72 expect(limitsEvm[6]).to.deep.eq([CollectionLimitField.OwnerCanTransfer.toString(), [true, limits.ownerCanTransfer.toString()]]);73 expect(limitsEvm[7]).to.deep.eq([CollectionLimitField.OwnerCanDestroy.toString(), [true, limits.ownerCanDestroy.toString()]]);74 expect(limitsEvm[8]).to.deep.eq([CollectionLimitField.TransferEnabled.toString(), [true, limits.transfersEnabled.toString()]]);75 }));76});7778describe('Cannot set invalid collection limits', () => {79 let donor: IKeyringPair;8081 before(async () => {82 await usingEthPlaygrounds(async (_helper, privateKey) => {83 donor = await privateKey({url: import.meta.url});84 });85 });8687 [88 {case: 'nft' as const},89 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},90 {case: 'ft' as const},91 ].map(testCase =>92 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {93 const invalidLimits = {94 accountTokenOwnershipLimit: BigInt(Number.MAX_SAFE_INTEGER),95 transfersEnabled: 3,96 };9798 const owner = await helper.eth.createAccountWithBalance(donor);99 const {collectionAddress} = await helper.eth.createCollection(owner, new CreateCollectionData('Limits', 'absolutely anything', 'ISNI', testCase.case, 18)).send();100 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);101102 103 await expect(collectionEvm.methods104 .setCollectionLimit({field: 9, value: {status: true, value: 1}})105 .call()).to.be.rejectedWith('Returned error: VM Exception while processing transaction: revert value not convertible into enum "CollectionLimitField"');106107 108 await expect(collectionEvm.methods109 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: false, value: 0}})110 .call()).to.be.rejectedWith('user can\'t disable limits');111112 await expect(collectionEvm.methods113 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: invalidLimits.accountTokenOwnershipLimit}})114 .call()).to.be.rejectedWith(`can't convert value to u32 "${invalidLimits.accountTokenOwnershipLimit}"`);115116 await expect(collectionEvm.methods117 .setCollectionLimit({field: CollectionLimitField.TransferEnabled, value: {status: true, value: 3}})118 .call()).to.be.rejectedWith(`can't convert value to boolean "${invalidLimits.transfersEnabled}"`);119120 expect(() => collectionEvm.methods121 .setCollectionLimit({field: CollectionLimitField.SponsoredDataSize, value: {status: true, value: -1}}).send()).to.throw('value out-of-bounds');122 }));123124 [125 {case: 'nft' as const, requiredPallets: []},126 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},127 {case: 'ft' as const, requiredPallets: []},128 ].map(testCase =>129 itEth.ifWithPallets(`Non-owner and non-admin cannot set collection limits for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {130 const owner = await helper.eth.createAccountWithBalance(donor);131 const nonOwner = await helper.eth.createAccountWithBalance(donor);132 const {collectionAddress} = await helper.eth.createCollection(owner, new CreateCollectionData('Limits', 'absolutely anything', 'FLO', testCase.case, 18)).send();133134 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);135 await expect(collectionEvm.methods136 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})137 .call({from: nonOwner}))138 .to.be.rejectedWith('NoPermission');139140 await expect(collectionEvm.methods141 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})142 .send({from: nonOwner}))143 .to.be.rejected;144 }));145});