git.delta.rocks / unique-network / refs/commits / 07025d803c3e

difftreelog

Merge pull request #63 from usetech-llc/feature/NFTPAR-235-258

Greg Zaitsev2021-01-18parents: #42edda1 #b05ce7a.patch.diff
in: master
Feature/nftpar 235 258

6 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -22,7 +22,10 @@
     "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
     "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",
     "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
-    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
+    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
+    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",
+    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",
+    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
   },
   "author": "",
   "license": "Apache 2.0",
addedtests/src/approve.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/approve.test.ts
@@ -0,0 +1,140 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+import { ApiPromise } from '@polkadot/api';
+import BN from 'bn.js';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import {
+  approveExpectFail,
+  approveExpectSuccess,
+  createCollectionExpectSuccess,
+  createItemExpectSuccess,
+  destroyCollectionExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {
+  it('Execute the extrinsic and check approvedList', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const nftCollectionId = await createCollectionExpectSuccess();
+      // nft
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+      // reFungible
+      const reFungibleCollectionId =
+        await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+    });
+  });
+
+  it('Remove approval by using 0 amount', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const nftCollectionId = await createCollectionExpectSuccess();
+      // nft
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);
+      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);
+      // reFungible
+      const reFungibleCollectionId =
+        await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);
+      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);
+    });
+  });
+});
+
+describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {
+  it('Approve for a collection that does not exist', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      // nft
+      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);
+      // fungible
+      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);
+      // reFungible
+      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);
+    });
+  });
+
+  it('Approve for a collection that was destroyed', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      await destroyCollectionExpectSuccess(nftCollectionId);
+      await approveExpectFail(nftCollectionId, 1, Alice, Bob);
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      await destroyCollectionExpectSuccess(fungibleCollectionId);
+      await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);
+      // reFungible
+      const reFungibleCollectionId =
+        await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      await destroyCollectionExpectSuccess(reFungibleCollectionId);
+      await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);
+    });
+  });
+
+  it('Approve transfer of a token that does not exist', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      await approveExpectFail(nftCollectionId, 2, Alice, Bob);
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);
+      // reFungible
+      const reFungibleCollectionId =
+        await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);
+    });
+  });
+
+  it('Approve using the address that does not own the approved token', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const nftCollectionId = await createCollectionExpectSuccess();
+      // nft
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);
+      // reFungible
+      const reFungibleCollectionId =
+        await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);
+    });
+  });
+});
modifiedtests/src/createCollection.test.tsdiffbeforeafterboth
--- a/tests/src/createCollection.test.ts
+++ b/tests/src/createCollection.test.ts
@@ -5,8 +5,8 @@
 
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
-import { default as usingApi } from "./substrate/substrate-api";
-import { createCollectionExpectSuccess, createCollectionExpectFailure } from "./util/helpers";
+import { default as usingApi } from './substrate/substrate-api';
+import { createCollectionExpectFailure, createCollectionExpectSuccess } from './util/helpers';
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -35,24 +35,25 @@
 describe('(!negative test!) integration test: ext. createCollection():', () => {
   it('(!negative test!) create new NFT collection whith incorrect data (mode)', async () => {
     await usingApi(async (api) => {
-      const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
+      const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
 
-      const badTransaction = async function () { 
+      const badTransaction = async () => {
         await createCollectionExpectSuccess({mode: {type: 'Invalid'}});
       };
+      // tslint:disable-next-line:no-unused-expression
       expect(badTransaction()).to.be.rejected;
 
-      const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
+      const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
       expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Incorrect collection created.');
     });
   });
   it('(!negative test!) create new NFT collection whith incorrect data (collection_name)', async () => {
-    await createCollectionExpectFailure({name: 'A'.repeat(65)});
+    await createCollectionExpectFailure({ name: 'A'.repeat(65), mode: {type: 'NFT'}});
   });
   it('(!negative test!) create new NFT collection whith incorrect data (collection_description)', async () => {
-    await createCollectionExpectFailure({description: 'A'.repeat(257)});
+    await createCollectionExpectFailure({ description: 'A'.repeat(257), mode: { type: 'NFT' }});
   });
   it('(!negative test!) create new NFT collection whith incorrect data (token_prefix)', async () => {
-    await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17)});
+    await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17), mode: {type: 'NFT'}});
   });
 });
modifiedtests/src/substrate/substrate-api.tsdiffbeforeafterboth
--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -90,7 +90,6 @@
         res(rec);
         console.error = consoleError;
         console.log = consoleLog;
