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

difftreelog

NFTPAR-237 Integration Test changeCollectionOwner(collection_id, new_owner). Fixed tests.

sotmorskiy2020-12-29parent: #91e3663.patch.diff
in: master

4 files changed

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();
+    const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
     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();
+    const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
@@ -210,7 +210,7 @@
   });
 
   it('Fungible: Sponsoring is rate limited', async () => {
-    const collectionId = await createCollectionExpectSuccess();
+    const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
@@ -247,7 +247,7 @@
   });
 
   it('ReFungible: Sponsoring is rate limited', async () => {
-    const collectionId = await createCollectionExpectSuccess();
+    const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
     await setCollectionSponsorExpectSuccess(collectionId, bob.address);
     await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
 
modifiedtests/src/creditFeesToTreasury.test.tsdiffbeforeafterboth
--- a/tests/src/creditFeesToTreasury.test.ts
+++ b/tests/src/creditFeesToTreasury.test.ts
@@ -5,7 +5,7 @@
 
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
-import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
 import { alicesPublicKey, bobsPublicKey } from "./accounts";
 import privateKey from "./substrate/privateKey";
 import { BigNumber } from 'bignumber.js';
@@ -63,14 +63,13 @@
       const bobBalanceBefore = new BigNumber((await api.query.system.account(bobsPublicKey)).data.free.toString());
 
       const badTx = api.tx.balances.setBalance(alicesPublicKey, 0, 0);
-      const result = getGenericResult(await submitTransactionAsync(bobPrivateKey, badTx));
+      await expect(submitTransactionExpectFailAsync(bobPrivateKey, badTx)).to.be.rejected;
 
       const treasuryBalanceAfter = new BigNumber((await api.query.system.account(Treasury)).data.free.toString());
       const bobBalanceAfter = new BigNumber((await api.query.system.account(bobsPublicKey)).data.free.toString());
       const fee = bobBalanceBefore.minus(bobBalanceAfter);
       const treasuryIncrease = treasuryBalanceAfter.minus(treasuryBalanceBefore);
 
-      expect(result.success).to.be.false;
       expect(treasuryIncrease.toFixed()).to.be.equal(fee.toFixed());
     });
   });
