git.delta.rocks / unique-network / refs/commits / 641d3fa29e37

difftreelog

refactor use playgrounds in RMRK isolation test

Daniel Shiposha2022-10-11parent: #698c51f.patch.diff
in: master

1 file changed

modifiedtests/src/rmrk/rmrkIsolation.test.tsdiffbeforeafterboth
1import {expect} from 'chai';1import {executeTransaction} from '../substrate/substrate-api';
2import usingApi, {executeTransaction} from '../substrate/substrate-api';
3import {
4 createCollectionExpectSuccess,
5 createItemExpectSuccess,
6 getCreateCollectionResult,
7 getDetailedCollectionInfo,
8 getGenericResult,
9 requirePallets,
10 normalizeAccountId,
11 Pallets,
12} from '../deprecated-helpers/helpers';
13import {IKeyringPair} from '@polkadot/types/types';2import {IKeyringPair} from '@polkadot/types/types';
14import {ApiPromise} from '@polkadot/api';3import {itSub, expect, usingPlaygrounds, Pallets} from '../util/playgrounds';
15import {it} from 'mocha';4import {UniqueHelper} from '../util/playgrounds/unique';
165
17let alice: IKeyringPair;6let alice: IKeyringPair;
18let bob: IKeyringPair;7let bob: IKeyringPair;
198
20async function createRmrkCollection(api: ApiPromise, sender: IKeyringPair): Promise<{uniqueId: number, rmrkId: number}> {9async function createRmrkCollection(helper: UniqueHelper, sender: IKeyringPair): Promise<{uniqueId: number, rmrkId: number}> {
21 const tx = api.tx.rmrkCore.createCollection('metadata', null, 'symbol');10 const result = await helper.executeExtrinsic(sender, 'api.tx.rmrkCore.createCollection', ['metadata', null, 'symbol'], true);
22 const events = await executeTransaction(api, sender, tx);
2311
24 const uniqueResult = getCreateCollectionResult(events);12 const uniqueId = helper.util.extractCollectionIdFromCreationResult(result);
13
25 const rmrkResult = getGenericResult(events, 'rmrkCore', 'CollectionCreated', (data) => {14 let rmrkId = null;
15 result.result.events.forEach(({event: {data, method, section}}) => {
16 if ((section === 'rmrkCore') && (method === 'CollectionCreated')) {
26 return parseInt(data[1].toString(), 10);17 rmrkId = parseInt(data[1].toString(), 10);
18 }
27 });19 });
2820
21 if (rmrkId === null) {
22 throw Error('No rmrkCore.CollectionCreated event was found!');
23 }
24
29 return {25 return {
30 uniqueId: uniqueResult.collectionId,26 uniqueId,
31 rmrkId: rmrkResult.data!,27 rmrkId,
32 };28 };
33}29}
3430
35async function createRmrkNft(api: ApiPromise, sender: IKeyringPair, collectionId: number): Promise<number> {31async function createRmrkNft(helper: UniqueHelper, sender: IKeyringPair, collectionId: number): Promise<number> {
36 const tx = api.tx.rmrkCore.mintNft(32 const result = await helper.executeExtrinsic(
33 sender,
34 'api.tx.rmrkCore.mintNft',
37 sender.address,35 [
36 sender.address,
38 collectionId,37 collectionId,
39 sender.address,38 sender.address,
40 null,39 null,
41 'nft-metadata',40 'nft-metadata',
41 true,
42 null,
43 ],
42 true,44 true,
43 null,
44 );45 );
46
45 const events = await executeTransaction(api, sender, tx);47 let rmrkNftId = null;
48 result.result.events.forEach(({event: {data, method, section}}) => {
46 const result = getGenericResult(events, 'rmrkCore', 'NftMinted', (data) => {49 if ((section === 'rmrkCore') && (method === 'NftMinted')) {
47 return parseInt(data[2].toString(), 10);50 rmrkNftId = parseInt(data[2].toString(), 10);
51 }
48 });52 });
4953
50 return result.data!;54 if (rmrkNftId === null) {
55 throw Error('No rmrkCore.NftMinted event was found!');
51}56 }
5257
53async function isUnique(): Promise<boolean> {58 return rmrkNftId;
54 return usingApi(async api => {
55 const chain = await api.rpc.system.chain();
56
57 return chain.eq('UNIQUE');
58 });
59}59}
6060
61describe('RMRK External Integration Test', async () => {61describe('RMRK External Integration Test', async () => {
62 const it_rmrk = (await isUnique() ? it : it.skip);
63
64 before(async function() {62 before(async function() {
65 await usingApi(async (api, privateKeyWrapper) => {63 await usingPlaygrounds(async (_helper, privateKey) => {
66 alice = privateKeyWrapper('//Alice');64 alice = privateKey('//Alice');
67 await requirePallets(this, [Pallets.RmrkCore]);
68 });65 });
69 });66 });
7067
71 it_rmrk('Creates a new RMRK collection that is mapped to a different ID and is tagged as external', async () => {68 itSub.ifWithPallets('Creates a new RMRK collection that is mapped to a different ID and is tagged as external', [Pallets.RmrkCore], async ({helper}) => {
72 await usingApi(async api => {69 // throw away collection to bump last Unique collection ID to test ID mapping
73 // throwaway collection to bump last Unique collection ID to test ID mapping
74 await createCollectionExpectSuccess();70 await helper.nft.mintCollection(alice, {tokenPrefix: 'unqt'});
7571
76 const collectionIds = await createRmrkCollection(api, alice);72 const collectionIds = await createRmrkCollection(helper, alice);
7773
78 expect(collectionIds.rmrkId).to.be.lessThan(collectionIds.uniqueId, 'collection ID mapping');74 expect(collectionIds.rmrkId).to.be.lessThan(collectionIds.uniqueId, 'collection ID mapping');
7975
80 const collection = (await getDetailedCollectionInfo(api, collectionIds.uniqueId))!;76 const collection = (await helper.nft.getCollectionObject(collectionIds.uniqueId).getData())!; // (await getDetailedCollectionInfo(api, collectionIds.uniqueId))!;
81 expect(collection.readOnly.toHuman(), 'tagged external').to.be.true;77 expect(collection.raw.readOnly, 'tagged external').to.be.true;
82 });
83 });78 });
84});79});
8580
86describe('Negative Integration Test: External Collections, Internal Ops', async () => {81describe('Negative Integration Test: External Collections, Internal Ops', async () => {
87 let uniqueCollectionId: number;82 let uniqueCollectionId: number;
88 let rmrkCollectionId: number;83 let rmrkCollectionId: number;
89 let rmrkNftId: number;84 let rmrkNftId: number;
85 let normalizedAlice: {Substrate: string};
9086
91 const it_rmrk = (await isUnique() ? it : it.skip);
92
93 before(async () => {87 before(async () => {
94 await usingApi(async (api, privateKeyWrapper) => {88 await usingPlaygrounds(async (helper, privateKey) => {
95 alice = privateKeyWrapper('//Alice');89 alice = privateKey('//Alice');
96 bob = privateKeyWrapper('//Bob');90 bob = privateKey('//Bob');
91 normalizedAlice = {Substrate: helper.address.normalizeSubstrateToChainFormat(alice.address)};
9792
98 const collectionIds = await createRmrkCollection(api, alice);93 const collectionIds = await createRmrkCollection(helper, alice);
99 uniqueCollectionId = collectionIds.uniqueId;94 uniqueCollectionId = collectionIds.uniqueId;
100 rmrkCollectionId = collectionIds.rmrkId;95 rmrkCollectionId = collectionIds.rmrkId;
10196
102 rmrkNftId = await createRmrkNft(api, alice, rmrkCollectionId);97 rmrkNftId = await createRmrkNft(helper, alice, rmrkCollectionId);
103 });98 });
104 });99 });
105100
106 it_rmrk('[Negative] Forbids Unique operations with an external collection, handled by dispatch_call', async () => {101 itSub.ifWithPallets('[Negative] Forbids Unique operations with an external collection, handled by dispatch_call', [Pallets.RmrkCore], async ({helper}) => {
107 await usingApi(async api => {
108 // Collection item creation102 // Collection item creation
109103
110 const txCreateItem = api.tx.unique.createItem(uniqueCollectionId, normalizeAccountId(alice), 'NFT');104 await expect(helper.nft.mintToken(alice, {collectionId: uniqueCollectionId, owner: {Substrate: alice.address}}), 'creating item')
111 await expect(executeTransaction(api, alice, txCreateItem), 'creating item')
112 .to.be.rejectedWith(/common\.CollectionIsExternal/);105 .to.be.rejectedWith(/common\.CollectionIsExternal/);
113106
114 const txCreateMultipleItems = api.tx.unique.createMultipleItems(uniqueCollectionId, normalizeAccountId(alice), [{NFT: {}}, {NFT: {}}]);107 const txCreateMultipleItems = helper.getApi().tx.unique.createMultipleItems(uniqueCollectionId, normalizedAlice, [{NFT: {}}, {NFT: {}}]);
115 await expect(executeTransaction(api, alice, txCreateMultipleItems), 'creating multiple')108 await expect(executeTransaction(helper.getApi(), alice, txCreateMultipleItems), 'creating multiple')
116 .to.be.rejectedWith(/common\.CollectionIsExternal/);109 .to.be.rejectedWith(/common\.CollectionIsExternal/);
110
111 await expect(helper.nft.mintMultipleTokens(alice, uniqueCollectionId, [{owner: {Substrate: alice.address}}]), 'creating multiple ex')
112 .to.be.rejectedWith(/common\.CollectionIsExternal/);
117113
118 const txCreateMultipleItemsEx = api.tx.unique.createMultipleItemsEx(uniqueCollectionId, {NFT: [{}]});114 // Collection properties
119 await expect(executeTransaction(api, alice, txCreateMultipleItemsEx), 'creating multiple ex')
120 .to.be.rejectedWith(/common\.CollectionIsExternal/);
121115
122 // Collection properties116 await expect(helper.nft.setProperties(alice, uniqueCollectionId, [{key: 'a', value: '1'}, {key: 'b'}]), 'setting collection properties')
117 .to.be.rejectedWith(/common\.CollectionIsExternal/);
123118
124 const txSetCollectionProperties = api.tx.unique.setCollectionProperties(uniqueCollectionId, [{key: 'a', value: '1'}, {key: 'b'}]);119 await expect(helper.nft.deleteProperties(alice, uniqueCollectionId, ['a']), 'deleting collection properties')
125 await expect(executeTransaction(api, alice, txSetCollectionProperties), 'setting collection properties')
126 .to.be.rejectedWith(/common\.CollectionIsExternal/);120 .to.be.rejectedWith(/common\.CollectionIsExternal/);
127121
128 const txDeleteCollectionProperties = api.tx.unique.deleteCollectionProperties(uniqueCollectionId, ['a']);122 await expect(helper.nft.setTokenPropertyPermissions(alice, uniqueCollectionId, [{key: 'a', permission: {mutable: true}}]), 'setting property permissions')
129 await expect(executeTransaction(api, alice, txDeleteCollectionProperties), 'deleting collection properties')
130 .to.be.rejectedWith(/common\.CollectionIsExternal/);123 .to.be.rejectedWith(/common\.CollectionIsExternal/);
131124
132 const txsetTokenPropertyPermissions = api.tx.unique.setTokenPropertyPermissions(uniqueCollectionId, [{key: 'a', permission: {mutable: true}}]);125 // NFT
133 await expect(executeTransaction(api, alice, txsetTokenPropertyPermissions), 'setting property permissions')
134 .to.be.rejectedWith(/common\.CollectionIsExternal/);
135126
136 // NFT127 await expect(helper.nft.burnToken(alice, uniqueCollectionId, rmrkNftId, 1n), 'burning')
128 .to.be.rejectedWith(/common\.CollectionIsExternal/);
137129
138 const txBurn = api.tx.unique.burnItem(uniqueCollectionId, rmrkNftId, 1);130 await expect(helper.nft.burnTokenFrom(alice, uniqueCollectionId, rmrkNftId, {Substrate: alice.address}, 1n), 'burning-from')
139 await expect(executeTransaction(api, alice, txBurn), 'burning').to.be.rejectedWith(/common\.CollectionIsExternal/);131 .to.be.rejectedWith(/common\.CollectionIsExternal/);
140132
141 const txBurnFrom = api.tx.unique.burnFrom(uniqueCollectionId, normalizeAccountId(alice), rmrkNftId, 1);133 await expect(helper.nft.transferToken(alice, uniqueCollectionId, rmrkNftId, {Substrate: bob.address}), 'transferring')
142 await expect(executeTransaction(api, alice, txBurnFrom), 'burning-from').to.be.rejectedWith(/common\.CollectionIsExternal/);134 .to.be.rejectedWith(/common\.CollectionIsExternal/);
143135
144 const txTransfer = api.tx.unique.transfer(normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);136 await expect(helper.nft.approveToken(alice, uniqueCollectionId, rmrkNftId, {Substrate: bob.address}), 'approving')
145 await expect(executeTransaction(api, alice, txTransfer), 'transferring').to.be.rejectedWith(/common\.CollectionIsExternal/);137 .to.be.rejectedWith(/common\.CollectionIsExternal/);
146138
147 const txApprove = api.tx.unique.approve(normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);139 await expect(helper.nft.transferTokenFrom(alice, uniqueCollectionId, rmrkNftId, {Substrate: alice.address}, {Substrate: bob.address}), 'transferring-from')
148 await expect(executeTransaction(api, alice, txApprove), 'approving').to.be.rejectedWith(/common\.CollectionIsExternal/);140 .to.be.rejectedWith(/common\.CollectionIsExternal/);
149141
150 const txTransferFrom = api.tx.unique.transferFrom(normalizeAccountId(alice), normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);142 // NFT properties
151 await expect(executeTransaction(api, alice, txTransferFrom), 'transferring-from').to.be.rejectedWith(/common\.CollectionIsExternal/);
152143
153 // NFT properties144 await expect(helper.nft.setTokenProperties(alice, uniqueCollectionId, rmrkNftId, [{key: 'a', value: '2'}]), 'setting token properties')
145 .to.be.rejectedWith(/common\.CollectionIsExternal/);
154146
155 const txSetTokenProperties = api.tx.unique.setTokenProperties(uniqueCollectionId, rmrkNftId, [{key: 'a', value: '2'}]);147 await expect(helper.nft.deleteTokenProperties(alice, uniqueCollectionId, rmrkNftId, ['a']))
156 await expect(executeTransaction(api, alice, txSetTokenProperties), 'setting token properties')
157 .to.be.rejectedWith(/common\.CollectionIsExternal/);
158
159 const txDeleteTokenProperties = api.tx.unique.deleteTokenProperties(uniqueCollectionId, rmrkNftId, ['a']);
160 await expect(executeTransaction(api, alice, txDeleteTokenProperties), 'deleting token properties')
161 .to.be.rejectedWith(/common\.CollectionIsExternal/);148 .to.be.rejectedWith(/common\.CollectionIsExternal/);
162 });
163 });149 });
164150
165 it_rmrk('[Negative] Forbids Unique collection operations with an external collection', async () => {151 itSub.ifWithPallets('[Negative] Forbids Unique collection operations with an external collection', [Pallets.RmrkCore], async ({helper}) => {
166 await usingApi(async api => {152 await expect(helper.nft.burn(alice, uniqueCollectionId), 'destroying collection')
167 const txDestroyCollection = api.tx.unique.destroyCollection(uniqueCollectionId);
168 await expect(executeTransaction(api, alice, txDestroyCollection), 'destroying collection')
169 .to.be.rejectedWith(/common\.CollectionIsExternal/);153 .to.be.rejectedWith(/common\.CollectionIsExternal/);
170154
171 // Allow list155 // Allow list
172156
173 const txAddAllowList = api.tx.unique.addToAllowList(uniqueCollectionId, normalizeAccountId(bob));157 await expect(helper.nft.addToAllowList(alice, uniqueCollectionId, {Substrate: bob.address}), 'adding to allow list')
174 await expect(executeTransaction(api, alice, txAddAllowList), 'adding to allow list')
175 .to.be.rejectedWith(/common\.CollectionIsExternal/);158 .to.be.rejectedWith(/common\.CollectionIsExternal/);
176159
177 const txRemoveAllowList = api.tx.unique.removeFromAllowList(uniqueCollectionId, normalizeAccountId(bob));160 await expect(helper.nft.removeFromAllowList(alice, uniqueCollectionId, {Substrate: bob.address}), 'removing from allowlist')
178 await expect(executeTransaction(api, alice, txRemoveAllowList), 'removing from allowlist')
179 .to.be.rejectedWith(/common\.CollectionIsExternal/);161 .to.be.rejectedWith(/common\.CollectionIsExternal/);
180162
181 // Owner / Admin / Sponsor163 // Owner / Admin / Sponsor
182164
183 const txChangeOwner = api.tx.unique.changeCollectionOwner(uniqueCollectionId, bob.address);165 await expect(helper.nft.changeOwner(alice, uniqueCollectionId, bob.address), 'changing owner')
184 await expect(executeTransaction(api, alice, txChangeOwner), 'changing owner')
185 .to.be.rejectedWith(/common\.CollectionIsExternal/);166 .to.be.rejectedWith(/common\.CollectionIsExternal/);
186167
187 const txAddAdmin = api.tx.unique.addCollectionAdmin(uniqueCollectionId, normalizeAccountId(bob));168 await expect(helper.nft.addAdmin(alice, uniqueCollectionId, {Substrate: bob.address}), 'adding admin')
188 await expect(executeTransaction(api, alice, txAddAdmin), 'adding admin')
189 .to.be.rejectedWith(/common\.CollectionIsExternal/);169 .to.be.rejectedWith(/common\.CollectionIsExternal/);
190170
191 const txRemoveAdmin = api.tx.unique.removeCollectionAdmin(uniqueCollectionId, normalizeAccountId(bob));171 await expect(helper.nft.removeAdmin(alice, uniqueCollectionId, {Substrate: bob.address}), 'removing admin')
192 await expect(executeTransaction(api, alice, txRemoveAdmin), 'removing admin')
193 .to.be.rejectedWith(/common\.CollectionIsExternal/);172 .to.be.rejectedWith(/common\.CollectionIsExternal/);
194173
195 const txAddCollectionSponsor = api.tx.unique.setCollectionSponsor(uniqueCollectionId, bob.address);174 await expect(helper.nft.setSponsor(alice, uniqueCollectionId, bob.address), 'setting sponsor')
196 await expect(executeTransaction(api, alice, txAddCollectionSponsor), 'setting sponsor')
197 .to.be.rejectedWith(/common\.CollectionIsExternal/);175 .to.be.rejectedWith(/common\.CollectionIsExternal/);
198176
199 const txConfirmCollectionSponsor = api.tx.unique.confirmSponsorship(uniqueCollectionId);177 await expect(helper.nft.confirmSponsorship(alice, uniqueCollectionId), 'confirming sponsor')
200 await expect(executeTransaction(api, alice, txConfirmCollectionSponsor), 'confirming sponsor')
201 .to.be.rejectedWith(/common\.CollectionIsExternal/);178 .to.be.rejectedWith(/common\.CollectionIsExternal/);
202179
203 const txRemoveCollectionSponsor = api.tx.unique.removeCollectionSponsor(uniqueCollectionId);180 await expect(helper.nft.removeSponsor(alice, uniqueCollectionId), 'removing sponsor')
204 await expect(executeTransaction(api, alice, txRemoveCollectionSponsor), 'removing sponsor')
205 .to.be.rejectedWith(/common\.CollectionIsExternal/);181 .to.be.rejectedWith(/common\.CollectionIsExternal/);
206 182
207 // Limits / permissions / transfers183 // Limits / permissions / transfers
208184
209 const txSetTransfers = api.tx.unique.setTransfersEnabledFlag(uniqueCollectionId, true);185 const txSetTransfers = helper.getApi().tx.unique.setTransfersEnabledFlag(uniqueCollectionId, true);
210 await expect(executeTransaction(api, alice, txSetTransfers), 'setting transfers enabled flag')186 await expect(executeTransaction(helper.getApi(), alice, txSetTransfers), 'setting transfers enabled flag')
211 .to.be.rejectedWith(/common\.CollectionIsExternal/);187 .to.be.rejectedWith(/common\.CollectionIsExternal/);
212188
213 const txSetLimits = api.tx.unique.setCollectionLimits(uniqueCollectionId, {transfersEnabled: false});189 await expect(helper.nft.setLimits(alice, uniqueCollectionId, {transfersEnabled: false}), 'setting collection limits')
214 await expect(executeTransaction(api, alice, txSetLimits), 'setting collection limits')
215 .to.be.rejectedWith(/common\.CollectionIsExternal/);190 .to.be.rejectedWith(/common\.CollectionIsExternal/);
216191
217 const txSetPermissions = api.tx.unique.setCollectionPermissions(uniqueCollectionId, {access: 'AllowList'});192 await expect(helper.nft.setPermissions(alice, uniqueCollectionId, {access: 'AllowList'}), 'setting collection permissions')
218 await expect(executeTransaction(api, alice, txSetPermissions), 'setting collection permissions')
219 .to.be.rejectedWith(/common\.CollectionIsExternal/);193 .to.be.rejectedWith(/common\.CollectionIsExternal/);
220 });
221 });194 });
222});195});
223196
224describe('Negative Integration Test: Internal Collections, External Ops', async () => {197describe('Negative Integration Test: Internal Collections, External Ops', async () => {
225 let collectionId: number;198 let collectionId: number;
226 let nftId: number;199 let nftId: number;
227200
228 const it_rmrk = (await isUnique() ? it : it.skip);
229
230 before(async () => {201 before(async () => {
231 await usingApi(async (api, privateKeyWrapper) => {202 await usingPlaygrounds(async (helper, privateKey) => {
232 alice = privateKeyWrapper('//Alice');203 alice = privateKey('//Alice');
233 bob = privateKeyWrapper('//Bob');204 bob = privateKey('//Bob');
234205
235 collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});206 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'iceo'});
236 nftId = await createItemExpectSuccess(alice, collectionId, 'NFT');207 collectionId = collection.collectionId;
208 nftId = (await collection.mintToken(alice)).tokenId;
237 });209 });
238 });210 });
239211
240 it_rmrk('[Negative] Forbids RMRK operations with the internal collection and NFT (due to the lack of mapping)', async () => {212 itSub.ifWithPallets('[Negative] Forbids RMRK operations with the internal collection and NFT (due to the lack of mapping)', [Pallets.RmrkCore], async ({helper}) => {
241 await usingApi(async api => {213 const api = helper.getApi();
242 const txChangeOwner = api.tx.rmrkCore.changeCollectionIssuer(collectionId, bob.address);
243 await expect(executeTransaction(api, alice, txChangeOwner), 'changing collection issuer')
244 .to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);
245214
246 const maxBurns = 10;215 const txChangeOwner = api.tx.rmrkCore.changeCollectionIssuer(collectionId, bob.address);
216 await expect(executeTransaction(api, alice, txChangeOwner), 'changing collection issuer')
217 .to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);
218
219 const maxBurns = 10;
247 const txBurnItem = api.tx.rmrkCore.burnNft(collectionId, nftId, maxBurns);220 const txBurnItem = api.tx.rmrkCore.burnNft(collectionId, nftId, maxBurns);
248 await expect(executeTransaction(api, alice, txBurnItem), 'burning NFT').to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);221 await expect(executeTransaction(api, alice, txBurnItem), 'burning NFT').to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);
249 });
250 });222 });
251});223});
252224