1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';2021let alice: IKeyringPair;2223before(async () => {24 await usingPlaygrounds(async (helper, privateKey) => {25 const donor = await privateKey({filename: __filename});26 [alice] = await helper.arrange.createAccounts([100n], donor);27 });28});2930[31 {mode: 'nft' as const, requiredPallets: []},32 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},33].map(testCase => {34 itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()} in NFT`, testCase.requiredPallets, async ({helper}) => {35 36 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});37 const targetToken = await aliceNFTCollection.mintToken(alice);3839 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4041 42 const nestedToken1 = testCase.mode === 'nft'43 ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())44 : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());45 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});46 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4748 49 const nestedToken2 = await collectionForNesting.mintToken(alice);50 await nestedToken2.nest(alice, targetToken);51 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});52 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());53 });54});555657itSub('Owner can nest FT in NFT', async ({helper}) => {58 59 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});60 const targetToken = await aliceNFTCollection.mintToken(alice);6162 const collectionForNesting = await helper.ft.mintCollection(alice);6364 65 await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());66 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);67 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);6869 70 await collectionForNesting.mint(alice, 100n);71 await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);72 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);73});747576itSub.ifWithPallets('Owner can transferFrom nested tokens', [Pallets.ReFungible], async ({helper}) => {77 const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});78 const tokenA = await collectionToNest.mintToken(alice);79 const tokenB = await collectionToNest.mintToken(alice);8081 82 const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});83 const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});84 const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});8586 const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());87 const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());88 const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());89 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());90 expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());91 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);9293 94 await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());95 await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);96 await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);9798 expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});99 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());100101 expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);102 expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);103104 expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);105 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);106});