difftreelog
WaitNewBlocks instead timeout function
in: master
11 files changed
tests/src/collision-tests/admVsOwnerChanges.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/admVsOwnerChanges.test.ts
+++ b/tests/src/collision-tests/admVsOwnerChanges.test.ts
@@ -4,13 +4,10 @@
import privateKey from '../substrate/privateKey';
import usingApi, { submitTransactionAsync } from '../substrate/substrate-api';
import {
- addCollectionAdminExpectSuccess,
- approveExpectSuccess,
createCollectionExpectSuccess,
createItemExpectSuccess,
- transferFromExpectSuccess,
- transferExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -35,7 +32,6 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTxBob = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTxBob);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
const changeAdminTxFerdie = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Ferdie.address));
await submitTransactionAsync(Bob, changeAdminTxFerdie);
const itemId = await createItemExpectSuccess(Ferdie, collectionId, 'NFT');
@@ -50,7 +46,7 @@
]);
const itemBefore: any = await api.query.nft.nftItemList(collectionId, itemId);
expect(itemBefore.Owner).not.to.be.eq(Bob.address);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/admVsOwnerData.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/admVsOwnerData.test.ts
+++ b/tests/src/collision-tests/admVsOwnerData.test.ts
@@ -7,6 +7,7 @@
createCollectionExpectSuccess,
createItemExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -29,7 +30,6 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTx);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
const itemId = await createItemExpectSuccess(Bob, collectionId, 'NFT');
//
// tslint:disable-next-line: max-line-length
@@ -42,7 +42,7 @@
]);
const item: any = await api.query.nft.nftItemList(collectionId, itemId);
expect(item.VariableData).not.to.be.eq(null); // Pseudo-random selection of one of two values
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/admVsOwnerTake.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/admVsOwnerTake.test.ts
+++ b/tests/src/collision-tests/admVsOwnerTake.test.ts
@@ -7,6 +7,7 @@
createCollectionExpectSuccess,
createItemExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -30,7 +31,6 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTx);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
const itemId = await createItemExpectSuccess(Bob, collectionId, 'NFT');
//
const sendItem = api.tx.nft.transfer(normalizeAccountId(Ferdie.address), collectionId, itemId, 1);
@@ -39,12 +39,12 @@
sendItem.signAndSend(Bob),
burnItem.signAndSend(Alice),
]);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
let itemBurn = false;
itemBurn = (await (api.query.nft.nftItemList(collectionId, itemId))).toJSON() as boolean;
// tslint:disable-next-line: no-unused-expression
expect(itemBurn).to.be.null;
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/adminDestroyCollection.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/adminDestroyCollection.test.ts
+++ b/tests/src/collision-tests/adminDestroyCollection.test.ts
@@ -6,6 +6,7 @@
import {
createCollectionExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -29,8 +30,7 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTx);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
- await timeoutPromise(10000);
+ await waitNewBlocks(1);
//
const addWhitelistAdm = api.tx.nft.addToWhiteList(collectionId, normalizeAccountId(Ferdie.address));
const destroyCollection = api.tx.nft.destroyCollection(collectionId);
@@ -38,12 +38,12 @@
addWhitelistAdm.signAndSend(Bob),
destroyCollection.signAndSend(Alice),
]);
- await timeoutPromise(10000);
+ await waitNewBlocks(1);
let whiteList = false;
whiteList = (await api.query.nft.whiteList(collectionId, Ferdie.address)).toJSON() as boolean;
// tslint:disable-next-line: no-unused-expression
expect(whiteList).to.be.false;
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/adminLimitsOff.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/adminLimitsOff.test.ts
+++ b/tests/src/collision-tests/adminLimitsOff.test.ts
@@ -7,6 +7,7 @@
import {
createCollectionExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -45,14 +46,13 @@
const changeAdminTx3 = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTx3);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
const addAdmOne = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Ferdie.address));
const addAdmTwo = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Charlie.address));
await Promise.all([
addAdmOne.signAndSend(Bob),
addAdmTwo.signAndSend(Alice),
]);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
const changeAdminTx4 = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Alice.address));
await expect(submitTransactionExpectFailAsync(Alice, changeAdminTx4)).to.be.rejected;
@@ -60,7 +60,7 @@
expect(adminListAfterAddAdmin).to.be.contains(normalizeAccountId(Eve.address));
expect(adminListAfterAddAdmin).to.be.contains(normalizeAccountId(Ferdie.address));
expect(adminListAfterAddAdmin).not.to.be.contains(normalizeAccountId(Alice.address));
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/adminRightsOff.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/adminRightsOff.test.ts
+++ b/tests/src/collision-tests/adminRightsOff.test.ts
@@ -7,6 +7,7 @@
import {
createCollectionExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -28,8 +29,7 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTx);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
- await timeoutPromise(10000);
+ await waitNewBlocks(1);
const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }];
const addItemAdm = api.tx.nft.createMultipleItems(collectionId, normalizeAccountId(Bob.address), args);
const removeAdm = api.tx.nft.removeCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
@@ -37,12 +37,12 @@
addItemAdm.signAndSend(Bob),
removeAdm.signAndSend(Alice),
]);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
const itemsListIndex = await api.query.nft.itemListIndex(collectionId) as unknown as BN;
expect(itemsListIndex.toNumber()).to.be.equal(0);
const adminList: any = (await api.query.nft.adminList(collectionId));
expect(adminList).not.to.be.contains(normalizeAccountId(Bob.address));
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/setSponsorNewOwner.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/setSponsorNewOwner.test.ts
+++ b/tests/src/collision-tests/setSponsorNewOwner.test.ts
@@ -6,7 +6,7 @@
import {
createCollectionExpectSuccess,
setCollectionSponsorExpectSuccess,
- normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -29,19 +29,18 @@
await usingApi(async (api) => {
const collectionId = await createCollectionExpectSuccess();
await setCollectionSponsorExpectSuccess(collectionId, Bob.address);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
const confirmSponsorship = api.tx.nft.confirmSponsorship(collectionId);
const changeCollectionOwner = api.tx.nft.changeCollectionOwner(collectionId, Ferdie.address);
await Promise.all([
confirmSponsorship.signAndSend(Bob),
changeCollectionOwner.signAndSend(Alice),
]);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON();
expect(collection.Sponsorship.confirmed).to.be.eq(Bob.address);
expect(collection.Owner).to.be.eq(Ferdie.address);
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/sponsorPayments.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/sponsorPayments.test.ts
+++ b/tests/src/collision-tests/sponsorPayments.test.ts
@@ -11,6 +11,7 @@
createItemExpectSuccess,
setCollectionSponsorExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -32,7 +33,6 @@
const collectionId = await createCollectionExpectSuccess();
const changeAdminTxBob = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Bob.address));
await submitTransactionAsync(Alice, changeAdminTxBob);
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
const itemId = await createItemExpectSuccess(Bob, collectionId, 'NFT');
await setCollectionSponsorExpectSuccess(collectionId, Bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -49,7 +49,7 @@
expect(alicesBalanceAfter === alicesBalanceBefore).to.be.true;
// tslint:disable-next-line:no-unused-expression
expect(bobsBalanceAfter === bobsBalanceBefore).to.be.true;
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/collision-tests/tokenLimitsOff.test.tsdiffbeforeafterboth1import { IKeyringPair } from '@polkadot/types/types';2import BN from 'bn.js';3import chai from 'chai';4import chaiAsPromised from 'chai-as-promised';5import privateKey from '../substrate/privateKey';6import usingApi, { submitTransactionAsync } from '../substrate/substrate-api';7import {8 addToWhiteListExpectSuccess,9 createCollectionExpectSuccess,10 getCreateItemResult,11 setMintPermissionExpectSuccess,12 normalizeAccountId,13} from '../util/helpers';1415chai.use(chaiAsPromised);16const expect = chai.expect;17let Alice: IKeyringPair;18let Bob: IKeyringPair;19let Ferdie: IKeyringPair;2021const AccountTokenOwnershipLimit = 4;22const SponsoredMintSize = 4294967295;23const TokenLimit = 4;24const SponsorTimeout = 14400;25const OwnerCanTransfer = false;26const OwnerCanDestroy = false;2728before(async () => {29 await usingApi(async () => {30 Alice = privateKey('//Alice');31 Bob = privateKey('//Bob');32 Ferdie = privateKey('//Ferdie');33 });34});3536describe('Token limit exceeded collection: ', () => {37 // tslint:disable-next-line: max-line-length38 it('The number of tokens created in the collection from different addresses exceeds the allowed collection limit ', async () => {39 await usingApi(async (api) => {40 const collectionId = await createCollectionExpectSuccess();41 await setMintPermissionExpectSuccess(Alice, collectionId, true);42 await addToWhiteListExpectSuccess(Alice, collectionId, Ferdie.address);43 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);44 const setCollectionLim = api.tx.nft.setCollectionLimits(45 collectionId,46 {47 AccountTokenOwnershipLimit,48 SponsoredMintSize,49 TokenLimit,50 // tslint:disable-next-line: object-literal-sort-keys51 SponsorTimeout,52 OwnerCanTransfer,53 OwnerCanDestroy,54 },55 );56 const subTx = await submitTransactionAsync(Alice, setCollectionLim);57 const subTxTesult = getCreateItemResult(subTx);58 // tslint:disable-next-line:no-unused-expression59 expect(subTxTesult.success).to.be.true;60 const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));61 await timeoutPromise(20000);6263 const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }];64 const mintItemOne = api.tx.nft65 .createMultipleItems(collectionId, normalizeAccountId(Ferdie.address), args);66 const mintItemTwo = api.tx.nft67 .createMultipleItems(collectionId, normalizeAccountId(Bob.address), args);68 await Promise.all([69 mintItemOne.signAndSend(Ferdie),70 mintItemTwo.signAndSend(Bob),71 ]);72 await timeoutPromise(20000);73 const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;74 expect(itemsListIndexAfter.toNumber()).to.be.equal(3);75 // TokenLimit = 4. The first transaction is successful. The second should fail.76 await timeoutPromise(20000);77 });78 });79});1import { IKeyringPair } from '@polkadot/types/types';2import BN from 'bn.js';3import chai from 'chai';4import chaiAsPromised from 'chai-as-promised';5import privateKey from '../substrate/privateKey';6import usingApi, { submitTransactionAsync } from '../substrate/substrate-api';7import {8 addToWhiteListExpectSuccess,9 createCollectionExpectSuccess,10 getCreateItemResult,11 setMintPermissionExpectSuccess,12 normalizeAccountId,13 waitNewBlocks,14} from '../util/helpers';1516chai.use(chaiAsPromised);17const expect = chai.expect;18let Alice: IKeyringPair;19let Bob: IKeyringPair;20let Ferdie: IKeyringPair;2122const AccountTokenOwnershipLimit = 4;23const SponsoredMintSize = 4294967295;24const TokenLimit = 4;25const SponsorTimeout = 14400;26const OwnerCanTransfer = false;27const OwnerCanDestroy = false;2829before(async () => {30 await usingApi(async () => {31 Alice = privateKey('//Alice');32 Bob = privateKey('//Bob');33 Ferdie = privateKey('//Ferdie');34 });35});3637describe('Token limit exceeded collection: ', () => {38 // tslint:disable-next-line: max-line-length39 it('The number of tokens created in the collection from different addresses exceeds the allowed collection limit ', async () => {40 await usingApi(async (api) => {41 const collectionId = await createCollectionExpectSuccess();42 await setMintPermissionExpectSuccess(Alice, collectionId, true);43 await addToWhiteListExpectSuccess(Alice, collectionId, Ferdie.address);44 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);45 const setCollectionLim = api.tx.nft.setCollectionLimits(46 collectionId,47 {48 AccountTokenOwnershipLimit,49 SponsoredMintSize,50 TokenLimit,51 // tslint:disable-next-line: object-literal-sort-keys52 SponsorTimeout,53 OwnerCanTransfer,54 OwnerCanDestroy,55 },56 );57 const subTx = await submitTransactionAsync(Alice, setCollectionLim);58 const subTxTesult = getCreateItemResult(subTx);59 // tslint:disable-next-line:no-unused-expression60 expect(subTxTesult.success).to.be.true;61 await waitNewBlocks(2);6263 const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }];64 const mintItemOne = api.tx.nft65 .createMultipleItems(collectionId, normalizeAccountId(Ferdie.address), args);66 const mintItemTwo = api.tx.nft67 .createMultipleItems(collectionId, normalizeAccountId(Bob.address), args);68 await Promise.all([69 mintItemOne.signAndSend(Ferdie),70 mintItemTwo.signAndSend(Bob),71 ]);72 await waitNewBlocks(2);73 const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;74 expect(itemsListIndexAfter.toNumber()).to.be.equal(3);75 // TokenLimit = 4. The first transaction is successful. The second should fail.76 await waitNewBlocks(2);77 });78 });79});tests/src/collision-tests/turnsOffMinting.test.tsdiffbeforeafterboth--- a/tests/src/collision-tests/turnsOffMinting.test.ts
+++ b/tests/src/collision-tests/turnsOffMinting.test.ts
@@ -8,6 +8,7 @@
createCollectionExpectSuccess,
setMintPermissionExpectSuccess,
normalizeAccountId,
+ waitNewBlocks,
} from '../util/helpers';
chai.use(chaiAsPromised);
@@ -27,7 +28,6 @@
it('The collection owner turns off minting mode and there are minting transactions in the same block ', async () => {
await usingApi(async (api) => {
const collectionId = await createCollectionExpectSuccess();
- const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout));
await setMintPermissionExpectSuccess(Alice, collectionId, true);
await addToWhiteListExpectSuccess(Alice, collectionId, Ferdie.address);
@@ -41,7 +41,7 @@
itemList = (await (api.query.nft.nftItemList(collectionId, mintItem))).toJSON() as boolean;
// tslint:disable-next-line: no-unused-expression
expect(itemList).to.be.null;
- await timeoutPromise(20000);
+ await waitNewBlocks(2);
});
});
});
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -1171,4 +1171,21 @@
export async function queryNftOwner(api: ApiPromise, collectionId: number, tokenId: number): Promise<CrossAccountId> {
return normalizeAccountId((await api.query.nft.nftItemList(collectionId, tokenId) as any).toJSON().Owner);
-}
\ No newline at end of file
+}
+
+export async function waitNewBlocks(blocksCount = 1): Promise<void> {
+ await usingApi(async (api) => {
+ const promise = new Promise<void>(async (resolve) => {
+
+ const unsubscribe = await api.rpc.chain.subscribeNewHeads(() => {
+ if (blocksCount > 0) {
+ blocksCount--;
+ } else {
+ unsubscribe();
+ resolve();
+ }
+ });
+ });
+ return promise;
+ });
+}