1import {expect} from 'chai';2import usingApi, {executeTransaction} from '../substrate/substrate-api';3import {4 createCollectionExpectSuccess,5 createItemExpectSuccess,6 getCreateCollectionResult,7 getDetailedCollectionInfo,8 getGenericResult,9 normalizeAccountId,10} from '../util/helpers';11import {IKeyringPair} from '@polkadot/types/types';12import {ApiPromise} from '@polkadot/api';13import {it} from 'mocha';1415let alice: IKeyringPair;16let bob: IKeyringPair;1718async function createRmrkCollection(api: ApiPromise, sender: IKeyringPair): Promise<{uniqueId: number, rmrkId: number}> {19 const tx = api.tx.rmrkCore.createCollection('metadata', null, 'symbol');20 const events = await executeTransaction(api, sender, tx);2122 const uniqueResult = getCreateCollectionResult(events);23 const rmrkResult = getGenericResult(events, 'rmrkCore', 'CollectionCreated', (data) => {24 return parseInt(data[1].toString(), 10);25 });2627 return {28 uniqueId: uniqueResult.collectionId,29 rmrkId: rmrkResult.data!,30 };31}3233async function createRmrkNft(api: ApiPromise, sender: IKeyringPair, collectionId: number): Promise<number> {34 const tx = api.tx.rmrkCore.mintNft(35 sender.address,36 collectionId,37 sender.address,38 null,39 'nft-metadata',40 true,41 null,42 );43 const events = await executeTransaction(api, sender, tx);44 const result = getGenericResult(events, 'rmrkCore', 'NftMinted', (data) => {45 return parseInt(data[2].toString(), 10);46 });4748 return result.data!;49}5051async function isUnique(): Promise<boolean> {52 return usingApi(async api => {53 const chain = await api.rpc.system.chain();5455 return chain.eq('UNIQUE');56 });57}5859describe('RMRK External Integration Test', async () => {60 const it_rmrk = (await isUnique() ? it : it.skip);6162 before(async () => {63 await usingApi(async (api, privateKeyWrapper) => {64 alice = privateKeyWrapper('//Alice');6566 67 });68 });6970 it_rmrk('Creates a new RMRK collection that is mapped to a different ID and is tagged as external', async () => {71 await usingApi(async api => {72 73 await createCollectionExpectSuccess();7475 const collectionIds = await createRmrkCollection(api, alice);7677 expect(collectionIds.rmrkId).to.be.lessThan(collectionIds.uniqueId, 'collection ID mapping');7879 const collection = (await getDetailedCollectionInfo(api, collectionIds.uniqueId))!;80 expect(collection.readOnly.toHuman(), 'tagged external').to.be.true;81 });82 });83});8485describe('Negative Integration Test: External Collections, Internal Ops', async () => {86 let uniqueCollectionId: number;87 let rmrkCollectionId: number;88 let rmrkNftId: number;8990 const it_rmrk = (await isUnique() ? it : it.skip);9192 before(async () => {93 await usingApi(async (api, privateKeyWrapper) => {94 alice = privateKeyWrapper('//Alice');95 bob = privateKeyWrapper('//Bob');9697 const collectionIds = await createRmrkCollection(api, alice);98 uniqueCollectionId = collectionIds.uniqueId;99 rmrkCollectionId = collectionIds.rmrkId;100101 rmrkNftId = await createRmrkNft(api, alice, rmrkCollectionId);102 });103 });104105 it_rmrk('[Negative] Forbids Unique operations with an external collection, handled by dispatch_call', async () => {106 await usingApi(async api => {107 108109 const txCreateItem = api.tx.unique.createItem(uniqueCollectionId, normalizeAccountId(alice), 'NFT');110 await expect(executeTransaction(api, alice, txCreateItem), 'creating item')111 .to.be.rejectedWith(/common\.CollectionIsExternal/);112113 const txCreateMultipleItems = api.tx.unique.createMultipleItems(uniqueCollectionId, normalizeAccountId(alice), [{NFT: {}}, {NFT: {}}]);114 await expect(executeTransaction(api, alice, txCreateMultipleItems), 'creating multiple')115 .to.be.rejectedWith(/common\.CollectionIsExternal/);116117 const txCreateMultipleItemsEx = api.tx.unique.createMultipleItemsEx(uniqueCollectionId, {NFT: [{}]});118 await expect(executeTransaction(api, alice, txCreateMultipleItemsEx), 'creating multiple ex')119 .to.be.rejectedWith(/common\.CollectionIsExternal/);120121 122123 const txSetCollectionProperties = api.tx.unique.setCollectionProperties(uniqueCollectionId, [{key: 'a', value: '1'}, {key: 'b'}]);124 await expect(executeTransaction(api, alice, txSetCollectionProperties), 'setting collection properties')125 .to.be.rejectedWith(/common\.CollectionIsExternal/);126127 const txDeleteCollectionProperties = api.tx.unique.deleteCollectionProperties(uniqueCollectionId, ['a']);128 await expect(executeTransaction(api, alice, txDeleteCollectionProperties), 'deleting collection properties')129 .to.be.rejectedWith(/common\.CollectionIsExternal/);130131 const txsetTokenPropertyPermissions = api.tx.unique.setTokenPropertyPermissions(uniqueCollectionId, [{key: 'a', permission: {mutable: true}}]);132 await expect(executeTransaction(api, alice, txsetTokenPropertyPermissions), 'setting property permissions')133 .to.be.rejectedWith(/common\.CollectionIsExternal/);134135 136137 const txBurn = api.tx.unique.burnItem(uniqueCollectionId, rmrkNftId, 1);138 await expect(executeTransaction(api, alice, txBurn), 'burning').to.be.rejectedWith(/common\.CollectionIsExternal/);139140 const txBurnFrom = api.tx.unique.burnFrom(uniqueCollectionId, normalizeAccountId(alice), rmrkNftId, 1);141 await expect(executeTransaction(api, alice, txBurnFrom), 'burning-from').to.be.rejectedWith(/common\.CollectionIsExternal/);142143 const txTransfer = api.tx.unique.transfer(normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);144 await expect(executeTransaction(api, alice, txTransfer), 'transferring').to.be.rejectedWith(/common\.CollectionIsExternal/);145146 const txApprove = api.tx.unique.approve(normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);147 await expect(executeTransaction(api, alice, txApprove), 'approving').to.be.rejectedWith(/common\.CollectionIsExternal/);148149 const txTransferFrom = api.tx.unique.transferFrom(normalizeAccountId(alice), normalizeAccountId(bob), uniqueCollectionId, rmrkNftId, 1);150 await expect(executeTransaction(api, alice, txTransferFrom), 'transferring-from').to.be.rejectedWith(/common\.CollectionIsExternal/);151152 153154 const txSetTokenProperties = api.tx.unique.setTokenProperties(uniqueCollectionId, rmrkNftId, [{key: 'a', value: '2'}]);155 await expect(executeTransaction(api, alice, txSetTokenProperties), 'setting token properties')156 .to.be.rejectedWith(/common\.CollectionIsExternal/);157158 const txDeleteTokenProperties = api.tx.unique.deleteTokenProperties(uniqueCollectionId, rmrkNftId, ['a']);159 await expect(executeTransaction(api, alice, txDeleteTokenProperties), 'deleting token properties')160 .to.be.rejectedWith(/common\.CollectionIsExternal/);161 });162 });163164 it_rmrk('[Negative] Forbids Unique collection operations with an external collection', async () => {165 await usingApi(async api => {166 const txDestroyCollection = api.tx.unique.destroyCollection(uniqueCollectionId);167 await expect(executeTransaction(api, alice, txDestroyCollection), 'destroying collection')168 .to.be.rejectedWith(/common\.CollectionIsExternal/);169170 171172 const txAddAllowList = api.tx.unique.addToAllowList(uniqueCollectionId, normalizeAccountId(bob));173 await expect(executeTransaction(api, alice, txAddAllowList), 'adding to allow list')174 .to.be.rejectedWith(/common\.CollectionIsExternal/);175176 const txRemoveAllowList = api.tx.unique.removeFromAllowList(uniqueCollectionId, normalizeAccountId(bob));177 await expect(executeTransaction(api, alice, txRemoveAllowList), 'removing from allowlist')178 .to.be.rejectedWith(/common\.CollectionIsExternal/);179180 181182 const txChangeOwner = api.tx.unique.changeCollectionOwner(uniqueCollectionId, bob.address);183 await expect(executeTransaction(api, alice, txChangeOwner), 'changing owner')184 .to.be.rejectedWith(/common\.CollectionIsExternal/);185186 const txAddAdmin = api.tx.unique.addCollectionAdmin(uniqueCollectionId, normalizeAccountId(bob));187 await expect(executeTransaction(api, alice, txAddAdmin), 'adding admin')188 .to.be.rejectedWith(/common\.CollectionIsExternal/);189190 const txRemoveAdmin = api.tx.unique.removeCollectionAdmin(uniqueCollectionId, normalizeAccountId(bob));191 await expect(executeTransaction(api, alice, txRemoveAdmin), 'removing admin')192 .to.be.rejectedWith(/common\.CollectionIsExternal/);193194 const txAddCollectionSponsor = api.tx.unique.setCollectionSponsor(uniqueCollectionId, bob.address);195 await expect(executeTransaction(api, alice, txAddCollectionSponsor), 'setting sponsor')196 .to.be.rejectedWith(/common\.CollectionIsExternal/);197198 const txConfirmCollectionSponsor = api.tx.unique.confirmSponsorship(uniqueCollectionId);199 await expect(executeTransaction(api, alice, txConfirmCollectionSponsor), 'confirming sponsor')200 .to.be.rejectedWith(/common\.CollectionIsExternal/);201202 const txRemoveCollectionSponsor = api.tx.unique.removeCollectionSponsor(uniqueCollectionId);203 await expect(executeTransaction(api, alice, txRemoveCollectionSponsor), 'removing sponsor')204 .to.be.rejectedWith(/common\.CollectionIsExternal/);205 206 207208 const txSetTransfers = api.tx.unique.setTransfersEnabledFlag(uniqueCollectionId, true);209 await expect(executeTransaction(api, alice, txSetTransfers), 'setting transfers enabled flag')210 .to.be.rejectedWith(/common\.CollectionIsExternal/);211212 const txSetLimits = api.tx.unique.setCollectionLimits(uniqueCollectionId, {transfersEnabled: false});213 await expect(executeTransaction(api, alice, txSetLimits), 'setting collection limits')214 .to.be.rejectedWith(/common\.CollectionIsExternal/);215216 const txSetPermissions = api.tx.unique.setCollectionPermissions(uniqueCollectionId, {access: 'AllowList'});217 await expect(executeTransaction(api, alice, txSetPermissions), 'setting collection permissions')218 .to.be.rejectedWith(/common\.CollectionIsExternal/);219 });220 });221});222223describe('Negative Integration Test: Internal Collections, External Ops', async () => {224 let collectionId: number;225 let nftId: number;226227 const it_rmrk = (await isUnique() ? it : it.skip);228229 before(async () => {230 await usingApi(async (api, privateKeyWrapper) => {231 alice = privateKeyWrapper('//Alice');232 bob = privateKeyWrapper('//Bob');233234 collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});235 nftId = await createItemExpectSuccess(alice, collectionId, 'NFT');236 });237 });238239 it_rmrk('[Negative] Forbids RMRK operations with the internal collection and NFT (due to the lack of mapping)', async () => {240 await usingApi(async api => {241 const txChangeOwner = api.tx.rmrkCore.changeCollectionIssuer(collectionId, bob.address);242 await expect(executeTransaction(api, alice, txChangeOwner), 'changing collection issuer')243 .to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);244245 const maxBurns = 10;246 const txBurnItem = api.tx.rmrkCore.burnNft(collectionId, nftId, maxBurns);247 await expect(executeTransaction(api, alice, txBurnItem), 'burning NFT').to.be.rejectedWith(/rmrkCore\.CollectionUnknown/);248 });249 });250});