git.delta.rocks / unique-network / refs/commits / 4fa2a1bb0fae

difftreelog

source

tests/src/setCollectionLimits.test.ts8.2 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits7import { ApiPromise, Keyring } from '@polkadot/api';8import { IKeyringPair } from '@polkadot/types/types';9import chai from 'chai';10import chaiAsPromised from 'chai-as-promised';11import usingApi, {submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';12import { ICollectionInterface } from './types';13import {14  createCollectionExpectSuccess, getCreatedCollectionCount,15  getCreateItemResult,16  getDetailedCollectionInfo,17  setCollectionLimitsExpectFailure,18  setCollectionLimitsExpectSuccess,19} from './util/helpers';2021chai.use(chaiAsPromised);22const expect = chai.expect;2324let alice: IKeyringPair;25let bob: IKeyringPair;26let collectionIdForTesting: number;2728const accountTokenOwnershipLimit = 0;29const sponsoredDataSize = 0;30const sponsoredMintSize = 0;31const sponsorTimeout = 1;32const tokenLimit = 10;3334describe('setCollectionLimits positive', () => {35  let tx;36  before(async () => {37    await usingApi(async () => {38      const keyring = new Keyring({ type: 'sr25519' });39      alice = keyring.addFromUri('//Alice');40      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});41    });42  });43  it('execute setCollectionLimits with predefined params ', async () => {44    await usingApi(async (api: ApiPromise) => {45      tx = api.tx.nft.setCollectionLimits(46        collectionIdForTesting,47        {48          AccountTokenOwnershipLimit: accountTokenOwnershipLimit,49          SponsoredMintSize: sponsoredDataSize,50          TokenLimit: tokenLimit,51          SponsorTimeout: sponsorTimeout,52          OwnerCanTransfer: true,53          OwnerCanDestroy: true,54        },55      );56      const events = await submitTransactionAsync(alice, tx);57      const result = getCreateItemResult(events);5859      // get collection limits defined previously60      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;6162      // tslint:disable-next-line:no-unused-expression63      expect(result.success).to.be.true;64      expect(collectionInfo.Limits.AccountTokenOwnershipLimit).to.be.equal(accountTokenOwnershipLimit);65      expect(collectionInfo.Limits.SponsoredDataSize).to.be.equal(sponsoredDataSize);66      expect(collectionInfo.Limits.TokenLimit).to.be.equal(tokenLimit);67      expect(collectionInfo.Limits.SponsorTimeout).to.be.equal(sponsorTimeout);68      expect(collectionInfo.Limits.OwnerCanTransfer).to.be.true;69      expect(collectionInfo.Limits.OwnerCanDestroy).to.be.true;70    });71  });7273  it('Set the same token limit twice', async () => {74    await usingApi(async (api: ApiPromise) => {7576      const collectionLimits = {77        AccountTokenOwnershipLimit: accountTokenOwnershipLimit,78        SponsoredMintSize: sponsoredDataSize,79        TokenLimit: tokenLimit,80        SponsorTimeout: sponsorTimeout,81        OwnerCanTransfer: true,82        OwnerCanDestroy: true,83      };8485      // The first time86      const tx1 = api.tx.nft.setCollectionLimits(87        collectionIdForTesting,88        collectionLimits,89      );90      const events1 = await submitTransactionAsync(alice, tx1);91      const result1 = getCreateItemResult(events1);92      const collectionInfo1 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;9394      // The second time95      const tx2 = api.tx.nft.setCollectionLimits(96        collectionIdForTesting,97        collectionLimits,98      );99      const events2 = await submitTransactionAsync(alice, tx2);100      const result2 = getCreateItemResult(events2);101      const collectionInfo2 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;102103      // tslint:disable-next-line:no-unused-expression104      expect(result1.success).to.be.true;105      expect(collectionInfo1.Limits.TokenLimit).to.be.equal(tokenLimit);106      expect(result2.success).to.be.true;107      expect(collectionInfo2.Limits.TokenLimit).to.be.equal(tokenLimit);108    });109  });110111});112113describe('setCollectionLimits negative', () => {114  let tx;115  before(async () => {116    await usingApi(async () => {117      const keyring = new Keyring({ type: 'sr25519' });118      alice = keyring.addFromUri('//Alice');119      bob = keyring.addFromUri('//Bob');120      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});121    });122  });123  it('execute setCollectionLimits for not exists collection', async () => {124    await usingApi(async (api: ApiPromise) => {125      const collectionCount = await getCreatedCollectionCount(api);126      const nonExistedCollectionId = collectionCount + 1;127      tx = api.tx.nft.setCollectionLimits(128        nonExistedCollectionId,129        {130          accountTokenOwnershipLimit,131          sponsoredDataSize,132          sponsoredMintSize,133          tokenLimit,134        },135      );136      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;137    });138  });139  it('execute setCollectionLimits from user who is not owner of this collection', async () => {140    await usingApi(async (api: ApiPromise) => {141      tx = api.tx.nft.setCollectionLimits(142        collectionIdForTesting,143        {144          accountTokenOwnershipLimit,145          sponsoredDataSize,146          sponsoredMintSize,147          tokenLimit,148        },149      );150      await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;151    });152  });153  it('execute setCollectionLimits with incorrect limits', async () => {154    await usingApi(async (api: ApiPromise) => {155      tx = api.tx.nft.setCollectionLimits(156        collectionIdForTesting,157        {158          accountTokenOwnershipLimit: 'awdawd',159          sponsorTransferTimeout: 'awd',160          sponsoredDataSize: '12312312312312312',161          tokenLimit: '-100',162        },163      );164      await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;165    });166  });167168  it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {169    const collectionId = await createCollectionExpectSuccess();170    await setCollectionLimitsExpectSuccess(alice, collectionId, { 171      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,172      SponsoredMintSize: sponsoredDataSize,173      TokenLimit: tokenLimit,174      SponsorTimeout: sponsorTimeout,175      OwnerCanTransfer: false,176      OwnerCanDestroy: true,177    });178    await setCollectionLimitsExpectFailure(alice, collectionId, { 179      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,180      SponsoredMintSize: sponsoredDataSize,181      TokenLimit: tokenLimit,182      SponsorTimeout: sponsorTimeout,183      OwnerCanTransfer: true,184      OwnerCanDestroy: true,185    });186  });187188  it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {189    const collectionId = await createCollectionExpectSuccess();190    await setCollectionLimitsExpectSuccess(alice, collectionId, {191      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,192      SponsoredMintSize: sponsoredDataSize,193      TokenLimit: tokenLimit,194      SponsorTimeout: sponsorTimeout,195      OwnerCanTransfer: true,196      OwnerCanDestroy: false,197    });198    await setCollectionLimitsExpectFailure(alice, collectionId, { 199      AccountTokenOwnershipLimit: accountTokenOwnershipLimit,200      SponsoredMintSize: sponsoredDataSize,201      TokenLimit: tokenLimit,202      SponsorTimeout: sponsorTimeout,203      OwnerCanTransfer: true,204      OwnerCanDestroy: true,205    });206  });207208  it('Setting the higher token limit fails', async () => {209    await usingApi(async () => {210211      const collectionId = await createCollectionExpectSuccess();212      const collectionLimits = {213        AccountTokenOwnershipLimit: accountTokenOwnershipLimit,214        SponsoredMintSize: sponsoredDataSize,215        TokenLimit: tokenLimit,216        SponsorTimeout: sponsorTimeout,217        OwnerCanTransfer: true,218        OwnerCanDestroy: true,219      };220221      // The first time222      await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);223224      // The second time - higher token limit225      collectionLimits.TokenLimit += 1;226      await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);227    });228  });229230});