git.delta.rocks / unique-network / refs/commits / 5ec07294ed3b

difftreelog

source

tests/src/createMultipleItemsEx.test.ts15.8 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 usingApi, {executeTransaction} from './substrate/substrate-api';19import {addCollectionAdminExpectSuccess, createCollectionExpectSuccess, createCollectionWithPropsExpectSuccess, getBalance, getLastTokenId} from './util/helpers';2021describe('Integration Test: createMultipleItemsEx', () => {22  it('can initialize multiple NFT with different owners', async () => {23    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});24    await usingApi(async (api, privateKeyWrapper) => {25      const alice = privateKeyWrapper('//Alice');26      const bob = privateKeyWrapper('//Bob');27      const charlie = privateKeyWrapper('//Charlie');28      const data = [29        {30          owner: {substrate: alice.address},31        }, {32          owner: {substrate: bob.address},33        }, {34          owner: {substrate: charlie.address},35        },36      ];3738      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {39        NFT: data,40      }));41      const tokens = await api.query.nonfungible.tokenData.entries(collection);42      const json = tokens.map(([, token]) => token.toJSON());43      expect(json).to.be.deep.equal(data);44    });45  });4647  it('createMultipleItemsEx with property Admin', async () => {48    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: false}}]});49    await usingApi(async (api, privateKeyWrapper) => {50      const alice = privateKeyWrapper('//Alice');51      const bob = privateKeyWrapper('//Bob');52      const charlie = privateKeyWrapper('//Charlie');53      const data = [54        {55          owner: {substrate: alice.address},56          properties: [{key: 'k', value: 'v1'}],57        }, {58          owner: {substrate: bob.address},59          properties: [{key: 'k', value: 'v2'}],60        }, {61          owner: {substrate: charlie.address},62          properties: [{key: 'k', value: 'v3'}],63        },64      ];6566      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {67        NFT: data,68      }));69      for (let i = 1; i < 4; i++) {70        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;71      }72    });73  });7475  it('createMultipleItemsEx with property AdminConst', async () => {76    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: false, collectionAdmin: true, tokenOwner: false}}]});77    await usingApi(async (api, privateKeyWrapper) => {78      const alice = privateKeyWrapper('//Alice');79      const bob = privateKeyWrapper('//Bob');80      const charlie = privateKeyWrapper('//Charlie');81      const data = [82        {83          owner: {substrate: alice.address},84          properties: [{key: 'k', value: 'v1'}],85        }, {86          owner: {substrate: bob.address},87          properties: [{key: 'k', value: 'v2'}],88        }, {89          owner: {substrate: charlie.address},90          properties: [{key: 'k', value: 'v3'}],91        },92      ];9394      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {95        NFT: data,96      }));97      for (let i = 1; i < 4; i++) {98        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;99      }100    });101  });102103  it('createMultipleItemsEx with property itemOwnerOrAdmin', async () => {104    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: false, collectionAdmin: true, tokenOwner: true}}]});105    await usingApi(async (api, privateKeyWrapper) => {106      const alice = privateKeyWrapper('//Alice');107      const bob = privateKeyWrapper('//Bob');108      const charlie = privateKeyWrapper('//Charlie');109      const data = [110        {111          owner: {substrate: alice.address},112          properties: [{key: 'k', value: 'v1'}],113        }, {114          owner: {substrate: bob.address},115          properties: [{key: 'k', value: 'v2'}],116        }, {117          owner: {substrate: charlie.address},118          properties: [{key: 'k', value: 'v3'}],119        },120      ];121122      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {123        NFT: data,124      }));125      for (let i = 1; i < 4; i++) {126        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;127      }128    });129  });130131  it('can initialize fungible with multiple owners', async () => {132    const collection = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});133    await usingApi(async (api, privateKeyWrapper) => {134      const alice = privateKeyWrapper('//Alice');135      const bob = privateKeyWrapper('//Bob');136137      const users = new Map();138      users.set(JSON.stringify({Substrate: alice.address}), 50);139      users.set(JSON.stringify({Substrate: bob.address}), 100);140141      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {142        Fungible: users,143      }));144145      expect(await getBalance(api, collection, alice.address, 0)).to.equal(50n);146      expect(await getBalance(api, collection, bob.address, 0)).to.equal(100n);147    });148  });149150  it('can initialize an RFT with multiple owners', async () => {151    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});152    await usingApi(async (api, privateKeyWrapper) => {153      const alice = privateKeyWrapper('//Alice');154      const bob = privateKeyWrapper('//Bob');155156      const users = new Map();157      users.set(JSON.stringify({Substrate: alice.address}), 1);158      users.set(JSON.stringify({Substrate: bob.address}), 2);159160      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {161        RefungibleMultipleOwners: {162          users: users,163        },164      }));165      166      const itemsListIndexAfter = await getLastTokenId(api, collection);167      expect(itemsListIndexAfter).to.be.equal(1);168169      expect(await getBalance(api, collection, alice.address, 1)).to.be.equal(1n);170      expect(await getBalance(api, collection, bob.address, 1)).to.be.equal(2n);171    });172  });173174  it('can initialize multiple RFTs with the same owner', async () => {175    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});176    await usingApi(async (api, privateKeyWrapper) => {177      const alice = privateKeyWrapper('//Alice');178179      const item1User = new Map();180      item1User.set(JSON.stringify({Substrate: alice.address}), 1);181182      const item2User = new Map();183      item2User.set(JSON.stringify({Substrate: alice.address}), 3);184185      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {186        RefungibleMultipleItems: [187          {users: item1User},188          {users: item2User},189        ],190      }));191      192      const itemsListIndexAfter = await getLastTokenId(api, collection);193      expect(itemsListIndexAfter).to.be.equal(2);194195      expect(await getBalance(api, collection, alice.address, 1)).to.be.equal(1n);196      expect(await getBalance(api, collection, alice.address, 2)).to.be.equal(3n);197    });198  });199});200201describe('Negative test: createMultipleItemsEx', () => {202  it('No editing rights', async () => {203    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],204      propPerm:   [{key: 'key1', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}]});205    await usingApi(async (api, privateKeyWrapper) => {206      const alice = privateKeyWrapper('//Alice');207      const bob = privateKeyWrapper('//Bob');208      const charlie = privateKeyWrapper('//Charlie');209      await addCollectionAdminExpectSuccess(alice, collection, bob.address);210      const data = [211        {212          owner: {substrate: alice.address},213          properties: [{key: 'key1', value: 'v2'}],214        }, {215          owner: {substrate: bob.address},216          properties: [{key: 'key1', value: 'v2'}],217        }, {218          owner: {substrate: charlie.address},219          properties: [{key: 'key1', value: 'v2'}],220        },221      ];222223      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});224      // await executeTransaction(api, alice, tx);225226      //await submitTransactionExpectFailAsync(alice, tx);227      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);228    });229  });230231  it('User doesnt have editing rights', async () => {232    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],233      propPerm:   [{key: 'key1', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}]});234    await usingApi(async (api, privateKeyWrapper) => {235      const alice = privateKeyWrapper('//Alice');236      const bob = privateKeyWrapper('//Bob');237      await addCollectionAdminExpectSuccess(alice, collection, bob.address);238      const data = [239        {240          owner: {substrate: alice.address},241          properties: [{key: 'key1', value: 'v2'}],242        }, {243          owner: {substrate: alice.address},244          properties: [{key: 'key1', value: 'v2'}],245        }, {246          owner: {substrate: alice.address},247          properties: [{key: 'key1', value: 'v2'}],248        },249      ];250251      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});252      // await executeTransaction(api, alice, tx);253254      //await submitTransactionExpectFailAsync(alice, tx);255      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);256    });257  });258259  it('Adding property without access rights', async () => {260    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}]});261    await usingApi(async (api, privateKeyWrapper) => {262      const alice = privateKeyWrapper('//Alice');263      const bob = privateKeyWrapper('//Bob');264      const charlie = privateKeyWrapper('//Charlie');265      await addCollectionAdminExpectSuccess(alice, collection, bob.address);266      const data = [267        {268          owner: {substrate: alice.address},269          properties: [{key: 'key1', value: 'v2'}],270        }, {271          owner: {substrate: bob.address},272          properties: [{key: 'key1', value: 'v2'}],273        }, {274          owner: {substrate: charlie.address},275          properties: [{key: 'key1', value: 'v2'}],276        },277      ];278279      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});280281      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);282      //await submitTransactionExpectFailAsync(alice, tx);283    });284  });285286  it('Adding more than 64 properties', async () => {287    const propPerms = [{key: 'key', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}];288289    for (let i = 0; i < 65; i++) {290      propPerms.push({key: `key${i}`, permission: {mutable: true, collectionAdmin: true, tokenOwner: true}});291    }292293    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});294    await usingApi(async (api, privateKeyWrapper) => {295      const alice = privateKeyWrapper('//Alice');296      await expect(executeTransaction(api, alice, api.tx.unique.setTokenPropertyPermissions(collection, propPerms))).to.be.rejectedWith(/common\.PropertyLimitReached/);297    });298  });299300  it('Trying to add bigger property than allowed', async () => {301    const collection = await createCollectionWithPropsExpectSuccess({propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]});302    await usingApi(async (api, privateKeyWrapper) => {303      const alice = privateKeyWrapper('//Alice');304      const bob = privateKeyWrapper('//Bob');305      const charlie = privateKeyWrapper('//Charlie');306      await addCollectionAdminExpectSuccess(alice, collection, bob.address);307      const data = [308        {309          owner: {substrate: alice.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],310        }, {311          owner: {substrate: bob.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],312        }, {313          owner: {substrate: charlie.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],314        },315      ];316317      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});318319      //await submitTransactionExpectFailAsync(alice, tx);320      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);321    });322  });323324  it('can initialize multiple NFT with different owners', async () => {325    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});326    await usingApi(async (api, privateKeyWrapper) => {327      const alice = privateKeyWrapper('//Alice');328      const bob = privateKeyWrapper('//Bob');329      const charlie = privateKeyWrapper('//Charlie');330      const data = [331        {332          owner: {substrate: alice.address},333        }, {334          owner: {substrate: bob.address},335        }, {336          owner: {substrate: charlie.address},337        },338      ];339340      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {341        NFT: data,342      }));343      const tokens = await api.query.nonfungible.tokenData.entries(collection);344      const json = tokens.map(([, token]) => token.toJSON());345      expect(json).to.be.deep.equal(data);346    });347  });348349  it('can initialize multiple NFT with different owners', async () => {350    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});351    await usingApi(async (api, privateKeyWrapper) => {352      const alice = privateKeyWrapper('//Alice');353      const bob = privateKeyWrapper('//Bob');354      const charlie = privateKeyWrapper('//Charlie');355      const data = [356        {357          owner: {substrate: alice.address},358        }, {359          owner: {substrate: bob.address},360        }, {361          owner: {substrate: charlie.address},362        },363      ];364365      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {366        NFT: data,367      }));368      const tokens = await api.query.nonfungible.tokenData.entries(collection);369      const json = tokens.map(([, token]) => token.toJSON());370      expect(json).to.be.deep.equal(data);371    });372  });373374  it('fails when trying to set multiple owners when creating multiple refungibles', async () => {375    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});376    377    await usingApi(async (api, privateKeyWrapper) => {378      const alice = privateKeyWrapper('//Alice');379      const bob = privateKeyWrapper('//Bob');380      // Polkadot requires map, and yet requires keys to be JSON encoded381      const users = new Map();382      users.set(JSON.stringify({substrate: alice.address}), 1);383      users.set(JSON.stringify({substrate: bob.address}), 1);384385      // TODO: better error message?386      await expect(executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {387        RefungibleMultipleItems: [388          {users},389          {users},390        ],391      }))).to.be.rejectedWith(/^refungible\.NotRefungibleDataUsedToMintFungibleCollectionToken$/);392    });393  });394});