difftreelog
Nesting tests refactoring WIP
in: master
5 files changed
tests/src/nesting/nest.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {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 /// TODO move32 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 // Create an immediately nested token37 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 // Create a token to be nested42 const newToken = await collection.mintToken(alice);4344 // Nest45 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 // Move bundle to different user50 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 // Unnest57 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 // Create an immediately nested token81 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 // Create a token to be nested and nest86 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 // Create an immediately nested token by an administrator98 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 // Create a token to be nested and nest103 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 // Create an immediately nested token118 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 // Create a token to be nested and nest123 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 // ---------- Non-Fungible ----------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 // Create an immediately nested token138 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 // Create a token to be nested and nest143 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 //await collectionB.addAdmin(alice, {Substrate: bob.address});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 // Create an immediately nested token164 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 // Create a token to be nested and nest169 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 // ---------- Fungible ----------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 // Create an immediately nested token190 await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());191 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);192193 // Create a token to be nested and nest194 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 // Create an immediately nested token213 await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());214 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);215216 // Create a token to be nested and nest217 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 // Create a nested-token matryoshka273 for (let i = 0; i < maxNestingLevel; i++) {274 token = await collection.mintToken(alice, token.nestingAccount());275 }276277 // The nesting depth is limited by `maxNestingLevel`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 // ---------- Admin ------------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 // Try to create an immediately nested token as collection admin when it's disallowed292 await expect(collection.mintToken(bob, targetToken.nestingAccount()))293 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);294295 // Try to create a token to be nested and nest296 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 // Try to create a nested token as token owner when it's disallowed311 await expect(collection.mintToken(bob, targetToken.nestingAccount()))312 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);313314 // Try to create a token to be nested and nest315 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 //await collection.addAdmin(alice, {Substrate: bob.address});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 // Try to nest somebody else's token331 const newToken = await collection.mintToken(bob);332 await expect(newToken.nest(alice, targetToken))333 .to.be.rejectedWith(/common\.NoPermission/);334335 // Try to unnest a token belonging to someone else as collection admin336 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 // Try to create a nested token from another collection352 await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))353 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);354355 // Create a token in another collection yet to be nested and try to nest356 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 // ---------- Non-Fungible ----------365366 itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {367 // Collection is implicitly not allowed nesting at creation368 const collection = await helper.nft.mintCollection(alice);369 const targetToken = await collection.mintToken(alice);370371 // Try to create a nested token as token owner when it's disallowed372 await expect(collection.mintToken(alice, targetToken.nestingAccount()))373 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);374375 // Try to create a token to be nested and nest376 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 // Try to create a token to be nested and nest393 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 // Try to create a token to be nested and nest413 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 // Create collection with restricted nesting -- even self is not allowed422 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 // Try to mint in own collection after allowlisting the accounts429 await expect(collection.mintToken(bob, targetToken.nestingAccount()))430 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);431 });432433 // ---------- Fungible ----------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 // Try to create an immediately nested token441 await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))442 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);443444 // Try to create a token to be nested and nest445 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 // Nest some tokens as Alice into Bob's token457 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());458459 // Try to pull it out460 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 // Nest some tokens as Alice into Bob's token473 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());474475 // Try to pull it out as Alice still476 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 // Try to mint an immediately nested token487 await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))488 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);489490 // Mint a token and try to nest it491 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});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {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 /// TODO move32 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 // Create an immediately nested token37 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 // Create a token to be nested42 const newToken = await collection.mintToken(alice);4344 // Nest45 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 // Move bundle to different user50 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 // Unnest57 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.ifWithPallets('ReFungible: getTopmostOwner works correctly with Nesting', [Pallets.ReFungible], async({helper}) => {76 const collectionNFT = await helper.nft.mintCollection(alice, {77 permissions: {78 nesting: {79 tokenOwner: true,80 },81 },82 });83 const collectionRFT = await helper.rft.mintCollection(alice);8485 const nft = await collectionNFT.mintToken(alice, {Substrate: alice.address});86 const rft = await collectionRFT.mintToken(alice, 100n, {Substrate: alice.address});8788 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});8990 await rft.transfer(alice, nft.nestingAccount(), 40n);9192 expect(await rft.getTopmostOwner()).deep.equal(null);9394 await rft.transfer(alice, nft.nestingAccount(), 60n);9596 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});9798 await rft.transferFrom(alice, nft.nestingAccount(), {Substrate: alice.address}, 30n);99100 expect(await rft.getTopmostOwner()).deep.equal(null);101102 await rft.transferFrom(alice, nft.nestingAccount(), {Substrate: alice.address}, 70n);103104 expect(await rft.getTopmostOwner()).deep.equal({Substrate: alice.address});105 });106});107108describe('Negative Test: Nesting', () => {109 let alice: IKeyringPair;110 let bob: IKeyringPair;111112 before(async () => {113 await usingPlaygrounds(async (helper, privateKey) => {114 const donor = await privateKey({filename: __filename});115 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);116 });117 });118119 itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {120 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});121 await collection.addAdmin(alice, {Substrate: bob.address});122 const targetToken = await collection.mintToken(alice);123124 // Try to create an immediately nested token as collection admin when it's disallowed125 await expect(collection.mintToken(bob, targetToken.nestingAccount()))126 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);127128 // Try to create a token to be nested and nest129 const newToken = await collection.mintToken(bob);130 await expect(newToken.nest(bob, targetToken))131 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);132133 expect(await targetToken.getChildren()).to.be.length(0);134 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});135 });136137 itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {138 const collectionAlice = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});139 const targetTokenBob = await collectionAlice.mintToken(alice, {Substrate: bob.address});140 await collectionAlice.addToAllowList(alice, {Substrate: bob.address});141 await collectionAlice.addToAllowList(alice, targetTokenBob.nestingAccount());142143 // Try to create a nested token as token owner when it's disallowed144 await expect(collectionAlice.mintToken(bob, targetTokenBob.nestingAccount()))145 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);146147 // Try to create a token to be nested and nest148 const newToken = await collectionAlice.mintToken(bob);149 await expect(newToken.nest(bob, targetTokenBob))150 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);151152 expect(await targetTokenBob.getChildren()).to.be.length(0);153 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});154 });155156 itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {157 const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});158 //await collection.addAdmin(alice, {Substrate: bob.address});159 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});160 await collection.addToAllowList(alice, {Substrate: bob.address});161 await collection.addToAllowList(alice, targetToken.nestingAccount());162163 // Try to nest somebody else's token164 const newToken = await collection.mintToken(bob);165 await expect(newToken.nest(alice, targetToken))166 .to.be.rejectedWith(/common\.NoPermission/);167168 // Try to unnest a token belonging to someone else as collection admin169 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());170 await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))171 .to.be.rejectedWith(/common\.AddressNotInAllowlist/);172173 expect(await targetToken.getChildren()).to.be.length(1);174 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});175 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());176 });177178 itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {179 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});180 const collectionFT = await helper.ft.mintCollection(alice);181 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});182183 // Nest some tokens as Alice into Bob's token184 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());185186 // Try to pull it out187 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))188 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);189 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);190 });191192 itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {193 const collectionNFT = await helper.nft.mintCollection(alice);194 const collectionFT = await helper.ft.mintCollection(alice);195 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});196197 await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});198199 // Nest some tokens as Alice into Bob's token200 await collectionFT.mint(alice, 5n, targetToken.nestingAccount());201202 // Try to pull it out as Alice still203 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))204 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);205 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);206 });207208});tests/src/sub/nesting/admin.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/sub/nesting/admin.test.ts
@@ -0,0 +1,87 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
+
+describe('Collection admin', () => {
+ let alice: IKeyringPair;
+ let bob: IKeyringPair;
+ let charlie: IKeyringPair;
+
+ before(async () => {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ const donor = await privateKey({filename: __filename});
+ [alice, bob, charlie] = await helper.arrange.createAccounts([200n, 10n, 10n], donor);
+ });
+ });
+
+ [
+ {restricted: true},
+ {restricted: false},
+ ].map(testCase => {
+ itSub(`can nest tokens if "collectionAdmin" permission set ${testCase.restricted ? ', in restricted mode' : ''}`, async ({helper}) => {
+ const collectionA = await helper.nft.mintCollection(alice);
+ await collectionA.addAdmin(alice, {Substrate: bob.address});
+ const collectionB = await helper.nft.mintCollection(alice);
+ await collectionB.addAdmin(alice, {Substrate: bob.address});
+ // Collection has permission for collectionAdmin to nest:
+ await collectionA.setPermissions(alice, {nesting:
+ {collectionAdmin: true, restricted: testCase.restricted ? [collectionA.collectionId, collectionB.collectionId] : null},
+ });
+ // Token for nesting in from collectionA:
+ const targetTokenA = await collectionA.mintToken(alice, {Substrate: charlie.address});
+
+ // 1. Create an immediately nested tokens:
+ // 1.1 From own collection:
+ const nestedTokenA = await collectionA.mintToken(bob, targetTokenA.nestingAccount());
+ // 1.2 From different collection:
+ const nestedTokenB = await collectionB.mintToken(bob, targetTokenA.nestingAccount());
+ expect(await nestedTokenA.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
+ expect(await nestedTokenA.getOwner()).to.be.deep.equal(targetTokenA.nestingAccount().toLowerCase());
+ expect(await nestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
+ expect(await nestedTokenB.getOwner()).to.be.deep.equal(targetTokenA.nestingAccount().toLowerCase());
+
+ // 2. Create a token to be nested and nest:
+ const newNestedTokenA = await collectionA.mintToken(bob);
+ const newNestedTokenB = await collectionB.mintToken(bob);
+ // 2.1 From own collection:
+ await newNestedTokenA.nest(bob, targetTokenA);
+ // 2.2 From different collection:
+ await newNestedTokenB.nest(bob, targetTokenA);
+ expect(await newNestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
+ expect(await newNestedTokenB.getOwner()).to.be.deep.equal(targetTokenA.nestingAccount().toLowerCase());
+ });
+ });
+
+ itSub('can operate together with token owner if "collectionAdmin" and "tokenOwner" permissions set', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});
+ await collection.addAdmin(alice, {Substrate: bob.address});
+ const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});
+
+ // Admin can create an immediately nested token:
+ const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());
+ expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
+ expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
+
+ // Owner can create and and nest:
+ const newToken = await collection.mintToken(alice, {Substrate: charlie.address});
+ await newToken.nest(charlie, targetToken);
+ expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
+ expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
+ });
+});
+
tests/src/sub/nesting/common.test.tsdiffbeforeafterboth--- a/tests/src/sub/nesting/common.test.ts
+++ b/tests/src/sub/nesting/common.test.ts
@@ -19,61 +19,77 @@
import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
let alice: IKeyringPair;
+let bob: IKeyringPair;
before(async () => {
await usingPlaygrounds(async (helper, privateKey) => {
const donor = await privateKey({filename: __filename});
- [alice] = await helper.arrange.createAccounts([100n], donor);
+ [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);
});
});
[
- {mode: 'nft' as const, requiredPallets: []},
- {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+ {mode: 'nft' as const, restrictedMode: true, requiredPallets: []},
+ {mode: 'nft' as const, restrictedMode: false, requiredPallets: []},
+ {mode: 'rft' as const, restrictedMode: true, requiredPallets: [Pallets.ReFungible]},
+ {mode: 'rft' as const, restrictedMode: false, requiredPallets: [Pallets.ReFungible]},
].map(testCase => {
- itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()} in NFT`, testCase.requiredPallets, async ({helper}) => {
- // Only NFT allows nesting, permissions should be set:
- const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
- const targetToken = await aliceNFTCollection.mintToken(alice);
+ itSub.ifWithPallets(`Token owner can nest ${testCase.mode.toUpperCase()} in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, testCase.requiredPallets, async ({helper}) => {
+ // Only NFT can be target for nesting in
+ const targetNFTCollection = await helper.nft.mintCollection(alice);
+ const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});
- const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
+ const collectionForNesting = await helper[testCase.mode].mintCollection(bob);
+ // permissions should be set:
+ await targetNFTCollection.setPermissions(alice, {
+ nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},
+ });
- // 1. Alice can immediately create nested token:
+ // 1. Bob can immediately create nested token:
const nestedToken1 = testCase.mode === 'nft'
- ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
- : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
- expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
- expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
+ ? await (collectionForNesting as UniqueNFTCollection).mintToken(bob, targetTokenBob.nestingAccount())
+ : await (collectionForNesting as UniqueRFTCollection).mintToken(bob, 10n, targetTokenBob.nestingAccount());
+ expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
+ expect(await nestedToken1.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());
- // 2. Alice can mint and nest token:
- const nestedToken2 = await collectionForNesting.mintToken(alice);
- await nestedToken2.nest(alice, targetToken);
- expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
- expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
+ // 2. Bob can mint and nest token:
+ const nestedToken2 = await collectionForNesting.mintToken(bob);
+ await nestedToken2.nest(bob, targetTokenBob);
+ expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
+ expect(await nestedToken2.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());
});
});
-itSub('Owner can nest FT in NFT', async ({helper}) => {
- // Only NFT allows nesting, permissions should be set:
- const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
- const targetToken = await aliceNFTCollection.mintToken(alice);
+[
+ {restrictedMode: true},
+ {restrictedMode: false},
+].map(testCase => {
+ itSub(`Token owner can nest FT in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {
+ // Only NFT allows nesting, permissions should be set:
+ const targetNFTCollection = await helper.nft.mintCollection(alice);
+ const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});
- const collectionForNesting = await helper.ft.mintCollection(alice);
+ const collectionForNesting = await helper.ft.mintCollection(bob);
+ // permissions should be set:
+ await targetNFTCollection.setPermissions(alice, {
+ nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},
+ });
- // 1. Alice can immediately create nested tokens:
- await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());
- expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);
- expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);
+ // 1. Alice can immediately create nested tokens:
+ await collectionForNesting.mint(bob, 100n, targetTokenBob.nestingAccount());
+ expect(await collectionForNesting.getTop10Owners()).deep.eq([targetTokenBob.nestingAccount().toLowerCase()]);
+ expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(100n);
- // 2. Alice can mint and nest token:
- await collectionForNesting.mint(alice, 100n);
- await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);
- expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);
+ // 2. Alice can mint and nest token:
+ await collectionForNesting.mint(bob, 100n);
+ await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);
+ expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(150n);
+ });
});
-itSub.ifWithPallets('Owner can transferFrom nested tokens', [Pallets.ReFungible], async ({helper}) => {
+itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {
const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
const tokenA = await collectionToNest.mintToken(alice);
const tokenB = await collectionToNest.mintToken(alice);
tests/src/sub/nesting/negative.test.tsdiffbeforeafterboth--- a/tests/src/sub/nesting/negative.test.ts
+++ b/tests/src/sub/nesting/negative.test.ts
@@ -16,15 +16,16 @@
import {IKeyringPair} from '@polkadot/types/types';
import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
-import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
+import {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '../../util/playgrounds/unique';
import {itEth} from '../../eth/util';
let alice: IKeyringPair;
+let bob: IKeyringPair;
before(async () => {
await usingPlaygrounds(async (helper, privateKey) => {
const donor = await privateKey({filename: __filename});
- [alice] = await helper.arrange.createAccounts([50n], donor);
+ [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);
});
});
@@ -32,7 +33,7 @@
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase => {
- itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {
+ itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {
// Create default collection, permissions are not set:
const aliceNFTCollection = await helper.nft.mintCollection(alice);
const targetToken = await aliceNFTCollection.mintToken(alice);
@@ -51,7 +52,7 @@
});
});
-itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {
+itSub('Owner cannot nest FT if nesting is disabled', async ({helper}) => {
// Create default collection, permissions are not set:
const aliceNFTCollection = await helper.nft.mintCollection(alice);
const targetToken = await aliceNFTCollection.mintToken(alice);
@@ -66,6 +67,65 @@
await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
});
+[
+ {mode: 'nft' as const},
+ {mode: 'rft' as const},
+ {mode: 'ft' as const},
+].map(testCase => {
+ itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {
+ const targetCollection = await helper.nft.mintCollection(alice, {permissions:
+ {nesting: {tokenOwner: true, collectionAdmin: true}},
+ });
+ const targetToken = await targetCollection.mintToken(alice);
+
+ const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);
+
+ let nestedTokenBob: UniqueNFToken | UniqueRFToken;
+ switch (testCase.mode) {
+ case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;
+ case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;
+ case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;
+ }
+
+ // Bob non-owner of targetToken and non admin of targetCollection, so
+ // 1. cannot mint nested token:
+ switch (testCase.mode) {
+ case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
+ case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
+ case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
+ }
+
+ // 2. cannot nest existing token:
+ switch (testCase.mode) {
+ case 'nft':
+ case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
+ case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
+ }
+ });
+});
+
+[
+ {mode: 'nft' as const, nesting: {tokenOwner: true, collectionAdmin: false}},
+ {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},
+].map(testCase => {
+ itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {
+ // Create collection with tokenOwner or create collection with collectionAdmin permission:
+ const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});
+ const targetTokenAlice = await targetCollection.mintToken(alice);
+ await targetCollection.addAdmin(alice, {Substrate: bob.address});
+
+ const nestedCollectionAlice = await helper[testCase.mode].mintCollection(alice);
+ const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);
+ // if nesting permissions restricted for token owner – minter is bob (admin),
+ // if collectionAdmin – alice (owner)
+
+ // FIXME: nesting allowed only for admin but token owner can nest anyway:
+ testCase.nesting.tokenOwner
+ ? await expect(nestedCollectionBob.mintToken(bob, targetTokenAlice.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)
+ : await expect(nestedCollectionAlice.mintToken(alice, targetTokenAlice.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
+ });
+});
+
itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {
const collection = await helper.nft.mintCollection(alice);
// To avoid UserIsNotAllowedToNest error
@@ -104,7 +164,7 @@
}
});
-itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {
+itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {
const existingCollection = await helper.nft.mintCollection(alice);
const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);
const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);
@@ -140,3 +200,62 @@
await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
});
+
+itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {
+ const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);
+ const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);
+ const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);
+ const notAllowedCollectionFT = await helper.ft.mintCollection(alice);
+
+ // Collection restricted to allowedCollectionId
+ const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:
+ {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},
+ });
+ // Create collection with restricted nesting -- even self is not allowed
+ const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:
+ {nesting: {tokenOwner: true, restricted: []}},
+ });
+
+ const targetTokenA = await restrictedCollectionA.mintToken(alice);
+ const targetTokenB = await restrictedCollectionB.mintToken(alice);
+
+ // 1. Cannot mint in own collection after allowlisting the accounts:
+ await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+
+ // 2. Cannot mint from notAllowedCollection:
+ await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+ await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
+});
+
+itSub('Cannot create nesting chains greater than 5', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+ let token = await collection.mintToken(alice);
+
+ const maxNestingLevel = 5;
+
+ // Create a nested-token matryoshka
+ for (let i = 0; i < maxNestingLevel; i++) {
+ token = await collection.mintToken(alice, token.nestingAccount());
+ }
+
+ // The nesting depth is limited by `maxNestingLevel`
+ // 1. Cannot mint:
+ await expect(collection.mintToken(alice, token.nestingAccount()))
+ .to.be.rejectedWith(/structure\.DepthLimit/);
+ // 2. Cannot transfer:
+ const anotherToken = await collection.mintToken(alice);
+ await expect(anotherToken.transfer(alice, token.nestingAccount()))
+ .to.be.rejectedWith(/structure\.DepthLimit/);
+ // 3. Cannot nest FT pieces:
+ const ftCollection = await helper.ft.mintCollection(alice);
+ await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))
+ .to.be.rejectedWith(/structure\.DepthLimit/);
+
+ expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
+ expect(await token.getChildren()).to.has.length(0);
+});
tests/src/sub/refungible/nesting.test.tsdiffbeforeafterboth--- a/tests/src/sub/refungible/nesting.test.ts
+++ b/tests/src/sub/refungible/nesting.test.ts
@@ -116,22 +116,6 @@
});
});
- itSub('cannot nest token if nesting is disabled', async ({helper}) => {
- const collectionNFT = await helper.nft.mintCollection(alice);
- const collectionRFT = await helper.rft.mintCollection(alice);
- const targetToken = await collectionNFT.mintToken(alice);
-
- // Try to create an immediately nested token
- await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))
- .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
-
- // Try to create a token to be nested and nest
- const token = await collectionRFT.mintToken(alice, 5n);
- await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))
- .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
- expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);
- });
-
[
{restrictedMode: true},
{restrictedMode: false},
@@ -160,21 +144,5 @@
.to.be.rejectedWith(/common\.ApprovedValueTooLow/);
expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);
});
- });
-
- itSub('ReFungible: disallows to nest token to an unlisted collection', async ({helper}) => {
- const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});
- const collectionRFT = await helper.rft.mintCollection(alice);
- const targetToken = await collectionNFT.mintToken(alice);
-
- // Try to create an immediately nested token
- await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))
- .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
-
- // Try to create a token to be nested and nest
- const token = await collectionRFT.mintToken(alice, 5n);
- await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))
- .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
- expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);
});
});