1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util/index.js';19import {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '@unique/playgrounds/src/unique.js';20import {itEth} from '../../eth/util/index.js';2122let alice: IKeyringPair;23let bob: IKeyringPair;24let charlie: IKeyringPair;2526describe('Negative Test: Nesting', () => {27 before(async () => {28 await usingPlaygrounds(async (helper, privateKey) => {29 const donor = await privateKey({url: import.meta.url});30 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);31 });32 });3334 [35 {mode: 'nft' as const, requiredPallets: []},36 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},37 ].map(testCase => {38 itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {39 40 const aliceNFTCollection = await helper.nft.mintCollection(alice);41 const targetToken = await aliceNFTCollection.mintToken(alice);4243 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4445 46 const nestingTx = testCase.mode === 'nft'47 ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())48 : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());49 await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');5051 52 const nestedToken2 = await collectionForNesting.mintToken(alice);53 await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');54 });55 });5657 [58 {mode: 'ft'},59 {mode: 'nativeFt'},60 ].map(testCase => {61 itSub(`Owner cannot nest [${testCase.mode}] if nesting is disabled (except for native fungible collection)`, async ({helper}) => {62 63 const aliceNFTCollection = await helper.nft.mintCollection(alice);64 const targetToken = await aliceNFTCollection.mintToken(alice);6566 const collectionForNesting = testCase.mode === 'ft' ? await helper.ft.mintCollection(alice) : helper.ft.getCollectionObject(0);6768 69 if(testCase.mode === 'ft') {70 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');71 } else {72 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 100n)).to.be.not.rejected;73 }7475 76 if(testCase.mode === 'ft') {77 await collectionForNesting.mint(alice, 100n);78 }7980 if(testCase.mode === 'ft') {81 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');82 } else {83 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.not.rejected;84 }85 });86 });8788 [89 {mode: 'nft' as const},90 {mode: 'rft' as const},91 {mode: 'ft' as const},92 {mode: 'native ft' as const},93 ].map(testCase => {94 itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens (except for native fungible collection)`, async ({helper}) => {95 const targetCollection = await helper.nft.mintCollection(alice, {permissions:96 {nesting: {tokenOwner: true, collectionAdmin: true}},97 });98 const targetToken = await targetCollection.mintToken(alice);99100 const nestedCollectionBob = await (101 testCase.mode === 'native ft'102 ? helper.ft.getCollectionObject(0)103 : helper[testCase.mode].mintCollection(bob)104 );105106 let nestedTokenBob: UniqueNFToken | UniqueRFToken;107 switch (testCase.mode) {108 case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;109 case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;110 case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;111 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n)).to.be.rejectedWith('common.UnsupportedOperation'); break;112 }113114 115 116 switch (testCase.mode) {117 case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;118 case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;119 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;120 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UnsupportedOperation'); break;121 }122123 124 switch (testCase.mode) {125 case 'nft':126 case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;127 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;128 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.not.rejected; break;129 }130 });131 });132133 [134 {mode: 'nft' as const, nesting: {tokenOwner: true, collectionAdmin: false}},135 {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},136 ].map(testCase => {137 itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {138 139 const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});140 const targetTokenCharlie = await targetCollection.mintToken(alice, {Substrate: charlie.address});141 await targetCollection.addAdmin(alice, {Substrate: bob.address});142143 const nestedCollectionCharlie = await helper[testCase.mode].mintCollection(charlie);144 const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);145 146 147 testCase.nesting.tokenOwner148 ? await expect(nestedCollectionBob.mintToken(bob, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)149 : await expect(nestedCollectionCharlie.mintToken(charlie, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);150 });151 });152153 itSub.ifWithPallets('Cannot nest in non existing token (except for native fungible collection)', [Pallets.ReFungible], async ({helper}) => {154 const collection = await helper.nft.mintCollection(alice);155 156 await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});157158 159 const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);160 const tokenBurnt = await collection.mintToken(alice);161 await tokenBurnt.burn(alice);162 const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);163164 165 const nftCollectionForNesting = await helper.nft.mintCollection(alice);166 const rftCollectionForNesting = await helper.rft.mintCollection(alice);167 const ftCollectionForNesting = await helper.ft.mintCollection(alice);168 const nativeFtCollectionForNesting = helper.ft.getCollectionObject(0);169170 const testCases = [171 {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},172 {token: tokenBurnt, error: 'TokenNotFound'},173 {token: tokenNotMintedYet, error: 'TokenNotFound'},174 ];175176 for(const testCase of testCases) {177 178 await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);179 await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);180 await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);181182 183 const nft = await nftCollectionForNesting.mintToken(alice);184 const rft = await rftCollectionForNesting.mintToken(alice, 100n);185 await ftCollectionForNesting.mint(alice, 100n);186 await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);187 await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);188 await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);189 await expect(nativeFtCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.not.rejected;190 }191 });192193 itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {194 const existingCollection = await helper.nft.mintCollection(alice);195 const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);196 const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);197198 const nftCollectionForNesting = await helper.nft.mintCollection(alice);199200 201 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');202 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');203204 205 const nft = await nftCollectionForNesting.mintToken(alice);206 await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');207 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');208 });209210 itEth.ifWithPallets('Cannot nest in RFT or FT (except for native fungible collection)', [Pallets.ReFungible], async ({helper}) => {211 212 const rftCollection = await helper.rft.mintCollection(alice);213 const ftCollection = await helper.ft.mintCollection(alice);214 const nativeFtCollection = helper.ft.getCollectionObject(0);215216 const rftToken = await rftCollection.mintToken(alice);217 await ftCollection.mint(alice, 100n);218219 const collectionForNesting = await helper.nft.mintCollection(alice);220221 222 await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');223 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');224 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');225226 227 const nestedToken2 = await collectionForNesting.mintToken(alice);228 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');229 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');230 await expect(nativeFtCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.not.rejected;231 });232233 itSub('Cannot nest in restricted collection if collection is not in the list (except native fungible collection)', async ({helper}) => {234 const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);235 const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);236 const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);237 const notAllowedCollectionFT = await helper.ft.mintCollection(alice);238 const allowedCollectionNativeFT = helper.ft.getCollectionObject(0);239240 241 const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:242 {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},243 });244 245 const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:246 {nesting: {tokenOwner: true, restricted: []}},247 });248249 const targetTokenA = await restrictedCollectionA.mintToken(alice);250 const targetTokenB = await restrictedCollectionB.mintToken(alice);251252 253 await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);254 await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);255256 257 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);258 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);259 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);260 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);261 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);262 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);263 await expect(allowedCollectionNativeFT.transfer(alice, targetTokenA.nestingAccount(), 100n)).to.be.not.rejected;264 await expect(allowedCollectionNativeFT.transfer(alice, targetTokenB.nestingAccount(), 100n)).to.be.not.rejected;265 });266267 itSub('Cannot create nesting chains greater than 5', async ({helper}) => {268 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});269 let token = await collection.mintToken(alice);270271 const maxNestingLevel = 5;272273 274 for(let i = 0; i < maxNestingLevel; i++) {275 token = await collection.mintToken(alice, token.nestingAccount());276 }277278 279 280 await expect(collection.mintToken(alice, token.nestingAccount()))281 .to.be.rejectedWith(/structure\.DepthLimit/);282 283 const anotherToken = await collection.mintToken(alice);284 await expect(anotherToken.transfer(alice, token.nestingAccount()))285 .to.be.rejectedWith(/structure\.DepthLimit/);286 287 const ftCollection = await helper.ft.mintCollection(alice);288 await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))289 .to.be.rejectedWith(/structure\.DepthLimit/);290291 expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});292 expect(await token.getChildren()).to.has.length(0);293 });294});