difftreelog
approve and transferFrom test scenarios
in: master
6 files changed
tests/package.jsondiffbeforeafterboth21 "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",21 "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",22 "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",22 "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",23 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",23 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",24 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"24 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",25 "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",26 "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",27 "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"25 },28 },26 "author": "",29 "author": "",27 "license": "Apache 2.0",30 "license": "Apache 2.0",tests/src/approve.test.tsdiffbeforeafterboth9import privateKey from './substrate/privateKey';9import privateKey from './substrate/privateKey';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';11import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';11import {12 approveExpectFail,13 approveExpectSuccess,14 createCollectionExpectSuccess,15 createItemExpectSuccess,16 destroyCollectionExpectSuccess,17} from './util/helpers';121813chai.use(chaiAsPromised);19chai.use(chaiAsPromised);14const expect = chai.expect;20const expect = chai.expect;1516interface ITokenDataType {17 Owner: number[];18 ConstData: number[];19 VariableData: number[];20}212122describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {22describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {23 it('Execute the extrinsic and check approvedList', async () => {23 it('Execute the extrinsic and check approvedList', async () => {24 await usingApi(async (api: ApiPromise) => {24 await usingApi(async (api: ApiPromise) => {2525 const Alice = privateKey('//Alice');26 const Bob = privateKey('//Bob');27 const nftCollectionId = await createCollectionExpectSuccess();28 // nft29 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');30 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);31 // fungible32 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});33 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');34 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);35 // reFungible36 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});37 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');38 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);3940 // garbage collection :-D41 await destroyCollectionExpectSuccess(nftCollectionId);42 await destroyCollectionExpectSuccess(fungibleCollectionId);43 await destroyCollectionExpectSuccess(reFungibleCollectionId);26 });44 });27 });45 });4647 it('Remove approval by using 0 amount', async () => {48 await usingApi(async (api: ApiPromise) => {49 const Alice = privateKey('//Alice');50 const Bob = privateKey('//Bob');51 const nftCollectionId = await createCollectionExpectSuccess();52 // nft53 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');54 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);55 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);56 // fungible57 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});58 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');59 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);60 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);61 // reFungible62 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});63 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');64 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);65 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);6667 // garbage collection :-D68 await destroyCollectionExpectSuccess(nftCollectionId);69 await destroyCollectionExpectSuccess(fungibleCollectionId);70 await destroyCollectionExpectSuccess(reFungibleCollectionId);71 });72 });28});73});297430describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {75describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {31 it('Approve for a collection that does not exist', async () => {76 it('Approve for a collection that does not exist', async () => {32 await usingApi(async (api: ApiPromise) => {77 await usingApi(async (api: ApiPromise) => {3378 const Alice = privateKey('//Alice');79 const Bob = privateKey('//Bob');80 // nft81 const nftCollectionId = await createCollectionExpectSuccess();82 await approveExpectFail(nftCollectionId + 1, 1, Alice, Bob);83 // fungible84 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});85 await approveExpectFail(fungibleCollectionId + 1, 1, Alice, Bob);86 // reFungible87 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});88 await approveExpectFail(reFungibleCollectionId + 1, 1, Alice, Bob);89 // garbage collection :-D90 await destroyCollectionExpectSuccess(nftCollectionId);91 await destroyCollectionExpectSuccess(fungibleCollectionId);92 await destroyCollectionExpectSuccess(reFungibleCollectionId);34 });93 });35 });94 });369537 it('Approve for a collection that was destroyed', async () => {96 it('Approve for a collection that was destroyed', async () => {38 await usingApi(async (api: ApiPromise) => {97 await usingApi(async (api: ApiPromise) => {3998 const Alice = privateKey('//Alice');99 const Bob = privateKey('//Bob');100 // nft101 const nftCollectionId = await createCollectionExpectSuccess();102 await destroyCollectionExpectSuccess(nftCollectionId);103 await approveExpectFail(nftCollectionId, 1, Alice, Bob);104 // fungible105 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});106 await destroyCollectionExpectSuccess(fungibleCollectionId);107 await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);108 // reFungible109 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});110 await destroyCollectionExpectSuccess(reFungibleCollectionId);111 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);40 });112 });41 });113 });4211443 it('Approve transfer of a token that does not exist', async () => {115 it('Approve transfer of a token that does not exist', async () => {44 await usingApi(async (api: ApiPromise) => {116 await usingApi(async (api: ApiPromise) => {45117 const Alice = privateKey('//Alice');118 const Bob = privateKey('//Bob');119 // nft120 const nftCollectionId = await createCollectionExpectSuccess();121 await approveExpectFail(nftCollectionId, 2, Alice, Bob);122 // fungible123 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});124 await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);125 // reFungible126 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});127 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);128 // garbage collection :-D129 await destroyCollectionExpectSuccess(nftCollectionId);130 await destroyCollectionExpectSuccess(fungibleCollectionId);131 await destroyCollectionExpectSuccess(reFungibleCollectionId);46 });132 });47 });133 });4813449 it('Approve using the address that does not own the approved token', async () => {135 it('Approve using the address that does not own the approved token', async () => {50 await usingApi(async (api: ApiPromise) => {136 await usingApi(async (api: ApiPromise) => {51137 const Alice = privateKey('//Alice');138 const Bob = privateKey('//Bob');139 const nftCollectionId = await createCollectionExpectSuccess();140 // nft141 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');142 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);143 // fungible144 const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});145 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');146 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);147 // reFungible148 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});149 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');150 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);151152 // garbage collection :-D153 await destroyCollectionExpectSuccess(nftCollectionId);154 await destroyCollectionExpectSuccess(fungibleCollectionId);155 await destroyCollectionExpectSuccess(reFungibleCollectionId);52 });156 });53 });157 });5455 it('Remove approval by using 0 amount', async () => {56 await usingApi(async (api: ApiPromise) => {5758 });59 });60});158});61159tests/src/createCollection.test.tsdiffbeforeafterboth5
5
6import chai from 'chai';
6import chai from 'chai';
7import chaiAsPromised from 'chai-as-promised';
7import chaiAsPromised from 'chai-as-promised';
8import { default as usingApi } from "./substrate/substrate-api";
8import { default as usingApi } from './substrate/substrate-api';
9import { createCollectionExpectSuccess, createCollectionExpectFailure, CollectionMode } from "./util/helpers";
9import { createCollectionExpectFailure, createCollectionExpectSuccess } from './util/helpers';
10
10
11chai.use(chaiAsPromised);
11chai.use(chaiAsPromised);
12const expect = chai.expect;
12const expect = chai.expect;
13
13
14describe('integration test: ext. createCollection():', () => {
14describe('integration test: ext. createCollection():', () => {
15 it('Create new NFT collection', async () => {
15 it('Create new NFT collection', async () => {
16 await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
16 await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
17 });
17 });
18 it('Create new NFT collection whith collection_name of maximum length (64 bytes)', async () => {
18 it('Create new NFT collection whith collection_name of maximum length (64 bytes)', async () => {
19 await createCollectionExpectSuccess({name: 'A'.repeat(64)});
19 await createCollectionExpectSuccess({name: 'A'.repeat(64)});
25 await createCollectionExpectSuccess({tokenPrefix: 'A'.repeat(16)});
25 await createCollectionExpectSuccess({tokenPrefix: 'A'.repeat(16)});
26 });
26 });
27 it('Create new Fungible collection', async () => {
27 it('Create new Fungible collection', async () => {
28 await createCollectionExpectSuccess({mode: 'Fungible'});
28 await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
29 });
29 });
30 it('Create new ReFungible collection', async () => {
30 it('Create new ReFungible collection', async () => {
31 await createCollectionExpectSuccess({mode: 'ReFungible'});
31 await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
32 });
32 });
33});
33});
34
34
35describe('(!negative test!) integration test: ext. createCollection():', () => {
35describe('(!negative test!) integration test: ext. createCollection():', () => {
36 it('(!negative test!) create new NFT collection whith incorrect data (mode)', async () => {
36 it('(!negative test!) create new NFT collection whith incorrect data (mode)', async () => {
37 await usingApi(async (api) => {
37 await usingApi(async (api) => {
38 const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
38 const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
39
39
40 const badTransaction = async function () {
40 const badTransaction = async () => await createCollectionExpectSuccess({mode: {type: 'Invalid'}});
41 await createCollectionExpectSuccess({mode: 'BadMode' as CollectionMode});
41 // tslint:disable-next-line:no-unused-expression
42 };
43 expect(badTransaction()).to.be.rejected;
42 expect(badTransaction()).to.be.rejected;
44
43
45 const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
44 const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
46 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Incorrect collection created.');
45 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Incorrect collection created.');
47 });
46 });
48 });
47 });
49 it('(!negative test!) create new NFT collection whith incorrect data (collection_name)', async () => {
48 it('(!negative test!) create new NFT collection whith incorrect data (collection_name)', async () => {
50 await createCollectionExpectFailure({name: 'A'.repeat(65)});
49 await createCollectionExpectFailure({ name: 'A'.repeat(65), mode: {type: 'NFT'}});
51 });
50 });
52 it('(!negative test!) create new NFT collection whith incorrect data (collection_description)', async () => {
51 it('(!negative test!) create new NFT collection whith incorrect data (collection_description)', async () => {
53 await createCollectionExpectFailure({description: 'A'.repeat(257)});
52 await createCollectionExpectFailure({ description: 'A'.repeat(257), mode: { type: 'NFT' }});
54 });
53 });
55 it('(!negative test!) create new NFT collection whith incorrect data (token_prefix)', async () => {
54 it('(!negative test!) create new NFT collection whith incorrect data (token_prefix)', async () => {
56 await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17)});
55 await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17), mode: {type: 'NFT'}});
57 });
56 });
58});
57});
5958tests/src/substrate/substrate-api.tsdiffbeforeafterboth104 await transaction.signAndSend(sender, ({ events = [], status }) => {103 await transaction.signAndSend(sender, ({ events = [], status }) => {105 const transactionStatus = getTransactionStatus(events, status);104 const transactionStatus = getTransactionStatus(events, status);105106 console.log('transactionStatus', transactionStatus, 'events', events);106107107 if (transactionStatus == TransactionStatus.Success) {108 if (transactionStatus == TransactionStatus.Success) {108 resolve(events);109 resolve(events);tests/src/transferFrom.test.tsdiffbeforeafterboth9import privateKey from './substrate/privateKey';9import privateKey from './substrate/privateKey';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';11import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';11import {12 approveExpectSuccess,13 createCollectionExpectSuccess,14 createItemExpectSuccess,15 destroyCollectionExpectSuccess,16 transferFromExpectSuccess,17} from './util/helpers';18import {IKeyringPair} from "@polkadot/types/types";121913chai.use(chaiAsPromised);20chai.use(chaiAsPromised);14const expect = chai.expect;21const expect = chai.expect;1516interface ITokenDataType {17 Owner: number[];18 ConstData: number[];19 VariableData: number[];20}212222describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {23describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {23 it('Execute the extrinsic and check nftItemList - owner of toren', async () => {24 it('Execute the extrinsic and check nftItemList - owner of token', async () => {24 await usingApi(async (api: ApiPromise) => {25 await usingApi(async (api: ApiPromise) => {2526 const Alice = privateKey('//Alice');27 const Bob = privateKey('//Bob');28 const Charlie = privateKey('//CHARLIE');29 const nftCollectionId = await createCollectionExpectSuccess();30 console.log('nftCollectionId', nftCollectionId);31 const collectionInfo = await api.query.nft.collection(nftCollectionId);32 console.log('collectionInfo', collectionInfo);33 // nft34 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');35 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);3637 await transferFromExpectSuccess(nftCollectionId, newNftTokenId, Alice, Charlie, 1, 'NFT');3839 // fungible40 /*const fungibleCollectionId = await createCollectionExpectSuccess({mode: 'Fungible'});41 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');42 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);43 // reFungible44 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});45 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');46 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);*/4748 // garbage collection :-D49 await destroyCollectionExpectSuccess(nftCollectionId);50 // await destroyCollectionExpectSuccess(fungibleCollectionId);51 // await destroyCollectionExpectSuccess(reFungibleCollectionId);26 });52 });27 });53 });28});54});tests/src/util/helpers.tsdiffbeforeafterboth3// file 'LICENSE', which is part of this source code package.3// file 'LICENSE', which is part of this source code package.4//4//556import chai from 'chai';6import { ApiPromise, Keyring } from '@polkadot/api';7import chaiAsPromised from 'chai-as-promised';7import { Enum, Struct } from '@polkadot/types/codec';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { ApiPromise, Keyring } from "@polkadot/api";9import { u128 } from '@polkadot/types/primitive';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";10import { IKeyringPair } from '@polkadot/types/types';11import privateKey from '../substrate/privateKey';11import { BigNumber } from 'bignumber.js';12import { alicesPublicKey, nullPublicKey } from "../accounts";12import BN from 'bn.js';13import { strToUTF16, utf16ToStr, hexToStr } from './util';13import chai from 'chai';14import { IKeyringPair } from '@polkadot/types/types';14import chaiAsPromised from 'chai-as-promised';15import { BigNumber } from 'bignumber.js';15import { alicesPublicKey, nullPublicKey } from '../accounts';16import { Struct, Enum } from '@polkadot/types/codec';16import privateKey from '../substrate/privateKey';17import { u128 } from '@polkadot/types/primitive';17import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from '../substrate/substrate-api';18import { ICollectionInterface } from '../types';18import { ICollectionInterface } from '../types';19import BN from "bn.js";19import { hexToStr, strToUTF16, utf16ToStr } from './util';202021chai.use(chaiAsPromised);21chai.use(chaiAsPromised);22const expect = chai.expect;22const expect = chai.expect;25 success: boolean,25 success: boolean,26};26};272728type CreateCollectionResult = {28interface CreateCollectionResult {29 success: boolean,29 success: boolean;30 collectionId: number30 collectionId: number;31};31}323233type CreateItemResult = {33interface CreateItemResult {34 success: boolean,34 success: boolean;35 collectionId: number,35 collectionId: number;36 itemId: number36 itemId: number;37};37}3839interface ITokenDataType {40 Owner: number[];41 ConstData: number[];42 VariableData: number[];43}384439export function getGenericResult(events: EventRecord[]): GenericResult {45export function getGenericResult(events: EventRecord[]): GenericResult {40 let result: GenericResult = {46 const result: GenericResult = {41 success: false47 success: false,42 }48 };43 events.forEach(({ phase, event: { data, method, section } }) => {49 events.forEach(({ phase, event: { data, method, section } }) => {44 // console.log(` ${phase}: ${section}.${method}:: ${data}`);50 // console.log(` ${phase}: ${section}.${method}:: ${data}`);45 if (method == 'ExtrinsicSuccess') {51 if (method === 'ExtrinsicSuccess') {46 result.success = true;52 result.success = true;47 }53 }48 });54 });60 collectionId = parseInt(data[0].toString());66 collectionId = parseInt(data[0].toString());61 }67 }62 });68 });63 let result: CreateCollectionResult = {69 const result: CreateCollectionResult = {64 success,70 success,65 collectionId71 collectionId,66 }72 };67 return result;73 return result;68}74}697580 itemId = parseInt(data[1].toString());86 itemId = parseInt(data[1].toString());81 }87 }82 });88 });83 let result: CreateItemResult = {89 const result: CreateItemResult = {84 success,90 success,85 collectionId,91 collectionId,86 itemId92 itemId,87 }93 };88 return result;94 return result;89}95}9697interface Invalid {98 type: 'Invalid';99}100101interface Nft {102 type: 'NFT';103}104105interface Fungible {106 type: 'Fungible';107 decimalPoints: number;108}109110interface ReFungible {111 type: 'ReFungible';112 decimalPoints: number;113}9011491export type CollectionMode = 'NFT' | 'Fungible' | 'ReFungible';115type CollectionMode = Nft | Fungible | ReFungible | Invalid;11692export type CreateCollectionParams = {117export type CreateCollectionParams = {93 mode: CollectionMode,118 mode: CollectionMode,94 name: string,119 name: string,95 description: string,120 description: string,96 tokenPrefix: string121 tokenPrefix: string,97};122};9812399const defaultCreateCollectionParams: CreateCollectionParams = {124const defaultCreateCollectionParams: CreateCollectionParams = {100 name: 'name',101 description: 'description',125 description: 'description',102 mode: 'NFT',126 mode: { type: 'NFT' },127 name: 'name',103 tokenPrefix: 'prefix'128 tokenPrefix: 'prefix',104}129}105130106export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {131export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {109 let collectionId: number = 0;134 let collectionId: number = 0;110 await usingApi(async (api) => {135 await usingApi(async (api) => {111 // Get number of collections before the transaction136 // Get number of collections before the transaction112 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());137 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);113138114 // Run the CreateCollection transaction139 // Run the CreateCollection transaction115 const alicePrivateKey = privateKey('//Alice');140 const alicePrivateKey = privateKey('//Alice');141142 let modeprm = {};143 if (mode.type === 'NFT') {144 modeprm = {nft: null};145 } else if (mode.type === 'Fungible') {146 modeprm = {fungible: mode.decimalPoints};147 } else if (mode.type === 'ReFungible') {148 modeprm = {refungible: mode.decimalPoints};149 } else if (mode.type === 'Invalid') {150 modeprm = {invalid: null};151 }152116 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);153 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);117 const events = await submitTransactionAsync(alicePrivateKey, tx);154 const events = await submitTransactionAsync(alicePrivateKey, tx);118 const result = getCreateCollectionResult(events);155 const result = getCreateCollectionResult(events);119156120 // Get number of collections after the transaction157 // Get number of collections after the transaction121 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());158 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);122159123 // Get the collection 160 // Get the collection124 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();161 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();125162126 // What to expect163 // What to expect164 // tslint:disable-next-line:no-unused-expression127 expect(result.success).to.be.true;165 expect(result.success).to.be.true;128 expect(result.collectionId).to.be.equal(BcollectionCount);166 expect(result.collectionId).to.be.equal(BcollectionCount);167 // tslint:disable-next-line:no-unused-expression129 expect(collection).to.be.not.null;168 expect(collection).to.be.not.null;130 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');169 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');131 expect(collection.Owner).to.be.equal(alicesPublicKey);170 expect(collection.Owner).to.be.equal(alicesPublicKey);142export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {181export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {143 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};182 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};183184 let modeprm = {};185 if (mode.type === 'NFT') {186 modeprm = {nft: null};187 } else if (mode.type === 'Fungible') {188 modeprm = {fungible: mode.decimalPoints};189 } else if (mode.type === 'ReFungible') {190 modeprm = {refungible: mode.decimalPoints};191 } else if (mode.type === 'Invalid') {192 modeprm = {invalid: null};193 }144194145 await usingApi(async (api) => {195 await usingApi(async (api) => {146 // Get number of collections before the transaction196 // Get number of collections before the transaction147 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());197 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());148198149 // Run the CreateCollection transaction199 // Run the CreateCollection transaction150 const alicePrivateKey = privateKey('//Alice');200 const alicePrivateKey = privateKey('//Alice');151 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);201 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);152 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;202 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;153 const result = getCreateCollectionResult(events);203 const result = getCreateCollectionResult(events);154204155 // Get number of collections after the transaction205 // Get number of collections after the transaction156 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());206 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());157207158 // What to expect208 // What to expect209 // tslint:disable-next-line:no-unused-expression159 expect(result.success).to.be.false;210 expect(result.success).to.be.false;160 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');211 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');161 });212 });201 const events = await submitTransactionAsync(alicePrivateKey, tx);252 const events = await submitTransactionAsync(alicePrivateKey, tx);202 const result = getDestroyResult(events);253 const result = getDestroyResult(events);203254204 // Get the collection 255 // Get the collection205 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();256 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();206257207 // What to expect258 // What to expect220 const events = await submitTransactionAsync(alicePrivateKey, tx);271 const events = await submitTransactionAsync(alicePrivateKey, tx);221 const result = getGenericResult(events);272 const result = getGenericResult(events);222273223 // Get the collection 274 // Get the collection224 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();275 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();225276226 // What to expect277 // What to expect239 const events = await submitTransactionAsync(alicePrivateKey, tx);290 const events = await submitTransactionAsync(alicePrivateKey, tx);240 const result = getGenericResult(events);291 const result = getGenericResult(events);241292242 // Get the collection 293 // Get the collection243 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();294 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();244295245 // What to expect296 // What to expect278 const events = await submitTransactionAsync(sender, tx);329 const events = await submitTransactionAsync(sender, tx);279 const result = getGenericResult(events);330 const result = getGenericResult(events);280331281 // Get the collection 332 // Get the collection282 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();333 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();283334284 // What to expect335 // What to expect300351301export interface CreateFungibleData extends Struct {352export interface CreateFungibleData extends Struct {302 readonly value: u128;353 readonly value: u128;303};354}304355305export interface CreateReFungibleData extends Struct {};356export interface CreateReFungibleData extends Struct {}306export interface CreateNftData extends Struct {};357export interface CreateNftData extends Struct {}307358308export interface CreateItemData extends Enum {359export interface CreateItemData extends Enum {309 NFT: CreateNftData,360 NFT: CreateNftData;310 Fungible: CreateFungibleData,361 Fungible: CreateFungibleData;311 ReFungible: CreateReFungibleData362 ReFungible: CreateReFungibleData;312};363}364365export async function366approveExpectSuccess(collectionId: number,367 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {368 await usingApi(async (api: ApiPromise) => {369 const allowanceBefore =370 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;371 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);372 const events = await submitTransactionAsync(owner, approveNftTx);373 const result = getCreateItemResult(events);374 // tslint:disable-next-line:no-unused-expression375 expect(result.success).to.be.true;376 const allowanceAfter =377 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;378 expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);379 });380}381382export async function383transferFromExpectSuccess(collectionId: number, tokenId: number, accountFrom: IKeyringPair,384 accountTo: IKeyringPair, value: number = 1, type: string = 'NFT') {385 await usingApi(async (api: ApiPromise) => {386 const transferFromTx = await api.tx.nft.transferFrom(387 accountFrom.address, accountTo.address, collectionId, tokenId, value);388 const events = await submitTransactionAsync(accountFrom, transferFromTx);389 const result = getCreateItemResult(events);390 // tslint:disable-next-line:no-unused-expression391 expect(result.success).to.be.true;392 if (type === 'NFT') {393 const nftItemData = api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;394 console.log('NFT data', nftItemData);395 }396 if (type === 'Fungible') {397 const nftItemData = api.query.nft.fungibleItemList(collectionId, tokenId) as unknown as ITokenDataType;398 console.log('Fungible data', nftItemData);399 }400 if (type === 'ReFungible') {401 const nftItemData = api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as ITokenDataType;402 console.log('reFungibleItemList data', nftItemData);403 }404 });405}406407export async function408approveExpectFail(collectionId: number,409 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {410 await usingApi(async (api: ApiPromise) => {411 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);412 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;413 const result = getCreateCollectionResult(events);414 // tslint:disable-next-line:no-unused-expression415 expect(result.success).to.be.false;416 });417}313418314export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {419export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {315 let newItemId: number = 0;420 let newItemId: number = 0;316 await usingApi(async (api) => {421 await usingApi(async (api) => {317 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());422 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);318 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 423 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();319 const AItemBalance = new BigNumber(Aitem.Value);424 const AItemBalance = new BigNumber(Aitem.Value);320425321 if (owner === '') owner = sender.address;426 if (owner === '') {427 owner = sender.address;428 }322429323 let tx;430 let tx;324 if (createMode == 'Fungible') {431 if (createMode === 'Fungible') {325 let createData = {fungible: {value: 10}};432 const createData = {fungible: {value: 10}};326 tx = api.tx.nft.createItem(collectionId, owner, createData);433 tx = api.tx.nft.createItem(collectionId, owner, createData);327 }434 } else {328 else {329 tx = api.tx.nft.createItem(collectionId, owner, createMode);435 tx = api.tx.nft.createItem(collectionId, owner, createMode);330 }436 }331 const events = await submitTransactionAsync(sender, tx);437 const events = await submitTransactionAsync(sender, tx);332 const result = getCreateItemResult(events);438 const result = getCreateItemResult(events);333439334 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());440 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);335 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 441 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();336 const BItemBalance = new BigNumber(Bitem.Value);442 const BItemBalance = new BigNumber(Bitem.Value);337443338 // What to expect444 // What to expect445 // tslint:disable-next-line:no-unused-expression339 expect(result.success).to.be.true;446 expect(result.success).to.be.true;340 if (createMode == 'Fungible') {447 if (createMode === 'Fungible') {341 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);448 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);342 }449 } else {343 else {358 const events = await submitTransactionAsync(sender, tx);464 const events = await submitTransactionAsync(sender, tx);359 const result = getGenericResult(events);465 const result = getGenericResult(events);360466361 // Get the collection 467 // Get the collection362 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();468 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();363469364 // What to expect470 // What to expect375 const events = await submitTransactionAsync(sender, tx);481 const events = await submitTransactionAsync(sender, tx);376 const result = getGenericResult(events);482 const result = getGenericResult(events);377483378 // Get the collection 484 // Get the collection379 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();485 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();380486381 // What to expect487 // What to expect392 const events = await submitTransactionAsync(sender, tx);498 const events = await submitTransactionAsync(sender, tx);393 const result = getGenericResult(events);499 const result = getGenericResult(events);394500395 // Get the collection 501 // Get the collection396 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();502 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();397503398 // What to expect504 // What to expect