123456789101112131415161718import {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 bob = privateKeyWrapper('//Bob');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 70 const collectionInfo = await queryCollectionExpectSuccess(api, collectionIdForTesting);7172 73 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 96 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 107 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 it('execute setCollectionLimits from admin collection', async () => {120 await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);121 await usingApi(async (api: ApiPromise) => {122 tx = api.tx.unique.setCollectionLimits(123 collectionIdForTesting,124 {125 accountTokenOwnershipLimit,126 sponsoredDataSize,127 128 tokenLimit,129 },130 );131 await expect(submitTransactionAsync(bob, tx)).to.be.not.rejected;132 });133 });134});135136describe('setCollectionLimits negative', () => {137 let tx;138 before(async () => {139 await usingApi(async (api, privateKeyWrapper) => {140 alice = privateKeyWrapper('//Alice');141 bob = privateKeyWrapper('//Bob');142 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});143 });144 });145 it('execute setCollectionLimits for not exists collection', async () => {146 await usingApi(async (api: ApiPromise) => {147 const collectionCount = await getCreatedCollectionCount(api);148 const nonExistedCollectionId = collectionCount + 1;149 tx = api.tx.unique.setCollectionLimits(150 nonExistedCollectionId,151 {152 accountTokenOwnershipLimit,153 sponsoredDataSize,154 155 tokenLimit,156 },157 );158 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;159 });160 });161 it('execute setCollectionLimits from user who is not owner of this collection', async () => {162 await usingApi(async (api: ApiPromise) => {163 tx = api.tx.unique.setCollectionLimits(164 collectionIdForTesting,165 {166 accountTokenOwnershipLimit,167 sponsoredDataSize,168 169 tokenLimit,170 },171 );172 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;173 });174 });175176 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {177 const collectionId = await createCollectionExpectSuccess();178 await setCollectionLimitsExpectSuccess(alice, collectionId, {179 accountTokenOwnershipLimit: accountTokenOwnershipLimit,180 sponsoredMintSize: sponsoredDataSize,181 tokenLimit: tokenLimit,182 sponsorTransferTimeout,183 ownerCanTransfer: false,184 ownerCanDestroy: true,185 });186 await setCollectionLimitsExpectFailure(alice, collectionId, {187 accountTokenOwnershipLimit: accountTokenOwnershipLimit,188 sponsoredMintSize: sponsoredDataSize,189 tokenLimit: tokenLimit,190 sponsorTransferTimeout,191 ownerCanTransfer: true,192 ownerCanDestroy: true,193 });194 });195196 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {197 const collectionId = await createCollectionExpectSuccess();198 await setCollectionLimitsExpectSuccess(alice, collectionId, {199 accountTokenOwnershipLimit: accountTokenOwnershipLimit,200 sponsoredMintSize: sponsoredDataSize,201 tokenLimit: tokenLimit,202 sponsorTransferTimeout,203 ownerCanTransfer: true,204 ownerCanDestroy: false,205 });206 await setCollectionLimitsExpectFailure(alice, collectionId, {207 accountTokenOwnershipLimit: accountTokenOwnershipLimit,208 sponsoredMintSize: sponsoredDataSize,209 tokenLimit: tokenLimit,210 sponsorTransferTimeout,211 ownerCanTransfer: true,212 ownerCanDestroy: true,213 });214 });215216 it('Setting the higher token limit fails', async () => {217 await usingApi(async () => {218219 const collectionId = await createCollectionExpectSuccess();220 const collectionLimits = {221 accountTokenOwnershipLimit: accountTokenOwnershipLimit,222 sponsoredMintSize: sponsoredDataSize,223 tokenLimit: tokenLimit,224 sponsorTransferTimeout,225 ownerCanTransfer: true,226 ownerCanDestroy: true,227 };228229 230 await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);231232 233 collectionLimits.tokenLimit += 1;234 await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);235 });236 });237238});