123456import { ApiPromise } from '@polkadot/api';7import BN from 'bn.js';8import chai from 'chai';9import chaiAsPromised from 'chai-as-promised';10import privateKey from './substrate/privateKey';11import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';12import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';1314chai.use(chaiAsPromised);15const expect = chai.expect;1617describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {18 it('Add collection admin.', async () => {19 await usingApi(async (api) => {20 const collectionId = await createCollectionExpectSuccess();21 const alice = privateKey('//Alice');22 const bob = privateKey('//Bob');2324 const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON();25 expect(collection.Owner.toString()).to.be.eq(alice.address);2627 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);28 await submitTransactionAsync(alice, changeAdminTx);2930 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));31 expect(adminListAfterAddAdmin).to.be.contains(bob.address);32 });33 });3435 it('Add admin using added collection admin.', async () => {36 await usingApi(async (api) => {37 const collectionId = await createCollectionExpectSuccess();38 const Alice = privateKey('//Alice');39 const Bob = privateKey('//Bob');40 const Charlie = privateKey('//CHARLIE');4142 const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON();43 expect(collection.Owner.toString()).to.be.eq(Alice.address);4445 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);46 await submitTransactionAsync(Alice, changeAdminTx);4748 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));49 expect(adminListAfterAddAdmin).to.be.contains(Bob.address);5051 const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, Charlie.address);52 await submitTransactionAsync(Bob, changeAdminTxCharlie);53 const adminListAfterAddNewAdmin: any = (await api.query.nft.adminList(collectionId));54 expect(adminListAfterAddNewAdmin).to.be.contains(Bob.address);55 expect(adminListAfterAddNewAdmin).to.be.contains(Charlie.address);56 });57 });58});5960describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {61 it("Not owner can't add collection admin.", async () => {62 await usingApi(async (api) => {63 const collectionId = await createCollectionExpectSuccess();64 const alice = privateKey('//Alice');65 const nonOwner = privateKey('//Bob_stash');6667 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, alice.address);68 await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;6970 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));71 expect(adminListAfterAddAdmin).not.to.be.contains(alice.address);7273 74 await createCollectionExpectSuccess();75 });76 });77 it("Can't add collection admin of not existing collection.", async () => {78 await usingApi(async (api) => {79 80 const collectionId = (1 << 32) - 1;81 const alice = privateKey('//Alice');82 const bob = privateKey('//Bob');8384 const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);85 await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;8687 88 await createCollectionExpectSuccess();89 });90 });9192 it("Can't add an admin to a destroyed collection.", async () => {93 await usingApi(async (api) => {94 const collectionId = await createCollectionExpectSuccess();95 const Alice = privateKey('//Alice');96 const Bob = privateKey('//Bob');97 await destroyCollectionExpectSuccess(collectionId);98 const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);99 await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;100101 102 await createCollectionExpectSuccess();103 });104 });105106 it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {107 await usingApi(async (api: ApiPromise) => {108 const Alice = privateKey('//Alice');109 const accounts = [110 'GsvVmjr1CBHwQHw84pPHMDxgNY3iBLz6Qn7qS3CH8qPhrHz',111 'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',112 'JKspFU6ohf1Grg3Phdzj2pSgWvsYWzSfKghhfzMbdhNBWs5',113 'Fr4NzY1udSFFLzb2R3qxVQkwz9cZraWkyfH4h3mVVk7BK7P',114 'DfnTB4z7eUvYRqcGtTpFsLC69o6tvBSC1pEv8vWPZFtCkaK',115 'HnMAUz7r2G8G3hB27SYNyit5aJmh2a5P4eMdDtACtMFDbam',116 'DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM',117 ];118 const collectionId = await createCollectionExpectSuccess();119120 const chainLimit = await api.query.nft.chainLimit() as unknown as { CollectionAdminsLimit: BN };121 const chainAdminLimit = chainLimit.CollectionAdminsLimit.toNumber();122 expect(chainAdminLimit).to.be.equal(5);123124 for (let i = 0; i < chainAdminLimit; i++) {125 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, accounts[i]);126 await submitTransactionAsync(Alice, changeAdminTx);127 const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));128 expect(adminListAfterAddAdmin).to.be.contains(accounts[i]);129 }130131 const tx = api.tx.nft.addCollectionAdmin(collectionId, accounts[chainAdminLimit]);132 await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;133 });134 });135});