git.delta.rocks / unique-network / refs/commits / 147c8928fe2f

difftreelog

Merge pull request #61 from usetech-llc/feature/NFTPAR-236_burn_item

str-mv2021-01-14parents: #aa3aa05 #c356c0a.patch.diff
in: master
Feature/nftpar 236 burn item

9 files changed

addedtests/src/burnItem.test.tsdiffbeforeafterboth
after · tests/src/burnItem.test.ts
1import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';2import { Keyring } from "@polkadot/api";3import { IKeyringPair } from "@polkadot/types/types";4import { 5  createCollectionExpectSuccess, 6  createItemExpectSuccess,7  getGenericResult,8  destroyCollectionExpectSuccess9} from './util/helpers';10import { nullPublicKey } from "./accounts";1112import chai from 'chai';13import chaiAsPromised from 'chai-as-promised';14chai.use(chaiAsPromised);15const expect = chai.expect;1617let alice: IKeyringPair;18let bob: IKeyringPair;1920describe('integration test: ext. burnItem():', () => {21  before(async () => {22    await usingApi(async (api) => {23      const keyring = new Keyring({ type: 'sr25519' });24      alice = keyring.addFromUri(`//Alice`);25      bob = keyring.addFromUri(`//Bob`);26    });27  });2829  it('Burn item in NFT collection', async () => {30    const createMode = 'NFT';31    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});32    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);3334    await usingApi(async (api) => {35      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);36      const events = await submitTransactionAsync(alice, tx);37      const result = getGenericResult(events);38  39      // Get the item 40      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();41 42      // What to expect43      expect(result.success).to.be.true;44      expect(item).to.be.not.null;45      expect(item.Owner).to.be.equal(nullPublicKey);46    });47  48  });49  it('Burn item in Fungible collection', async () => {50    const createMode = 'Fungible';51    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});52    await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens53    const tokenId = 0; // ignored5455    await usingApi(async (api) => {56      // Destroy 1 of 1057      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);58      const events = await submitTransactionAsync(alice, tx);59      const result = getGenericResult(events);60  61      // Get alice balance 62      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();63 64      // What to expect65      expect(result.success).to.be.true;66      expect(balance).to.be.not.null;67      expect(balance.Value).to.be.equal(9);68    });6970  });71  it('Burn item in ReFungible collection', async () => {72    const createMode = 'ReFungible';73    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});74    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);7576    await usingApi(async (api) => {77      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);78      const events = await submitTransactionAsync(alice, tx);79      const result = getGenericResult(events);80  81      // Get alice balance 82      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();83      84      // What to expect85      expect(result.success).to.be.true;86      expect(balance).to.be.not.null;87      expect(balance.Owner.length).to.be.equal(0);88    });8990  });9192  it('Burn owned portion of item in ReFungible collection', async () => {93    const createMode = 'ReFungible';94    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});95    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);9697    await usingApi(async (api) => {98      // Transfer 1/100 of the token to Bob99      const transfertx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 1);100      const events1 = await submitTransactionAsync(alice, transfertx);101      const result1 = getGenericResult(events1);102103      // Get balances104      const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();105106      // Bob burns his portion107      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);108      const events2 = await submitTransactionAsync(bob, tx);109      const result2 = getGenericResult(events2);110  111      // Get balances 112      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();113      // console.log(balance);114      115      // What to expect before burning116      expect(result1.success).to.be.true;117      expect(balanceBefore).to.be.not.null;118      expect(balanceBefore.Owner.length).to.be.equal(2);119      expect(balanceBefore.Owner[0].Owner).to.be.equal(alice.address);120      expect(balanceBefore.Owner[0].Fraction).to.be.equal(99);121      expect(balanceBefore.Owner[1].Owner).to.be.equal(bob.address);122      expect(balanceBefore.Owner[1].Fraction).to.be.equal(1);123124      // What to expect after burning125      expect(result2.success).to.be.true;126      expect(balance).to.be.not.null;127      expect(balance.Owner.length).to.be.equal(1);128      expect(balance.Owner[0].Fraction).to.be.equal(99);129      expect(balance.Owner[0].Owner).to.be.equal(alice.address);130    });131132  });133134});135136describe('Negative integration test: ext. burnItem():', () => {137  before(async () => {138    await usingApi(async (api) => {139      const keyring = new Keyring({ type: 'sr25519' });140      alice = keyring.addFromUri(`//Alice`);141      bob = keyring.addFromUri(`//Bob`);142    });143  });144145  it('Burn a token in a destroyed collection', async () => {146    const createMode = 'NFT';147    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});148    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);149    await destroyCollectionExpectSuccess(collectionId);150151    await usingApi(async (api) => {152      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);153      const badTransaction = async function () { 154        await submitTransactionExpectFailAsync(alice, tx);155      };156      await expect(badTransaction()).to.be.rejected;157    });158159  });160161  it('Burn a token that was never created', async () => {162    const createMode = 'NFT';163    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});164    const tokenId = 10;165166    await usingApi(async (api) => {167      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);168      const badTransaction = async function () { 169        await submitTransactionExpectFailAsync(alice, tx);170      };171      await expect(badTransaction()).to.be.rejected;172    });173174  });175176  it('Burn a token using the address that does not own it', async () => {177    const createMode = 'NFT';178    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});179    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);180181    await usingApi(async (api) => {182      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);183      const badTransaction = async function () { 184        await submitTransactionExpectFailAsync(bob, tx);185      };186      await expect(badTransaction()).to.be.rejected;187    });188189  });190191  it('Transfer a burned a token', async () => {192    const createMode = 'NFT';193    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});194    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);195196    await usingApi(async (api) => {197198      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);199      const events1 = await submitTransactionAsync(alice, burntx);200      const result1 = getGenericResult(events1);201      expect(result1.success).to.be.true;202  203      const tx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 0);204      const badTransaction = async function () { 205        await submitTransactionExpectFailAsync(alice, tx);206      };207      await expect(badTransaction()).to.be.rejected;208    });209210  });211212  it('Burn more than owned in Fungible collection', async () => {213    const createMode = 'Fungible';214    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});215    // Helper creates 10 fungible tokens216    await createItemExpectSuccess(alice, collectionId, createMode);217    const tokenId = 0; // ignored218219    await usingApi(async (api) => {220      // Destroy 11 of 10221      const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);222      const badTransaction = async function () { 223        await submitTransactionExpectFailAsync(alice, tx);224      };225      await expect(badTransaction()).to.be.rejected;226      227      // Get alice balance 228      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();229 230      // What to expect231      expect(balance).to.be.not.null;232      expect(balance.Value).to.be.equal(10);233    });234235  });236237});
modifiedtests/src/confirmSponsorship.test.tsdiffbeforeafterboth
--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -89,7 +89,7 @@
   });
 
   it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {
-    const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0 }});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
@@ -115,7 +115,7 @@
   });
 
   it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async () => {
-    const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0 }});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
@@ -210,7 +210,7 @@
   });
 
   it('Fungible: Sponsoring is rate limited', async () => {
-    const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0 }});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
@@ -247,7 +247,7 @@
   });
 
   it('ReFungible: Sponsoring is rate limited', async () => {
-    const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0 }});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
modifiedtests/src/createCollection.test.tsdiffbeforeafterboth
--- a/tests/src/createCollection.test.ts
+++ b/tests/src/createCollection.test.ts
@@ -6,14 +6,14 @@
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
 import { default as usingApi } from "./substrate/substrate-api";
