git.delta.rocks / unique-network / refs/commits / 547ee7a8f073

difftreelog

source

tests/src/createMultipleItemsEx.test.ts14.9 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect} from 'chai';18import privateKey from './substrate/privateKey';19import usingApi, {executeTransaction, submitTransactionAsync} from './substrate/substrate-api';20import {createCollectionExpectSuccess, createCollectionWithPropsExpectSuccess, addCollectionAdminExpectSuccess, getCreateItemsResult} from './util/helpers';2122describe('createMultipleItemsEx', () => {23  it('can initialize multiple NFT with different owners', async () => {24    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});25    const alice = privateKey('//Alice');26    const bob = privateKey('//Bob');27    const charlie = privateKey('//Charlie');28    await usingApi(async (api) => {29      const data = [30        {31          owner: {substrate: alice.address},32          constData: '0x0000',33          variableData: '0x1111',34        }, {35          owner: {substrate: bob.address},36          constData: '0x2222',37          variableData: '0x3333',38        }, {39          owner: {substrate: charlie.address},40          constData: '0x4444',41          variableData: '0x5555',42        },43      ];4445      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {46        NFT: data,47      }));48      const tokens = await api.query.nonfungible.tokenData.entries(collection);49      const json = tokens.map(([, token]) => token.toJSON());50      expect(json).to.be.deep.equal(data);51    });52  });5354  it('createMultipleItemsEx with property Admin', async () => {55    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});56    const alice = privateKey('//Alice');57    const bob = privateKey('//Bob');58    const charlie = privateKey('//Charlie');59    await usingApi(async (api) => {60      const data = [61        {62          owner: {substrate: alice.address},63          constData: '0x0000',64          variableData: '0x1111',65        }, {66          owner: {substrate: bob.address},67          constData: '0x2222',68          variableData: '0x3333',69        }, {70          owner: {substrate: charlie.address},71          constData: '0x4444',72          variableData: '0x5555',73        },74      ];7576      await expect(executeTransaction(77        api, 78        alice, 79        api.tx.unique.setPropertyPermissions(collection, [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: false}}]), 80      )).to.not.be.rejected;8182      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {83        NFT: data,84      }));85      const tokens = await api.query.nonfungible.tokenData.entries(collection);86      const json = tokens.map(([, token]) => token.toJSON());87      expect(json).to.be.deep.equal(data);88    });89  });9091  it('createMultipleItemsEx with property AdminConst', async () => {92    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});93    const alice = privateKey('//Alice');94    const bob = privateKey('//Bob');95    const charlie = privateKey('//Charlie');96    await usingApi(async (api) => {97      const data = [98        {99          owner: {substrate: alice.address},100          constData: '0x0000',101          variableData: '0x1111',102        }, {103          owner: {substrate: bob.address},104          constData: '0x2222',105          variableData: '0x3333',106        }, {107          owner: {substrate: charlie.address},108          constData: '0x4444',109          variableData: '0x5555',110        },111      ];112      await expect(executeTransaction(113        api, 114        alice, 115        api.tx.unique.setPropertyPermissions(collection, [{key: 'k', permission: {mutable: false, collectionAdmin: true, tokenOwner: false}}]), 116      )).to.not.be.rejected;117118      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {119        NFT: data,120      }));121      122123      const tokens = await api.query.nonfungible.tokenData.entries(collection);124      const json = tokens.map(([, token]) => token.toJSON());125      expect(json).to.be.deep.equal(data);126    });127  });128129  it('createMultipleItemsEx with property itemOwnerOrAdmin', async () => {130    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});131    const alice = privateKey('//Alice');132    const bob = privateKey('//Bob');133    const charlie = privateKey('//Charlie');134    await usingApi(async (api) => {135      const data = [136        {137          owner: {substrate: alice.address},138          constData: '0x0000',139          variableData: '0x1111',140        }, {141          owner: {substrate: bob.address},142          constData: '0x2222',143          variableData: '0x3333',144        }, {145          owner: {substrate: charlie.address},146          constData: '0x4444',147          variableData: '0x5555',148        },149      ];150      await expect(executeTransaction(151        api, 152        alice, 153        api.tx.unique.setPropertyPermissions(collection, [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]), 154      )).to.not.be.rejected;155156      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {157        NFT: data,158      }));159      160161      const tokens = await api.query.nonfungible.tokenData.entries(collection);162      const json = tokens.map(([, token]) => token.toJSON());163      expect(json).to.be.deep.equal(data);164    });165  });166167  it('No editing rights', async () => {168    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],169      propPerm:   [{key: 'key1', mutable: true, collectionAdmin: false, tokenOwner: false}]});170    const alice = privateKey('//Alice');171    const bob = privateKey('//Bob');172    const charlie = privateKey('//Charlie');173    await addCollectionAdminExpectSuccess(alice, collection, bob.address);174    await usingApi(async (api) => {175      const data = [176        {177          owner: {substrate: alice.address},178        }, {179          owner: {substrate: bob.address},180        }, {181          owner: {substrate: charlie.address},182        },183      ];184185      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});186      await executeTransaction(api, alice, tx);187      188      const events = await submitTransactionAsync(alice, tx);189      const result = getCreateItemsResult(events);190      191      for (const elem of result) {192        await expect(executeTransaction(193          api, 194          bob, 195          api.tx.unique.setTokenProperties(elem.collectionId, elem.itemId, [{key: 'key1', value: 'v2'}]), 196        )).to.be.rejected;197      }198    });199  });200201  it('User doesnt have editing rights', async () => {202    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],203      propPerm:   [{key: 'key1', mutable: false, collectionAdmin: false, tokenOwner: false}]});204    const alice = privateKey('//Alice');205    const bob = privateKey('//Bob');206    const charlie = privateKey('//Charlie');207    await addCollectionAdminExpectSuccess(alice, collection, bob.address);208    await usingApi(async (api) => {209      const data = [210        {211          owner: {substrate: alice.address},212        }, {213          owner: {substrate: bob.address},214        }, {215          owner: {substrate: charlie.address},216        },217      ];218219      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});220      await executeTransaction(api, alice, tx);221      222      const events = await submitTransactionAsync(alice, tx);223      const result = getCreateItemsResult(events);224      225      for (const elem of result) {226        await expect(executeTransaction(227          api, 228          bob, 229          api.tx.unique.setTokenProperties(elem.collectionId, elem.itemId, [{key: 'key1', value: 'v2'}]), 230        )).to.be.rejected;231      }232    });233  });234235  it('Adding property without access rights', async () => {236    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}]});237    const alice = privateKey('//Alice');238    const bob = privateKey('//Bob');239    const charlie = privateKey('//Charlie');240    await addCollectionAdminExpectSuccess(alice, collection, bob.address);241    await usingApi(async (api) => {242      const data = [243        {244          owner: {substrate: alice.address},245        }, {246          owner: {substrate: bob.address},247        }, {248          owner: {substrate: charlie.address},249        },250      ];251252      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});253      await executeTransaction(api, alice, tx);254      255      const events = await submitTransactionAsync(alice, tx);256      const result = getCreateItemsResult(events);257      258      for (const elem of result) {259        await expect(executeTransaction(260          api, 261          bob, 262          api.tx.unique.setTokenProperties(elem.collectionId, elem.itemId, [{key: 'key1', value: 'v2'}]), 263        )).to.be.rejected;264      }265    });266  });267268  it('Adding more than 64 prps', async () => {269    const collection = await createCollectionWithPropsExpectSuccess();270    const alice = privateKey('//Alice');271    const bob = privateKey('//Bob');272    const charlie = privateKey('//Charlie');273    await addCollectionAdminExpectSuccess(alice, collection, bob.address);274    await usingApi(async (api) => {275      const data = [276        {277          owner: {substrate: alice.address},278        }, {279          owner: {substrate: bob.address},280        }, {281          owner: {substrate: charlie.address},282        },283      ];284285      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});286      await executeTransaction(api, alice, tx);287      288      const events = await submitTransactionAsync(alice, tx);289      const result = getCreateItemsResult(events);290291      const prps = [];292      293      for (let i = 0; i < 65; i++) {294        prps.push({key: `key${i}`, value: `value${i}`});295      }296297      await expect(executeTransaction(api, bob, api.tx.unique.setCollectionProperties(collection, prps))).to.be.rejectedWith(/common\.PropertyLimitReached/);298299      300      for (const elem of result) {301        await expect(executeTransaction(302          api, 303          bob, 304          api.tx.unique.setTokenProperties(elem.collectionId, elem.itemId, prps), 305        )).to.be.rejected;306      }307    });308  });309310  it('Trying to add bigger property than allowed', async () => {311    const collection = await createCollectionWithPropsExpectSuccess();312    const alice = privateKey('//Alice');313    const bob = privateKey('//Bob');314    const charlie = privateKey('//Charlie');315    await addCollectionAdminExpectSuccess(alice, collection, bob.address);316    await usingApi(async (api) => {317      const data = [318        {319          owner: {substrate: alice.address},320        }, {321          owner: {substrate: bob.address},322        }, {323          owner: {substrate: charlie.address},324        },325      ];326327      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});328      await executeTransaction(api, alice, tx);329      330      const events = await submitTransactionAsync(alice, tx);331      const result = getCreateItemsResult(events);332333      const prps = [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}];334335      await expect(executeTransaction(api, bob, api.tx.unique.setCollectionProperties(collection, prps))).to.be.rejectedWith(/common\.NoSpaceForProperty/);336337      338      for (const elem of result) {339        await expect(executeTransaction(340          api, 341          bob, 342          api.tx.unique.setTokenProperties(elem.collectionId, elem.itemId, prps), 343        )).to.be.rejected;344      }345    });346  });347348  it('can initialize multiple NFT with different owners', async () => {349    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});350    const alice = privateKey('//Alice');351    const bob = privateKey('//Bob');352    const charlie = privateKey('//Charlie');353    await usingApi(async (api) => {354      const data = [355        {356          owner: {substrate: alice.address},357          constData: '0x0000',358          variableData: '0x1111',359        }, {360          owner: {substrate: bob.address},361          constData: '0x2222',362          variableData: '0x3333',363        }, {364          owner: {substrate: charlie.address},365          constData: '0x4444',366          variableData: '0x5555',367        },368      ];369370      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {371        NFT: data,372      }));373      const tokens = await api.query.nonfungible.tokenData.entries(collection);374      const json = tokens.map(([, token]) => token.toJSON());375      expect(json).to.be.deep.equal(data);376    });377  });378379  it('can initialize multiple NFT with different owners', async () => {380    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});381    const alice = privateKey('//Alice');382    const bob = privateKey('//Bob');383    const charlie = privateKey('//Charlie');384    await usingApi(async (api) => {385      const data = [386        {387          owner: {substrate: alice.address},388          constData: '0x0000',389          variableData: '0x1111',390        }, {391          owner: {substrate: bob.address},392          constData: '0x2222',393          variableData: '0x3333',394        }, {395          owner: {substrate: charlie.address},396          constData: '0x4444',397          variableData: '0x5555',398        },399      ];400401      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {402        NFT: data,403      }));404      const tokens = await api.query.nonfungible.tokenData.entries(collection);405      const json = tokens.map(([, token]) => token.toJSON());406      expect(json).to.be.deep.equal(data);407    });408  });409410  it('fails when trying to set multiple owners when creating multiple refungibles', async () => {411    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});412    const alice = privateKey('//Alice');413    const bob = privateKey('//Bob');414415    await usingApi(async (api) => {416      // Polkadot requires map, and yet requires keys to be JSON encoded417      const users = new Map();418      users.set(JSON.stringify({substrate: alice.address}), 1);419      users.set(JSON.stringify({substrate: bob.address}), 1);420421      // TODO: better error message?422      await expect(executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {423        RefungibleMultipleItems: [424          {users},425          {users},426        ],427      }))).to.be.rejectedWith(/^refungible\.NotRefungibleDataUsedToMintFungibleCollectionToken$/);428    });429  });430});431'';