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

difftreelog

test multiple modified of token properties / minor fixes

Daniel Shiposha2022-12-14parent: #dcbb9ef.patch.diff
in: master

1 file changed

modifiedtests/src/nesting/tokenProperties.test.tsdiffbeforeafterboth
321 expect(await targetToken.getProperties()).to.be.empty;321 expect(await targetToken.getProperties()).to.be.empty;
322 });322 });
323
324 [
325 {mode: 'nft' as const, storage: 'nonfungible' as const, pieces: undefined, requiredPallets: []},
326 {mode: 'rft' as const, storage: 'refungible' as const, pieces: 100n, requiredPallets: [Pallets.ReFungible]},
327 ].map(testCase =>
328 itSub.ifWithPallets(`Allows modifying a token property multiple times (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {
329 const propKey = 'tok-prop';
330
331 const collection = await helper[testCase.mode].mintCollection(alice, {
332 tokenPropertyPermissions: [
333 {
334 key: propKey,
335 permission: {mutable: true, tokenOwner: true},
336 },
337 ],
338 });
339
340 const maxTokenPropertiesSize = 32768;
341
342 const propDataSize = 4096;
343
344 let propDataChar = 'a';
345 const makeNewPropData = () => {
346 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);
347 return `${propDataChar}`.repeat(propDataSize);
348 };
349
350 const token = await (
351 testCase.pieces
352 ? collection.mintToken(alice, testCase.pieces)
353 : collection.mintToken(alice)
354 );
355
356 const getConsumedSpace = async () => {
357 const props = (await helper.getApi().query[testCase.storage].tokenProperties(token.collectionId, token.tokenId)).toJSON();
358
359 return (props! as any).consumedSpace;
360 };
361
362 await token.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);
363 const originalSpace = await getConsumedSpace();
364 expect(originalSpace).to.be.equal(propDataSize);
365
366 const sameSizePropertiesPossibleNum = maxTokenPropertiesSize / propDataSize;
367
368 // It is possible to modify a property as many times as needed.
369 // It will not consume any additional space.
370 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {
371 await token.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);
372 const consumedSpace = await getConsumedSpace();
373 expect(consumedSpace).to.be.equal(originalSpace);
374 }
375 }));
323});376});
324377
325describe('Negative Integration Test: Token Properties', () => {378describe('Negative Integration Test: Token Properties', () => {
476 await testForbidsAddingPropertiesIfPropertyNotDeclared(token, amount);529 await testForbidsAddingPropertiesIfPropertyNotDeclared(token, amount);
477 });530 });
478531
479 async function testForbidsAddingTooManyProperties(token: UniqueNFToken | UniqueRFToken, pieces: bigint) {532 async function testForbidsAddingTooLargeProperties(token: UniqueNFToken | UniqueRFToken, pieces: bigint) {
480 const originalSpace = await prepare(token, pieces);533 const originalSpace = await prepare(token, pieces);
481534
482 await expect(535 await expect(
504 expect(consumedSpace).to.be.equal(originalSpace);557 expect(consumedSpace).to.be.equal(originalSpace);
505 }558 }
506559
507 itSub('Forbids adding too many properties to a token (NFT)', async ({helper}) => {560 itSub('Forbids adding too large properties to a token (NFT)', async ({helper}) => {
508 const [token, amount] = await mintCollectionWithAllPermissionsAndToken(helper, 'NFT');561 const [token, amount] = await mintCollectionWithAllPermissionsAndToken(helper, 'NFT');
509 await testForbidsAddingTooManyProperties(token, amount);562 await testForbidsAddingTooLargeProperties(token, amount);
510 });563 });
511564
512 itSub.ifWithPallets('Forbids adding too many properties to a token (ReFungible)', [Pallets.ReFungible], async ({helper}) => {565 itSub.ifWithPallets('Forbids adding too large properties to a token (ReFungible)', [Pallets.ReFungible], async ({helper}) => {
513 const [token, amount] = await mintCollectionWithAllPermissionsAndToken(helper, 'RFT');566 const [token, amount] = await mintCollectionWithAllPermissionsAndToken(helper, 'RFT');
514 await testForbidsAddingTooManyProperties(token, amount);567 await testForbidsAddingTooLargeProperties(token, amount);
515 });568 });
569
570 [
571 {mode: 'nft' as const, requiredPallets: []},
572 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
573 ].map(testCase =>
574 itSub.ifWithPallets(`Forbids adding too many propeties to a token (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {
575 const collection = await helper[testCase.mode].mintCollection(alice);
576 const maxPropertiesPerItem = 64;
577
578 for (let i = 0; i < maxPropertiesPerItem; i++) {
579 await collection.setTokenPropertyPermissions(alice, [{
580 key: `${i+1}`,
581 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},
582 }]);
583 }
584
585 await expect(collection.setTokenPropertyPermissions(alice, [{
586 key: `${maxPropertiesPerItem}-th`,
587 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},
588 }])).to.be.rejectedWith(/common\.PropertyLimitReached/);
589 }));
516});590});
517591
518describe('ReFungible token properties permissions tests', () => {592describe('ReFungible token properties permissions tests', () => {