-import { createCollectionExpectSuccess, createCollectionExpectFailure, CollectionMode } from "./util/helpers";
+import { createCollectionExpectSuccess, createCollectionExpectFailure } from "./util/helpers";
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
 
 describe('integration test: ext. createCollection():', () => {
   it('Create new NFT collection', async () => {
-    await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+    await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
   });
   it('Create new NFT collection whith collection_name of maximum length (64 bytes)', async () => {
     await createCollectionExpectSuccess({name: 'A'.repeat(64)});
@@ -25,10 +25,10 @@
     await createCollectionExpectSuccess({tokenPrefix: 'A'.repeat(16)});
   });
   it('Create new Fungible collection', async () => {
-    await createCollectionExpectSuccess({mode: 'Fungible'});
+    await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
   });
   it('Create new ReFungible collection', async () => {
-    await createCollectionExpectSuccess({mode: 'ReFungible'});
+    await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
   });
 });
 
@@ -38,7 +38,7 @@
       const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
 
       const badTransaction = async function () { 
-        await createCollectionExpectSuccess({mode: 'BadMode' as CollectionMode});
+        await createCollectionExpectSuccess({mode: {type: 'Invalid'}});
       };
       expect(badTransaction()).to.be.rejected;
 
modifiedtests/src/createItem.test.tsdiffbeforeafterboth
--- a/tests/src/createItem.test.ts
+++ b/tests/src/createItem.test.ts
@@ -18,17 +18,17 @@
 
   it('Create new item in NFT collection', async () => {
     const createMode = 'NFT';
-    const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});
     await createItemExpectSuccess(alice, newCollectionID, createMode);
   });
   it('Create new item in Fungible collection', async () => {
     const createMode = 'Fungible';
-    const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
     await createItemExpectSuccess(alice, newCollectionID, createMode);
   });
   it('Create new item in ReFungible collection', async () => {
     const createMode = 'ReFungible';
-    const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+    const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
     await createItemExpectSuccess(alice, newCollectionID, createMode);
   });
 });
