difftreelog
refactor(tests-nesting) use charlie instead of bob
in: master
1 file changed
tests/src/nesting/nest.test.tsdiffbeforeafterboth1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import usingApi, {executeTransaction} from '../substrate/substrate-api';4import {5 addCollectionAdminExpectSuccess,6 addToAllowListExpectSuccess,7 createCollectionExpectSuccess,8 createItemExpectSuccess,9 enableAllowListExpectSuccess,10 enablePublicMintingExpectSuccess,11 getTokenChildren,12 getTokenOwner,13 getTopmostTokenOwner,14 normalizeAccountId,15 setCollectionPermissionsExpectSuccess,16 transferExpectFailure,17 transferExpectSuccess,18 transferFromExpectSuccess,19} from '../util/helpers';20import {IKeyringPair} from '@polkadot/types/types';2122let alice: IKeyringPair;23let bob: IKeyringPair;2425describe('Integration Test: Composite nesting tests', () => {26 before(async () => {27 await usingApi(async (_, privateKeyWrapper) => {28 alice = privateKeyWrapper('//Alice');29 bob = privateKeyWrapper('//Bob');30 });31 });3233 it('Performs the full suite: bundles a token, transfers, and unnests', async () => {34 await usingApi(async api => {35 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});36 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});37 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');3839 // Create a nested token40 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});41 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});42 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});4344 // Create a token to be nested45 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');4647 // Nest48 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});49 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});50 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5152 // Move bundle to different user53 await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});54 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});55 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5657 // Unnest58 await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});59 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});60 });61 });6263 it('Transfers an already bundled token', async () => {64 await usingApi(async api => {65 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});66 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});6768 const tokenA = await createItemExpectSuccess(alice, collection, 'NFT');69 const tokenB = await createItemExpectSuccess(alice, collection, 'NFT');7071 // Create a nested token72 const tokenC = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, tokenA)});73 expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});74 expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenA).toLowerCase()});7576 // Transfer the nested token to another token77 await expect(executeTransaction(78 api,79 alice,80 api.tx.unique.transferFrom(81 normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenA)}),82 normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenB)}),83 collection,84 tokenC,85 1,86 ),87 )).to.not.be.rejected;88 expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});89 expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenB).toLowerCase()});90 });91 });9293 it('Checks token children', async () => {94 await usingApi(async api => {95 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});96 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {tokenOwner: true}});97 const collectionB = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});9899 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');100 const targetAddress = {Ethereum: tokenIdToAddress(collectionA, targetToken)};101 let children = await getTokenChildren(api, collectionA, targetToken);102 expect(children.length).to.be.equal(0, 'Children length check at creation');103104 // Create a nested NFT token105 const tokenA = await createItemExpectSuccess(alice, collectionA, 'NFT', targetAddress);106 children = await getTokenChildren(api, collectionA, targetToken);107 expect(children.length).to.be.equal(1, 'Children length check at nesting #1');108 expect(children).to.have.deep.members([109 {token: tokenA, collection: collectionA},110 ], 'Children contents check at nesting #1');111112 // Create then nest113 const tokenB = await createItemExpectSuccess(alice, collectionA, 'NFT');114 await transferExpectSuccess(collectionA, tokenB, alice, targetAddress);115 children = await getTokenChildren(api, collectionA, targetToken);116 expect(children.length).to.be.equal(2, 'Children length check at nesting #2');117 expect(children).to.have.deep.members([118 {token: tokenA, collection: collectionA},119 {token: tokenB, collection: collectionA},120 ], 'Children contents check at nesting #2');121122 // Move token B to a different user outside the nesting tree123 await transferExpectSuccess(collectionA, tokenB, alice, bob);124 children = await getTokenChildren(api, collectionA, targetToken);125 expect(children.length).to.be.equal(1, 'Children length check at unnesting');126 expect(children).to.be.have.deep.members([127 {token: tokenA, collection: collectionA},128 ], 'Children contents check at unnesting');129130 // Create a fungible token in another collection and then nest131 const tokenC = await createItemExpectSuccess(alice, collectionB, 'Fungible');132 await transferExpectSuccess(collectionB, tokenC, alice, targetAddress, 1, 'Fungible');133 children = await getTokenChildren(api, collectionA, targetToken);134 expect(children.length).to.be.equal(2, 'Children length check at nesting #3 (from another collection)');135 expect(children).to.be.have.deep.members([136 {token: tokenA, collection: collectionA},137 {token: tokenC, collection: collectionB},138 ], 'Children contents check at nesting #3 (from another collection)');139140 // Move the fungible token inside token A deeper in the nesting tree141 await transferFromExpectSuccess(collectionB, tokenC, alice, targetAddress, {Ethereum: tokenIdToAddress(collectionA, tokenA)}, 1, 'Fungible');142 children = await getTokenChildren(api, collectionA, targetToken);143 expect(children.length).to.be.equal(1, 'Children length check at deeper nesting');144 expect(children).to.be.have.deep.members([145 {token: tokenA, collection: collectionA},146 ], 'Children contents check at deeper nesting');147 });148 });149});150151describe('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 });158159 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: {collectionAdmin: true}});163 await addCollectionAdminExpectSuccess(alice, collection, bob.address);164 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');165166 // Create a nested token167 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()});170171 // Create a token to be nested and nest172 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 });178179 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: {collectionAdmin: true, restricted:[collectionA, collectionB]}});186 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');187188 // Create a nested token189 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()});192193 // Create a token to be nested and nest194 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 });200201 // ---------- Non-Fungible ----------202203 it('NFT: allows an Owner to nest/unnest their token', async () => {204 await usingApi(async api => {205 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});206 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});207 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');208209 // Create a nested token210 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});211 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});212 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});213214 // Create a token to be nested and nest215 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');216 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});217 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});218 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});219 });220 });221222 it('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {223 await usingApi(async api => {224 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});225 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[collection]}});226 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');227228 // Create a nested token229 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});230 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});231 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});232233 // Create a token to be nested and nest234 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');235 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});236 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});237 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});238 });239 });240241 // ---------- Fungible ----------242243 it('Fungible: allows an Owner to nest/unnest their token', async () => {244 await usingApi(async api => {245 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});246 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});247 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});248 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};249250 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});251252 // Create a nested token253 await expect(executeTransaction(api, alice, api.tx.unique.createItem(254 collectionFT,255 targetAddress,256 {Fungible: {Value: 10}},257 ))).to.not.be.rejected;258259 // Nest a new token260 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');261 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');262 });263 });264265 it('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {266 await usingApi(async api => {267 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});268 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});269 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};270271 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});272273 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted: [collectionFT]}});274275 // Create a nested token276 await expect(executeTransaction(api, alice, api.tx.unique.createItem(277 collectionFT,278 targetAddress,279 {Fungible: {Value: 10}},280 ))).to.not.be.rejected;281282 // Nest a new token283 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');284 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');285 });286 });287288 // ---------- Re-Fungible ----------289290 it('ReFungible: allows an Owner to nest/unnest their token', async () => {291 await usingApi(async api => {292 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});293 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});294 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});295 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};296297 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});298299 // Create a nested token300 await expect(executeTransaction(api, alice, api.tx.unique.createItem(301 collectionRFT,302 targetAddress,303 {ReFungible: {const_data: [], pieces: 100}},304 ))).to.not.be.rejected;305306 // Nest a new token307 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');308 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');309 });310 });311312 it('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {313 await usingApi(async api => {314 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});315 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});316 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};317318 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});319320 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionRFT]}});321322 // Create a nested token323 await expect(executeTransaction(api, alice, api.tx.unique.createItem(324 collectionRFT,325 targetAddress,326 {ReFungible: {const_data: [], pieces: 100}},327 ))).to.not.be.rejected;328329 // Nest a new token330 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');331 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');332 });333 });334});335336describe('Negative Test: Nesting', async() => {337 before(async () => {338 await usingApi(async (_, privateKeyWrapper) => {339 alice = privateKeyWrapper('//Alice');340 bob = privateKeyWrapper('//Bob');341 });342 });343344 it('Disallows excessive token nesting', async () => {345 await usingApi(async api => {346 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});347 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});348 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');349350 const maxNestingLevel = 5;351 let prevToken = targetToken;352353 // Create a nested-token matryoshka354 for (let i = 0; i < maxNestingLevel; i++) {355 const nestedToken = await createItemExpectSuccess(356 alice,357 collection,358 'NFT',359 {Ethereum: tokenIdToAddress(collection, prevToken)},360 );361362 prevToken = nestedToken;363 }364365 // The nesting depth is limited by `maxNestingLevel`366 await expect(executeTransaction(api, alice, api.tx.unique.createItem(367 collection,368 {Ethereum: tokenIdToAddress(collection, prevToken)},369 {nft: {const_data: [], variable_data: []}} as any,370 )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);371372 expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});373 });374 });375376 // ---------- Admin ------------377378 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');384385 // Try to create a nested token as collection admin when it's disallowed386 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/);391392 // Try to create and nest a token in the wrong collection393 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 });398399 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: {collectionAdmin: 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');407408 // Try to create a nested token as collection admin when it's disallowed409 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?414415 // Try to create and nest a token in the wrong collection416 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 });421422 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: {collectionAdmin: true}});426427 await addToAllowListExpectSuccess(alice, collection, bob.address);428 await enableAllowListExpectSuccess(alice, collection);429 await enablePublicMintingExpectSuccess(alice, collection);430431 // Create a token to attempt to be nested into432 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');433 const targetAddress = {Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()};434435 // Try to nest somebody else's token436 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});443444 // Nest a token as admin and try to unnest it, now belonging to someone else445 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 });455456 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: {collectionAdmin: true, restricted:[collectionA]}});461462 // Create a token to attempt to be nested into463 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');464465 // Try to create and nest a token in the wrong collection466 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});473 });474 });475476 // ---------- Non-Fungible ----------477478 it('NFT: disallows to nest token if nesting is disabled', async () => {479 await usingApi(async api => {480 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});481 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {}});482 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');483484 // Try to create a nested token485 await expect(executeTransaction(api, alice, api.tx.unique.createItem(486 collection,487 {Ethereum: tokenIdToAddress(collection, targetToken)},488 {nft: {const_data: [], variable_data: []}} as any,489 )), 'while creating nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);490491 // Create a token to be nested492 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');493 // Try to nest494 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);495 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});496 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});497 });498 });499500 it('NFT: disallows a non-Owner to nest someone else\'s token', async () => {501 await usingApi(async api => {502 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});503 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});504505 await addToAllowListExpectSuccess(alice, collection, bob.address);506 await enableAllowListExpectSuccess(alice, collection);507 await enablePublicMintingExpectSuccess(alice, collection);508509 // Create a token to attempt to be nested into510 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');511512 // Try to create a nested token in the wrong collection513 await expect(executeTransaction(api, alice, api.tx.unique.createItem(514 collection,515 {Ethereum: tokenIdToAddress(collection, targetToken)},516 {nft: {const_data: [], variable_data: []}} as any,517 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);518519 // Try to create and nest a token in the wrong collection520 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');521 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);522 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});523 });524 });525526 it('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {527 await usingApi(async api => {528 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});529 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[collection]}});530531 await addToAllowListExpectSuccess(alice, collection, bob.address);532 await enableAllowListExpectSuccess(alice, collection);533 await enablePublicMintingExpectSuccess(alice, collection);534535 // Create a token to attempt to be nested into536 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');537538 // Try to create a nested token in the wrong collection539 await expect(executeTransaction(api, alice, api.tx.unique.createItem(540 collection,541 {Ethereum: tokenIdToAddress(collection, targetToken)},542 {nft: {const_data: [], variable_data: []}} as any,543 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);544545 // Try to create and nest a token in the wrong collection546 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');547 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);548 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});549 });550 });551552 it('NFT: disallows to nest token in an unlisted collection', async () => {553 await usingApi(async api => {554 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});555 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[]}});556557 // Create a token to attempt to be nested into558 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');559560 // Try to create a nested token in the wrong collection561 await expect(executeTransaction(api, alice, api.tx.unique.createItem(562 collection,563 {Ethereum: tokenIdToAddress(collection, targetToken)},564 {nft: {const_data: [], variable_data: []}} as any,565 )), 'while creating nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);566567 // Try to create and nest a token in the wrong collection568 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');569 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);570 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});571 });572 });573574 // ---------- Fungible ----------575576 it('Fungible: disallows to nest token if nesting is disabled', async () => {577 await usingApi(async api => {578 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});579 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {}});580 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');581 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};582583 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});584585 // Try to create a nested token586 await expect(executeTransaction(api, alice, api.tx.unique.createItem(587 collectionFT,588 targetAddress,589 {Fungible: {Value: 10}},590 )), 'while creating nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);591592 // Create a token to be nested593 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');594 // Try to nest595 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);596597 // Create another token to be nested598 const newToken2 = await createItemExpectSuccess(alice, collectionFT, 'Fungible');599 // Try to nest inside a fungible token600 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionFT, newToken)}, collectionFT, newToken2, 1)), 'while nesting new token inside fungible').to.be.rejectedWith(/fungible\.FungibleDisallowsNesting/);601 });602 });603604 it('Fungible: disallows a non-Owner to nest someone else\'s token', async () => {605 await usingApi(async api => {606 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});607 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});608609 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);610 await enableAllowListExpectSuccess(alice, collectionNFT);611 await enablePublicMintingExpectSuccess(alice, collectionNFT);612613 // Create a token to attempt to be nested into614 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');615 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};616617 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});618619 // Try to create a nested token in the wrong collection620 await expect(executeTransaction(api, alice, api.tx.unique.createItem(621 collectionFT,622 targetAddress,623 {Fungible: {Value: 10}},624 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);625626 // Try to create and nest a token in the wrong collection627 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');628 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);629 });630 });631632 it('Fungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {633 await usingApi(async api => {634 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});635 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);636 await enableAllowListExpectSuccess(alice, collectionNFT);637 await enablePublicMintingExpectSuccess(alice, collectionNFT);638639 // Create a token to attempt to be nested into640 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');641 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};642643 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});644 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionFT]}});645646 // Try to create a nested token in the wrong collection647 await expect(executeTransaction(api, alice, api.tx.unique.createItem(648 collectionFT,649 targetAddress,650 {Fungible: {Value: 10}},651 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);652653 // Try to create and nest a token in the wrong collection654 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');655 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);656 });657 });658659 it('Fungible: disallows to nest token in an unlisted collection', async () => {660 await usingApi(async api => {661 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});662 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[]}});663664 // Create a token to attempt to be nested into665 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');666 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};667668 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});669670 // Try to create a nested token in the wrong collection671 await expect(executeTransaction(api, alice, api.tx.unique.createItem(672 collectionFT,673 targetAddress,674 {Fungible: {Value: 10}},675 )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);676677 // Try to create and nest a token in the wrong collection678 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');679 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);680 });681 });682683 // ---------- Re-Fungible ----------684685 it('ReFungible: disallows to nest token if nesting is disabled', async () => {686 await usingApi(async api => {687 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});688 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {}});689 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');690 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};691692 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});693694 // Create a nested token695 await expect(executeTransaction(api, alice, api.tx.unique.createItem(696 collectionRFT,697 targetAddress,698 {ReFungible: {const_data: [], pieces: 100}},699 )), 'while creating a nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);700701 // Create a token to be nested702 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');703 // Try to nest704 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);705 // Try to nest706 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);707708 // Create another token to be nested709 const newToken2 = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');710 // Try to nest inside a fungible token711 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionRFT, newToken)}, collectionRFT, newToken2, 1)), 'while nesting new token inside refungible').to.be.rejectedWith(/refungible\.RefungibleDisallowsNesting/);712 });713 });714715 it('ReFungible: disallows a non-Owner to nest someone else\'s token', async () => {716 await usingApi(async api => {717 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});718 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});719720 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);721 await enableAllowListExpectSuccess(alice, collectionNFT);722 await enablePublicMintingExpectSuccess(alice, collectionNFT);723724 // Create a token to attempt to be nested into725 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');726 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};727728 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});729730 // Try to create a nested token in the wrong collection731 await expect(executeTransaction(api, alice, api.tx.unique.createItem(732 collectionRFT,733 targetAddress,734 {ReFungible: {const_data: [], pieces: 100}},735 )), 'while creating a nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);736737 // Try to create and nest a token in the wrong collection738 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');739 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);740 });741 });742743 it('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {744 await usingApi(async api => {745 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});746 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);747 await enableAllowListExpectSuccess(alice, collectionNFT);748 await enablePublicMintingExpectSuccess(alice, collectionNFT);749750 // Create a token to attempt to be nested into751 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');752 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};753754 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});755 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionRFT]}});756757 // Try to create a nested token in the wrong collection758 await expect(executeTransaction(api, alice, api.tx.unique.createItem(759 collectionRFT,760 targetAddress,761 {ReFungible: {const_data: [], pieces: 100}},762 )), 'while creating a nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);763764 // Try to create and nest a token in the wrong collection765 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');766 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);767 });768 });769770 it('ReFungible: disallows to nest token to an unlisted collection', async () => {771 await usingApi(async api => {772 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});773 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[]}});774775 // Create a token to attempt to be nested into776 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');777 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};778779 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});780781 // Try to create a nested token in the wrong collection782 await expect(executeTransaction(api, alice, api.tx.unique.createItem(783 collectionRFT,784 targetAddress,785 {ReFungible: {const_data: [], pieces: 100}},786 )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);787788 // Try to create and nest a token in the wrong collection789 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');790 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);791 });792 });793});1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import usingApi, {executeTransaction} from '../substrate/substrate-api';4import {5 addCollectionAdminExpectSuccess,6 addToAllowListExpectSuccess,7 createCollectionExpectSuccess,8 createItemExpectSuccess,9 enableAllowListExpectSuccess,10 enablePublicMintingExpectSuccess,11 getTokenChildren,12 getTokenOwner,13 getTopmostTokenOwner,14 normalizeAccountId,15 setCollectionPermissionsExpectSuccess,16 transferExpectFailure,17 transferExpectSuccess,18 transferFromExpectSuccess,19} from '../util/helpers';20import {IKeyringPair} from '@polkadot/types/types';2122let alice: IKeyringPair;23let bob: IKeyringPair;24let charlie: IKeyringPair;2526describe('Integration Test: Composite nesting tests', () => {27 before(async () => {28 await usingApi(async (_, privateKeyWrapper) => {29 alice = privateKeyWrapper('//Alice');30 bob = privateKeyWrapper('//Bob');31 });32 });3334 it('Performs the full suite: bundles a token, transfers, and unnests', async () => {35 await usingApi(async api => {36 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});37 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});38 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');3940 // Create a nested token41 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});42 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});43 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});4445 // Create a token to be nested46 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');4748 // Nest49 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});50 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});51 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5253 // Move bundle to different user54 await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});55 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});56 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5758 // Unnest59 await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});60 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});61 });62 });6364 it('Transfers an already bundled token', async () => {65 await usingApi(async api => {66 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});67 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});6869 const tokenA = await createItemExpectSuccess(alice, collection, 'NFT');70 const tokenB = await createItemExpectSuccess(alice, collection, 'NFT');7172 // Create a nested token73 const tokenC = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, tokenA)});74 expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});75 expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenA).toLowerCase()});7677 // Transfer the nested token to another token78 await expect(executeTransaction(79 api,80 alice,81 api.tx.unique.transferFrom(82 normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenA)}),83 normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenB)}),84 collection,85 tokenC,86 1,87 ),88 )).to.not.be.rejected;89 expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});90 expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenB).toLowerCase()});91 });92 });9394 it('Checks token children', async () => {95 await usingApi(async api => {96 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});97 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {tokenOwner: true}});98 const collectionB = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});99100 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');101 const targetAddress = {Ethereum: tokenIdToAddress(collectionA, targetToken)};102 let children = await getTokenChildren(api, collectionA, targetToken);103 expect(children.length).to.be.equal(0, 'Children length check at creation');104105 // Create a nested NFT token106 const tokenA = await createItemExpectSuccess(alice, collectionA, 'NFT', targetAddress);107 children = await getTokenChildren(api, collectionA, targetToken);108 expect(children.length).to.be.equal(1, 'Children length check at nesting #1');109 expect(children).to.have.deep.members([110 {token: tokenA, collection: collectionA},111 ], 'Children contents check at nesting #1');112113 // Create then nest114 const tokenB = await createItemExpectSuccess(alice, collectionA, 'NFT');115 await transferExpectSuccess(collectionA, tokenB, alice, targetAddress);116 children = await getTokenChildren(api, collectionA, targetToken);117 expect(children.length).to.be.equal(2, 'Children length check at nesting #2');118 expect(children).to.have.deep.members([119 {token: tokenA, collection: collectionA},120 {token: tokenB, collection: collectionA},121 ], 'Children contents check at nesting #2');122123 // Move token B to a different user outside the nesting tree124 await transferExpectSuccess(collectionA, tokenB, alice, bob);125 children = await getTokenChildren(api, collectionA, targetToken);126 expect(children.length).to.be.equal(1, 'Children length check at unnesting');127 expect(children).to.be.have.deep.members([128 {token: tokenA, collection: collectionA},129 ], 'Children contents check at unnesting');130131 // Create a fungible token in another collection and then nest132 const tokenC = await createItemExpectSuccess(alice, collectionB, 'Fungible');133 await transferExpectSuccess(collectionB, tokenC, alice, targetAddress, 1, 'Fungible');134 children = await getTokenChildren(api, collectionA, targetToken);135 expect(children.length).to.be.equal(2, 'Children length check at nesting #3 (from another collection)');136 expect(children).to.be.have.deep.members([137 {token: tokenA, collection: collectionA},138 {token: tokenC, collection: collectionB},139 ], 'Children contents check at nesting #3 (from another collection)');140141 // Move the fungible token inside token A deeper in the nesting tree142 await transferFromExpectSuccess(collectionB, tokenC, alice, targetAddress, {Ethereum: tokenIdToAddress(collectionA, tokenA)}, 1, 'Fungible');143 children = await getTokenChildren(api, collectionA, targetToken);144 expect(children.length).to.be.equal(1, 'Children length check at deeper nesting');145 expect(children).to.be.have.deep.members([146 {token: tokenA, collection: collectionA},147 ], 'Children contents check at deeper nesting');148 });149 });150});151152describe('Integration Test: Various token type nesting', async () => {153 before(async () => {154 await usingApi(async (_, privateKeyWrapper) => {155 alice = privateKeyWrapper('//Alice');156 bob = privateKeyWrapper('//Bob');157 charlie = privateKeyWrapper('//Charlie');158 });159 });160161 it('Admin (NFT): allows an Admin to nest a token', async () => {162 await usingApi(async api => {163 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});164 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {collectionAdmin: true}});165 await addCollectionAdminExpectSuccess(alice, collection, charlie.address);166 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');167168 // Create a nested token169 const nestedToken = await createItemExpectSuccess(charlie, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});170 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});171 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});172173 // Create a token to be nested and nest174 const newToken = await createItemExpectSuccess(charlie, collection, 'NFT');175 await transferExpectSuccess(collection, newToken, charlie, {Ethereum: tokenIdToAddress(collection, targetToken)});176 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});177 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});178 });179 });180181 it('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async () => {182 await usingApi(async api => {183 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});184 await addCollectionAdminExpectSuccess(alice, collectionA, charlie.address);185 const collectionB = await createCollectionExpectSuccess({mode: {type: 'NFT'}});186 await addCollectionAdminExpectSuccess(alice, collectionB, charlie.address);187 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {collectionAdmin: true, restricted:[collectionA, collectionB]}});188 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');189190 // Create a nested token191 const nestedToken = await createItemExpectSuccess(charlie, collectionB, 'NFT', {Ethereum: tokenIdToAddress(collectionA, targetToken)});192 expect(await getTopmostTokenOwner(api, collectionB, nestedToken)).to.be.deep.equal({Substrate: alice.address});193 expect(await getTokenOwner(api, collectionB, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collectionA, targetToken).toLowerCase()});194195 // Create a token to be nested and nest196 const newToken = await createItemExpectSuccess(charlie, collectionB, 'NFT');197 await transferExpectSuccess(collectionB, newToken, charlie, {Ethereum: tokenIdToAddress(collectionA, targetToken)});198 expect(await getTopmostTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Substrate: alice.address});199 expect(await getTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collectionA, targetToken).toLowerCase()});200 });201 });202203 // ---------- Non-Fungible ----------204205 it('NFT: allows an Owner to nest/unnest their token', async () => {206 await usingApi(async api => {207 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});208 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});209 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');210211 // Create a nested token212 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});213 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});214 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});215216 // Create a token to be nested and nest217 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');218 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});219 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});220 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});221 });222 });223224 it('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {225 await usingApi(async api => {226 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});227 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[collection]}});228 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');229230 // Create a nested token231 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});232 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});233 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});234235 // Create a token to be nested and nest236 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');237 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});238 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});239 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});240 });241 });242243 // ---------- Fungible ----------244245 it('Fungible: allows an Owner to nest/unnest their token', async () => {246 await usingApi(async api => {247 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});248 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});249 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});250 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};251252 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});253254 // Create a nested token255 await expect(executeTransaction(api, alice, api.tx.unique.createItem(256 collectionFT,257 targetAddress,258 {Fungible: {Value: 10}},259 ))).to.not.be.rejected;260261 // Nest a new token262 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');263 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');264 });265 });266267 it('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {268 await usingApi(async api => {269 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});270 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});271 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};272273 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});274275 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted: [collectionFT]}});276277 // Create a nested token278 await expect(executeTransaction(api, alice, api.tx.unique.createItem(279 collectionFT,280 targetAddress,281 {Fungible: {Value: 10}},282 ))).to.not.be.rejected;283284 // Nest a new token285 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');286 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');287 });288 });289290 // ---------- Re-Fungible ----------291292 it('ReFungible: allows an Owner to nest/unnest their token', async () => {293 await usingApi(async api => {294 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});295 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});296 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});297 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};298299 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});300301 // Create a nested token302 await expect(executeTransaction(api, alice, api.tx.unique.createItem(303 collectionRFT,304 targetAddress,305 {ReFungible: {const_data: [], pieces: 100}},306 ))).to.not.be.rejected;307308 // Nest a new token309 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');310 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');311 });312 });313314 it('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {315 await usingApi(async api => {316 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});317 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});318 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};319320 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});321322 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionRFT]}});323324 // Create a nested token325 await expect(executeTransaction(api, alice, api.tx.unique.createItem(326 collectionRFT,327 targetAddress,328 {ReFungible: {const_data: [], pieces: 100}},329 ))).to.not.be.rejected;330331 // Nest a new token332 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');333 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');334 });335 });336});337338describe('Negative Test: Nesting', async() => {339 before(async () => {340 await usingApi(async (_, privateKeyWrapper) => {341 alice = privateKeyWrapper('//Alice');342 bob = privateKeyWrapper('//Bob');343 charlie = privateKeyWrapper('//Charlie');344 });345 });346347 it('Disallows excessive token nesting', async () => {348 await usingApi(async api => {349 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});350 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});351 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');352353 const maxNestingLevel = 5;354 let prevToken = targetToken;355356 // Create a nested-token matryoshka357 for (let i = 0; i < maxNestingLevel; i++) {358 const nestedToken = await createItemExpectSuccess(359 alice,360 collection,361 'NFT',362 {Ethereum: tokenIdToAddress(collection, prevToken)},363 );364365 prevToken = nestedToken;366 }367368 // The nesting depth is limited by `maxNestingLevel`369 await expect(executeTransaction(api, alice, api.tx.unique.createItem(370 collection,371 {Ethereum: tokenIdToAddress(collection, prevToken)},372 {nft: {const_data: [], variable_data: []}} as any,373 )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);374375 expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});376 });377 });378379 // ---------- Admin ------------380381 it('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async () => {382 await usingApi(async api => {383 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});384 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});385 await addCollectionAdminExpectSuccess(alice, collection, charlie.address);386 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');387388 // Try to create a nested token as collection admin when it's disallowed389 await expect(executeTransaction(api, charlie, api.tx.unique.createItem(390 collection,391 {Ethereum: tokenIdToAddress(collection, targetToken)},392 {nft: {const_data: [], variable_data: []}} as any,393 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);394395 // Try to create and nest a token in the wrong collection396 const newToken = await createItemExpectSuccess(charlie, collection, 'NFT');397 await expect(executeTransaction(398 api, 399 charlie, 400 api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1),401 ), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);402 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: charlie.address});403 });404 });405406 it('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async () => {407 await usingApi(async api => {408 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});409 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {collectionAdmin: true}});410 await addToAllowListExpectSuccess(alice, collection, charlie.address);411 await enableAllowListExpectSuccess(alice, collection);412 await enablePublicMintingExpectSuccess(alice, collection);413 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');414415 // Try to create a nested token as collection admin when it's disallowed416 await expect(executeTransaction(api, charlie, api.tx.unique.createItem(417 collection,418 {Ethereum: tokenIdToAddress(collection, targetToken)},419 {nft: {const_data: [], variable_data: []}} as any,420 )), 'while creating nested token').to.be.rejectedWith(/common\.AddressNotInAllowlist/); // todo is this right?421422 // Try to create and nest a token in the wrong collection423 const newToken = await createItemExpectSuccess(charlie, collection, 'NFT');424 await expect(executeTransaction(425 api, 426 charlie, 427 api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1),428 ), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);429 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: charlie.address});430 });431 });432433 it('Admin (NFT): disallows an Admin to nest and unnest someone else\'s token', async () => {434 await usingApi(async api => {435 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});436 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {collectionAdmin: true}});437438 await addToAllowListExpectSuccess(alice, collection, charlie.address);439 await enableAllowListExpectSuccess(alice, collection);440 await enablePublicMintingExpectSuccess(alice, collection);441442 // Create a token to attempt to be nested into443 const targetToken = await createItemExpectSuccess(charlie, collection, 'NFT');444 const targetAddress = {Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()};445446 // Try to nest somebody else's token447 const newToken = await createItemExpectSuccess(charlie, collection, 'NFT');448 await expect(executeTransaction(449 api, 450 alice, 451 api.tx.unique.transfer(targetAddress, collection, newToken, 1),452 ), 'while nesting another\'s token token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);453 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: charlie.address});454455 // Nest a token as admin and try to unnest it, now belonging to someone else456 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', targetAddress);457 await expect(executeTransaction(458 api, 459 alice, 460 api.tx.unique.transferFrom(targetAddress, normalizeAccountId(alice), collection, nestedToken, 1),461 ), 'while unnesting another\'s token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);462 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal(targetAddress);463 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: charlie.address});464 });465 });466467 it('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async () => {468 await usingApi(async api => {469 const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});470 const collectionB = await createCollectionExpectSuccess({mode: {type: 'NFT'}});471 await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: {collectionAdmin: true, restricted:[collectionA]}});472473 // Create a token to attempt to be nested into474 const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');475476 // Try to create and nest a token in the wrong collection477 const newToken = await createItemExpectSuccess(alice, collectionB, 'NFT');478 await expect(executeTransaction(479 api, 480 alice, 481 api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionA, targetToken)}, collectionB, newToken, 1),482 ), 'while nesting a foreign token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);483 expect(await getTokenOwner(api, collectionB, newToken)).to.be.deep.equal({Substrate: alice.address});484 });485 });486487 // ---------- Non-Fungible ----------488489 it('NFT: disallows to nest token if nesting is disabled', async () => {490 await usingApi(async api => {491 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});492 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {}});493 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');494495 // Try to create a nested token496 await expect(executeTransaction(api, alice, api.tx.unique.createItem(497 collection,498 {Ethereum: tokenIdToAddress(collection, targetToken)},499 {nft: {const_data: [], variable_data: []}} as any,500 )), 'while creating nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);501502 // Create a token to be nested503 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');504 // Try to nest505 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);506 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});507 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});508 });509 });510511 it('NFT: disallows a non-Owner to nest someone else\'s token', async () => {512 await usingApi(async api => {513 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});514 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true}});515516 await addToAllowListExpectSuccess(alice, collection, bob.address);517 await enableAllowListExpectSuccess(alice, collection);518 await enablePublicMintingExpectSuccess(alice, collection);519520 // Create a token to attempt to be nested into521 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');522523 // Try to create a nested token in the wrong collection524 await expect(executeTransaction(api, alice, api.tx.unique.createItem(525 collection,526 {Ethereum: tokenIdToAddress(collection, targetToken)},527 {nft: {const_data: [], variable_data: []}} as any,528 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);529530 // Try to create and nest a token in the wrong collection531 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');532 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);533 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});534 });535 });536537 it('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {538 await usingApi(async api => {539 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});540 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[collection]}});541542 await addToAllowListExpectSuccess(alice, collection, bob.address);543 await enableAllowListExpectSuccess(alice, collection);544 await enablePublicMintingExpectSuccess(alice, collection);545546 // Create a token to attempt to be nested into547 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');548549 // Try to create a nested token in the wrong collection550 await expect(executeTransaction(api, alice, api.tx.unique.createItem(551 collection,552 {Ethereum: tokenIdToAddress(collection, targetToken)},553 {nft: {const_data: [], variable_data: []}} as any,554 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);555556 // Try to create and nest a token in the wrong collection557 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');558 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);559 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});560 });561 });562563 it('NFT: disallows to nest token in an unlisted collection', async () => {564 await usingApi(async api => {565 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});566 await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {tokenOwner: true, restricted:[]}});567568 // Create a token to attempt to be nested into569 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');570571 // Try to create a nested token in the wrong collection572 await expect(executeTransaction(api, alice, api.tx.unique.createItem(573 collection,574 {Ethereum: tokenIdToAddress(collection, targetToken)},575 {nft: {const_data: [], variable_data: []}} as any,576 )), 'while creating nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);577578 // Try to create and nest a token in the wrong collection579 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');580 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);581 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});582 });583 });584585 // ---------- Fungible ----------586587 it('Fungible: disallows to nest token if nesting is disabled', async () => {588 await usingApi(async api => {589 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});590 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {}});591 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');592 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};593594 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});595596 // Try to create a nested token597 await expect(executeTransaction(api, alice, api.tx.unique.createItem(598 collectionFT,599 targetAddress,600 {Fungible: {Value: 10}},601 )), 'while creating nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);602603 // Create a token to be nested604 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');605 // Try to nest606 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);607608 // Create another token to be nested609 const newToken2 = await createItemExpectSuccess(alice, collectionFT, 'Fungible');610 // Try to nest inside a fungible token611 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionFT, newToken)}, collectionFT, newToken2, 1)), 'while nesting new token inside fungible').to.be.rejectedWith(/fungible\.FungibleDisallowsNesting/);612 });613 });614615 it('Fungible: disallows a non-Owner to nest someone else\'s token', async () => {616 await usingApi(async api => {617 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});618 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});619620 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);621 await enableAllowListExpectSuccess(alice, collectionNFT);622 await enablePublicMintingExpectSuccess(alice, collectionNFT);623624 // Create a token to attempt to be nested into625 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');626 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};627628 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});629630 // Try to create a nested token in the wrong collection631 await expect(executeTransaction(api, alice, api.tx.unique.createItem(632 collectionFT,633 targetAddress,634 {Fungible: {Value: 10}},635 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);636637 // Try to create and nest a token in the wrong collection638 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');639 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);640 });641 });642643 it('Fungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {644 await usingApi(async api => {645 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});646 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);647 await enableAllowListExpectSuccess(alice, collectionNFT);648 await enablePublicMintingExpectSuccess(alice, collectionNFT);649650 // Create a token to attempt to be nested into651 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');652 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};653654 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});655 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionFT]}});656657 // Try to create a nested token in the wrong collection658 await expect(executeTransaction(api, alice, api.tx.unique.createItem(659 collectionFT,660 targetAddress,661 {Fungible: {Value: 10}},662 )), 'while creating nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);663664 // Try to create and nest a token in the wrong collection665 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');666 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);667 });668 });669670 it('Fungible: disallows to nest token in an unlisted collection', async () => {671 await usingApi(async api => {672 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});673 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[]}});674675 // Create a token to attempt to be nested into676 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');677 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};678679 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});680681 // Try to create a nested token in the wrong collection682 await expect(executeTransaction(api, alice, api.tx.unique.createItem(683 collectionFT,684 targetAddress,685 {Fungible: {Value: 10}},686 )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);687688 // Try to create and nest a token in the wrong collection689 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');690 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);691 });692 });693694 // ---------- Re-Fungible ----------695696 it('ReFungible: disallows to nest token if nesting is disabled', async () => {697 await usingApi(async api => {698 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});699 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {}});700 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');701 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};702703 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});704705 // Create a nested token706 await expect(executeTransaction(api, alice, api.tx.unique.createItem(707 collectionRFT,708 targetAddress,709 {ReFungible: {const_data: [], pieces: 100}},710 )), 'while creating a nested token').to.be.rejectedWith(/^common\.UserIsNotAllowedToNest$/);711712 // Create a token to be nested713 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');714 // Try to nest715 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);716 // Try to nest717 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);718719 // Create another token to be nested720 const newToken2 = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');721 // Try to nest inside a fungible token722 await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionRFT, newToken)}, collectionRFT, newToken2, 1)), 'while nesting new token inside refungible').to.be.rejectedWith(/refungible\.RefungibleDisallowsNesting/);723 });724 });725726 it('ReFungible: disallows a non-Owner to nest someone else\'s token', async () => {727 await usingApi(async api => {728 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});729 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true}});730731 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);732 await enableAllowListExpectSuccess(alice, collectionNFT);733 await enablePublicMintingExpectSuccess(alice, collectionNFT);734735 // Create a token to attempt to be nested into736 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');737 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};738739 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});740741 // Try to create a nested token in the wrong collection742 await expect(executeTransaction(api, alice, api.tx.unique.createItem(743 collectionRFT,744 targetAddress,745 {ReFungible: {const_data: [], pieces: 100}},746 )), 'while creating a nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);747748 // Try to create and nest a token in the wrong collection749 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');750 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);751 });752 });753754 it('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {755 await usingApi(async api => {756 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});757 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);758 await enableAllowListExpectSuccess(alice, collectionNFT);759 await enablePublicMintingExpectSuccess(alice, collectionNFT);760761 // Create a token to attempt to be nested into762 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');763 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};764765 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});766 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[collectionRFT]}});767768 // Try to create a nested token in the wrong collection769 await expect(executeTransaction(api, alice, api.tx.unique.createItem(770 collectionRFT,771 targetAddress,772 {ReFungible: {const_data: [], pieces: 100}},773 )), 'while creating a nested token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);774775 // Try to create and nest a token in the wrong collection776 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');777 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);778 });779 });780781 it('ReFungible: disallows to nest token to an unlisted collection', async () => {782 await usingApi(async api => {783 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});784 await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {tokenOwner: true, restricted:[]}});785786 // Create a token to attempt to be nested into787 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');788 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};789790 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});791792 // Try to create a nested token in the wrong collection793 await expect(executeTransaction(api, alice, api.tx.unique.createItem(794 collectionRFT,795 targetAddress,796 {ReFungible: {const_data: [], pieces: 100}},797 )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);798799 // Try to create and nest a token in the wrong collection800 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');801 await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);802 });803 });804});