modifiedtests/src/substrate/substrate-api.tsdiffbeforeafterboth
before · tests/src/substrate/substrate-api.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 { WsProvider, ApiPromise } from "@polkadot/api";7import type { AccountId, Address, ApplyExtrinsicResult, DispatchError, DispatchInfo, EventRecord, Extrinsic, ExtrinsicStatus, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';8import { IKeyringPair } from "@polkadot/types/types";910import config from "../config";11import promisifySubstrate from "./promisify-substrate";12import { ApiOptions, SubmittableExtrinsic, ApiTypes } from "@polkadot/api/types";13import rtt from "../../../runtime_types.json";1415function defaultApiOptions(): ApiOptions {16  const wsProvider = new WsProvider(config.substrateUrl);17  return { provider: wsProvider, types: rtt };18}1920export default async function usingApi(action: (api: ApiPromise) => Promise<void>, settings: ApiOptions | undefined = undefined): Promise<void> {21  settings = settings || defaultApiOptions();22  let api: ApiPromise = new ApiPromise(settings);2324  try {25    await promisifySubstrate(api, async () => {26      if(api) {27        await api.isReadyOrError;28        await action(api);29      }30    })();31  } finally {32    await api.disconnect();33  }34}3536enum TransactionStatus {37  Success,38  Fail,39  NotReady40}4142function getTransactionStatus(events: EventRecord[], status: ExtrinsicStatus): TransactionStatus {43  if (status.isReady) {44    return TransactionStatus.NotReady;45  }46  if (status.isBroadcast) {47    return TransactionStatus.NotReady;48  } 49  if (status.isInBlock || status.isFinalized) {50    if(events.filter(e => e.event.data.method === 'ExtrinsicFailed').length > 0) {51      return TransactionStatus.Fail;52    }53    if(events.filter(e => e.event.data.method === 'ExtrinsicSuccess').length > 0) {54      return TransactionStatus.Success;55    }56  }5758  return TransactionStatus.Fail;59}6061export function submitTransactionAsync(sender: IKeyringPair, transaction: SubmittableExtrinsic<ApiTypes>): Promise<EventRecord[]> {62  return new Promise(async function(resolve, reject) {63    try {64      await transaction.signAndSend(sender, ({ events = [], status }) => {65        const transactionStatus = getTransactionStatus(events, status);6667        if (transactionStatus == TransactionStatus.Success) {68          resolve(events);69        } else if (transactionStatus == TransactionStatus.Fail) {70          console.log(`Something went wrong with transaction. Status: ${status}`);71          reject("Transaction failed");72        }73      });74    } catch (e) {75      console.log("Error: ", e);76      reject(e);77    }78  });79}8081export function submitTransactionExpectFailAsync(sender: IKeyringPair, transaction: SubmittableExtrinsic<ApiTypes>): Promise<EventRecord[]> {82  const consoleError = console.error;83  const consoleLog = console.log;84  console.error = () => {};85  console.log = () => {};8687  return new Promise<EventRecord[]>(async function(res, rej) {88    const resolve = (rec: EventRecord[]) => {89      setTimeout(() => {90        res(rec);91        console.error = consoleError;92        console.log = consoleLog;93        94      });95    };96    const reject = (errror: any) => {97      setTimeout(() => {98        rej(errror);99        console.error = consoleError;100        console.log = consoleLog;101      });102    };103    try {104      await transaction.signAndSend(sender, ({ events = [], status }) => {105        const transactionStatus = getTransactionStatus(events, status);106107        if (transactionStatus == TransactionStatus.Success) {108          resolve(events);109        } else if (transactionStatus == TransactionStatus.Fail) {110          reject("Transaction failed");111        }112      });113    } catch (e) {114      reject(e);115    }116  });117}
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -7,7 +7,7 @@
 import chaiAsPromised from 'chai-as-promised';
 import type { AccountId, EventRecord } from '@polkadot/types/interfaces';
 import { ApiPromise, Keyring } from "@polkadot/api";
-import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";
 import privateKey from '../substrate/privateKey';
 import { alicesPublicKey, nullPublicKey } from "../accounts";
 import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';
@@ -147,7 +147,7 @@
     // Run the CreateCollection transaction
     const alicePrivateKey = privateKey('//Alice');
     const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);
-    const events = await submitTransactionAsync(alicePrivateKey, tx);
+    const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
     const result = getCreateCollectionResult(events);
 
     // Get number of collections after the transaction
@@ -187,11 +187,7 @@
     // Run the DestroyCollection transaction
     const alicePrivateKey = privateKey(senderSeed);
     const tx = api.tx.nft.destroyCollection(collectionId);
-    const events = await submitTransactionAsync(alicePrivateKey, tx);
-    const result = getDestroyResult(events);
-
-    // What to expect
-    expect(result).to.be.false;
+    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
   });
 }
 
@@ -257,11 +253,7 @@
     // Run the transaction
     const alicePrivateKey = privateKey('//Alice');
     const tx = api.tx.nft.removeCollectionSponsor(collectionId);
-    const events = await submitTransactionAsync(alicePrivateKey, tx);
-    const result = getGenericResult(events);
-
-    // What to expect
-    expect(result.success).to.be.false;
+    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
   });
 }
 
@@ -271,11 +263,7 @@
     // Run the transaction
     const alicePrivateKey = privateKey(senderSeed);
     const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);
-    const events = await submitTransactionAsync(alicePrivateKey, tx);
-    const result = getGenericResult(events);
-
-    // What to expect
-    expect(result.success).to.be.false;
+    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
   });
 }
 
@@ -304,11 +292,7 @@
     // Run the transaction
     const sender = privateKey(senderSeed);
     const tx = api.tx.nft.confirmSponsorship(collectionId);
-    const events = await submitTransactionAsync(sender, tx);
-    const result = getGenericResult(events);
-
-    // What to expect
-    expect(result.success).to.be.false;
+    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
   });
 }