1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';19import {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '../../util/playgrounds/unique';20import {itEth} from '../../eth/util';2122let alice: IKeyringPair;23let bob: IKeyringPair;2425before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({filename: __filename});28 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);29 });30});3132[33 {mode: 'nft' as const, requiredPallets: []},34 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},35].map(testCase => {36 itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {37 38 const aliceNFTCollection = await helper.nft.mintCollection(alice);39 const targetToken = await aliceNFTCollection.mintToken(alice);4041 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4243 44 const nestingTx = testCase.mode === 'nft'45 ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())46 : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());47 await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');4849 50 const nestedToken2 = await collectionForNesting.mintToken(alice);51 await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');52 });53});5455itSub('Owner cannot nest FT if nesting is disabled', async ({helper}) => {56 57 const aliceNFTCollection = await helper.nft.mintCollection(alice);58 const targetToken = await aliceNFTCollection.mintToken(alice);5960 const collectionForNesting = await helper.ft.mintCollection(alice);6162 63 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');6465 66 await collectionForNesting.mint(alice, 100n);67 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');68});6970[71 {mode: 'nft' as const},72 {mode: 'rft' as const},73 {mode: 'ft' as const},74].map(testCase => {75 itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {76 const targetCollection = await helper.nft.mintCollection(alice, {permissions:77 {nesting: {tokenOwner: true, collectionAdmin: true}},78 });79 const targetToken = await targetCollection.mintToken(alice);8081 const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);8283 let nestedTokenBob: UniqueNFToken | UniqueRFToken;84 switch (testCase.mode) {85 case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;86 case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;87 case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;88 }8990 91 92 switch (testCase.mode) {93 case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;94 case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;95 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;96 }9798 99 switch (testCase.mode) {100 case 'nft':101 case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;102 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;103 }104 });105});106107[108 {mode: 'nft' as const, nesting: {tokenOwner: true, collectionAdmin: false}},109 {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},110].map(testCase => {111 itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {112 113 const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});114 const targetTokenAlice = await targetCollection.mintToken(alice);115 await targetCollection.addAdmin(alice, {Substrate: bob.address});116117 const nestedCollectionAlice = await helper[testCase.mode].mintCollection(alice);118 const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);119 120 121122 123 testCase.nesting.tokenOwner124 ? await expect(nestedCollectionBob.mintToken(bob, targetTokenAlice.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)125 : await expect(nestedCollectionAlice.mintToken(alice, targetTokenAlice.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);126 });127});128129itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {130 const collection = await helper.nft.mintCollection(alice);131 132 await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});133134 135 const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);136 const tokenBurnt = await collection.mintToken(alice);137 await tokenBurnt.burn(alice);138 const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);139140 141 const nftCollectionForNesting = await helper.nft.mintCollection(alice);142 const rftCollectionForNesting = await helper.rft.mintCollection(alice);143 const ftCollectionForNesting = await helper.ft.mintCollection(alice);144145 const testCases = [146 {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},147 {token: tokenBurnt, error: 'TokenNotFound'},148 {token: tokenNotMintedYet, error: 'TokenNotFound'},149 ];150151 for(const testCase of testCases) {152 153 await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);154 await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);155 await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);156157 158 const nft = await nftCollectionForNesting.mintToken(alice);159 const rft = await rftCollectionForNesting.mintToken(alice, 100n);160 const _ft = await ftCollectionForNesting.mint(alice, 100n);161 await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);162 await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);163 await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);164 }165});166167itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {168 const existingCollection = await helper.nft.mintCollection(alice);169 const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);170 const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);171172 const nftCollectionForNesting = await helper.nft.mintCollection(alice);173174 175 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');176 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');177178 179 const nft = await nftCollectionForNesting.mintToken(alice);180 await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');181 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');182});183184itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {185 186 const rftCollection = await helper.rft.mintCollection(alice);187 const ftCollection = await helper.ft.mintCollection(alice);188189 const rftToken = await rftCollection.mintToken(alice);190 const _ftToken = await ftCollection.mint(alice, 100n);191192 const collectionForNesting = await helper.nft.mintCollection(alice);193194 195 await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');196 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');197198 199 const nestedToken2 = await collectionForNesting.mintToken(alice);200 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');201 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');202});203204itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {205 const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);206 const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);207 const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);208 const notAllowedCollectionFT = await helper.ft.mintCollection(alice);209210 211 const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:212 {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},213 });214 215 const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:216 {nesting: {tokenOwner: true, restricted: []}},217 });218219 const targetTokenA = await restrictedCollectionA.mintToken(alice);220 const targetTokenB = await restrictedCollectionB.mintToken(alice);221222 223 await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);224 await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);225226 227 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);228 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);229 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);230 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);231 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);232 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);233});234235itSub('Cannot create nesting chains greater than 5', async ({helper}) => {236 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});237 let token = await collection.mintToken(alice);238239 const maxNestingLevel = 5;240241 242 for (let i = 0; i < maxNestingLevel; i++) {243 token = await collection.mintToken(alice, token.nestingAccount());244 }245246 247 248 await expect(collection.mintToken(alice, token.nestingAccount()))249 .to.be.rejectedWith(/structure\.DepthLimit/);250 251 const anotherToken = await collection.mintToken(alice);252 await expect(anotherToken.transfer(alice, token.nestingAccount()))253 .to.be.rejectedWith(/structure\.DepthLimit/);254 255 const ftCollection = await helper.ft.mintCollection(alice);256 await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))257 .to.be.rejectedWith(/structure\.DepthLimit/);258259 expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});260 expect(await token.getChildren()).to.has.length(0);261});