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;24let charlie: IKeyringPair;2526before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 const donor = await privateKey({url: import.meta.url});29 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30 });31});3233[34 {mode: 'nft' as const, requiredPallets: []},35 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37 itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {38 39 const aliceNFTCollection = await helper.nft.mintCollection(alice);40 const targetToken = await aliceNFTCollection.mintToken(alice);4142 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4344 45 const nestingTx = testCase.mode === 'nft'46 ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())47 : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());48 await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');4950 51 const nestedToken2 = await collectionForNesting.mintToken(alice);52 await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');53 });54});5556[57 {mode: 'ft'},58 {mode: 'nativeFt'},59].map(testCase => {60 itSub(`Owner cannot nest [${testCase.mode}] if nesting is disabled`, async ({helper}) => {61 62 const aliceNFTCollection = await helper.nft.mintCollection(alice);63 const targetToken = await aliceNFTCollection.mintToken(alice);6465 const collectionForNesting = testCase.mode === 'ft' ? await helper.ft.mintCollection(alice) : helper.ft.getCollectionObject(0);6667 68 await expect(testCase.mode === 'ft'69 ? collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())70 : collectionForNesting.transfer(alice, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');7172 73 if (testCase.mode === 'ft') {74 await collectionForNesting.mint(alice, 100n);75 }76 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');77 });78});7980[81 {mode: 'nft' as const},82 {mode: 'rft' as const},83 {mode: 'ft' as const},84 {mode: 'native ft' as const},85].map(testCase => {86 itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {87 const targetCollection = await helper.nft.mintCollection(alice, {permissions:88 {nesting: {tokenOwner: true, collectionAdmin: true}},89 });90 const targetToken = await targetCollection.mintToken(alice);9192 const nestedCollectionBob = await (93 testCase.mode === 'native ft'94 ? helper.ft.getCollectionObject(0)95 : helper[testCase.mode].mintCollection(bob)96 );9798 let nestedTokenBob: UniqueNFToken | UniqueRFToken;99 switch (testCase.mode) {100 case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;101 case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;102 case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;103 case 'native ft': break;104 }105106 107 108 switch (testCase.mode) {109 case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;110 case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;111 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;112 case 'native ft': break;113 }114115 116 switch (testCase.mode) {117 case 'nft':118 case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;119 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;120 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;121 }122 });123});124125[126 {mode: 'nft' as const, nesting: {tokenOwner: true, collectionAdmin: false}},127 {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},128].map(testCase => {129 itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {130 131 const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});132 const targetTokenCharlie = await targetCollection.mintToken(alice, {Substrate: charlie.address});133 await targetCollection.addAdmin(alice, {Substrate: bob.address});134135 const nestedCollectionCharlie = await helper[testCase.mode].mintCollection(charlie);136 const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);137 138 139 testCase.nesting.tokenOwner140 ? await expect(nestedCollectionBob.mintToken(bob, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)141 : await expect(nestedCollectionCharlie.mintToken(charlie, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);142 });143});144145itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {146 const collection = await helper.nft.mintCollection(alice);147 148 await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});149150 151 const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);152 const tokenBurnt = await collection.mintToken(alice);153 await tokenBurnt.burn(alice);154 const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);155156 157 const nftCollectionForNesting = await helper.nft.mintCollection(alice);158 const rftCollectionForNesting = await helper.rft.mintCollection(alice);159 const ftCollectionForNesting = await helper.ft.mintCollection(alice);160 const nativeFtCollectionForNesting = helper.ft.getCollectionObject(0);161162 const testCases = [163 {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},164 {token: tokenBurnt, error: 'TokenNotFound'},165 {token: tokenNotMintedYet, error: 'TokenNotFound'},166 ];167168 for(const testCase of testCases) {169 170 await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);171 await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);172 await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);173174 175 const nft = await nftCollectionForNesting.mintToken(alice);176 const rft = await rftCollectionForNesting.mintToken(alice, 100n);177 const _ft = await ftCollectionForNesting.mint(alice, 100n);178 await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);179 await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);180 await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);181 await expect(nativeFtCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);182 }183});184185itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {186 const existingCollection = await helper.nft.mintCollection(alice);187 const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);188 const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);189190 const nftCollectionForNesting = await helper.nft.mintCollection(alice);191192 193 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');194 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');195196 197 const nft = await nftCollectionForNesting.mintToken(alice);198 await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');199 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');200});201202itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {203 204 const rftCollection = await helper.rft.mintCollection(alice);205 const ftCollection = await helper.ft.mintCollection(alice);206 const nativeFtCollection = helper.ft.getCollectionObject(0);207208 const rftToken = await rftCollection.mintToken(alice);209 const _ftToken = await ftCollection.mint(alice, 100n);210211 const collectionForNesting = await helper.nft.mintCollection(alice);212213 214 await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');215 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');216 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');217218 219 const nestedToken2 = await collectionForNesting.mintToken(alice);220 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');221 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');222 await expect(nativeFtCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');223});224225itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {226 const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);227 const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);228 const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);229 const notAllowedCollectionFT = await helper.ft.mintCollection(alice);230 const notAllowedCollectionNativeFT = helper.ft.getCollectionObject(0);231232 233 const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:234 {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},235 });236 237 const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:238 {nesting: {tokenOwner: true, restricted: []}},239 });240241 const targetTokenA = await restrictedCollectionA.mintToken(alice);242 const targetTokenB = await restrictedCollectionB.mintToken(alice);243244 245 await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);246 await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);247248 249 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);250 await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);251 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);252 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);253 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);254 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);255 await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenA.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);256 await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenB.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);257});258259itSub('Cannot create nesting chains greater than 5', async ({helper}) => {260 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});261 let token = await collection.mintToken(alice);262263 const maxNestingLevel = 5;264265 266 for (let i = 0; i < maxNestingLevel; i++) {267 token = await collection.mintToken(alice, token.nestingAccount());268 }269270 271 272 await expect(collection.mintToken(alice, token.nestingAccount()))273 .to.be.rejectedWith(/structure\.DepthLimit/);274 275 const anotherToken = await collection.mintToken(alice);276 await expect(anotherToken.transfer(alice, token.nestingAccount()))277 .to.be.rejectedWith(/structure\.DepthLimit/);278 279 const ftCollection = await helper.ft.mintCollection(alice);280 await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))281 .to.be.rejectedWith(/structure\.DepthLimit/);282283 expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});284 expect(await token.getChildren()).to.has.length(0);285});