1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../util';1920describe('Integration Test: Composite nesting tests', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({filename: __filename});27 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);28 });29 });3031 32 itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});34 const targetToken = await collection.mintToken(alice);3536 37 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());38 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});39 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4041 42 const newToken = await collection.mintToken(alice);4344 45 await newToken.nest(alice, targetToken);46 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});47 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4849 50 await targetToken.transfer(alice, {Substrate: bob.address});51 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});52 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());53 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});54 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());5556 57 await newToken.unnest(bob, targetToken, {Substrate: bob.address});58 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});59 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});60 });61});6263describe('Integration Test: Various token type nesting', () => {64 let alice: IKeyringPair;65 let bob: IKeyringPair;66 let charlie: IKeyringPair;6768 before(async () => {69 await usingPlaygrounds(async (helper, privateKey) => {70 const donor = await privateKey({filename: __filename});71 [alice, bob, charlie] = await helper.arrange.createAccounts([200n, 10n, 10n], donor);72 });73 });7475 itSub('Admin (NFT): allows an Admin to nest a token', async ({helper}) => {76 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true}}});77 await collection.addAdmin(alice, {Substrate: bob.address});78 const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});7980 81 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());82 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});83 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());8485 86 const newToken = await collection.mintToken(bob);87 await newToken.nest(bob, targetToken);88 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});89 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());90 });9192 itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {93 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});94 await collection.addAdmin(alice, {Substrate: bob.address});95 const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});9697 98 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());99 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});100 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());101102 103 const newToken = await collection.mintToken(alice, {Substrate: charlie.address});104 await newToken.nest(charlie, targetToken);105 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});106 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());107 });108109 itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {110 const collectionA = await helper.nft.mintCollection(alice);111 await collectionA.addAdmin(alice, {Substrate: bob.address});112 const collectionB = await helper.nft.mintCollection(alice);113 await collectionB.addAdmin(alice, {Substrate: bob.address});114 await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted:[collectionB.collectionId]}});115 const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});116117 118 const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());119 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});120 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());121122 123 const newToken = await collectionB.mintToken(bob);124 await newToken.nest(bob, targetToken);125 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});126 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());127 });128129 130131 itSub('NFT: allows an Owner to nest/unnest their token', async ({helper}) => {132 const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});133 await collection.addToAllowList(alice, {Substrate: charlie.address});134 const targetToken = await collection.mintToken(charlie);135 await collection.addToAllowList(alice, targetToken.nestingAccount());136137 138 const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());139 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});140 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());141142 143 const newToken = await collection.mintToken(charlie);144 await newToken.nest(charlie, targetToken);145 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});146 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());147 });148149 itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {150 const collectionA = await helper.nft.mintCollection(alice);151 const collectionB = await helper.nft.mintCollection(alice);152 153 const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});154155 await collectionA.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionB.collectionId]}});156 await collectionA.addToAllowList(alice, {Substrate: charlie.address});157 await collectionA.addToAllowList(alice, targetToken.nestingAccount());158159 await collectionB.setPermissions(alice, {access: 'AllowList', mintMode: true});160 await collectionB.addToAllowList(alice, {Substrate: charlie.address});161 await collectionB.addToAllowList(alice, targetToken.nestingAccount());162163 164 const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());165 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});166 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());167168 169 const newToken = await collectionB.mintToken(charlie);170 await newToken.nest(charlie, targetToken);171 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});172 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());173 });174175 176177 itSub('Fungible: allows an Owner to nest/unnest their token', async ({helper}) => {178 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});179 const collectionFT = await helper.ft.mintCollection(alice);180 const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});181182 await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});183 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());184185 await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});186 await collectionFT.addToAllowList(alice, {Substrate: charlie.address});187 await collectionFT.addToAllowList(alice, targetToken.nestingAccount());188189 190 await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());191 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);192193 194 await collectionFT.mint(charlie, 5n);195 await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);196 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);197 });198199 itSub('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {200 const collectionNFT = await helper.nft.mintCollection(alice);201 const collectionFT = await helper.ft.mintCollection(alice);202 const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});203204 await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionFT.collectionId]}});205 await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});206 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());207208 await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});209 await collectionFT.addToAllowList(alice, {Substrate: charlie.address});210 await collectionFT.addToAllowList(alice, targetToken.nestingAccount());211212 213 await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());214 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);215216 217 await collectionFT.mint(charlie, 5n);218 await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);219 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);220 });221222 itSub.ifWithPallets('ReFungible: getTopmostOwner works correctly with Nesting', [Pallets.ReFungible], async({helper}) => {223 const collectionNFT = await helper.nft.mintCollection(alice, {224 permissions: {225 nesting: {226 tokenOwner: true,227 },228 },229 });230 const collectionRFT = await helper.rft.mintCollection(alice);231232 const nft = await collectionNFT.mintToken(alice, {Substrate: alice.address});233 const rft = await collectionRFT.mintToken(alice, 100n, {Substrate: alice.address});234235 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});236237 await rft.transfer(alice, nft.nestingAccount(), 40n);238239 expect(await rft.getTopmostOwner()).deep.equal(null);240241 await rft.transfer(alice, nft.nestingAccount(), 60n);242243 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});244245 await rft.transferFrom(alice, nft.nestingAccount(), {Substrate: alice.address}, 30n);246247 expect(await rft.getTopmostOwner()).deep.equal(null);248249 await rft.transferFrom(alice, nft.nestingAccount(), {Substrate: alice.address}, 70n);250251 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});252 });253});254255describe('Negative Test: Nesting', () => {256 let alice: IKeyringPair;257 let bob: IKeyringPair;258259 before(async () => {260 await usingPlaygrounds(async (helper, privateKey) => {261 const donor = await privateKey({filename: __filename});262 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);263 });264 });265266 itSub('Disallows excessive token nesting', async ({helper}) => {267 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});268 let token = await collection.mintToken(alice);269270 const maxNestingLevel = 5;271272 273 for (let i = 0; i < maxNestingLevel; i++) {274 token = await collection.mintToken(alice, token.nestingAccount());275 }276277 278 await expect(collection.mintToken(alice, token.nestingAccount()))279 .to.be.rejectedWith(/structure\.DepthLimit/);280 expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});281 expect(await token.getChildren()).to.be.length(0);282 });283284 285286 itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {287 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});288 await collection.addAdmin(alice, {Substrate: bob.address});289 const targetToken = await collection.mintToken(alice);290291 292 await expect(collection.mintToken(bob, targetToken.nestingAccount()))293 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);294295 296 const newToken = await collection.mintToken(bob);297 await expect(newToken.nest(bob, targetToken))298 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);299300 expect(await targetToken.getChildren()).to.be.length(0);301 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});302 });303304 itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {305 const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});306 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});307 await collection.addToAllowList(alice, {Substrate: bob.address});308 await collection.addToAllowList(alice, targetToken.nestingAccount());309310 311 await expect(collection.mintToken(bob, targetToken.nestingAccount()))312 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);313314 315 const newToken = await collection.mintToken(bob);316 await expect(newToken.nest(bob, targetToken))317 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);318319 expect(await targetToken.getChildren()).to.be.length(0);320 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});321 });322323 itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {324 const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});325 326 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});327 await collection.addToAllowList(alice, {Substrate: bob.address});328 await collection.addToAllowList(alice, targetToken.nestingAccount());329330 331 const newToken = await collection.mintToken(bob);332 await expect(newToken.nest(alice, targetToken))333 .to.be.rejectedWith(/common\.NoPermission/);334335 336 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());337 await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))338 .to.be.rejectedWith(/common\.AddressNotInAllowlist/);339340 expect(await targetToken.getChildren()).to.be.length(1);341 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});342 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());343 });344345 itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {346 const collectionA = await helper.nft.mintCollection(alice);347 const collectionB = await helper.nft.mintCollection(alice);348 await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted: [collectionA.collectionId]}});349 const targetToken = await collectionA.mintToken(alice);350351 352 await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))353 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);354355 356 const newToken = await collectionB.mintToken(alice);357 await expect(newToken.nest(alice, targetToken))358 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);359360 expect(await targetToken.getChildren()).to.be.length(0);361 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});362 });363364 365366 itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {367 368 const collection = await helper.nft.mintCollection(alice);369 const targetToken = await collection.mintToken(alice);370371 372 await expect(collection.mintToken(alice, targetToken.nestingAccount()))373 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);374375 376 const newToken = await collection.mintToken(alice);377 await expect(newToken.nest(alice, targetToken))378 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);379380 expect(await targetToken.getChildren()).to.be.length(0);381 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});382 });383384 itSub('NFT: disallows a non-Owner to nest someone else\'s token', async ({helper}) => {385 const collection = await helper.nft.mintCollection(alice);386 const targetToken = await collection.mintToken(alice);387388 await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});389 await collection.addToAllowList(alice, {Substrate: bob.address});390 await collection.addToAllowList(alice, targetToken.nestingAccount());391392 393 const newToken = await collection.mintToken(alice);394 await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);395396 expect(await targetToken.getChildren()).to.be.length(0);397 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});398 });399400 itSub('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({helper}) => {401 const collection = await helper.nft.mintCollection(alice);402 const targetToken = await collection.mintToken(alice);403404 await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});405 await collection.addToAllowList(alice, {Substrate: bob.address});406 await collection.addToAllowList(alice, targetToken.nestingAccount());407408 const collectionB = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true}});409 await collectionB.addToAllowList(alice, {Substrate: bob.address});410 await collectionB.addToAllowList(alice, targetToken.nestingAccount());411412 413 const newToken = await collectionB.mintToken(alice);414 await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);415416 expect(await targetToken.getChildren()).to.be.length(0);417 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});418 });419420 itSub('NFT: disallows to nest token in an unlisted collection', async ({helper}) => {421 422 const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: []}}});423 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});424425 await collection.addToAllowList(alice, {Substrate: bob.address});426 await collection.addToAllowList(alice, targetToken.nestingAccount());427428 429 await expect(collection.mintToken(bob, targetToken.nestingAccount()))430 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);431 });432433 434435 itSub('Fungible: disallows to nest token if nesting is disabled', async ({helper}) => {436 const collectionNFT = await helper.nft.mintCollection(alice);437 const collectionFT = await helper.ft.mintCollection(alice);438 const targetToken = await collectionNFT.mintToken(alice);439440 441 await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))442 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);443444 445 await collectionFT.mint(alice, 5n);446 await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 2n))447 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);448 expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);449 });450451 itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {452 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});453 const collectionFT = await helper.ft.mintCollection(alice);454 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});455456 457 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());458459 460 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))461 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);462 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);463 });464465 itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {466 const collectionNFT = await helper.nft.mintCollection(alice);467 const collectionFT = await helper.ft.mintCollection(alice);468 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});469470 await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});471472 473 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());474475 476 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))477 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);478 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);479 });480481 itSub('Fungible: disallows to nest token in an unlisted collection', async ({helper}) => {482 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true, restricted: []}}});483 const collectionFT = await helper.ft.mintCollection(alice);484 const targetToken = await collectionNFT.mintToken(alice);485486 487 await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))488 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);489490 491 await collectionFT.mint(alice, 5n);492 await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 1n))493 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);494495 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);496 expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);497 });498});