1234567import { 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 addCollectionAdminExpectSuccess,20} from './util/helpers';2122chai.use(chaiAsPromised);23const expect = chai.expect;2425let alice: IKeyringPair;26let bob: IKeyringPair;27let collectionIdForTesting: number;2829const accountTokenOwnershipLimit = 0;30const sponsoredDataSize = 0;31const sponsoredMintSize = 0;32const sponsorTimeout = 1;33const tokenLimit = 10;3435describe('setCollectionLimits positive', () => {36 let tx;37 before(async () => {38 await usingApi(async () => {39 const keyring = new Keyring({ type: 'sr25519' });40 alice = keyring.addFromUri('//Alice');41 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});42 });43 });44 it('execute setCollectionLimits with predefined params ', async () => {45 await usingApi(async (api: ApiPromise) => {46 tx = api.tx.nft.setCollectionLimits(47 collectionIdForTesting,48 {49 accountTokenOwnershipLimit: accountTokenOwnershipLimit,50 sponsoredMintSize: sponsoredDataSize,51 tokenLimit: tokenLimit,52 sponsorTimeout: sponsorTimeout,53 ownerCanTransfer: true,54 ownerCanDestroy: true,55 },56 );57 const events = await submitTransactionAsync(alice, tx);58 const result = getCreateItemResult(events);5960 61 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;6263 64 expect(result.success).to.be.true;65 expect(collectionInfo.limits.accountTokenOwnershipLimit).to.be.equal(accountTokenOwnershipLimit);66 expect(collectionInfo.limits.sponsoredDataSize).to.be.equal(sponsoredDataSize);67 expect(collectionInfo.limits.tokenLimit).to.be.equal(tokenLimit);68 expect(collectionInfo.limits.sponsorTimeout).to.be.equal(sponsorTimeout);69 expect(collectionInfo.limits.ownerCanTransfer).to.be.true;70 expect(collectionInfo.limits.ownerCanDestroy).to.be.true;71 });72 });7374 it('Set the same token limit twice', async () => {75 await usingApi(async (api: ApiPromise) => {7677 const collectionLimits = {78 accountTokenOwnershipLimit: accountTokenOwnershipLimit,79 sponsoredMintSize: sponsoredDataSize,80 tokenLimit: tokenLimit,81 sponsorTimeout: sponsorTimeout,82 ownerCanTransfer: true,83 ownerCanDestroy: true,84 };8586 87 const tx1 = api.tx.nft.setCollectionLimits(88 collectionIdForTesting,89 collectionLimits,90 );91 const events1 = await submitTransactionAsync(alice, tx1);92 const result1 = getCreateItemResult(events1);93 const collectionInfo1 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;9495 96 const tx2 = api.tx.nft.setCollectionLimits(97 collectionIdForTesting,98 collectionLimits,99 );100 const events2 = await submitTransactionAsync(alice, tx2);101 const result2 = getCreateItemResult(events2);102 const collectionInfo2 = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;103104 105 expect(result1.success).to.be.true;106 expect(collectionInfo1.limits.tokenLimit).to.be.equal(tokenLimit);107 expect(result2.success).to.be.true;108 expect(collectionInfo2.limits.tokenLimit).to.be.equal(tokenLimit);109 });110 });111112});113114describe('setCollectionLimits negative', () => {115 let tx;116 before(async () => {117 await usingApi(async () => {118 const keyring = new Keyring({ type: 'sr25519' });119 alice = keyring.addFromUri('//Alice');120 bob = keyring.addFromUri('//Bob');121 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});122 });123 });124 it('execute setCollectionLimits for not exists collection', async () => {125 await usingApi(async (api: ApiPromise) => {126 const collectionCount = await getCreatedCollectionCount(api);127 const nonExistedCollectionId = collectionCount + 1;128 tx = api.tx.nft.setCollectionLimits(129 nonExistedCollectionId,130 {131 accountTokenOwnershipLimit,132 sponsoredDataSize,133 sponsoredMintSize,134 tokenLimit,135 },136 );137 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;138 });139 });140 it('execute setCollectionLimits from user who is not owner of this collection', async () => {141 await usingApi(async (api: ApiPromise) => {142 tx = api.tx.nft.setCollectionLimits(143 collectionIdForTesting,144 {145 accountTokenOwnershipLimit,146 sponsoredDataSize,147 sponsoredMintSize,148 tokenLimit,149 },150 );151 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;152 });153 });154 it('execute setCollectionLimits from admin collection', async () => {155 await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob);156 await usingApi(async (api: ApiPromise) => {157 tx = api.tx.nft.setCollectionLimits(158 collectionIdForTesting,159 {160 accountTokenOwnershipLimit,161 sponsoredDataSize,162 sponsoredMintSize,163 tokenLimit,164 },165 );166 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;167 });168 });169170 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {171 const collectionId = await createCollectionExpectSuccess();172 await setCollectionLimitsExpectSuccess(alice, collectionId, {173 accountTokenOwnershipLimit: accountTokenOwnershipLimit,174 sponsoredMintSize: sponsoredDataSize,175 tokenLimit: tokenLimit,176 sponsorTimeout: sponsorTimeout,177 ownerCanTransfer: false,178 ownerCanDestroy: true,179 });180 await setCollectionLimitsExpectFailure(alice, collectionId, {181 accountTokenOwnershipLimit: accountTokenOwnershipLimit,182 sponsoredMintSize: sponsoredDataSize,183 tokenLimit: tokenLimit,184 sponsorTimeout: sponsorTimeout,185 ownerCanTransfer: true,186 ownerCanDestroy: true,187 });188 });189190 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {191 const collectionId = await createCollectionExpectSuccess();192 await setCollectionLimitsExpectSuccess(alice, collectionId, {193 accountTokenOwnershipLimit: accountTokenOwnershipLimit,194 sponsoredMintSize: sponsoredDataSize,195 tokenLimit: tokenLimit,196 sponsorTimeout: sponsorTimeout,197 ownerCanTransfer: true,198 ownerCanDestroy: false,199 });200 await setCollectionLimitsExpectFailure(alice, collectionId, {201 accountTokenOwnershipLimit: accountTokenOwnershipLimit,202 sponsoredMintSize: sponsoredDataSize,203 tokenLimit: tokenLimit,204 sponsorTimeout: sponsorTimeout,205 ownerCanTransfer: true,206 ownerCanDestroy: true,207 });208 });209210 it('Setting the higher token limit fails', async () => {211 await usingApi(async () => {212213 const collectionId = await createCollectionExpectSuccess();214 const collectionLimits = {215 accountTokenOwnershipLimit: accountTokenOwnershipLimit,216 sponsoredMintSize: sponsoredDataSize,217 tokenLimit: tokenLimit,218 sponsorTimeout: sponsorTimeout,219 ownerCanTransfer: true,220 ownerCanDestroy: true,221 };222223 224 await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);225226 227 collectionLimits.tokenLimit += 1;228 await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);229 });230 });231232});