git.delta.rocks / unique-network / refs/commits / ab4d534976e8

difftreelog

test(nesting) collection admin test suite

Fahrrader2022-06-14parent: #681a5b7.patch.diff
in: master

2 files changed

modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -98,7 +98,7 @@
     ...encodeIntBE(collection),
     ...encodeIntBE(token),
   ]);
-  return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));
+  return Web3.utils.toChecksumAddress('0x' + buf.toString('hex')).toLowerCase();
 }
 export function tokenIdToCross(collection: number, token: number): CrossAccountId {
   return {
modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
2import {tokenIdToAddress} from '../eth/util/helpers';2import {tokenIdToAddress} from '../eth/util/helpers';
3import usingApi, {executeTransaction} from '../substrate/substrate-api';3import usingApi, {executeTransaction} from '../substrate/substrate-api';
4import {4import {
5 addCollectionAdminExpectSuccess,
5 addToAllowListExpectSuccess,6 addToAllowListExpectSuccess,
6 createCollectionExpectSuccess,7 createCollectionExpectSuccess,
7 createItemExpectSuccess,8 createItemExpectSuccess,
21let alice: IKeyringPair;22let alice: IKeyringPair;
22let bob: IKeyringPair;23let bob: IKeyringPair;
2324
24describe('Integration Test: Nesting', () => {25describe('Integration Test: Composite nesting tests', () => {
25 before(async () => {26 before(async () => {
26 await usingApi(async (api, privateKeyWrapper) => {27 await usingApi(async (_, privateKeyWrapper) => {
27 alice = privateKeyWrapper('//Alice');28 alice = privateKeyWrapper('//Alice');
28 bob = privateKeyWrapper('//Bob');29 bob = privateKeyWrapper('//Bob');
29 });30 });
145 ], 'Children contents check at deeper nesting');146 ], 'Children contents check at deeper nesting');
146 });147 });
147 });148 });
149});
148150
151describe('Integration Test: Various token type nesting', async () => {
152 before(async () => {
153 await usingApi(async (_, privateKeyWrapper) => {
154 alice = privateKeyWrapper('//Alice');
155 bob = privateKeyWrapper('//Bob');
156 });
157 });
158
159 it('Admin (NFT): allows an Admin to nest a token', async () => {
160 await usingApi(async api => {
161 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
162 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {admin: true}});
163 await addCollectionAdminExpectSuccess(alice, collection, bob.address);
164 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
165
166 // Create a nested token
167 const nestedToken = await createItemExpectSuccess(bob, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});
168 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});
169 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});
170
171 // Create a token to be nested and nest
172 const newToken = await createItemExpectSuccess(bob, collection, 'NFT');
173 await transferExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)});
174 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});
175 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});
176 });
177 });
178
179 it('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async () => {
180 await usingApi(async api => {
181 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
182 await addCollectionAdminExpectSuccess(alice, collectionA, bob.address);
183 const collectionB = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
184 await addCollectionAdminExpectSuccess(alice, collectionB, bob.address);
185 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {admin: true, restricted:[collectionA, collectionB]}});
186 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');
187
188 // Create a nested token
189 const nestedToken = await createItemExpectSuccess(bob, collectionB, 'NFT', {Ethereum: tokenIdToAddress(collectionA, targetToken)});
190 expect(await getTopmostTokenOwner(api, collectionB, nestedToken)).to.be.deep.equal({Substrate: alice.address});
191 expect(await getTokenOwner(api, collectionB, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collectionA, targetToken).toLowerCase()});
192
193 // Create a token to be nested and nest
194 const newToken = await createItemExpectSuccess(bob, collectionB, 'NFT');
195 await transferExpectSuccess(collectionB, newToken, bob, {Ethereum: tokenIdToAddress(collectionA, targetToken)});
196 expect(await getTopmostTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Substrate: alice.address});
197 expect(await getTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collectionA, targetToken).toLowerCase()});
198 });
199 });
200
149 // ---------- Non-Fungible ----------201 // ---------- Non-Fungible ----------
150202
283335
284describe('Negative Test: Nesting', async() => {336describe('Negative Test: Nesting', async() => {
285 before(async () => {337 before(async () => {
286 await usingApi(async (api, privateKeyWrapper) => {338 await usingApi(async (_, privateKeyWrapper) => {
287 alice = privateKeyWrapper('//Alice');339 alice = privateKeyWrapper('//Alice');
288 bob = privateKeyWrapper('//Bob');340 bob = privateKeyWrapper('//Bob');
289 });341 });
318 )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);370 )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);
319371
320 expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});372 expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});
373 });
374 });
375
376 // ---------- Admin ------------
377
378 it('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async () => {
379 await usingApi(async api => {
380 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
381 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});
382 await addCollectionAdminExpectSuccess(alice, collection, bob.address);
383 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
384
385 // Try to create a nested token as collection admin when it's disallowed
386 await expect(executeTransaction(api, bob, api.tx.unique.createItem(
387 collection,
388 {Ethereum: tokenIdToAddress(collection, targetToken)},
389 {nft: {const_data: [], variable_data: []}} as any,
390 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
391
392 // Try to create and nest a token in the wrong collection
393 const newToken = await createItemExpectSuccess(bob, collection, 'NFT');
394 await expect(executeTransaction(api, bob, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
395 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});
396 });
397 });
398
399 it('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async () => {
400 await usingApi(async api => {
401 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
402 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {admin: true}});
403 await addToAllowListExpectSuccess(alice, collection, bob.address);
404 await enableAllowListExpectSuccess(alice, collection);
405 await enablePublicMintingExpectSuccess(alice, collection);
406 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
407
408 // Try to create a nested token as collection admin when it's disallowed
409 await expect(executeTransaction(api, bob, api.tx.unique.createItem(
410 collection,
411 {Ethereum: tokenIdToAddress(collection, targetToken)},
412 {nft: {const_data: [], variable_data: []}} as any,
413 )), 'while creating nested token').to.be.rejectedWith(/common\.AddressNotInAllowlist/); // todo is this right?
414
415 // Try to create and nest a token in the wrong collection
416 const newToken = await createItemExpectSuccess(bob, collection, 'NFT');
417 await expect(executeTransaction(api, bob, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);
418 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});
419 });
420 });
421
422 it('Admin (NFT): disallows an Admin to nest and unnest someone else\'s token', async () => {
423 await usingApi(async api => {
424 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
425 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {admin: true}});
426
427 await addToAllowListExpectSuccess(alice, collection, bob.address);
428 await enableAllowListExpectSuccess(alice, collection);
429 await enablePublicMintingExpectSuccess(alice, collection);
430
431 // Create a token to attempt to be nested into
432 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');
433 const targetAddress = {Ethereum: tokenIdToAddress(collection, targetToken)};
434
435 // Try to nest somebody else's token
436 const newToken = await createItemExpectSuccess(bob, collection, 'NFT');
437 await expect(executeTransaction(
438 api,
439 alice,
440 api.tx.unique.transfer(targetAddress, collection, newToken, 1),
441 ), 'while nesting another\'s token token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);
442 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});
443
444 // Nest a token as admin and try to unnest it, now belonging to someone else
445 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', targetAddress);
446 await expect(executeTransaction(
447 api,
448 alice,
449 api.tx.unique.transferFrom(targetAddress, normalizeAccountId(alice), collection, nestedToken, 1),
450 ), 'while unnesting another\'s token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);
451 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal(targetAddress);
452 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});
453 });
454 });
455
456 it('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async () => {
457 await usingApi(async api => {
458 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
459 const collectionB = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
460 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {admin: true, restricted:[collectionA]}});
461
462 // Create a token to attempt to be nested into
463 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');
464
465 // Try to create and nest a token in the wrong collection
466 const newToken = await createItemExpectSuccess(alice, collectionB, 'NFT');
467 await expect(executeTransaction(
468 api,
469 alice,
470 api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionA, targetToken)}, collectionB, newToken, 1),
471 ), 'while nesting a foreign token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
472 expect(await getTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Substrate: alice.address});
321 });473 });
322 });474 });
323475