1234567891011121314151617import {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import chai from 'chai';20import chaiAsPromised from 'chai-as-promised';21import privateKey from './substrate/privateKey';22import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';23import {24 createCollectionExpectSuccess,25 destroyCollectionExpectSuccess,26 getGenericResult,27 normalizeAccountId,28 setCollectionLimitsExpectSuccess,29 addCollectionAdminExpectSuccess,30 getBalance,31 getTokenOwner,32 getLastTokenId,33 getVariableMetadata,34 getConstMetadata,35 getCreatedCollectionCount,36} from './util/helpers';3738chai.use(chaiAsPromised);39const expect = chai.expect;4041describe('Integration Test createMultipleItems(collection_id, owner, items_data):', () => {42 it('Create 0x31, 0x32, 0x33 items in active NFT collection and verify tokens data in chain', async () => {43 await usingApi(async (api: ApiPromise) => {44 const collectionId = await createCollectionExpectSuccess();45 const itemsListIndexBefore = await getLastTokenId(api, collectionId);46 expect(itemsListIndexBefore).to.be.equal(0);47 const alice = privateKey('//Alice');48 const args = [{NFT: ['0x31', '0x31']}, {NFT: ['0x32', '0x32']}, {NFT: ['0x33', '0x33']}];49 const createMultipleItemsTx = api.tx.unique50 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);51 await submitTransactionAsync(alice, createMultipleItemsTx);52 const itemsListIndexAfter = await getLastTokenId(api, collectionId);53 expect(itemsListIndexAfter).to.be.equal(3);5455 expect(await getTokenOwner(api, collectionId, 1)).to.be.deep.equal(normalizeAccountId(alice.address));56 expect(await getTokenOwner(api, collectionId, 2)).to.be.deep.equal(normalizeAccountId(alice.address));57 expect(await getTokenOwner(api, collectionId, 3)).to.be.deep.equal(normalizeAccountId(alice.address));5859 expect(await getConstMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);60 expect(await getConstMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);61 expect(await getConstMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);6263 expect(await getVariableMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);64 expect(await getVariableMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);65 expect(await getVariableMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);66 });67 });6869 it('Create 0x01, 0x02, 0x03 items in active Fungible collection and verify tokens data in chain', async () => {70 await usingApi(async (api: ApiPromise) => {71 const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});72 const itemsListIndexBefore = await getLastTokenId(api, collectionId);73 expect(itemsListIndexBefore).to.be.equal(0);74 const alice = privateKey('//Alice');75 const args = [76 {Fungible: {value: 1}},77 {Fungible: {value: 2}},78 {Fungible: {value: 3}},79 ];80 const createMultipleItemsTx = api.tx.unique81 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);82 await submitTransactionAsync(alice, createMultipleItemsTx);83 const token1Data = await getBalance(api, collectionId, alice.address, 0);8485 expect(token1Data).to.be.equal(6n); 86 });87 });8889 it('Create 0x31, 0x32, 0x33 items in active ReFungible collection and verify tokens data in chain', async () => {90 await usingApi(async (api: ApiPromise) => {91 const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});92 const itemsListIndexBefore = await getLastTokenId(api, collectionId);93 expect(itemsListIndexBefore).to.be.equal(0);94 const alice = privateKey('//Alice');95 const args = [96 {ReFungible: {const_data: [0x31], variable_data: [0x31], pieces: 1}},97 {ReFungible: {const_data: [0x32], variable_data: [0x32], pieces: 1}},98 {ReFungible: {const_data: [0x33], variable_data: [0x33], pieces: 1}},99 ];100 const createMultipleItemsTx = api.tx.unique101 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);102 await submitTransactionAsync(alice, createMultipleItemsTx);103 const itemsListIndexAfter = await getLastTokenId(api, collectionId);104 expect(itemsListIndexAfter).to.be.equal(3);105106 expect(await getBalance(api, collectionId, alice.address, 1)).to.be.equal(1n);107 expect(await getBalance(api, collectionId, alice.address, 2)).to.be.equal(1n);108 expect(await getBalance(api, collectionId, alice.address, 3)).to.be.equal(1n);109110 expect(await getConstMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);111 expect(await getConstMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);112 expect(await getConstMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);113114 expect(await getVariableMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);115 expect(await getVariableMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);116 expect(await getVariableMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);117 });118 });119120 it('Can mint amount of items equals to collection limits', async () => {121 await usingApi(async (api) => {122 const alice = privateKey('//Alice');123124 const collectionId = await createCollectionExpectSuccess();125 await setCollectionLimitsExpectSuccess(alice, collectionId, {126 tokenLimit: 2,127 });128 const args = [129 {NFT: ['A', 'A']},130 {NFT: ['B', 'B']},131 ];132 const createMultipleItemsTx = api.tx.unique.createMultipleItems(collectionId, normalizeAccountId(alice.address), args);133 const events = await submitTransactionAsync(alice, createMultipleItemsTx);134 const result = getGenericResult(events);135 expect(result.success).to.be.true;136 });137 });138});139140describe('Integration Test createMultipleItems(collection_id, owner, items_data) with collection admin permissions:', () => {141142 let alice: IKeyringPair;143 let bob: IKeyringPair;144145 before(async () => {146 await usingApi(async () => {147 alice = privateKey('//Alice');148 bob = privateKey('//Bob');149 });150 });151152 it('Create 0x31, 0x32, 0x33 items in active NFT collection and verify tokens data in chain', async () => {153 await usingApi(async (api: ApiPromise) => {154 const collectionId = await createCollectionExpectSuccess();155 const itemsListIndexBefore = await getLastTokenId(api, collectionId);156 expect(itemsListIndexBefore).to.be.equal(0);157 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);158 const args = [{NFT: ['0x31', '0x31']}, {NFT: ['0x32', '0x32']}, {NFT: ['0x33', '0x33']}];159 const createMultipleItemsTx = api.tx.unique160 .createMultipleItems(collectionId, normalizeAccountId(bob.address), args);161 await submitTransactionAsync(bob, createMultipleItemsTx);162 const itemsListIndexAfter = await getLastTokenId(api, collectionId);163 expect(itemsListIndexAfter).to.be.equal(3);164165 expect(await getTokenOwner(api, collectionId, 1)).to.be.deep.equal(normalizeAccountId(bob.address));166 expect(await getTokenOwner(api, collectionId, 2)).to.be.deep.equal(normalizeAccountId(bob.address));167 expect(await getTokenOwner(api, collectionId, 3)).to.be.deep.equal(normalizeAccountId(bob.address));168169 expect(await getConstMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);170 expect(await getConstMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);171 expect(await getConstMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);172173 expect(await getVariableMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);174 expect(await getVariableMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);175 expect(await getVariableMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);176 });177 });178179 it('Create 0x01, 0x02, 0x03 items in active Fungible collection and verify tokens data in chain', async () => {180 await usingApi(async (api: ApiPromise) => {181 const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});182 const itemsListIndexBefore = await getLastTokenId(api, collectionId);183 expect(itemsListIndexBefore).to.be.equal(0);184 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);185 const args = [186 {Fungible: {value: 1}},187 {Fungible: {value: 2}},188 {Fungible: {value: 3}},189 ];190 const createMultipleItemsTx = api.tx.unique191 .createMultipleItems(collectionId, normalizeAccountId(bob.address), args);192 await submitTransactionAsync(bob, createMultipleItemsTx);193 const token1Data = await getBalance(api, collectionId, bob.address, 0);194195 expect(token1Data).to.be.equal(6n); 196 });197 });198199 it('Create 0x31, 0x32, 0x33 items in active ReFungible collection and verify tokens data in chain', async () => {200 await usingApi(async (api: ApiPromise) => {201 const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});202 const itemsListIndexBefore = await getLastTokenId(api, collectionId);203 expect(itemsListIndexBefore).to.be.equal(0);204 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);205 const args = [206 {ReFungible: {const_data: [0x31], variable_data: [0x31], pieces: 1}},207 {ReFungible: {const_data: [0x32], variable_data: [0x32], pieces: 1}},208 {ReFungible: {const_data: [0x33], variable_data: [0x33], pieces: 1}},209 ];210 const createMultipleItemsTx = api.tx.unique211 .createMultipleItems(collectionId, normalizeAccountId(bob.address), args);212 await submitTransactionAsync(bob, createMultipleItemsTx);213 const itemsListIndexAfter = await getLastTokenId(api, collectionId);214 expect(itemsListIndexAfter).to.be.equal(3);215216 expect(await getBalance(api, collectionId, bob.address, 1)).to.be.equal(1n);217 expect(await getBalance(api, collectionId, bob.address, 2)).to.be.equal(1n);218 expect(await getBalance(api, collectionId, bob.address, 3)).to.be.equal(1n);219220 expect(await getConstMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);221 expect(await getConstMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);222 expect(await getConstMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);223224 expect(await getVariableMetadata(api, collectionId, 1)).to.be.deep.equal([0x31]);225 expect(await getVariableMetadata(api, collectionId, 2)).to.be.deep.equal([0x32]);226 expect(await getVariableMetadata(api, collectionId, 3)).to.be.deep.equal([0x33]);227 });228 });229});230231describe('Negative Integration Test createMultipleItems(collection_id, owner, items_data):', () => {232233 let alice: IKeyringPair;234 let bob: IKeyringPair;235236 before(async () => {237 await usingApi(async () => {238 alice = privateKey('//Alice');239 bob = privateKey('//Bob');240 });241 });242243 it('Regular user cannot create items in active NFT collection', async () => {244 await usingApi(async (api: ApiPromise) => {245 const collectionId = await createCollectionExpectSuccess();246 const itemsListIndexBefore = await getLastTokenId(api, collectionId);247 expect(itemsListIndexBefore).to.be.equal(0);248 const args = [{NFT: ['0x31', '0x31']}, {NFT: ['0x32', '0x32']}, {NFT: ['0x33', '0x33']}];249 const createMultipleItemsTx = api.tx.unique250 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);251 await expect(submitTransactionAsync(bob, createMultipleItemsTx)).to.be.rejected;252 });253 });254255 it('Regular user cannot create items in active Fungible collection', async () => {256 await usingApi(async (api: ApiPromise) => {257 const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});258 const itemsListIndexBefore = await getLastTokenId(api, collectionId);259 expect(itemsListIndexBefore).to.be.equal(0);260 const args = [261 {Fungible: {value: 1}},262 {Fungible: {value: 2}},263 {Fungible: {value: 3}},264 ];265 const createMultipleItemsTx = api.tx.unique266 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);267 await expect(submitTransactionAsync(bob, createMultipleItemsTx)).to.be.rejected;268 });269 });270271 it('Regular user cannot create items in active ReFungible collection', async () => {272 await usingApi(async (api: ApiPromise) => {273 const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});274 const itemsListIndexBefore = await getLastTokenId(api, collectionId);275 expect(itemsListIndexBefore).to.be.equal(0);276 const args = [277 {ReFungible: {const_data: [0x31], variable_data: [0x31], pieces: 1}},278 {ReFungible: {const_data: [0x32], variable_data: [0x32], pieces: 1}},279 {ReFungible: {const_data: [0x33], variable_data: [0x33], pieces: 1}},280 ];281 const createMultipleItemsTx = api.tx.unique282 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);283 await expect(submitTransactionAsync(bob, createMultipleItemsTx)).to.be.rejected;284 });285 });286287 it('Create token in not existing collection', async () => {288 await usingApi(async (api: ApiPromise) => {289 const collectionId = await getCreatedCollectionCount(api) + 1;290 const createMultipleItemsTx = api.tx.unique291 .createMultipleItems(collectionId, normalizeAccountId(alice.address), ['NFT', 'NFT', 'NFT']);292 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;293 });294 });295296 it('Create NFT and Re-fungible tokens that has reached the maximum data limit', async () => {297 await usingApi(async (api: ApiPromise) => {298 299 const collectionId = await createCollectionExpectSuccess();300 const alice = privateKey('//Alice');301 const args = [302 {NFT: ['A'.repeat(2049), 'A'.repeat(2049)]},303 {NFT: ['B'.repeat(2049), 'B'.repeat(2049)]},304 {NFT: ['C'.repeat(2049), 'C'.repeat(2049)]},305 ];306 const createMultipleItemsTx = api.tx.unique307 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);308 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;309310 311 const collectionIdReFungible =312 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});313 const argsReFungible = [314 {ReFungible: ['1'.repeat(2049), '1'.repeat(2049), 10]},315 {ReFungible: ['2'.repeat(2049), '2'.repeat(2049), 10]},316 {ReFungible: ['3'.repeat(2049), '3'.repeat(2049), 10]},317 ];318 const createMultipleItemsTxFungible = api.tx.unique319 .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible);320 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected;321 });322 });323324 it('Create tokens with different types', async () => {325 await usingApi(async (api: ApiPromise) => {326 const collectionId = await createCollectionExpectSuccess();327 const createMultipleItemsTx = api.tx.unique328 .createMultipleItems(collectionId, normalizeAccountId(alice.address), ['NFT', 'Fungible', 'ReFungible']);329 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;330 331 await destroyCollectionExpectSuccess(collectionId);332 });333 });334335 it('Create tokens with different data limits <> maximum data limit', async () => {336 await usingApi(async (api: ApiPromise) => {337 const collectionId = await createCollectionExpectSuccess();338 const args = [339 {NFT: ['A', 'A']},340 {NFT: ['B', 'B'.repeat(2049)]},341 {NFT: ['C'.repeat(2049), 'C']},342 ];343 const createMultipleItemsTx = await api.tx.unique344 .createMultipleItems(collectionId, normalizeAccountId(alice.address), args);345 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;346 });347 });348349 it('Fails when minting tokens exceeds collectionLimits amount', async () => {350 await usingApi(async (api) => {351352 const collectionId = await createCollectionExpectSuccess();353 await setCollectionLimitsExpectSuccess(alice, collectionId, {354 tokenLimit: 1,355 });356 const args = [357 {NFT: ['A', 'A']},358 {NFT: ['B', 'B']},359 ];360 const createMultipleItemsTx = api.tx.unique.createMultipleItems(collectionId, normalizeAccountId(alice.address), args);361 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;362 });363 });364});