git.delta.rocks / unique-network / refs/commits / 9b1ad2d3650d

difftreelog

Confirm sponsorship tests in progress

Greg Zaitsev2020-12-23parents: #474b3ae #c3c0503.patch.diff
in: master

3 files changed

modifiedtests/src/confirmSponsorship.test.tsdiffbeforeafterboth
--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -13,10 +13,14 @@
   setCollectionSponsorExpectFailure,
   confirmSponsorshipExpectSuccess,
   confirmSponsorshipExpectFailure,
+  createItemExpectSuccess,
+  findUnusedAddress,
+  getGenericResult,
 } from "./util/helpers";
 import { Keyring } from "@polkadot/api";
 import { IKeyringPair } from "@polkadot/types/types";
 import type { AccountId } from '@polkadot/types/interfaces';
+import { BigNumber } from 'bignumber.js';
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -30,6 +34,7 @@
   before(async () => {
     await usingApi(async (api) => {
       const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri(`//Alice`);
       bob = keyring.addFromUri(`//Bob`);
       charlie = keyring.addFromUri(`//Charlie`);
     });
@@ -53,11 +58,31 @@
     await setCollectionSponsorExpectSuccess(collectionId, charlie.address);
   });
 
-  it.skip('Transfer fees are paid by the sponsor after confirmation', async () => {
-    // const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
-    // await setCollectionSponsorExpectSuccess(collectionId, bob.address);
-    // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
-    expect(false).to.be.true;
+  it.only('Transfer fees are paid by the sponsor after confirmation', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+    const itemId = await createItemExpectSuccess(collectionId, 'NFT', '//Alice');
+
+    await usingApi(async (api) => {
+      const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());
+
+      // Find unused address
+      const zeroBalance = await findUnusedAddress(api);
+      
+      const aliceToZero = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);
+      await submitTransactionAsync(alice, aliceToZero);
+
+      const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);
+      const events = await submitTransactionAsync(zeroBalance, zeroToAlice);
+      const result = getGenericResult(events);
+
+      const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());
+
+      expect(result.success).to.be.true;
+      expect(BsponsorBalance.toNumber()).to.be.lessThan(AsponsorBalance.toNumber());
+    });
+
   });
 
   it.skip('CreateItem fees are paid by the sponsor after confirmation', async () => {
addedtests/src/createItem.test.tsdiffbeforeafterboth
after · tests/src/createItem.test.ts
1import { assert } from 'chai';
2import { alicesPublicKey } from './accounts';
3import privateKey from './substrate/privateKey';
4import { default as usingApi } from './substrate/substrate-api';
5import waitNewBlocks from './substrate/wait-new-blocks';
6import { 
7  createCollectionExpectSuccess, 
8  createItemExpectSuccess
9} from './util/helpers';
10
11describe('integration test: ext. createItem():', () => {
12  it('Create new item in NFT collection', async () => {
13    const createMode = 'NFT';
14    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
15    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
16  });
17  it('Create new item in Fungible collection', async () => {
18    const createMode = 'Fungible';
19    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
20    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
21  });
22  it('Create new item in ReFungible collection', async () => {
23    const createMode = 'ReFungible';
24    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
25    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
26  });
27});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -26,6 +26,12 @@
   collectionId: number
 };
 
+type CreateItemResult = {
+  success: boolean,
+  collectionId: number,
+  itemId: number
+};
+
 export function getGenericResult(events: EventRecord[]): GenericResult {
   let result: GenericResult = {
     success: false
@@ -57,6 +63,27 @@
   return result;
 }
 
+function getCreateItemResult(events: EventRecord[]): CreateItemResult {
+  let success = false;
+  let collectionId: number = 0;
+  let itemId: number = 0;
+  events.forEach(({ phase, event: { data, method, section } }) => {
+    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);
+    if (method == 'ExtrinsicSuccess') {
+      success = true;
+    } else if ((section == 'nft') && (method == 'ItemCreated')) {
+      collectionId = parseInt(data[0].toString());
+      itemId = parseInt(data[1].toString());
+    }
+  });
+  let result: CreateItemResult = {
+    success,
+    collectionId,
+    itemId
+  }
+  return result;
+}
+
 export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {
   let collectionId: number = 0;
   await usingApi(async (api) => {
@@ -231,3 +258,24 @@
   });
 }
 
+export async function createItemExpectSuccess(collectionId: number, createMode: string, senderSeed: string = '//Alice') {
+  let newItemId: number = 0;
+  await usingApi(async (api) => {
+    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
+
+    const sender = privateKey(senderSeed);
+    const tx = api.tx.nft.createItem(collectionId, sender.address, createMode);
+    const events = await submitTransactionAsync(sender, tx);
+    const result = getCreateItemResult(events);
+  
+    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
+
+    // What to expect
+    expect(result.success).to.be.true;
+    expect(BItemCount).to.be.equal(AItemCount+1);
+    expect(collectionId).to.be.equal(result.collectionId);
+    expect(BItemCount).to.be.equal(result.itemId);
+    newItemId = result.itemId;
+  });
+  return newItemId;
+}