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});909192itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {93 const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});94 const tokenA = await collectionToNest.mintToken(alice);95 const tokenB = await collectionToNest.mintToken(alice);9697 98 const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});99 const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});100 const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});101102 const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());103 const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());104 const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());105 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());106 expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());107 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);108109 110 await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());111 await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);112 await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);113114 expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});115 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());116117 expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);118 expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);119120 expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);121 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);122});