difftreelog
Merge pull request #855 from UniqueNetwork/tests/generalization
in: master
8 files changed
tests/src/nesting/nest.test.tsdiffbeforeafterbothno changes
tests/src/sub/nesting/admin.test.tsdiffbeforeafterbothno changes
tests/src/sub/nesting/common.test.tsdiffbeforeafterboth17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';18import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';19import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';20import {itEth} from '../../eth/util';212022describe('Nesting', () => {21let alice: IKeyringPair;23 let alice: IKeyringPair;22let bob: IKeyringPair;242325 before(async () => {24before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {25 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({filename: __filename});26 const donor = await privateKey({filename: __filename});28 [alice] = await helper.arrange.createAccounts([50n], donor);27 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);29 });30 });28 });29});313031[32 [32 {mode: 'nft' as const, restrictedMode: true, requiredPallets: []},33 {mode: 'nft' as const, requiredPallets: []},33 {mode: 'nft' as const, restrictedMode: false, requiredPallets: []},34 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},34 {mode: 'rft' as const, restrictedMode: true, requiredPallets: [Pallets.ReFungible]},35 ].map(testCase => {35 {mode: 'rft' as const, restrictedMode: false, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {36 itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()}`, testCase.requiredPallets, async ({helper}) => {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}) => {37 // Only NFT allows nesting, permissions should be set:38 // Only NFT can be target for nesting in38 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});39 const targetNFTCollection = await helper.nft.mintCollection(alice);39 const targetToken = await aliceNFTCollection.mintToken(alice);40 const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});404141 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);42 const collectionForNesting = await helper[testCase.mode].mintCollection(bob);4243 // 1. Alice can immediately create nested token:43 // permissions should be set:44 const nestedToken1 = testCase.mode === 'nft'44 await targetNFTCollection.setPermissions(alice, {45 ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())46 : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());45 nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},47 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});48 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4950 // 2. Alice can mint and nest token:51 const nestedToken2 = await collectionForNesting.mintToken(alice);52 await nestedToken2.nest(alice, targetToken);53 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});54 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());55 });46 });56 });574758 itSub('Owner can nest FT', async ({helper}) => {48 // 1. Bob can immediately create nested token:49 const nestedToken1 = testCase.mode === 'nft'59 // Only NFT allows nesting, permissions should be set:50 ? await (collectionForNesting as UniqueNFTCollection).mintToken(bob, targetTokenBob.nestingAccount())60 const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});51 : await (collectionForNesting as UniqueRFTCollection).mintToken(bob, 10n, targetTokenBob.nestingAccount());52 expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});61 const targetToken = await aliceNFTCollection.mintToken(alice);53 expect(await nestedToken1.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());625463 const collectionForNesting = await helper.ft.mintCollection(alice);55 // 2. Bob can mint and nest token:6465 // 1. Alice can immediately create nested tokens:66 await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());56 const nestedToken2 = await collectionForNesting.mintToken(bob);67 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);57 await nestedToken2.nest(bob, targetTokenBob);68 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);58 expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});6970 // 2. Alice can mint and nest token:71 await collectionForNesting.mint(alice, 100n);72 await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);73 expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);59 expect(await nestedToken2.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());74 });60 });75});61});766277describe('Nesting negative', () => {78 let alice: IKeyringPair;796364[80 before(async () => {65 {restrictedMode: true},66 {restrictedMode: false},67].map(testCase => {81 await usingPlaygrounds(async (helper, privateKey) => {68 itSub(`Token owner can nest FT in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {82 const donor = await privateKey({filename: __filename});69 // Only NFT allows nesting, permissions should be set:83 [alice] = await helper.arrange.createAccounts([50n], donor);70 const targetNFTCollection = await helper.nft.mintCollection(alice);84 });71 const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});85 });867287 [73 const collectionForNesting = await helper.ft.mintCollection(bob);88 {mode: 'nft' as const, requiredPallets: []},89 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},90 ].map(testCase => {91 itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {92 // Create default collection, permissions are not set:93 const aliceNFTCollection = await helper.nft.mintCollection(alice);94 const targetToken = await aliceNFTCollection.mintToken(alice);9596 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);9798 // 1. Alice cannot create immediately nested tokens:74 // permissions should be set:99 const nestingTx = testCase.mode === 'nft'75 await targetNFTCollection.setPermissions(alice, {100 ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())101 : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());76 nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},102 await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');103104 // 2. Alice cannot mint and nest token:105 const nestedToken2 = await collectionForNesting.mintToken(alice);106 await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');107 });77 });108 });10978110 itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {79 // 1. Alice can immediately create nested tokens:111 // Create default collection, permissions are not set:80 await collectionForNesting.mint(bob, 100n, targetTokenBob.nestingAccount());112 const aliceNFTCollection = await helper.nft.mintCollection(alice);81 expect(await collectionForNesting.getTop10Owners()).deep.eq([targetTokenBob.nestingAccount().toLowerCase()]);113 const targetToken = await aliceNFTCollection.mintToken(alice);82 expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(100n);11483115 const collectionForNesting = await helper.ft.mintCollection(alice);116117 // 1. Alice cannot create immediately nested tokens:118 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');119120 // 2. Alice can mint and nest token:84 // 2. Alice can mint and nest token:121 await collectionForNesting.mint(alice, 100n);85 await collectionForNesting.mint(bob, 100n);122 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');86 await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);87 expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(150n);123 });88 });89});12490125 itSub.ifWithPallets('Cannot nest to a future collection', [Pallets.ReFungible], async ({helper}) => {126 const nonExistingCollectionId = await helper.collection.getTotalCount() + 1000;127 const futureToken = helper.nft.getTokenObject(nonExistingCollectionId, 1);12891129 const nftCollectionForNesting = await helper.nft.mintCollection(alice);92itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {93 const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});130 const rftCollectionForNesting = await helper.rft.mintCollection(alice);94 const tokenA = await collectionToNest.mintToken(alice);131 const ftCollectionForNesting = await helper.ft.mintCollection(alice);95 const tokenB = await collectionToNest.mintToken(alice);13296133 // 1. Alice cannot create nested token to future token97 // Create a nested token134 await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');98 const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});135 await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');99 const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});136 await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');100 const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});137101138 // 2. Alice cannot mint and nest token:102 const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());139 const nft = await nftCollectionForNesting.mintToken(alice);140 const rft = await rftCollectionForNesting.mintToken(alice, 100n);103 const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());141 const _ft = await ftCollectionForNesting.mint(alice, 100n);104 const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());142 await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');105 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());143 await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');106 expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());144 await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('CollectionNotFound');107 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);145 });146108147 itSub.ifWithPallets('Cannot nest to a future token in a NFT collection', [Pallets.ReFungible], async ({helper}) => {109 // Transfer the nested token to another token148 const {collectionId} = await helper.nft.mintCollection(alice);110 await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());149 // To avoid UserIsNotAllowedToNest error111 await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);150 await helper.collection.setPermissions(alice, collectionId, {nesting: {collectionAdmin: true}});151 const futureToken = helper.nft.getTokenObject(collectionId, 1);112 await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);152113153 const nftCollectionForNesting = await helper.nft.mintCollection(alice);114 expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});154 const rftCollectionForNesting = await helper.rft.mintCollection(alice);155 const ftCollectionForNesting = await helper.ft.mintCollection(alice);115 expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());156116157 // 1. Alice cannot create nested token to future token117 expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);158 await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');159 await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');160 await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');118 expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);161119162 // 2. Alice cannot mint and nest token:120 expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);163 const nft = await nftCollectionForNesting.mintToken(alice);164 const rft = await rftCollectionForNesting.mintToken(alice, 100n);165 const _ft = await ftCollectionForNesting.mint(alice, 100n);166 await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');167 await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');168 await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('TokenNotFound');121 expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);169 });170171 itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {172 const existingCollection = await helper.nft.mintCollection(alice);173 const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);174 const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);175176 const nftCollectionForNesting = await helper.nft.mintCollection(alice);177178 // 1. Alice cannot create nested token to collection address179 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');180 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');181182 // 2. Alice cannot mint and nest token to collection address:183 const nft = await nftCollectionForNesting.mintToken(alice);184 await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');185 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');186 });187188 itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {189 // Create default collection, permissions are not set:190 const rftCollection = await helper.rft.mintCollection(alice);191 const ftCollection = await helper.ft.mintCollection(alice);192193 const rftToken = await rftCollection.mintToken(alice);194 const _ftToken = await ftCollection.mint(alice, 100n);195196 const collectionForNesting = await helper.nft.mintCollection(alice);197198 // 1. Alice cannot create immediately nested tokens:199 await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');200 await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');201202 // 2. Alice cannot mint and nest token:203 const nestedToken2 = await collectionForNesting.mintToken(alice);204 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');205 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');206 });207});122});208123tests/src/sub/nesting/e2e.test.tsdiffbeforeafterbothno changes
tests/src/sub/nesting/nesting.negative.test.tsdiffbeforeafterbothno changes
tests/src/sub/nesting/refungible.test.tsdiffbeforeafterbothno changes
tests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterbothno changes
tests/src/sub/refungible/nesting.test.tsdiffbeforeafterboth116 });116 });117 });117 });118119 itSub('cannot nest token if nesting is disabled', async ({helper}) => {120 const collectionNFT = await helper.nft.mintCollection(alice);121 const collectionRFT = await helper.rft.mintCollection(alice);122 const targetToken = await collectionNFT.mintToken(alice);123124 // Try to create an immediately nested token125 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))126 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);127128 // Try to create a token to be nested and nest129 const token = await collectionRFT.mintToken(alice, 5n);130 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))131 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);132 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);133 });134118135 [119 [136 {restrictedMode: true},120 {restrictedMode: true},162 });146 });163 });147 });164165 itSub('ReFungible: disallows to nest token to an unlisted collection', async ({helper}) => {166 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});167 const collectionRFT = await helper.rft.mintCollection(alice);168 const targetToken = await collectionNFT.mintToken(alice);169170 // Try to create an immediately nested token171 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))172 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);173174 // Try to create a token to be nested and nest175 const token = await collectionRFT.mintToken(alice, 5n);176 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))177 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);178 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);179 });180});148});181149