-        
       });
     };
     const reject = (errror: any) => {
@@ -104,6 +103,8 @@
       await transaction.signAndSend(sender, ({ events = [], status }) => {
         const transactionStatus = getTransactionStatus(events, status);
 
+        console.log('transactionStatus', transactionStatus, 'events', events);
+
         if (transactionStatus == TransactionStatus.Success) {
           resolve(events);
         } else if (transactionStatus == TransactionStatus.Fail) {
@@ -114,4 +115,4 @@
       reject(e);
     }
   });
-}
\ No newline at end of file
+}
addedtests/src/transferFrom.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/transferFrom.test.ts
@@ -0,0 +1,188 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+import { ApiPromise } from '@polkadot/api';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import {
+  approveExpectFail,
+  approveExpectSuccess,
+  createCollectionExpectSuccess,
+  createItemExpectSuccess,
+  destroyCollectionExpectSuccess,
+  transferFromExpectFail,
+  transferFromExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
+  it('Execute the extrinsic and check nftItemList - owner of token', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+
+      await transferFromExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1, 'NFT');
+
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+      await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1, 'Fungible');
+      // reFungible
+      const reFungibleCollectionId = await
+        createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+      await transferFromExpectSuccess(reFungibleCollectionId,
+        newReFungibleTokenId, Bob, Alice, Charlie, 1, 'ReFungible');
+    });
+  });
+});
+
+describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
+  it('transferFrom for a collection that does not exist', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+      // nft
+      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);
+
+      await transferFromExpectFail(nftCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+
+      // fungible
+      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);
+
+      await transferFromExpectFail(fungibleCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+      // reFungible
+      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);
+
+      await transferFromExpectFail(reFungibleCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+    });
+  });
+
+  /* it('transferFrom for a collection that was destroyed', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      this test copies approve negative test
+    });
+  }); */
+
+  /* it('transferFrom a token that does not exist', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      this test copies approve negative test
+    });
+  }); */
+
+  /* it('transferFrom a token that was deleted', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      this test copies approve negative test
+    });
+  }); */
+
+  it('transferFrom for not approved address', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+
+      await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
+
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1);
+      // reFungible
+      const reFungibleCollectionId = await
+        createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await transferFromExpectFail(reFungibleCollectionId,
+        newReFungibleTokenId, Bob, Alice, Charlie, 1);
+    });
+  });
+
+  it('transferFrom incorrect token count', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+
+      await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 2);
+
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+      await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 2);
+      // reFungible
+      const reFungibleCollectionId = await
+        createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+      await transferFromExpectFail(reFungibleCollectionId,
+        newReFungibleTokenId, Bob, Alice, Charlie, 2);
+    });
+  });
+
+  it('execute transferFrom from account that is not owner of collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const Bob = privateKey('//Bob');
+      const Charlie = privateKey('//CHARLIE');
+      const Dave = privateKey('//DAVE');
+      // nft
+      const nftCollectionId = await createCollectionExpectSuccess();
+      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+      try {
+        await approveExpectFail(nftCollectionId, newNftTokenId, Dave, Bob);
+        await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+
+      // await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);
+
+      // fungible
+      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+      try {
+        await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Dave, Bob);
+        await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Dave, Alice, Charlie, 1);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+      // reFungible
+      const reFungibleCollectionId = await
+        createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+      try {
+        await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Dave, Bob);
+        await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Dave, Alice, Charlie, 1);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
3// file 'LICENSE', which is part of this source code package.3// file 'LICENSE', which is part of this source code package.
4//4//
55
6import 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';
2020
21chai.use(chaiAsPromised);21chai.use(chaiAsPromised);
22const expect = chai.expect;22const expect = chai.expect;
25 success: boolean,25 success: boolean,
26};26};
2727
28type CreateCollectionResult = {28interface CreateCollectionResult {
29 success: boolean,29 success: boolean;
30 collectionId: number30 collectionId: number;
31};31}
3232
33type CreateItemResult = {33interface CreateItemResult {
34 success: boolean,34 success: boolean;
35 collectionId: number,35 collectionId: number;
36 itemId: number36 itemId: number;
37};37}
38
39interface IReFungibleOwner {
40 Fraction: BN;
41 Owner: number[];
42}
43
44interface ITokenDataType {
45 Owner: number[];
46 ConstData: number[];
47 VariableData: number[];
48}
49
50interface IFungibleTokenDataType {
51 Value: BN;
52}
53
54interface IReFungibleTokenDataType {
55 Owner: IReFungibleOwner[];
56 ConstData: number[];
57 VariableData: number[];
58}
3859
39export function getGenericResult(events: EventRecord[]): GenericResult {60export function getGenericResult(events: EventRecord[]): GenericResult {
40 let result: GenericResult = {61 const result: GenericResult = {
41 success: false62 success: false,
42 }63 };
43 events.forEach(({ phase, event: { data, method, section } }) => {64 events.forEach(({ phase, event: { data, method, section } }) => {
44 // console.log(` ${phase}: ${section}.${method}:: ${data}`);65 // console.log(` ${phase}: ${section}.${method}:: ${data}`);
45 if (method == 'ExtrinsicSuccess') {66 if (method === 'ExtrinsicSuccess') {
46 result.success = true;67 result.success = true;
47 }68 }
48 });69 });
60 collectionId = parseInt(data[0].toString());81 collectionId = parseInt(data[0].toString());
61 }82 }
62 });83 });
63 let result: CreateCollectionResult = {84 const result: CreateCollectionResult = {
64 success,85 success,
65 collectionId86 collectionId,
66 }87 };
67 return result;88 return result;
68}89}
6990
80 itemId = parseInt(data[1].toString());101 itemId = parseInt(data[1].toString());
81 }102 }
82 });103 });
83 let result: CreateItemResult = {104 const result: CreateItemResult = {
84 success,105 success,
85 collectionId,106 collectionId,
86 itemId107 itemId,
87 }108 };
88 return result;109 return result;
89}110}
90111
91interface Invalid {112interface Invalid {
92 type: 'Invalid'113 type: 'Invalid';
93}114}
115
116interface Nft {
117 type: 'NFT';
118}
119
120interface Fungible {
121 type: 'Fungible';
122 decimalPoints: number;
123}
124
125interface ReFungible {
126 type: 'ReFungible';
127 decimalPoints: number;
128}
94129
95interface Nft {130interface Nft {
96 type: 'NFT'131 type: 'NFT'
112 mode: CollectionMode,147 mode: CollectionMode,
113 name: string,148 name: string,
114 description: string,149 description: string,
115 tokenPrefix: string150 tokenPrefix: string,
116};151};
117152
118const defaultCreateCollectionParams: CreateCollectionParams = {153const defaultCreateCollectionParams: CreateCollectionParams = {
119 name: 'name',
120 description: 'description',154 description: 'description',
121 mode: { type: "NFT" },155 mode: { type: 'NFT' },
156 name: 'name',
122 tokenPrefix: 'prefix'157 tokenPrefix: 'prefix',
123}158}
124159
125export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {160export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {
128 let collectionId: number = 0;163 let collectionId: number = 0;
129 await usingApi(async (api) => {164 await usingApi(async (api) => {
130 // Get number of collections before the transaction165 // Get number of collections before the transaction
131 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());166 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);
132167
133 // Run the CreateCollection transaction168 // Run the CreateCollection transaction
134 const alicePrivateKey = privateKey('//Alice');169 const alicePrivateKey = privateKey('//Alice');
135170
136 let modeprm = {};171 let modeprm = {};
137 if (mode.type == 'NFT') {172 if (mode.type === 'NFT') {
138 modeprm = {nft: null};173 modeprm = {nft: null};
139 }174 } else if (mode.type === 'Fungible') {
140 else if (mode.type == 'Fungible') {
141 modeprm = {fungible: mode.decimalPoints};175 modeprm = {fungible: mode.decimalPoints};
142 }176 } else if (mode.type === 'ReFungible') {
143 else if (mode.type == 'ReFungible') {
144 modeprm = {refungible: mode.decimalPoints};177 modeprm = {refungible: mode.decimalPoints};
145 }178 } else if (mode.type === 'Invalid') {
146 else if (mode.type == 'Invalid') {
147 modeprm = {invalid: null};179 modeprm = {invalid: null};
148 }180 }
149181
152 const result = getCreateCollectionResult(events);184 const result = getCreateCollectionResult(events);
153185
154 // Get number of collections after the transaction186 // Get number of collections after the transaction
155 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());187 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);
156188
157 // Get the collection 189 // Get the collection
158 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();190 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();
159191
160 // What to expect192 // What to expect
193 // tslint:disable-next-line:no-unused-expression
161 expect(result.success).to.be.true;194 expect(result.success).to.be.true;
162 expect(result.collectionId).to.be.equal(BcollectionCount);195 expect(result.collectionId).to.be.equal(BcollectionCount);
196 // tslint:disable-next-line:no-unused-expression
163 expect(collection).to.be.not.null;197 expect(collection).to.be.not.null;
164 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');198 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');
165 expect(collection.Owner).to.be.equal(alicesPublicKey);199 expect(collection.Owner).to.be.equal(alicesPublicKey);
176export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {210export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {
177 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};211 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};
212
213 let modeprm = {};
214 if (mode.type === 'NFT') {
215 modeprm = {nft: null};
216 } else if (mode.type === 'Fungible') {
217 modeprm = {fungible: mode.decimalPoints};
218 } else if (mode.type === 'ReFungible') {
219 modeprm = {refungible: mode.decimalPoints};
220 } else if (mode.type === 'Invalid') {
221 modeprm = {invalid: null};
222 }
178223
179 await usingApi(async (api) => {224 await usingApi(async (api) => {
180 // Get number of collections before the transaction225 // Get number of collections before the transaction
181 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());226 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
182227
183 // Run the CreateCollection transaction228 // Run the CreateCollection transaction
184 const alicePrivateKey = privateKey('//Alice');229 const alicePrivateKey = privateKey('//Alice');
185 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);230 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);
186 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;231 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
187 const result = getCreateCollectionResult(events);232 const result = getCreateCollectionResult(events);
188233
189 // Get number of collections after the transaction234 // Get number of collections after the transaction
190 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());235 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
191236
192 // What to expect237 // What to expect
238 // tslint:disable-next-line:no-unused-expression
193 expect(result.success).to.be.false;239 expect(result.success).to.be.false;
194 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');240 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');
195 });241 });
235 const events = await submitTransactionAsync(alicePrivateKey, tx);281 const events = await submitTransactionAsync(alicePrivateKey, tx);
236 const result = getDestroyResult(events);282 const result = getDestroyResult(events);
237283
238 // Get the collection 284 // Get the collection
239 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();285 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
240286
241 // What to expect287 // What to expect
254 const events = await submitTransactionAsync(alicePrivateKey, tx);300 const events = await submitTransactionAsync(alicePrivateKey, tx);
255 const result = getGenericResult(events);301 const result = getGenericResult(events);
256302
257 // Get the collection 303 // Get the collection
258 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();304 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
259305
260 // What to expect306 // What to expect
273 const events = await submitTransactionAsync(alicePrivateKey, tx);319 const events = await submitTransactionAsync(alicePrivateKey, tx);
274 const result = getGenericResult(events);320 const result = getGenericResult(events);
275321
276 // Get the collection 322 // Get the collection
277 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();323 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
278324
279 // What to expect325 // What to expect
312 const events = await submitTransactionAsync(sender, tx);358 const events = await submitTransactionAsync(sender, tx);
313 const result = getGenericResult(events);359 const result = getGenericResult(events);
314360
315 // Get the collection 361 // Get the collection
316 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();362 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
317363
318 // What to expect364 // What to expect
334380
335export interface CreateFungibleData extends Struct {381export interface CreateFungibleData extends Struct {
336 readonly value: u128;382 readonly value: u128;
337};383}
338384
339export interface CreateReFungibleData extends Struct {};385export interface CreateReFungibleData extends Struct {}
340export interface CreateNftData extends Struct {};386export interface CreateNftData extends Struct {}
341387
342export interface CreateItemData extends Enum {388export interface CreateItemData extends Enum {
343 NFT: CreateNftData,389 NFT: CreateNftData;
344 Fungible: CreateFungibleData,390 Fungible: CreateFungibleData;
345 ReFungible: CreateReFungibleData391 ReFungible: CreateReFungibleData;
346};392}
393
394export async function
395approveExpectSuccess(collectionId: number,
396 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {
397 await usingApi(async (api: ApiPromise) => {
398 const allowanceBefore =
399 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
400 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);
401 const events = await submitTransactionAsync(owner, approveNftTx);
402 const result = getCreateItemResult(events);
403 // tslint:disable-next-line:no-unused-expression
404 expect(result.success).to.be.true;
405 const allowanceAfter =
406 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
407 expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);
408 });
409}
410
411export async function
412transferFromExpectSuccess(collectionId: number,
413 tokenId: number,
414 accountApproved: IKeyringPair,
415 accountFrom: IKeyringPair,
416 accountTo: IKeyringPair,
417 value: number = 1,
418 type: string = 'NFT') {
419 await usingApi(async (api: ApiPromise) => {
420 let balanceBefore = new BN(0);
421 if (type === 'Fungible') {
422 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
423 }
424 const transferFromTx = await api.tx.nft.transferFrom(
425 accountFrom.address, accountTo.address, collectionId, tokenId, value);
426 const events = await submitTransactionAsync(accountFrom, transferFromTx);
427 const result = getCreateItemResult(events);
428 // tslint:disable-next-line:no-unused-expression
429 expect(result.success).to.be.true;
430 if (type === 'NFT') {
431 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
432 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);
433 }
434 if (type === 'Fungible') {
435 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
436 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);
437 }
438 if (type === 'ReFungible') {
439 const nftItemData =
440 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;
441 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);
442 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);
443 }
444 });
445}
446
447export async function
448transferFromExpectFail(collectionId: number,
449 tokenId: number,
450 accountApproved: IKeyringPair,
451 accountFrom: IKeyringPair,
452 accountTo: IKeyringPair,
453 value: number = 1) {
454 await usingApi(async (api: ApiPromise) => {
455 const transferFromTx = await api.tx.nft.transferFrom(
456 accountFrom.address, accountTo.address, collectionId, tokenId, value);
457 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;
458 const result = getCreateCollectionResult(events);
459 // tslint:disable-next-line:no-unused-expression
460 expect(result.success).to.be.false;
461 });
462}
463
464export async function
465approveExpectFail(collectionId: number,
466 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {
467 await usingApi(async (api: ApiPromise) => {
468 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);
469 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;
470 const result = getCreateCollectionResult(events);
471 // tslint:disable-next-line:no-unused-expression
472 expect(result.success).to.be.false;
473 });
474}
347475
348export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {476export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
349 let newItemId: number = 0;477 let newItemId: number = 0;
350 await usingApi(async (api) => {478 await usingApi(async (api) => {
351 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());479 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);
352 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 480 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
353 const AItemBalance = new BigNumber(Aitem.Value);481 const AItemBalance = new BigNumber(Aitem.Value);
354482
355 if (owner === '') owner = sender.address;483 if (owner === '') {
484 owner = sender.address;
485 }
356486
357 let tx;487 let tx;
358 if (createMode == 'Fungible') {488 if (createMode === 'Fungible') {
359 let createData = {fungible: {value: 10}};489 const createData = {fungible: {value: 10}};
360 tx = api.tx.nft.createItem(collectionId, owner, createData);490 tx = api.tx.nft.createItem(collectionId, owner, createData);
361 }491 } else {
362 else {
363 tx = api.tx.nft.createItem(collectionId, owner, createMode);492 tx = api.tx.nft.createItem(collectionId, owner, createMode);
364 }493 }
365 const events = await submitTransactionAsync(sender, tx);494 const events = await submitTransactionAsync(sender, tx);
366 const result = getCreateItemResult(events);495 const result = getCreateItemResult(events);
367496
368 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());497 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);
369 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 498 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
370 const BItemBalance = new BigNumber(Bitem.Value);499 const BItemBalance = new BigNumber(Bitem.Value);
371500
372 // What to expect501 // What to expect
502 // tslint:disable-next-line:no-unused-expression
373 expect(result.success).to.be.true;503 expect(result.success).to.be.true;
374 if (createMode == 'Fungible') {504 if (createMode === 'Fungible') {
375 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);505 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);
376 }506 } else {
377 else {
392 const events = await submitTransactionAsync(sender, tx);521 const events = await submitTransactionAsync(sender, tx);
393 const result = getGenericResult(events);522 const result = getGenericResult(events);
394523
395 // Get the collection 524 // Get the collection
396 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();525 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
397526
398 // What to expect527 // What to expect
409 const events = await submitTransactionAsync(sender, tx);538 const events = await submitTransactionAsync(sender, tx);
410 const result = getGenericResult(events);539 const result = getGenericResult(events);
411540
412 // Get the collection 541 // Get the collection
413 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();542 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
414543
415 // What to expect544 // What to expect
426 const events = await submitTransactionAsync(sender, tx);555 const events = await submitTransactionAsync(sender, tx);
427 const result = getGenericResult(events);556 const result = getGenericResult(events);
428557
429 // Get the collection 558 // Get the collection
430 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();559 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
431560
432 // What to expect561 // What to expect