From 81a57542f2f28bc114985340aed4143dd3fc6509 Mon Sep 17 00:00:00 2001 From: Fahrrader Date: Wed, 08 Jun 2022 14:45:42 +0000 Subject: [PATCH] test(rmrk-proxy): sample externality test --- --- a/tests/package.json +++ b/tests/package.json @@ -37,6 +37,7 @@ "testStructure": "mocha --timeout 9999999 -r ts-node/register ./**/nesting/**.test.ts", "testProperties": "mocha --timeout 9999999 -r ts-node/register ./**/properties.test.ts", "testMigrationStructure": "mocha --timeout 9999999 -r ts-node/register ./**/nesting/migration-check.test.ts", + "testRmrk": "mocha --timeout 9999999 -r ts-node/register ./**/rmrk/**.test.ts", "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts", "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts", "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts", --- /dev/null +++ b/tests/src/rmrk/rmrk.test.ts @@ -0,0 +1,64 @@ +import {expect} from 'chai'; +import privateKey from '../substrate/privateKey'; +import usingApi, {executeTransaction} from '../substrate/substrate-api'; +import { + getCreateCollectionResult, + getDetailedCollectionInfo, + getGenericResult, +} from '../util/helpers'; +import {IKeyringPair} from '@polkadot/types/types'; + +let alice: IKeyringPair; +let bob: IKeyringPair; + +describe('RMRK External Integration Test', () => { + before(async () => { + await usingApi(async () => { + alice = privateKey('//Alice'); + bob = privateKey('//Bob'); + }); + }); + + it('Creates a new RMRK collection that is tagged as external', async () => { + await usingApi(async api => { + const tx = api.tx.rmrkCore.createCollection('no-limit-metadata', null, 'no-limit-symbol'); + const events = await executeTransaction(api, alice, tx); + const result = getCreateCollectionResult(events); + + const collection = (await getDetailedCollectionInfo(api, result.collectionId))!; + expect(collection.readOnly.toHuman()).to.be.true; + }); + }); + + it('[Negative] Forbids NFT operations with an external collection', async () => { + await usingApi(async api => { + const tx1 = api.tx.rmrkCore.createCollection('metadata', null, 'symbol'); + const events1 = await executeTransaction(api, alice, tx1); + + const resultUnique1 = getCreateCollectionResult(events1); + const uniqueCollectionId = resultUnique1.collectionId; + + const resultRmrk1 = getGenericResult(events1, 'rmrkCore', 'CollectionCreated', (data) => { + return parseInt(data[1].toString(), 10); + }); + const rmrkCollectionId: number = resultRmrk1.data!; + + const tx2 = api.tx.rmrkCore.mintNft( + alice.address, + rmrkCollectionId, + alice.address, + null, + 'nft-metadata', + true, + ); + const events2 = await executeTransaction(api, alice, tx2); + const result2 = getGenericResult(events2, 'rmrkCore', 'NftMinted', (data) => { + return parseInt(data[2].toString(), 10); + }); + const rmrkNftId: number = result2.data!; + + const tx3 = api.tx.unique.burnItem(uniqueCollectionId, rmrkNftId, 1); + await expect(executeTransaction(api, alice, tx3)).to.be.rejectedWith(/common\.CollectionIsExternal/); + }); + }); +}); \ No newline at end of file --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -18,6 +18,7 @@ import '../interfaces/augment-api-query'; import {ApiPromise, Keyring} from '@polkadot/api'; import type {AccountId, EventRecord, Event} from '@polkadot/types/interfaces'; +import type {GenericEventData} from '@polkadot/types'; import {AnyTuple, IEvent, IKeyringPair} from '@polkadot/types/types'; import {evmToAddress} from '@polkadot/util-crypto'; import BN from 'bn.js'; @@ -88,9 +89,10 @@ const CENTIUNIQUE = 10n * MILLIUNIQUE; export const UNIQUE = 100n * CENTIUNIQUE; -type GenericResult = { - success: boolean, -}; +interface GenericResult { + success: boolean; + data: T | null; +} interface CreateCollectionResult { success: boolean; @@ -170,20 +172,27 @@ return event.event as T; } -export function getGenericResult(events: EventRecord[]): GenericResult { - const result: GenericResult = { - success: false, - }; - events.forEach(({event: {method}}) => { - // console.log(` ${phase}: ${section}.${method}:: ${data}`); +export function getGenericResult( + events: EventRecord[], + expectSection?: string, + expectMethod?: string, + extractAction?: (data: GenericEventData) => T, +): GenericResult { + let success = false; + let successData = null; + events.forEach(({event: {data, method, section}}) => { if (method === 'ExtrinsicSuccess') { - result.success = true; + success = true; + } else if ((expectSection == section) && (expectMethod == method)) { + successData = extractAction!(data); } }); + const result: GenericResult = { + success, + data: successData, + }; return result; } - - export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult { let success = false; -- gitstuff