1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';2021let alice: IKeyringPair;22let bob: IKeyringPair;2324before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({url: import.meta.url});27 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);28 });29});3031[32 {mode: 'nft' as const, restrictedMode: true, requiredPallets: []},33 {mode: 'nft' as const, restrictedMode: false, requiredPallets: []},34 {mode: 'rft' as const, restrictedMode: true, requiredPallets: [Pallets.ReFungible]},35 {mode: 'rft' as const, restrictedMode: false, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37 itSub.ifWithPallets(`Token owner can nest ${testCase.mode.toUpperCase()} in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, testCase.requiredPallets, async ({helper}) => {38 39 const targetNFTCollection = await helper.nft.mintCollection(alice);40 const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});4142 const collectionForNesting = await helper[testCase.mode].mintCollection(bob);43 44 await targetNFTCollection.setPermissions(alice, {45 nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},46 });4748 49 const nestedToken1 = testCase.mode === 'nft'50 ? await (collectionForNesting as UniqueNFTCollection).mintToken(bob, targetTokenBob.nestingAccount())51 : await (collectionForNesting as UniqueRFTCollection).mintToken(bob, 10n, targetTokenBob.nestingAccount());52 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});53 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());5455 56 const nestedToken2 = await collectionForNesting.mintToken(bob);57 await nestedToken2.nest(bob, targetTokenBob);58 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});59 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());60 });61});626364[65 {restrictedMode: true},66 {restrictedMode: false},67].map(testCase => {68 itSub(`Token owner can nest FT in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {69 70 const targetNFTCollection = await helper.nft.mintCollection(alice);71 const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});7273 const collectionForNesting = await helper.ft.mintCollection(bob);74 75 await targetNFTCollection.setPermissions(alice, {76 nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},77 });7879 80 await collectionForNesting.mint(bob, 100n, targetTokenBob.nestingAccount());81 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetTokenBob.nestingAccount().toLowerCase()]);82 expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(100n);8384 85 await collectionForNesting.mint(bob, 100n);86 await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);87 expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(150n);88 });89});9091[92 {restrictedMode: true},93 {restrictedMode: false},94].map(testCase => {95 itSub(`Token owner can nest Native FT in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {96 97 const targetNFTCollection = await helper.nft.mintCollection(alice);98 const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});99100 const collectionForNesting = helper.ft.getCollectionObject(0);101 102 await targetNFTCollection.setPermissions(alice, {103 nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},104 });105106 107 await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);108 expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(50n);109 });110});111112113itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {114 const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});115 const tokenA = await collectionToNest.mintToken(alice);116 const tokenB = await collectionToNest.mintToken(alice);117118 119 const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});120 const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});121 const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});122 const nativeFtCollectionToBeNested = helper.ft.getCollectionObject(0);123124 const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());125 const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());126 const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());127 await nativeFtCollectionToBeNested.transfer(alice, tokenA.nestingAccount(), 100n);128 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());129 expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());130 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);131 expect(await nativeFtCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);132133 134 await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());135 await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);136 await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);137 await nativeFtCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);138139 expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});140 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());141142 expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);143 expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);144145 expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);146 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);147148 expect(await nativeFtCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);149 expect(await nativeFtCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);150});