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

difftreelog

source

tests/src/setCollectionLimits.test.ts8.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/>.1617// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits18import {ApiPromise} from '@polkadot/api';19import {IKeyringPair} from '@polkadot/types/types';20import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';22import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';23import {24  createCollectionExpectSuccess, getCreatedCollectionCount,25  getCreateItemResult,26  setCollectionLimitsExpectFailure,27  setCollectionLimitsExpectSuccess,28  addCollectionAdminExpectSuccess,29  queryCollectionExpectSuccess,30} from './util/helpers';3132chai.use(chaiAsPromised);33const expect = chai.expect;3435let alice: IKeyringPair;36let bob: IKeyringPair;37let collectionIdForTesting: number;3839const accountTokenOwnershipLimit = 0;40const sponsoredDataSize = 0;41const sponsorTransferTimeout = 1;42const tokenLimit = 10;4344describe('setCollectionLimits positive', () => {45  let tx;46  before(async () => {47    await usingApi(async (api, privateKeyWrapper) => {48      alice = privateKeyWrapper('//Alice');49      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});50    });51  });52  it('execute setCollectionLimits with predefined params ', async () => {53    await usingApi(async (api: ApiPromise) => {54      tx = api.tx.unique.setCollectionLimits(55        collectionIdForTesting,56        {57          accountTokenOwnershipLimit: accountTokenOwnershipLimit,58          sponsoredDataSize: sponsoredDataSize,59          tokenLimit: tokenLimit,60          sponsorTransferTimeout,61          ownerCanTransfer: true,62          ownerCanDestroy: true,63        },64      );65      const events = await submitTransactionAsync(alice, tx);66      const result = getCreateItemResult(events);6768      // get collection limits defined previously69      const collectionInfo = await queryCollectionExpectSuccess(api, collectionIdForTesting);7071      // tslint:disable-next-line:no-unused-expression72      expect(result.success).to.be.true;73      expect(collectionInfo.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.equal(accountTokenOwnershipLimit);74      expect(collectionInfo.limits.sponsoredDataSize.unwrap().toNumber()).to.be.equal(sponsoredDataSize);75      expect(collectionInfo.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);76      expect(collectionInfo.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.equal(sponsorTransferTimeout);77      expect(collectionInfo.limits.ownerCanTransfer.unwrap().toJSON()).to.be.true;78      expect(collectionInfo.limits.ownerCanDestroy.unwrap().toJSON()).to.be.true;79    });80  });8182  it('Set the same token limit twice', async () => {83    await usingApi(async (api: ApiPromise) => {8485      const collectionLimits = {86        accountTokenOwnershipLimit: accountTokenOwnershipLimit,87        sponsoredMintSize: sponsoredDataSize,88        tokenLimit: tokenLimit,89        sponsorTransferTimeout,90        ownerCanTransfer: true,91        ownerCanDestroy: true,92      };9394      // The first time95      const tx1 = api.tx.unique.setCollectionLimits(96        collectionIdForTesting,97        collectionLimits,98      );99      const events1 = await submitTransactionAsync(alice, tx1);100      const result1 = getCreateItemResult(events1);101      expect(result1.success).to.be.true;102      const collectionInfo1 = await queryCollectionExpectSuccess(api, collectionIdForTesting);103      expect(collectionInfo1.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);104105      // The second time106      const tx2 = api.tx.unique.setCollectionLimits(107        collectionIdForTesting,108        collectionLimits,109      );110      const events2 = await submitTransactionAsync(alice, tx2);111      const result2 = getCreateItemResult(events2);112      expect(result2.success).to.be.true;113      const collectionInfo2 = await queryCollectionExpectSuccess(api, collectionIdForTesting);114      expect(collectionInfo2.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);115    });116  });117118});119120describe('setCollectionLimits negative', () => {121  let tx;122  before(async () => {123    await usingApi(async (api, privateKeyWrapper) => {124      alice = privateKeyWrapper('//Alice');125      bob = privateKeyWrapper('//Bob');126      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});127    });128  });129  it('execute setCollectionLimits for not exists collection', async () => {130    await usingApi(async (api: ApiPromise) => {131      const collectionCount = await getCreatedCollectionCount(api);132      const nonExistedCollectionId = collectionCount + 1;133      tx = api.tx.unique.setCollectionLimits(134        nonExistedCollectionId,135        {136          accountTokenOwnershipLimit,137          sponsoredDataSize,138          // sponsoredMintSize,139          tokenLimit,140        },141      );142      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;143    });144  });145  it('execute setCollectionLimits from user who is not owner of this collection', async () => {146    await usingApi(async (api: ApiPromise) => {147      tx = api.tx.unique.setCollectionLimits(148        collectionIdForTesting,149        {150          accountTokenOwnershipLimit,151          sponsoredDataSize,152          // sponsoredMintSize,153          tokenLimit,154        },155      );156      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;157    });158  });159  it('execute setCollectionLimits from admin collection', async () => {160    await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);161    await usingApi(async (api: ApiPromise) => {162      tx = api.tx.unique.setCollectionLimits(163        collectionIdForTesting,164        {165          accountTokenOwnershipLimit,166          sponsoredDataSize,167          // sponsoredMintSize,168          tokenLimit,169        },170      );171      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;172    });173  });174175  it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {176    const collectionId = await createCollectionExpectSuccess();177    await setCollectionLimitsExpectSuccess(alice, collectionId, {178      accountTokenOwnershipLimit: accountTokenOwnershipLimit,179      sponsoredMintSize: sponsoredDataSize,180      tokenLimit: tokenLimit,181      sponsorTransferTimeout,182      ownerCanTransfer: false,183      ownerCanDestroy: true,184    });185    await setCollectionLimitsExpectFailure(alice, collectionId, {186      accountTokenOwnershipLimit: accountTokenOwnershipLimit,187      sponsoredMintSize: sponsoredDataSize,188      tokenLimit: tokenLimit,189      sponsorTransferTimeout,190      ownerCanTransfer: true,191      ownerCanDestroy: true,192    });193  });194195  it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {196    const collectionId = await createCollectionExpectSuccess();197    await setCollectionLimitsExpectSuccess(alice, collectionId, {198      accountTokenOwnershipLimit: accountTokenOwnershipLimit,199      sponsoredMintSize: sponsoredDataSize,200      tokenLimit: tokenLimit,201      sponsorTransferTimeout,202      ownerCanTransfer: true,203      ownerCanDestroy: false,204    });205    await setCollectionLimitsExpectFailure(alice, collectionId, {206      accountTokenOwnershipLimit: accountTokenOwnershipLimit,207      sponsoredMintSize: sponsoredDataSize,208      tokenLimit: tokenLimit,209      sponsorTransferTimeout,210      ownerCanTransfer: true,211      ownerCanDestroy: true,212    });213  });214215  it('Setting the higher token limit fails', async () => {216    await usingApi(async () => {217218      const collectionId = await createCollectionExpectSuccess();219      const collectionLimits = {220        accountTokenOwnershipLimit: accountTokenOwnershipLimit,221        sponsoredMintSize: sponsoredDataSize,222        tokenLimit: tokenLimit,223        sponsorTransferTimeout,224        ownerCanTransfer: true,225        ownerCanDestroy: true,226      };227228      // The first time229      await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);230231      // The second time - higher token limit232      collectionLimits.tokenLimit += 1;233      await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);234    });235  });236237});