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
--- /dev/null
+++ b/tests/src/createItem.test.ts
@@ -0,0 +1,27 @@
+import { assert } from 'chai';
+import { alicesPublicKey } from './accounts';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import waitNewBlocks from './substrate/wait-new-blocks';
+import { 
+  createCollectionExpectSuccess, 
+  createItemExpectSuccess
+} from './util/helpers';
+
+describe('integration test: ext. createItem():', () => {
+  it('Create new item in NFT collection', async () => {
+    const createMode = 'NFT';
+    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+  });
+  it('Create new item in Fungible collection', async () => {
+    const createMode = 'Fungible';
+    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+  });
+  it('Create new item in ReFungible collection', async () => {
+    const createMode = 'ReFungible';
+    const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
before · tests/src/util/helpers.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import chai from 'chai';7import chaiAsPromised from 'chai-as-promised';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { ApiPromise, Keyring } from "@polkadot/api";10import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";11import privateKey from '../substrate/privateKey';12import { alicesPublicKey, nullPublicKey } from "../accounts";13import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';14import { IKeyringPair } from "@polkadot/types/types";15import { BigNumber } from 'bignumber.js';1617chai.use(chaiAsPromised);18const expect = chai.expect;1920type GenericResult = {21  success: boolean,22};2324type CreateCollectionResult = {25  success: boolean,26  collectionId: number27};2829export function getGenericResult(events: EventRecord[]): GenericResult {30  let result: GenericResult = {31    success: false32  }33  events.forEach(({ phase, event: { data, method, section } }) => {34    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);35    if (method == 'ExtrinsicSuccess') {36      result.success = true;37    }38  });39  return result;40}4142function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {43  let success = false;44  let collectionId: number = 0;45  events.forEach(({ phase, event: { data, method, section } }) => {46    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);47    if (method == 'ExtrinsicSuccess') {48      success = true;49    } else if ((section == 'nft') && (method == 'Created')) {50      collectionId = parseInt(data[0].toString());51    }52  });53  let result: CreateCollectionResult = {54    success,55    collectionId56  }57  return result;58}5960export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {61  let collectionId: number = 0;62  await usingApi(async (api) => {63    // Get number of collections before the transaction64    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6566    // Run the CreateCollection transaction67    const alicePrivateKey = privateKey('//Alice');68    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);69    const events = await submitTransactionAsync(alicePrivateKey, tx);70    const result = getCreateCollectionResult(events);7172    // Get number of collections after the transaction73    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());7475    // Get the collection 76    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();7778    // What to expect79    expect(result.success).to.be.true;80    expect(result.collectionId).to.be.equal(BcollectionCount);81    expect(collection).to.be.not.null;82    expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');83    expect(collection.Owner).to.be.equal(alicesPublicKey);84    expect(utf16ToStr(collection.Name)).to.be.equal(name);85    expect(utf16ToStr(collection.Description)).to.be.equal(description);86    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);8788    collectionId = result.collectionId;89  });9091  return collectionId;92}93  94export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {95  await usingApi(async (api) => {96    // Get number of collections before the transaction97    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9899    // Run the CreateCollection transaction100    const alicePrivateKey = privateKey('//Alice');101    const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);102    const events = await submitTransactionAsync(alicePrivateKey, tx);103    const result = getCreateCollectionResult(events);104105    // Get number of collections after the transaction106    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());107108    // What to expect109    expect(result.success).to.be.false;110    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');111  });112}113  114export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {115  let bal = new BigNumber(0);116  let unused;117  do {118    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));119    const keyring = new Keyring({ type: 'sr25519' });120    unused = keyring.addFromUri(`//${randomSeed}`);121    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());122  } while (bal.toFixed() != '0');123  return unused; 124}125126function getDestroyResult(events: EventRecord[]): boolean {127  let success: boolean = false;128  events.forEach(({ phase, event: { data, method, section } }) => {129    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);130    if (method == 'ExtrinsicSuccess') {131      success = true;132    }133  });134  return success;135}136137export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {138  await usingApi(async (api) => {139    // Run the DestroyCollection transaction140    const alicePrivateKey = privateKey(senderSeed);141    const tx = api.tx.nft.destroyCollection(collectionId);142    const events = await submitTransactionAsync(alicePrivateKey, tx);143    const result = getDestroyResult(events);144145    // What to expect146    expect(result).to.be.false;147  });148}149150export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {151  await usingApi(async (api) => {152    // Run the DestroyCollection transaction153    const alicePrivateKey = privateKey(senderSeed);154    const tx = api.tx.nft.destroyCollection(collectionId);155    const events = await submitTransactionAsync(alicePrivateKey, tx);156    const result = getDestroyResult(events);157158    // Get the collection 159    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();160161    // What to expect162    expect(result).to.be.true;163    expect(collection).to.be.not.null;164    expect(collection.Owner).to.be.equal(nullPublicKey);165  });166}167168export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {169  await usingApi(async (api) => {170171    // Run the transaction172    const alicePrivateKey = privateKey('//Alice');173    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);174    const events = await submitTransactionAsync(alicePrivateKey, tx);175    const result = getGenericResult(events);176177    // Get the collection 178    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();179180    // What to expect181    expect(result.success).to.be.true;182    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());183    expect(collection.SponsorConfirmed).to.be.false;184  });185}186187export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {188  await usingApi(async (api) => {189190    // Run the transaction191    const alicePrivateKey = privateKey(senderSeed);192    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);193    const events = await submitTransactionAsync(alicePrivateKey, tx);194    const result = getGenericResult(events);195196    // What to expect197    expect(result.success).to.be.false;198  });199}200201export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {202  await usingApi(async (api) => {203204    // Run the transaction205    const sender = privateKey(senderSeed);206    const tx = api.tx.nft.confirmSponsorship(collectionId);207    const events = await submitTransactionAsync(sender, tx);208    const result = getGenericResult(events);209210    // Get the collection 211    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();212213    // What to expect214    expect(result.success).to.be.true;215    expect(collection.Sponsor).to.be.equal(sender.address);216    expect(collection.SponsorConfirmed).to.be.true;217  });218}219220export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {221  await usingApi(async (api) => {222223    // Run the transaction224    const sender = privateKey(senderSeed);225    const tx = api.tx.nft.confirmSponsorship(collectionId);226    const events = await submitTransactionAsync(sender, tx);227    const result = getGenericResult(events);228229    // What to expect230    expect(result.success).to.be.false;231  });232}233