git.delta.rocks / unique-network / refs/commits / 2b9417a2a3f5

difftreelog

source

js-packages/tests/createMultipleItemsEx.test.ts12.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 type {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, Pallets, itSub} from './util/index.js';19import type {IProperty} from '@unique/playgrounds/types.js';2021describe('Integration Test: createMultipleItemsEx', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;24  let charlie: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      const donor = await privateKey({url: import.meta.url});29      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30    });31  });3233  itSub('can initialize multiple NFT with different owners', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice, {35      name: 'name',36      description: 'descr',37      tokenPrefix: 'COL',38    });39    const args = [40      {41        owner: {Substrate: alice.address},42      },43      {44        owner: {Substrate: bob.address},45      },46      {47        owner: {Substrate: charlie.address},48      },49    ];5051    const tokens = await collection.mintMultipleTokens(alice, args);52    for(const [i, token] of tokens.entries()) {53      expect(await token.getOwner()).to.be.deep.equal(args[i].owner);54    }55  });5657  itSub('createMultipleItemsEx with property Admin', async ({helper}) => {58    const collection = await helper.nft.mintCollection(alice, {59      name: 'name',60      description: 'descr',61      tokenPrefix: 'COL',62      tokenPropertyPermissions: [63        {64          key: 'k',65          permission: {66            mutable: true,67            collectionAdmin: true,68            tokenOwner: false,69          },70        },71      ],72    });7374    const args = [75      {76        owner: {Substrate: alice.address},77        properties: [{key: 'k', value: 'v1'}],78      },79      {80        owner: {Substrate: bob.address},81        properties: [{key: 'k', value: 'v2'}],82      },83      {84        owner: {Substrate: charlie.address},85        properties: [{key: 'k', value: 'v3'}],86      },87    ];8889    const tokens = await collection.mintMultipleTokens(alice, args);90    for(const [i, token] of tokens.entries()) {91      expect(await token.getOwner()).to.be.deep.equal(args[i].owner);92      expect(await token.getData()).to.not.be.empty;93    }94  });9596  itSub('createMultipleItemsEx with property AdminConst', async ({helper}) => {97    const collection = await helper.nft.mintCollection(alice, {98      name: 'name',99      description: 'descr',100      tokenPrefix: 'COL',101      tokenPropertyPermissions: [102        {103          key: 'k',104          permission: {105            mutable: false,106            collectionAdmin: true,107            tokenOwner: false,108          },109        },110      ],111    });112113    const args = [114      {115        owner: {Substrate: alice.address},116        properties: [{key: 'k', value: 'v1'}],117      },118      {119        owner: {Substrate: bob.address},120        properties: [{key: 'k', value: 'v2'}],121      },122      {123        owner: {Substrate: charlie.address},124        properties: [{key: 'k', value: 'v3'}],125      },126    ];127128    const tokens = await collection.mintMultipleTokens(alice, args);129    for(const [i, token] of tokens.entries()) {130      expect(await token.getOwner()).to.be.deep.equal(args[i].owner);131      expect(await token.getData()).to.not.be.empty;132    }133  });134135  itSub('createMultipleItemsEx with property itemOwnerOrAdmin', async ({helper}) => {136    const collection = await helper.nft.mintCollection(alice, {137      name: 'name',138      description: 'descr',139      tokenPrefix: 'COL',140      tokenPropertyPermissions: [141        {142          key: 'k',143          permission: {144            mutable: false,145            collectionAdmin: true,146            tokenOwner: true,147          },148        },149      ],150    });151152    const args = [153      {154        owner: {Substrate: alice.address},155        properties: [{key: 'k', value: 'v1'}],156      },157      {158        owner: {Substrate: bob.address},159        properties: [{key: 'k', value: 'v2'}],160      },161      {162        owner: {Substrate: charlie.address},163        properties: [{key: 'k', value: 'v3'}],164      },165    ];166167    const tokens = await collection.mintMultipleTokens(alice, args);168    for(const [i, token] of tokens.entries()) {169      expect(await token.getOwner()).to.be.deep.equal(args[i].owner);170      expect(await token.getData()).to.not.be.empty;171    }172  });173174  itSub('can initialize fungible with multiple owners', async ({helper}) => {175    const collection = await helper.ft.mintCollection(alice, {176      name: 'name',177      description: 'descr',178      tokenPrefix: 'COL',179    }, 0);180181    await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx',[collection.collectionId, {182      Fungible: new Map([183        [JSON.stringify({Substrate: alice.address}), 50],184        [JSON.stringify({Substrate: bob.address}), 100],185      ]),186    }], true);187188    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(50n);189    expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(100n);190  });191192  itSub.ifWithPallets('can initialize an RFT with multiple owners', [Pallets.ReFungible], async ({helper}) => {193    const collection = await helper.rft.mintCollection(alice, {194      name: 'name',195      description: 'descr',196      tokenPrefix: 'COL',197      tokenPropertyPermissions: [198        {key: 'k', permission: {tokenOwner: false, mutable: false, collectionAdmin: true}},199      ],200    });201202    await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx', [collection.collectionId, {203      RefungibleMultipleOwners: {204        users: new Map([205          [JSON.stringify({Substrate: alice.address}), 1],206          [JSON.stringify({Substrate: bob.address}), 2],207        ]),208        properties: [209          {key: 'k', value: 'v'},210        ],211      },212    }], true);213    const tokenId = await collection.getLastTokenId();214    expect(tokenId).to.be.equal(1);215    expect(await collection.getTokenBalance(1, {Substrate: alice.address})).to.be.equal(1n);216    expect(await collection.getTokenBalance(1, {Substrate: bob.address})).to.be.equal(2n);217  });218219  itSub.ifWithPallets('can initialize multiple RFTs with the same owner', [Pallets.ReFungible], async ({helper}) => {220    const collection = await helper.rft.mintCollection(alice, {221      name: 'name',222      description: 'descr',223      tokenPrefix: 'COL',224      tokenPropertyPermissions: [225        {key: 'k', permission: {tokenOwner: true, mutable: false, collectionAdmin: false}},226      ],227    });228229    await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx', [collection.collectionId, {230      RefungibleMultipleItems: [231        {232          user: {Substrate: alice.address}, pieces: 1,233          properties: [234            {key: 'k', value: 'v1'},235          ],236        },237        {238          user: {Substrate: alice.address}, pieces: 3,239          properties: [240            {key: 'k', value: 'v2'},241          ],242        },243      ],244    }], true);245246    expect(await collection.getLastTokenId()).to.be.equal(2);247    expect(await collection.getTokenBalance(1, {Substrate: alice.address})).to.be.equal(1n);248    expect(await collection.getTokenBalance(2, {Substrate: alice.address})).to.be.equal(3n);249250    const tokenData1 = await helper.rft.getToken(collection.collectionId, 1);251    expect(tokenData1).to.not.be.null;252    expect(tokenData1?.properties[0]).to.be.deep.equal({key: 'k', value: 'v1'});253254    const tokenData2 = await helper.rft.getToken(collection.collectionId, 2);255    expect(tokenData2).to.not.be.null;256    expect(tokenData2?.properties[0]).to.be.deep.equal({key: 'k', value: 'v2'});257  });258});259260describe('Negative test: createMultipleItemsEx', () => {261  let alice: IKeyringPair;262  let bob: IKeyringPair;263  let charlie: IKeyringPair;264265  before(async () => {266    await usingPlaygrounds(async (helper, privateKey) => {267      const donor = await privateKey({url: import.meta.url});268      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);269    });270  });271272  itSub('No editing rights', async ({helper}) => {273    const collection = await helper.nft.mintCollection(alice, {274      name: 'name',275      description: 'descr',276      tokenPrefix: 'COL',277      tokenPropertyPermissions: [278        {279          key: 'k',280          permission: {281            mutable: true,282            collectionAdmin: false,283            tokenOwner: false,284          },285        },286      ],287    });288289    const args = [290      {291        owner: {Substrate: alice.address},292        properties: [{key: 'k', value: 'v1'}],293      },294      {295        owner: {Substrate: bob.address},296        properties: [{key: 'k', value: 'v2'}],297      },298      {299        owner: {Substrate: charlie.address},300        properties: [{key: 'k', value: 'v3'}],301      },302    ];303304    await expect(collection.mintMultipleTokens(alice, args)).to.be.rejectedWith(/common\.NoPermission/);305  });306307  itSub('User doesnt have editing rights', async ({helper}) => {308    const collection = await helper.nft.mintCollection(alice, {309      name: 'name',310      description: 'descr',311      tokenPrefix: 'COL',312      tokenPropertyPermissions: [313        {314          key: 'k',315          permission: {316            mutable: false,317            collectionAdmin: false,318            tokenOwner: false,319          },320        },321      ],322    });323324    const args = [325      {326        owner: {Substrate: alice.address},327        properties: [{key: 'k', value: 'v1'}],328      },329      {330        owner: {Substrate: bob.address},331        properties: [{key: 'k', value: 'v2'}],332      },333      {334        owner: {Substrate: charlie.address},335        properties: [{key: 'k', value: 'v3'}],336      },337    ];338339    await expect(collection.mintMultipleTokens(alice, args)).to.be.rejectedWith(/common\.NoPermission/);340  });341342  itSub('Adding property without access rights', async ({helper}) => {343    const collection = await helper.nft.mintCollection(alice, {344      name: 'name',345      description: 'descr',346      tokenPrefix: 'COL',347    });348349    const args = [350      {351        owner: {Substrate: alice.address},352        properties: [{key: 'k', value: 'v1'}],353      },354      {355        owner: {Substrate: bob.address},356        properties: [{key: 'k', value: 'v2'}],357      },358      {359        owner: {Substrate: charlie.address},360        properties: [{key: 'k', value: 'v3'}],361      },362    ];363364    await expect(collection.mintMultipleTokens(alice, args)).to.be.rejectedWith(/common\.NoPermission/);365  });366367  itSub('Adding more than 64 properties', async ({helper}) => {368    const collection = await helper.nft.mintCollection(alice, {369      name: 'name',370      description: 'descr',371      tokenPrefix: 'COL',372      tokenPropertyPermissions: [373        {374          key: 'k',375          permission: {376            mutable: true,377            collectionAdmin: true,378            tokenOwner: true,379          },380        },381      ],382    });383384    const properties: IProperty[] = [];385386    for(let i = 0; i < 65; i++) {387      properties.push({key: `k${i}`, value: `v${i}`});388    }389390    const args = [391      {392        owner: {Substrate: alice.address},393        properties: properties,394      },395      {396        owner: {Substrate: bob.address},397        properties: properties,398      },399      {400        owner: {Substrate: charlie.address},401        properties: properties,402      },403    ];404405    await expect(collection.mintMultipleTokens(alice, args)).to.be.rejectedWith('Verification Error');406  });407408  itSub('Trying to add bigger property than allowed', async ({helper}) => {409    const collection = await helper.nft.mintCollection(alice, {410      name: 'name',411      description: 'descr',412      tokenPrefix: 'COL',413      tokenPropertyPermissions: [414        {415          key: 'k',416          permission: {417            mutable: true,418            collectionAdmin: true,419            tokenOwner: true,420          },421        },422      ],423    });424425    const args = [426      {427        owner: {Substrate: alice.address},428        properties: [{key: 'k', value: 'A'.repeat(32769)}],429      },430      {431        owner: {Substrate: bob.address},432        properties: [{key: 'k', value: 'A'.repeat(32769)}],433      },434      {435        owner: {Substrate: charlie.address},436        properties: [{key: 'k', value: 'A'.repeat(32769)}],437      },438    ];439440    await expect(collection.mintMultipleTokens(alice, args)).to.be.rejectedWith('Verification Error');441  });442});