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

difftreelog

source

tests/src/createMultipleItemsEx.test.ts13.3 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, submitTransactionExpectFailAsync} 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        }, {34          owner: {substrate: bob.address},35          constData: '0x2222',36        }, {37          owner: {substrate: charlie.address},38          constData: '0x4444',39        },40      ];4142      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {43        NFT: data,44      }));45      const tokens = await api.query.nonfungible.tokenData.entries(collection);46      const json = tokens.map(([, token]) => token.toJSON());47      expect(json).to.be.deep.equal(data);48    });49  });5051  it('createMultipleItemsEx with property Admin', async () => {52    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: false}}]});53    const alice = privateKey('//Alice');54    const bob = privateKey('//Bob');55    const charlie = privateKey('//Charlie');56    await usingApi(async (api) => {57      const data = [58        {59          owner: {substrate: alice.address},60          constData: '0x1111',61          properties: [{key: 'k', value: 'v1'}],62        }, {63          owner: {substrate: bob.address},64          constData: '0x2222',65          properties: [{key: 'k', value: 'v2'}],66        }, {67          owner: {substrate: charlie.address},68          constData: '0x4444',69          properties: [{key: 'k', value: 'v3'}],70        },71      ];7273      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {74        NFT: data,75      }));76      for (let i = 1; i < 4; i++) {77        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;78      }79    });80  });8182  it('createMultipleItemsEx with property AdminConst', async () => {83    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: false, collectionAdmin: true, tokenOwner: false}}]});84    const alice = privateKey('//Alice');85    const bob = privateKey('//Bob');86    const charlie = privateKey('//Charlie');87    await usingApi(async (api) => {88      const data = [89        {90          owner: {substrate: alice.address},91          constData: '0x0000',92          properties: [{key: 'k', value: 'v1'}],93        }, {94          owner: {substrate: bob.address},95          constData: '0x2222',96          properties: [{key: 'k', value: 'v2'}],97        }, {98          owner: {substrate: charlie.address},99          constData: '0x4444',100          properties: [{key: 'k', value: 'v3'}],101        },102      ];103104      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {105        NFT: data,106      }));107      for (let i = 1; i < 4; i++) {108        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;109      }110    });111  });112113  it('createMultipleItemsEx with property itemOwnerOrAdmin', async () => {114    const collection = await createCollectionWithPropsExpectSuccess({mode: {type: 'NFT'}, propPerm: [{key: 'k', permission: {mutable: false, collectionAdmin: true, tokenOwner: true}}]});115    const alice = privateKey('//Alice');116    const bob = privateKey('//Bob');117    const charlie = privateKey('//Charlie');118    await usingApi(async (api) => {119      const data = [120        {121          owner: {substrate: alice.address},122          constData: '0x0000',123          properties: [{key: 'k', value: 'v1'}],124        }, {125          owner: {substrate: bob.address},126          constData: '0x2222',127          properties: [{key: 'k', value: 'v2'}],128        }, {129          owner: {substrate: charlie.address},130          constData: '0x4444',131          properties: [{key: 'k', value: 'v3'}],132        },133      ];134135      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {136        NFT: data,137      }));138      for (let i = 1; i < 4; i++) {139        expect(await api.rpc.unique.tokenProperties(collection, i)).not.to.be.empty;140      }141    });142  });143144  it('No editing rights', async () => {145    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],146      propPerm:   [{key: 'key1', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}]});147    const alice = privateKey('//Alice');148    const bob = privateKey('//Bob');149    const charlie = privateKey('//Charlie');150    await addCollectionAdminExpectSuccess(alice, collection, bob.address);151    await usingApi(async (api) => {152      const data = [153        {154          owner: {substrate: alice.address},155          properties: [{key: 'key1', value: 'v2'}],156        }, {157          owner: {substrate: alice.address},158          properties: [{key: 'key1', value: 'v2'}],159        }, {160          owner: {substrate: alice.address},161          properties: [{key: 'key1', value: 'v2'}],162        },163      ];164165      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});166      // await executeTransaction(api, alice, tx);167168      await submitTransactionExpectFailAsync(alice, tx);169    });170  });171172  it('User doesnt have editing rights', async () => {173    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}],174      propPerm:   [{key: 'key1', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}]});175    const alice = privateKey('//Alice');176    const bob = privateKey('//Bob');177    const charlie = privateKey('//Charlie');178    await addCollectionAdminExpectSuccess(alice, collection, bob.address);179    await usingApi(async (api) => {180      const data = [181        {182          owner: {substrate: alice.address},183          properties: [{key: 'key1', value: 'v2'}],184        }, {185          owner: {substrate: alice.address},186          properties: [{key: 'key1', value: 'v2'}],187        }, {188          owner: {substrate: alice.address},189          properties: [{key: 'key1', value: 'v2'}],190        },191      ];192193      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});194      // await executeTransaction(api, alice, tx);195196      await submitTransactionExpectFailAsync(alice, tx);197    });198  });199200  it('Adding property without access rights', async () => {201    const collection = await createCollectionWithPropsExpectSuccess({properties: [{key: 'key1', value: 'v'}]});202    const alice = privateKey('//Alice');203    const bob = privateKey('//Bob');204    const charlie = privateKey('//Charlie');205    await addCollectionAdminExpectSuccess(alice, collection, bob.address);206    await usingApi(async (api) => {207      const data = [208        {209          owner: {substrate: alice.address},210          properties: [{key: 'key1', value: 'v2'}],211        }, {212          owner: {substrate: bob.address},213          properties: [{key: 'key1', value: 'v2'}],214        }, {215          owner: {substrate: charlie.address},216          properties: [{key: 'key1', value: 'v2'}],217        },218      ];219220      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});221222      await submitTransactionExpectFailAsync(alice, tx);223    });224  });225226  it('Adding more than 64 prps', async () => {227    const prps = [{key: 'key', value: 'v'}];228    const propPerm = [{key: 'key', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}];229230    for (let i = 0; i < 65; i++) {231      prps.push({key: `key${i}`, value: `value${i}`});232      propPerm.push({key: `key${i}`, permission: {mutable: true, collectionAdmin: true, tokenOwner: true}});233    }234235    const collection = await createCollectionWithPropsExpectSuccess({propPerm: propPerm});236    const alice = privateKey('//Alice');237    const bob = privateKey('//Bob');238    const charlie = privateKey('//Charlie');239    await addCollectionAdminExpectSuccess(alice, collection, bob.address);240    await usingApi(async (api) => {241      const data = [242        {243          owner: {substrate: alice.address},244          properties: prps,245        }, {246          owner: {substrate: alice.address},247          properties: prps,248        }, {249          owner: {substrate: alice.address},250          properties: prps,251        },252      ];253254      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});255256      await submitTransactionExpectFailAsync(alice, tx);257    });258  });259260  it('Trying to add bigger property than allowed', async () => {261    const collection = await createCollectionWithPropsExpectSuccess({propPerm: [{key: 'k', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}}]});262    const alice = privateKey('//Alice');263    const bob = privateKey('//Bob');264    const charlie = privateKey('//Charlie');265    await addCollectionAdminExpectSuccess(alice, collection, bob.address);266    await usingApi(async (api) => {267      const data = [268        {269          owner: {substrate: alice.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],270        }, {271          owner: {substrate: bob.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],272        }, {273          owner: {substrate: charlie.address}, properties: [{key: 'k', value: 'vvvvvv'.repeat(5000)}, {key: 'k2', value: 'vvv'.repeat(5000)}],274        },275      ];276277      const tx = api.tx.unique.createMultipleItemsEx(collection, {NFT: data});278279      await submitTransactionExpectFailAsync(alice, tx);280    });281  });282283  it('can initialize multiple NFT with different owners', async () => {284    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});285    const alice = privateKey('//Alice');286    const bob = privateKey('//Bob');287    const charlie = privateKey('//Charlie');288    await usingApi(async (api) => {289      const data = [290        {291          owner: {substrate: alice.address},292          constData: '0x0000',293        }, {294          owner: {substrate: bob.address},295          constData: '0x2222',296        }, {297          owner: {substrate: charlie.address},298          constData: '0x4444',299        },300      ];301302      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {303        NFT: data,304      }));305      const tokens = await api.query.nonfungible.tokenData.entries(collection);306      const json = tokens.map(([, token]) => token.toJSON());307      expect(json).to.be.deep.equal(data);308    });309  });310311  it('can initialize multiple NFT with different owners', async () => {312    const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});313    const alice = privateKey('//Alice');314    const bob = privateKey('//Bob');315    const charlie = privateKey('//Charlie');316    await usingApi(async (api) => {317      const data = [318        {319          owner: {substrate: alice.address},320          constData: '0x0000',321        }, {322          owner: {substrate: bob.address},323          constData: '0x2222',324        }, {325          owner: {substrate: charlie.address},326          constData: '0x4444',327        },328      ];329330      await executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {331        NFT: data,332      }));333      const tokens = await api.query.nonfungible.tokenData.entries(collection);334      const json = tokens.map(([, token]) => token.toJSON());335      expect(json).to.be.deep.equal(data);336    });337  });338339  it('fails when trying to set multiple owners when creating multiple refungibles', async () => {340    const collection = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});341    const alice = privateKey('//Alice');342    const bob = privateKey('//Bob');343344    await usingApi(async (api) => {345      // Polkadot requires map, and yet requires keys to be JSON encoded346      const users = new Map();347      users.set(JSON.stringify({substrate: alice.address}), 1);348      users.set(JSON.stringify({substrate: bob.address}), 1);349350      // TODO: better error message?351      await expect(executeTransaction(api, alice, api.tx.unique.createMultipleItemsEx(collection, {352        RefungibleMultipleItems: [353          {users},354          {users},355        ],356      }))).to.be.rejectedWith(/^refungible\.NotRefungibleDataUsedToMintFungibleCollectionToken$/);357    });358  });359});360'';