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

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, Keyring} 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 () => {48      const keyring = new Keyring({type: 'sr25519'});49      alice = keyring.addFromUri('//Alice');50      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});51    });52  });53  it('execute setCollectionLimits with predefined params ', async () => {54    await usingApi(async (api: ApiPromise) => {55      tx = api.tx.unique.setCollectionLimits(56        collectionIdForTesting,57        {58          accountTokenOwnershipLimit: accountTokenOwnershipLimit,59          sponsoredDataSize: sponsoredDataSize,60          tokenLimit: tokenLimit,61          sponsorTransferTimeout,62          ownerCanTransfer: true,63          ownerCanDestroy: true,64        },65      );66      const events = await submitTransactionAsync(alice, tx);67      const result = getCreateItemResult(events);6869      // get collection limits defined previously70      const collectionInfo = await queryCollectionExpectSuccess(api, collectionIdForTesting);7172      // tslint:disable-next-line:no-unused-expression73      expect(result.success).to.be.true;74      expect(collectionInfo.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.equal(accountTokenOwnershipLimit);75      expect(collectionInfo.limits.sponsoredDataSize.unwrap().toNumber()).to.be.equal(sponsoredDataSize);76      expect(collectionInfo.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);77      expect(collectionInfo.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.equal(sponsorTransferTimeout);78      expect(collectionInfo.limits.ownerCanTransfer.unwrap().toJSON()).to.be.true;79      expect(collectionInfo.limits.ownerCanDestroy.unwrap().toJSON()).to.be.true;80    });81  });8283  it('Set the same token limit twice', async () => {84    await usingApi(async (api: ApiPromise) => {8586      const collectionLimits = {87        accountTokenOwnershipLimit: accountTokenOwnershipLimit,88        sponsoredMintSize: sponsoredDataSize,89        tokenLimit: tokenLimit,90        sponsorTransferTimeout,91        ownerCanTransfer: true,92        ownerCanDestroy: true,93      };9495      // The first time96      const tx1 = api.tx.unique.setCollectionLimits(97        collectionIdForTesting,98        collectionLimits,99      );100      const events1 = await submitTransactionAsync(alice, tx1);101      const result1 = getCreateItemResult(events1);102      expect(result1.success).to.be.true;103      const collectionInfo1 = await queryCollectionExpectSuccess(api, collectionIdForTesting);104      expect(collectionInfo1.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);105106      // The second time107      const tx2 = api.tx.unique.setCollectionLimits(108        collectionIdForTesting,109        collectionLimits,110      );111      const events2 = await submitTransactionAsync(alice, tx2);112      const result2 = getCreateItemResult(events2);113      expect(result2.success).to.be.true;114      const collectionInfo2 = await queryCollectionExpectSuccess(api, collectionIdForTesting);115      expect(collectionInfo2.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);116    });117  });118119});120121describe('setCollectionLimits negative', () => {122  let tx;123  before(async () => {124    await usingApi(async () => {125      const keyring = new Keyring({type: 'sr25519'});126      alice = keyring.addFromUri('//Alice');127      bob = keyring.addFromUri('//Bob');128      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});129    });130  });131  it('execute setCollectionLimits for not exists collection', async () => {132    await usingApi(async (api: ApiPromise) => {133      const collectionCount = await getCreatedCollectionCount(api);134      const nonExistedCollectionId = collectionCount + 1;135      tx = api.tx.unique.setCollectionLimits(136        nonExistedCollectionId,137        {138          accountTokenOwnershipLimit,139          sponsoredDataSize,140          // sponsoredMintSize,141          tokenLimit,142        },143      );144      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;145    });146  });147  it('execute setCollectionLimits from user who is not owner of this collection', async () => {148    await usingApi(async (api: ApiPromise) => {149      tx = api.tx.unique.setCollectionLimits(150        collectionIdForTesting,151        {152          accountTokenOwnershipLimit,153          sponsoredDataSize,154          // sponsoredMintSize,155          tokenLimit,156        },157      );158      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;159    });160  });161  it('execute setCollectionLimits from admin collection', async () => {162    await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);163    await usingApi(async (api: ApiPromise) => {164      tx = api.tx.unique.setCollectionLimits(165        collectionIdForTesting,166        {167          accountTokenOwnershipLimit,168          sponsoredDataSize,169          // sponsoredMintSize,170          tokenLimit,171        },172      );173      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;174    });175  });176177  it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {178    const collectionId = await createCollectionExpectSuccess();179    await setCollectionLimitsExpectSuccess(alice, collectionId, {180      accountTokenOwnershipLimit: accountTokenOwnershipLimit,181      sponsoredMintSize: sponsoredDataSize,182      tokenLimit: tokenLimit,183      sponsorTransferTimeout,184      ownerCanTransfer: false,185      ownerCanDestroy: true,186    });187    await setCollectionLimitsExpectFailure(alice, collectionId, {188      accountTokenOwnershipLimit: accountTokenOwnershipLimit,189      sponsoredMintSize: sponsoredDataSize,190      tokenLimit: tokenLimit,191      sponsorTransferTimeout,192      ownerCanTransfer: true,193      ownerCanDestroy: true,194    });195  });196197  it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {198    const collectionId = await createCollectionExpectSuccess();199    await setCollectionLimitsExpectSuccess(alice, collectionId, {200      accountTokenOwnershipLimit: accountTokenOwnershipLimit,201      sponsoredMintSize: sponsoredDataSize,202      tokenLimit: tokenLimit,203      sponsorTransferTimeout,204      ownerCanTransfer: true,205      ownerCanDestroy: false,206    });207    await setCollectionLimitsExpectFailure(alice, collectionId, {208      accountTokenOwnershipLimit: accountTokenOwnershipLimit,209      sponsoredMintSize: sponsoredDataSize,210      tokenLimit: tokenLimit,211      sponsorTransferTimeout,212      ownerCanTransfer: true,213      ownerCanDestroy: true,214    });215  });216217  it('Setting the higher token limit fails', async () => {218    await usingApi(async () => {219220      const collectionId = await createCollectionExpectSuccess();221      const collectionLimits = {222        accountTokenOwnershipLimit: accountTokenOwnershipLimit,223        sponsoredMintSize: sponsoredDataSize,224        tokenLimit: tokenLimit,225        sponsorTransferTimeout,226        ownerCanTransfer: true,227        ownerCanDestroy: true,228      };229230      // The first time231      await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);232233      // The second time - higher token limit234      collectionLimits.tokenLimit += 1;235      await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);236    });237  });238239});