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 });169 it('execute setCollectionLimits with incorrect limits', async () => {170 await usingApi(async (api: ApiPromise) => {171 tx = api.tx.nft.setCollectionLimits(172 collectionIdForTesting,173 {174 accountTokenOwnershipLimit: 'awdawd',175 sponsorTransferTimeout: 'awd',176 sponsoredDataSize: '12312312312312312',177 tokenLimit: '-100',178 },179 );180 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;181 });182 });183184 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {185 const collectionId = await createCollectionExpectSuccess();186 await setCollectionLimitsExpectSuccess(alice, collectionId, { 187 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,188 SponsoredMintSize: sponsoredDataSize,189 TokenLimit: tokenLimit,190 SponsorTimeout: sponsorTimeout,191 OwnerCanTransfer: false,192 OwnerCanDestroy: true,193 });194 await setCollectionLimitsExpectFailure(alice, collectionId, { 195 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,196 SponsoredMintSize: sponsoredDataSize,197 TokenLimit: tokenLimit,198 SponsorTimeout: sponsorTimeout,199 OwnerCanTransfer: true,200 OwnerCanDestroy: true,201 });202 });203204 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {205 const collectionId = await createCollectionExpectSuccess();206 await setCollectionLimitsExpectSuccess(alice, collectionId, {207 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,208 SponsoredMintSize: sponsoredDataSize,209 TokenLimit: tokenLimit,210 SponsorTimeout: sponsorTimeout,211 OwnerCanTransfer: true,212 OwnerCanDestroy: false,213 });214 await setCollectionLimitsExpectFailure(alice, collectionId, { 215 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,216 SponsoredMintSize: sponsoredDataSize,217 TokenLimit: tokenLimit,218 SponsorTimeout: sponsorTimeout,219 OwnerCanTransfer: true,220 OwnerCanDestroy: true,221 });222 });223224 it('Setting the higher token limit fails', async () => {225 await usingApi(async () => {226227 const collectionId = await createCollectionExpectSuccess();228 const collectionLimits = {229 AccountTokenOwnershipLimit: accountTokenOwnershipLimit,230 SponsoredMintSize: sponsoredDataSize,231 TokenLimit: tokenLimit,232 SponsorTimeout: sponsorTimeout,233 OwnerCanTransfer: true,234 OwnerCanDestroy: true,235 };236237 238 await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);239240 241 collectionLimits.TokenLimit += 1;242 await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);243 });244 });245246});