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