git.delta.rocks / unique-network / refs/commits / f16723a5aa89

difftreelog

Merge pull request #51 from usetech-llc/feature/NFTPAR-246_remove_sponsor_tests

str-mv2020-12-25parents: #cd29302 #bd903ea.patch.diff
in: master
NFTPAR-246 remove sponsor tests

3 files changed

modifiedtests/src/confirmSponsorship.test.tsdiffbeforeafterboth
--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -32,7 +32,7 @@
 let bob: IKeyringPair;
 let charlie: IKeyringPair;
 
-describe.only('integration test: ext. confirmSponsorship():', () => {
+describe('integration test: ext. confirmSponsorship():', () => {
 
   before(async () => {
     await usingApi(async (api) => {
@@ -299,7 +299,7 @@
 
 });
 
-describe.only('(!negative test!) integration test: ext. setCollectionSponsor():', () => {
+describe('(!negative test!) integration test: ext. removeCollectionSponsor():', () => {
   before(async () => {
     await usingApi(async (api) => {
       const keyring = new Keyring({ type: 'sr25519' });
addedtests/src/removeCollectionSponsor.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/removeCollectionSponsor.test.ts
@@ -0,0 +1,141 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";
+import { 
+  createCollectionExpectSuccess, 
+  setCollectionSponsorExpectSuccess, 
+  destroyCollectionExpectSuccess, 
+  setCollectionSponsorExpectFailure,
+  confirmSponsorshipExpectSuccess,
+  confirmSponsorshipExpectFailure,
+  createItemExpectSuccess,
+  findUnusedAddress,
+  getGenericResult,
+  enableWhiteListExpectSuccess,
+  enablePublicMintingExpectSuccess,
+  addToWhiteListExpectSuccess,
+  removeCollectionSponsorExpectSuccess,
+  removeCollectionSponsorExpectFailure,
+} 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;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+let charlie: IKeyringPair;
+
+describe('integration test: ext. removeCollectionSponsor():', () => {
+
+  before(async () => {
+    await usingApi(async (api) => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri(`//Alice`);
+      bob = keyring.addFromUri(`//Bob`);
+      charlie = keyring.addFromUri(`//Charlie`);
+    });
+  });
+
+  it('Remove NFT collection sponsor stops sponsorship', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+    await removeCollectionSponsorExpectSuccess(collectionId);
+
+    await usingApi(async (api) => {
+      // Find unused address
+      const zeroBalance = await findUnusedAddress(api);
+
+      // Mint token for unused address
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', zeroBalance.address);
+
+      // Transfer this tokens from unused address to Alice - should fail
+      const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
+      const zeroToAlice = api.tx.nft.transfer(alice.address, collectionId, itemId, 0);
+      const badTransaction = async function () { 
+        console.log = function () {};
+        console.error = function () {};
+        await submitTransactionAsync(zeroBalance, zeroToAlice);
+        delete console.log;
+        delete console.error;
+      };
+      await expect(badTransaction()).to.be.rejectedWith("Inability to pay some fees");
+      const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
+
+      expect(BsponsorBalance.isEqualTo(AsponsorBalance)).to.be.true;
+    });
+  });
+
+  it('Remove a sponsor after it was already removed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+    await removeCollectionSponsorExpectSuccess(collectionId);
+    await removeCollectionSponsorExpectSuccess(collectionId);
+  });
+
+  it('Remove sponsor in a collection that never had the sponsor set', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await removeCollectionSponsorExpectSuccess(collectionId);
+  });
+
+  it('Remove sponsor for a collection that had the sponsor set, but not confirmed', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await removeCollectionSponsorExpectSuccess(collectionId);
+  });
+
+});
+
+describe('(!negative test!) integration test: ext. removeCollectionSponsor():', () => {
+  before(async () => {
+    await usingApi(async (api) => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri(`//Alice`);
+      bob = keyring.addFromUri(`//Bob`);
+      charlie = keyring.addFromUri(`//Charlie`);
+    });
+  });
+
+  it('(!negative test!) Remove sponsor for a collection that never existed', async () => {
+    // Find the collection that never existed
+    const collectionId = 0;
+    await usingApi(async (api) => {
+      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;
+    });
+
+    await removeCollectionSponsorExpectFailure(collectionId);
+  });
+
+  it('(!negative test!) Remove sponsor in a destroyed collection', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await destroyCollectionExpectSuccess(collectionId);
+    await removeCollectionSponsorExpectFailure(collectionId);
+  });
+
+  it('Set - remove - confirm: fails', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await removeCollectionSponsorExpectSuccess(collectionId);
+    await confirmSponsorshipExpectFailure(collectionId, '//Bob');
+  });
+
+  it('Set - confirm - remove - confirm: Sponsor cannot come back', async () => {
+    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+    await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+    await removeCollectionSponsorExpectSuccess(collectionId);
+    await confirmSponsorshipExpectFailure(collectionId, '//Bob');
+  });
+
+});
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';16import { Struct, Enum } from '@polkadot/types/codec';17import { u128 } from '@polkadot/types/primitive';1819chai.use(chaiAsPromised);20const expect = chai.expect;2122type GenericResult = {23  success: boolean,24};2526type CreateCollectionResult = {27  success: boolean,28  collectionId: number29};3031type CreateItemResult = {32  success: boolean,33  collectionId: number,34  itemId: number35};3637export function getGenericResult(events: EventRecord[]): GenericResult {38  let result: GenericResult = {39    success: false40  }41  events.forEach(({ phase, event: { data, method, section } }) => {42    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);43    if (method == 'ExtrinsicSuccess') {44      result.success = true;45    }46  });47  return result;48}4950function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {51  let success = false;52  let collectionId: number = 0;53  events.forEach(({ phase, event: { data, method, section } }) => {54    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);55    if (method == 'ExtrinsicSuccess') {56      success = true;57    } else if ((section == 'nft') && (method == 'Created')) {58      collectionId = parseInt(data[0].toString());59    }60  });61  let result: CreateCollectionResult = {62    success,63    collectionId64  }65  return result;66}6768function getCreateItemResult(events: EventRecord[]): CreateItemResult {69  let success = false;70  let collectionId: number = 0;71  let itemId: number = 0;72  events.forEach(({ phase, event: { data, method, section } }) => {73    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);74    if (method == 'ExtrinsicSuccess') {75      success = true;76    } else if ((section == 'nft') && (method == 'ItemCreated')) {77      collectionId = parseInt(data[0].toString());78      itemId = parseInt(data[1].toString());79    }80  });81  let result: CreateItemResult = {82    success,83    collectionId,84    itemId85  }86  return result;87}8889export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {90  let collectionId: number = 0;91  await usingApi(async (api) => {92    // Get number of collections before the transaction93    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9495    // Run the CreateCollection transaction96    const alicePrivateKey = privateKey('//Alice');97    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);98    const events = await submitTransactionAsync(alicePrivateKey, tx);99    const result = getCreateCollectionResult(events);100101    // Get number of collections after the transaction102    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());103104    // Get the collection 105    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();106107    // What to expect108    expect(result.success).to.be.true;109    expect(result.collectionId).to.be.equal(BcollectionCount);110    expect(collection).to.be.not.null;111    expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');112    expect(collection.Owner).to.be.equal(alicesPublicKey);113    expect(utf16ToStr(collection.Name)).to.be.equal(name);114    expect(utf16ToStr(collection.Description)).to.be.equal(description);115    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);116117    collectionId = result.collectionId;118  });119120  return collectionId;121}122  123export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {124  await usingApi(async (api) => {125    // Get number of collections before the transaction126    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());127128    // Run the CreateCollection transaction129    const alicePrivateKey = privateKey('//Alice');130    const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);131    const events = await submitTransactionAsync(alicePrivateKey, tx);132    const result = getCreateCollectionResult(events);133134    // Get number of collections after the transaction135    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());136137    // What to expect138    expect(result.success).to.be.false;139    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');140  });141}142  143export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {144  let bal = new BigNumber(0);145  let unused;146  do {147    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));148    const keyring = new Keyring({ type: 'sr25519' });149    unused = keyring.addFromUri(`//${randomSeed}`);150    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());151  } while (bal.toFixed() != '0');152  return unused; 153}154155function getDestroyResult(events: EventRecord[]): boolean {156  let success: boolean = false;157  events.forEach(({ phase, event: { data, method, section } }) => {158    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);159    if (method == 'ExtrinsicSuccess') {160      success = true;161    }162  });163  return success;164}165166export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {167  await usingApi(async (api) => {168    // Run the DestroyCollection transaction169    const alicePrivateKey = privateKey(senderSeed);170    const tx = api.tx.nft.destroyCollection(collectionId);171    const events = await submitTransactionAsync(alicePrivateKey, tx);172    const result = getDestroyResult(events);173174    // What to expect175    expect(result).to.be.false;176  });177}178179export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {180  await usingApi(async (api) => {181    // Run the DestroyCollection transaction182    const alicePrivateKey = privateKey(senderSeed);183    const tx = api.tx.nft.destroyCollection(collectionId);184    const events = await submitTransactionAsync(alicePrivateKey, tx);185    const result = getDestroyResult(events);186187    // Get the collection 188    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();189190    // What to expect191    expect(result).to.be.true;192    expect(collection).to.be.not.null;193    expect(collection.Owner).to.be.equal(nullPublicKey);194  });195}196197export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {198  await usingApi(async (api) => {199200    // Run the transaction201    const alicePrivateKey = privateKey('//Alice');202    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);203    const events = await submitTransactionAsync(alicePrivateKey, tx);204    const result = getGenericResult(events);205206    // Get the collection 207    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();208209    // What to expect210    expect(result.success).to.be.true;211    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());212    expect(collection.SponsorConfirmed).to.be.false;213  });214}215216export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {217  await usingApi(async (api) => {218219    // Run the transaction220    const alicePrivateKey = privateKey(senderSeed);221    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);222    const events = await submitTransactionAsync(alicePrivateKey, tx);223    const result = getGenericResult(events);224225    // What to expect226    expect(result.success).to.be.false;227  });228}229230export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {231  await usingApi(async (api) => {232233    // Run the transaction234    const sender = privateKey(senderSeed);235    const tx = api.tx.nft.confirmSponsorship(collectionId);236    const events = await submitTransactionAsync(sender, tx);237    const result = getGenericResult(events);238239    // Get the collection 240    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();241242    // What to expect243    expect(result.success).to.be.true;244    expect(collection.Sponsor).to.be.equal(sender.address);245    expect(collection.SponsorConfirmed).to.be.true;246  });247}248249export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {250  await usingApi(async (api) => {251252    // Run the transaction253    const sender = privateKey(senderSeed);254    const tx = api.tx.nft.confirmSponsorship(collectionId);255    const events = await submitTransactionAsync(sender, tx);256    const result = getGenericResult(events);257258    // What to expect259    expect(result.success).to.be.false;260  });261}262263export interface CreateFungibleData extends Struct {264  readonly value: u128;265};266267export interface CreateReFungibleData extends Struct {};268export interface CreateNftData extends Struct {};269270export interface CreateItemData extends Enum {271  NFT: CreateNftData,272  Fungible: CreateFungibleData,273  ReFungible: CreateReFungibleData274};275276export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {277  let newItemId: number = 0;278  await usingApi(async (api) => {279    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());280    const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();    281    const AItemBalance = new BigNumber(Aitem.Value);282283    if (owner === '') owner = sender.address;284285    let tx;286    if (createMode == 'Fungible') {287      let createData = {fungible: {value: 10}};288      tx = api.tx.nft.createItem(collectionId, owner, createData);289    }290    else {291      tx = api.tx.nft.createItem(collectionId, owner, createMode);292    }293    const events = await submitTransactionAsync(sender, tx);294    const result = getCreateItemResult(events);295296    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());297    const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();    298    const BItemBalance = new BigNumber(Bitem.Value);299300    // What to expect301    expect(result.success).to.be.true;302    if (createMode == 'Fungible') {303      expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);304    }305    else {306      expect(BItemCount).to.be.equal(AItemCount+1);307    }308    expect(collectionId).to.be.equal(result.collectionId);309    expect(BItemCount).to.be.equal(result.itemId);310    newItemId = result.itemId;311  });312  return newItemId;313}314315export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {316  await usingApi(async (api) => {317318    // Run the transaction319    const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');320    const events = await submitTransactionAsync(sender, tx);321    const result = getGenericResult(events);322323    // Get the collection 324    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();325326    // What to expect327    expect(result.success).to.be.true;328    expect(collection.Access).to.be.equal('WhiteList');329  });330}331332export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {333  await usingApi(async (api) => {334335    // Run the transaction336    const tx = api.tx.nft.setMintPermission(collectionId, true);337    const events = await submitTransactionAsync(sender, tx);338    const result = getGenericResult(events);339340    // Get the collection 341    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();342343    // What to expect344    expect(result.success).to.be.true;345    expect(collection.MintMode).to.be.equal(true);346  });347}348349export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {350  await usingApi(async (api) => {351352    // Run the transaction353    const tx = api.tx.nft.addToWhiteList(collectionId, address);354    const events = await submitTransactionAsync(sender, tx);355    const result = getGenericResult(events);356357    // Get the collection 358    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();359360    // What to expect361    expect(result.success).to.be.true;362    expect(collection.MintMode).to.be.equal(true);363  });364}365