modifiedtests/src/destroyCollection.test.tsdiffbeforeafterboth
--- a/tests/src/destroyCollection.test.ts
+++ b/tests/src/destroyCollection.test.ts
@@ -14,11 +14,11 @@
     await destroyCollectionExpectSuccess(collectionId);
   });
   it('Fungible collection can be destroyed', async () => {
-    const collectionId = await createCollectionExpectSuccess({ mode: 'Fungible' });
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
     await destroyCollectionExpectSuccess(collectionId);
   });
   it('ReFungible collection can be destroyed', async () => {
-    const collectionId = await createCollectionExpectSuccess({ mode: 'ReFungible' });
+    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
     await destroyCollectionExpectSuccess(collectionId);
   });
 });
modifiedtests/src/setCollectionLimits.test.tsdiffbeforeafterboth
--- a/tests/src/setCollectionLimits.test.ts
+++ b/tests/src/setCollectionLimits.test.ts
@@ -32,7 +32,7 @@
   });
   it('choose or create collection for testing', async () => {
     await usingApi(async () => {
-      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
     });
   });
 });
modifiedtests/src/setCollectionSponsor.test.tsdiffbeforeafterboth
--- a/tests/src/setCollectionSponsor.test.ts
+++ b/tests/src/setCollectionSponsor.test.ts
@@ -30,11 +30,11 @@
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
   });
   it('Set Fungible collection sponsor', async () => {
-    const collectionId = await createCollectionExpectSuccess({ mode: 'Fungible' });
+    const collectionId = await createCollectionExpectSuccess({ mode: {type: 'Fungible', decimalPoints: 0} });
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
   });
   it('Set ReFungible collection sponsor', async () => {
-    const collectionId = await createCollectionExpectSuccess({ mode: 'ReFungible' });
+    const collectionId = await createCollectionExpectSuccess({ mode: {type: 'ReFungible', decimalPoints: 0} });
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
   });
 
modifiedtests/src/setSchemaVersion.test.tsdiffbeforeafterboth
--- a/tests/src/setSchemaVersion.test.ts
+++ b/tests/src/setSchemaVersion.test.ts
@@ -35,7 +35,7 @@
   });
   it('choose or create collection for testing', async () => {
     await usingApi(async () => {
-      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
     });
   });
 });
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -88,7 +88,26 @@
   return result;
 }
 
-export type CollectionMode = 'NFT' | 'Fungible' | 'ReFungible';
+interface Invalid {
+  type: 'Invalid'
+}
+
+interface Nft {
+  type: 'NFT'
+}
+
+interface Fungible {
+  type: 'Fungible',
+  decimalPoints: number
+}
+
+interface ReFungible {
+  type: 'ReFungible',
+  decimalPoints: number
+}
+
+type CollectionMode = Nft | Fungible | ReFungible | Invalid;
+
 export type CreateCollectionParams = {
   mode: CollectionMode,
   name: string,
@@ -99,7 +118,7 @@
 const defaultCreateCollectionParams: CreateCollectionParams = {
   name: 'name',
   description: 'description',
-  mode: 'NFT',
+  mode: { type: "NFT" },
   tokenPrefix: 'prefix'
 }
 
@@ -113,7 +132,22 @@
 
     // Run the CreateCollection transaction
     const alicePrivateKey = privateKey('//Alice');
-    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);
+
+    let modeprm = {};
+    if (mode.type == 'NFT') {
+      modeprm = {nft: null};
+    }
+    else if (mode.type == 'Fungible') {
+      modeprm = {fungible: mode.decimalPoints};
+    }
+    else if (mode.type == 'ReFungible') {
+      modeprm = {refungible: mode.decimalPoints};
+    }
+    else if (mode.type == 'Invalid') {
+      modeprm = {invalid: null};
+    }
+
+    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);
     const events = await submitTransactionAsync(alicePrivateKey, tx);
     const result = getCreateCollectionResult(events);