git.delta.rocks / unique-network / refs/commits / 0bfb46901e51

difftreelog

Merge pull request #855 from UniqueNetwork/tests/generalization

Yaroslav Bolyukin2023-02-16parents: #59a9de8 #a17a394.patch.diff
in: master

8 files changed

deletedtests/src/nesting/nest.test.tsdiffbeforeafterboth

no changes

addedtests/src/sub/nesting/admin.test.tsdiffbeforeafterboth

no changes

modifiedtests/src/sub/nesting/common.test.tsdiffbeforeafterboth
17import {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';
2120
22describe('Nesting', () => {21let alice: IKeyringPair;
23 let alice: IKeyringPair;22let bob: IKeyringPair;
2423
25 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});
3130
31[
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 in
38 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});
4041
41 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);42 const collectionForNesting = await helper[testCase.mode].mintCollection(bob);
42
43 // 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());
49
50 // 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 });
5747
58 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());
6254
63 const collectionForNesting = await helper.ft.mintCollection(alice);55 // 2. Bob can mint and nest token:
64
65 // 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});
69
70 // 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});
7662
77describe('Nesting negative', () => {
78 let alice: IKeyringPair;
7963
64[
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 });
8672
87 [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);
95
96 const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
97
98 // 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');
103
104 // 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 });
10978
110 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);
11483
115 const collectionForNesting = await helper.ft.mintCollection(alice);
116
117 // 1. Alice cannot create immediately nested tokens:
118 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');
119
120 // 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});
12490
125 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);
12891
129 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);
13296
133 // 1. Alice cannot create nested token to future token97 // Create a nested token
134 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}}});
137101
138 // 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 });
146108
147 itSub.ifWithPallets('Cannot nest to a future token in a NFT collection', [Pallets.ReFungible], async ({helper}) => {109 // Transfer the nested token to another token
148 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);
152113
153 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());
156116
157 // 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);
161119
162 // 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 });
170
171 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);
175
176 const nftCollectionForNesting = await helper.nft.mintCollection(alice);
177
178 // 1. Alice cannot create nested token to collection address
179 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
180 await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
181
182 // 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 });
187
188 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);
192
193 const rftToken = await rftCollection.mintToken(alice);
194 const _ftToken = await ftCollection.mint(alice, 100n);
195
196 const collectionForNesting = await helper.nft.mintCollection(alice);
197
198 // 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');
201
202 // 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});
208123
addedtests/src/sub/nesting/e2e.test.tsdiffbeforeafterboth

no changes

addedtests/src/sub/nesting/nesting.negative.test.tsdiffbeforeafterboth

no changes

addedtests/src/sub/nesting/refungible.test.tsdiffbeforeafterboth

no changes

addedtests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterboth

no changes

modifiedtests/src/sub/refungible/nesting.test.tsdiffbeforeafterboth
116 });116 });
117 });117 });
118
119 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);
123
124 // Try to create an immediately nested token
125 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))
126 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);
127
128 // Try to create a token to be nested and nest
129 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 });
134118
135 [119 [
136 {restrictedMode: true},120 {restrictedMode: true},
162 });146 });
163 });147 });
164
165 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);
169
170 // Try to create an immediately nested token
171 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))
172 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
173
174 // Try to create a token to be nested and nest
175 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