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 {13 createCollectionExpectSuccess, getCreatedCollectionCount,14 getCreateItemResult,15 setCollectionLimitsExpectFailure,16 setCollectionLimitsExpectSuccess,17 addCollectionAdminExpectSuccess,18 queryCollectionExpectSuccess,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 sponsorTransferTimeout = 1;31const tokenLimit = 10;3233describe('setCollectionLimits positive', () => {34 let tx;35 before(async () => {36 await usingApi(async () => {37 const keyring = new Keyring({type: 'sr25519'});38 alice = keyring.addFromUri('//Alice');39 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});40 });41 });42 it('execute setCollectionLimits with predefined params ', async () => {43 await usingApi(async (api: ApiPromise) => {44 tx = api.tx.unique.setCollectionLimits(45 collectionIdForTesting,46 {47 accountTokenOwnershipLimit: accountTokenOwnershipLimit,48 sponsoredDataSize: sponsoredDataSize,49 tokenLimit: tokenLimit,50 sponsorTransferTimeout,51 ownerCanTransfer: true,52 ownerCanDestroy: true,53 },54 );55 const events = await submitTransactionAsync(alice, tx);56 const result = getCreateItemResult(events);5758 59 const collectionInfo = await queryCollectionExpectSuccess(api, collectionIdForTesting);6061 62 expect(result.success).to.be.true;63 expect(collectionInfo.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.equal(accountTokenOwnershipLimit);64 expect(collectionInfo.limits.sponsoredDataSize.unwrap().toNumber()).to.be.equal(sponsoredDataSize);65 expect(collectionInfo.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);66 expect(collectionInfo.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.equal(sponsorTransferTimeout);67 expect(collectionInfo.limits.ownerCanTransfer.unwrap().toJSON()).to.be.true;68 expect(collectionInfo.limits.ownerCanDestroy.unwrap().toJSON()).to.be.true;69 });70 });7172 it('Set the same token limit twice', async () => {73 await usingApi(async (api: ApiPromise) => {7475 const collectionLimits = {76 accountTokenOwnershipLimit: accountTokenOwnershipLimit,77 sponsoredMintSize: sponsoredDataSize,78 tokenLimit: tokenLimit,79 sponsorTransferTimeout,80 ownerCanTransfer: true,81 ownerCanDestroy: true,82 };8384 85 const tx1 = api.tx.unique.setCollectionLimits(86 collectionIdForTesting,87 collectionLimits,88 );89 const events1 = await submitTransactionAsync(alice, tx1);90 const result1 = getCreateItemResult(events1);91 expect(result1.success).to.be.true;92 const collectionInfo1 = await queryCollectionExpectSuccess(api, collectionIdForTesting);93 expect(collectionInfo1.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);9495 96 const tx2 = api.tx.unique.setCollectionLimits(97 collectionIdForTesting,98 collectionLimits,99 );100 const events2 = await submitTransactionAsync(alice, tx2);101 const result2 = getCreateItemResult(events2);102 expect(result2.success).to.be.true;103 const collectionInfo2 = await queryCollectionExpectSuccess(api, collectionIdForTesting);104 expect(collectionInfo2.limits.tokenLimit.unwrap().toNumber()).to.be.equal(tokenLimit);105 });106 });107108});109110describe('setCollectionLimits negative', () => {111 let tx;112 before(async () => {113 await usingApi(async () => {114 const keyring = new Keyring({type: 'sr25519'});115 alice = keyring.addFromUri('//Alice');116 bob = keyring.addFromUri('//Bob');117 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});118 });119 });120 it('execute setCollectionLimits for not exists collection', async () => {121 await usingApi(async (api: ApiPromise) => {122 const collectionCount = await getCreatedCollectionCount(api);123 const nonExistedCollectionId = collectionCount + 1;124 tx = api.tx.unique.setCollectionLimits(125 nonExistedCollectionId,126 {127 accountTokenOwnershipLimit,128 sponsoredDataSize,129 130 tokenLimit,131 },132 );133 await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;134 });135 });136 it('execute setCollectionLimits from user who is not owner of this collection', async () => {137 await usingApi(async (api: ApiPromise) => {138 tx = api.tx.unique.setCollectionLimits(139 collectionIdForTesting,140 {141 accountTokenOwnershipLimit,142 sponsoredDataSize,143 144 tokenLimit,145 },146 );147 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;148 });149 });150 it('execute setCollectionLimits from admin collection', async () => {151 await addCollectionAdminExpectSuccess(alice, collectionIdForTesting, bob.address);152 await usingApi(async (api: ApiPromise) => {153 tx = api.tx.unique.setCollectionLimits(154 collectionIdForTesting,155 {156 accountTokenOwnershipLimit,157 sponsoredDataSize,158 159 tokenLimit,160 },161 );162 await expect(submitTransactionExpectFailAsync(bob, tx)).to.be.rejected;163 });164 });165166 it('fails when trying to enable OwnerCanTransfer after it was disabled', async () => {167 const collectionId = await createCollectionExpectSuccess();168 await setCollectionLimitsExpectSuccess(alice, collectionId, {169 accountTokenOwnershipLimit: accountTokenOwnershipLimit,170 sponsoredMintSize: sponsoredDataSize,171 tokenLimit: tokenLimit,172 sponsorTransferTimeout,173 ownerCanTransfer: false,174 ownerCanDestroy: true,175 });176 await setCollectionLimitsExpectFailure(alice, collectionId, {177 accountTokenOwnershipLimit: accountTokenOwnershipLimit,178 sponsoredMintSize: sponsoredDataSize,179 tokenLimit: tokenLimit,180 sponsorTransferTimeout,181 ownerCanTransfer: true,182 ownerCanDestroy: true,183 });184 });185186 it('fails when trying to enable OwnerCanDestroy after it was disabled', async () => {187 const collectionId = await createCollectionExpectSuccess();188 await setCollectionLimitsExpectSuccess(alice, collectionId, {189 accountTokenOwnershipLimit: accountTokenOwnershipLimit,190 sponsoredMintSize: sponsoredDataSize,191 tokenLimit: tokenLimit,192 sponsorTransferTimeout,193 ownerCanTransfer: true,194 ownerCanDestroy: false,195 });196 await setCollectionLimitsExpectFailure(alice, collectionId, {197 accountTokenOwnershipLimit: accountTokenOwnershipLimit,198 sponsoredMintSize: sponsoredDataSize,199 tokenLimit: tokenLimit,200 sponsorTransferTimeout,201 ownerCanTransfer: true,202 ownerCanDestroy: true,203 });204 });205206 it('Setting the higher token limit fails', async () => {207 await usingApi(async () => {208209 const collectionId = await createCollectionExpectSuccess();210 const collectionLimits = {211 accountTokenOwnershipLimit: accountTokenOwnershipLimit,212 sponsoredMintSize: sponsoredDataSize,213 tokenLimit: tokenLimit,214 sponsorTransferTimeout,215 ownerCanTransfer: true,216 ownerCanDestroy: true,217 };218219 220 await setCollectionLimitsExpectSuccess(alice, collectionId, collectionLimits);221222 223 collectionLimits.tokenLimit += 1;224 await setCollectionLimitsExpectFailure(alice, collectionId, collectionLimits);225 });226 });227228});