1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util/index.js';19import {CrossAccountId} from '@unique/playgrounds/unique.js';2021describe('Nesting by collection admin', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 let charlie: IKeyringPair;2526 before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 const donor = await privateKey({url: import.meta.url});29 [alice, bob, charlie] = await helper.arrange.createAccounts([200n, 10n, 10n], donor);30 });31 });3233 [34 {restricted: true},35 {restricted: false},36 ].map(testCase => {37 itSub(`can nest tokens if "collectionAdmin" permission set ${testCase.restricted ? ', in restricted mode' : ''}`, async ({helper}) => {38 const collectionA = await helper.nft.mintCollection(alice);39 await collectionA.addAdmin(alice, {Substrate: bob.address});40 const collectionB = await helper.nft.mintCollection(alice);41 await collectionB.addAdmin(alice, {Substrate: bob.address});42 43 await collectionA.setPermissions(alice, {nesting:44 {collectionAdmin: true, restricted: testCase.restricted ? [collectionA.collectionId, collectionB.collectionId] : null},45 });46 47 const targetTokenA = await collectionA.mintToken(alice, {Substrate: charlie.address});4849 50 51 const nestedTokenA = await collectionA.mintToken(bob, targetTokenA.nestingAccount());52 53 const nestedTokenB = await collectionB.mintToken(bob, targetTokenA.nestingAccount());54 expect(await nestedTokenA.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});55 expect(await nestedTokenA.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));56 expect(await nestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});57 expect(await nestedTokenB.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));5859 60 const newNestedTokenA = await collectionA.mintToken(bob);61 const newNestedTokenB = await collectionB.mintToken(bob);62 63 await newNestedTokenA.nest(bob, targetTokenA);64 65 await newNestedTokenB.nest(bob, targetTokenA);66 expect(await newNestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});67 expect(await newNestedTokenB.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));68 });69 });7071 itSub('can operate together with token owner if "collectionAdmin" and "tokenOwner" permissions set', async ({helper}) => {72 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});73 await collection.addAdmin(alice, {Substrate: bob.address});74 const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});7576 77 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());78 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});79 expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));8081 82 const newToken = await collection.mintToken(alice, {Substrate: charlie.address});83 await newToken.nest(charlie, targetToken);84 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});85 expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));86 });87});