git.delta.rocks / unique-network / refs/commits / e256f7e1e4e2

difftreelog

source

tests/src/createMultipleItemsEx.test.ts16.6 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, getTokenProperties} 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      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {138        Fungible: new Map([139          [JSON.stringify({Substrate: alice.address}), 50],140          [JSON.stringify({Substrate: bob.address}), 100],141        ]),142      }));143144      expect(await getBalance(api, collection, alice.address, 0)).to.equal(50n);145      expect(await getBalance(api, collection, bob.address, 0)).to.equal(100n);146    });147  });148149  it('can initialize an RFT with multiple owners', async () => {150    await usingApi(async (api, privateKeyWrapper) => {151      const alice = privateKeyWrapper('//Alice');152      const bob = privateKeyWrapper('//Bob');153      const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});154      await executeTransaction(155        api,156        alice,157        api.tx.unique.setTokenPropertyPermissions(collection, [{key: 'data', permission: {tokenOwner: true}}]),158      );159160      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {161        RefungibleMultipleOwners: {162          users: new Map([163            [JSON.stringify({Substrate: alice.address}), 1],164            [JSON.stringify({Substrate: bob.address}), 2],165          ]),166          properties: [167            {key: 'data', value: 'testValue'},168          ],169        },170      }));171172      const itemsListIndexAfter = await getLastTokenId(api, collection);173      expect(itemsListIndexAfter).to.be.equal(1);174175      expect(await getBalance(api, collection, alice.address, 1)).to.be.equal(1n);176      expect(await getBalance(api, collection, bob.address, 1)).to.be.equal(2n);177      expect((await getTokenProperties(api, collection, 1, ['data']))[0].value).to.be.equal('testValue');178    });179  });180181  it('can initialize multiple RFTs with the same owner', async () => {182    await usingApi(async (api, privateKeyWrapper) => {183      const alice = privateKeyWrapper('//Alice');184      const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});185      await executeTransaction(186        api,187        alice,188        api.tx.unique.setTokenPropertyPermissions(collection, [{key: 'data', permission: {tokenOwner: true}}]),189      );190191      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {192        RefungibleMultipleItems: [193          {194            user: {Substrate: alice.address}, pieces: 1,195            properties: [196              {key: 'data', value: 'testValue1'},197            ],198          },199          {200            user: {Substrate: alice.address}, pieces: 3,201            properties: [202              {key: 'data', value: 'testValue2'},203            ],204          },205        ],206      }));207208      const itemsListIndexAfter = await getLastTokenId(api, collection);209      expect(itemsListIndexAfter).to.be.equal(2);210211      expect(await getBalance(api, collection, alice.address, 1)).to.be.equal(1n);212      expect(await getBalance(api, collection, alice.address, 2)).to.be.equal(3n);213      expect((await getTokenProperties(api, collection, 1, ['data']))[0].value).to.be.equal('testValue1');214      expect((await getTokenProperties(api, collection, 2, ['data']))[0].value).to.be.equal('testValue2');215    });216  });217});218219describe('Negative test: createMultipleItemsEx', () => {220  it('No editing rights', async () => {221    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],222      propPerm:   [{key: 'key1', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}]});223    await usingApi(async (api, privateKeyWrapper) => {224      const alice = privateKeyWrapper('//Alice');225      const bob = privateKeyWrapper('//Bob');226      const charlie = privateKeyWrapper('//Charlie');227      await addCollectionAdminExpectSuccess(alice, collection, bob.address);228      const data = [229        {230          owner: {substrate: alice.address},231          properties: [{key: 'key1', value: 'v2'}],232        }, {233          owner: {substrate: bob.address},234          properties: [{key: 'key1', value: 'v2'}],235        }, {236          owner: {substrate: charlie.address},237          properties: [{key: 'key1', value: 'v2'}],238        },239      ];240241      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});242      // await executeTransaction(api, alice, tx);243244      //await submitTransactionExpectFailAsync(alice, tx);245      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);246    });247  });248249  it('User doesnt have editing rights', async () => {250    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],251      propPerm:   [{key: 'key1', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}]});252    await usingApi(async (api, privateKeyWrapper) => {253      const alice = privateKeyWrapper('//Alice');254      const bob = privateKeyWrapper('//Bob');255      await addCollectionAdminExpectSuccess(alice, collection, bob.address);256      const data = [257        {258          owner: {substrate: alice.address},259          properties: [{key: 'key1', value: 'v2'}],260        }, {261          owner: {substrate: alice.address},262          properties: [{key: 'key1', value: 'v2'}],263        }, {264          owner: {substrate: alice.address},265          properties: [{key: 'key1', value: 'v2'}],266        },267      ];268269      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});270      // await executeTransaction(api, alice, tx);271272      //await submitTransactionExpectFailAsync(alice, tx);273      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);274    });275  });276277  it('Adding property without access rights', async () => {278    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}]});279    await usingApi(async (api, privateKeyWrapper) => {280      const alice = privateKeyWrapper('//Alice');281      const bob = privateKeyWrapper('//Bob');282      const charlie = privateKeyWrapper('//Charlie');283      await addCollectionAdminExpectSuccess(alice, collection, bob.address);284      const data = [285        {286          owner: {substrate: alice.address},287          properties: [{key: 'key1', value: 'v2'}],288        }, {289          owner: {substrate: bob.address},290          properties: [{key: 'key1', value: 'v2'}],291        }, {292          owner: {substrate: charlie.address},293          properties: [{key: 'key1', value: 'v2'}],294        },295      ];296297      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});298299      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);300      //await submitTransactionExpectFailAsync(alice, tx);301    });302  });303304  it('Adding more than 64 properties', async () => {305    const propPerms = [{key: 'key', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}];306307    for (let i = 0; i < 65; i++) {308      propPerms.push({key: `key${i}`, permission: {mutable: true, collectionAdmin: true, tokenOwner: true}});309    }310311    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});312    await usingApi(async (api, privateKeyWrapper) => {313      const alice = privateKeyWrapper('//Alice');314      await expect(executeTransaction(api, alice, api.tx.unique.setTokenPropertyPermissions(collection, propPerms))).to.be.rejectedWith(/common\.PropertyLimitReached/);315    });316  });317318  it('Trying to add bigger property than allowed', async () => {319    const collection = await createCollectionWithPropsExpectSuccess({propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]});320    await usingApi(async (api, privateKeyWrapper) => {321      const alice = privateKeyWrapper('//Alice');322      const bob = privateKeyWrapper('//Bob');323      const charlie = privateKeyWrapper('//Charlie');324      await addCollectionAdminExpectSuccess(alice, collection, bob.address);325      const data = [326        {327          owner: {substrate: alice.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],328        }, {329          owner: {substrate: bob.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],330        }, {331          owner: {substrate: charlie.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],332        },333      ];334335      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});336337      //await submitTransactionExpectFailAsync(alice, tx);338      await expect(executeTransaction(api, alice, tx)).to.be.rejectedWith(/common\.NoPermission/);339    });340  });341342  it('can initialize multiple NFT with different owners', async () => {343    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});344    await usingApi(async (api, privateKeyWrapper) => {345      const alice = privateKeyWrapper('//Alice');346      const bob = privateKeyWrapper('//Bob');347      const charlie = privateKeyWrapper('//Charlie');348      const data = [349        {350          owner: {substrate: alice.address},351        }, {352          owner: {substrate: bob.address},353        }, {354          owner: {substrate: charlie.address},355        },356      ];357358      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {359        NFT: data,360      }));361      const tokens = await api.query.nonfungible.tokenData.entries(collection);362      const json = tokens.map(([, token]) => token.toJSON());363      expect(json).to.be.deep.equal(data);364    });365  });366367  it('can initialize multiple NFT with different owners', async () => {368    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});369    await usingApi(async (api, privateKeyWrapper) => {370      const alice = privateKeyWrapper('//Alice');371      const bob = privateKeyWrapper('//Bob');372      const charlie = privateKeyWrapper('//Charlie');373      const data = [374        {375          owner: {substrate: alice.address},376        }, {377          owner: {substrate: bob.address},378        }, {379          owner: {substrate: charlie.address},380        },381      ];382383      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {384        NFT: data,385      }));386      const tokens = await api.query.nonfungible.tokenData.entries(collection);387      const json = tokens.map(([, token]) => token.toJSON());388      expect(json).to.be.deep.equal(data);389    });390  });391392  it('fails when trying to set multiple owners when creating multiple refungibles', async () => {393    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});394395    await usingApi(async (api, privateKeyWrapper) => {396      const alice = privateKeyWrapper('//Alice');397      const bob = privateKeyWrapper('//Bob');398      // Polkadot requires map, and yet requires keys to be JSON encoded399      const users = new Map();400      users.set(JSON.stringify({substrate: alice.address}), 1);401      users.set(JSON.stringify({substrate: bob.address}), 1);402403      // TODO: better error message?404      await expect(executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {405        RefungibleMultipleItems: [406          {users},407          {users},408        ],409      }))).to.be.rejectedWith(/^refungible\.NotRefungibleDataUsedToMintFungibleCollectionToken$/);410    });411  });412});