From e873ca1d6f23717cf0d6e5e49989ba2e349e7d80 Mon Sep 17 00:00:00 2001 From: kozyrevdev <73348153+kozyrevdev@users.noreply.github.com> Date: Thu, 09 Sep 2021 12:25:54 +0000 Subject: [PATCH] Merge pull request #200 from UniqueNetwork/feature/CORE-185 Collision tests fixed --- --- a/tests/src/collision-tests/admVsOwnerChanges.test.ts +++ b/tests/src/collision-tests/admVsOwnerChanges.test.ts @@ -6,6 +6,8 @@ import { createCollectionExpectSuccess, createItemExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -25,18 +27,18 @@ describe('Admin vs Owner changes token: ', () => { // tslint:disable-next-line: max-line-length it('The collection admin changes the owner of the token and in the same block the current owner transfers the token to another address ', async () => { + await usingApi(async (api) => { const collectionId = await createCollectionExpectSuccess(); - const changeAdminTxBob = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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, Ferdie.address); + const changeAdminTxFerdie = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Ferdie.address)); await submitTransactionAsync(Bob, changeAdminTxFerdie); const itemId = await createItemExpectSuccess(Ferdie, collectionId, 'NFT'); - // - const changeOwner = api.tx.nft.transferFrom(Ferdie.address, Bob.address, collectionId, itemId, 1); - const approve = api.tx.nft.approve(Bob.address, collectionId, itemId, 1); - const sendItem = api.tx.nft.transfer(Alice.address, collectionId, itemId, 1); + + const changeOwner = api.tx.nft.transferFrom(normalizeAccountId(Ferdie.address), normalizeAccountId(Bob.address), collectionId, itemId, 1); + const approve = api.tx.nft.approve(normalizeAccountId(Bob.address), collectionId, itemId, 1); + const sendItem = api.tx.nft.transfer(normalizeAccountId(Alice.address), collectionId, itemId, 1); await Promise.all([ changeOwner.signAndSend(Alice), approve.signAndSend(Bob), @@ -44,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); }); }); }); --- a/tests/src/collision-tests/admVsOwnerData.test.ts +++ b/tests/src/collision-tests/admVsOwnerData.test.ts @@ -6,6 +6,8 @@ import { createCollectionExpectSuccess, createItemExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -26,9 +28,8 @@ const AliceData = 1; const BobData = 2; const collectionId = await createCollectionExpectSuccess(); - const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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 @@ -41,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); }); }); }); --- a/tests/src/collision-tests/admVsOwnerTake.test.ts +++ b/tests/src/collision-tests/admVsOwnerTake.test.ts @@ -6,6 +6,8 @@ import { createCollectionExpectSuccess, createItemExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -27,23 +29,22 @@ it('The collection admin burns the token and in the same block the token owner performs a transaction on it ', async () => { await usingApi(async (api) => { const collectionId = await createCollectionExpectSuccess(); - const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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(Ferdie.address, collectionId, itemId, 1); + const sendItem = api.tx.nft.transfer(normalizeAccountId(Ferdie.address), collectionId, itemId, 1); const burnItem = api.tx.nft.burnItem(collectionId, itemId, 1); await Promise.all([ sendItem.signAndSend(Bob), burnItem.signAndSend(Alice), ]); - await timeoutPromise(10000); + 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); }); }); }); --- a/tests/src/collision-tests/adminDestroyCollection.test.ts +++ b/tests/src/collision-tests/adminDestroyCollection.test.ts @@ -5,6 +5,8 @@ import usingApi, { submitTransactionAsync } from '../substrate/substrate-api'; import { createCollectionExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -26,23 +28,22 @@ it('Adding an address to the collection whitelist in a block by the admin, and deleting the collection by the owner ', async () => { await usingApi(async (api) => { const collectionId = await createCollectionExpectSuccess(); - const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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, Ferdie.address); + const addWhitelistAdm = api.tx.nft.addToWhiteList(collectionId, normalizeAccountId(Ferdie.address)); const destroyCollection = api.tx.nft.destroyCollection(collectionId); await Promise.all([ 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); }); }); }); --- a/tests/src/collision-tests/adminLimitsOff.test.ts +++ b/tests/src/collision-tests/adminLimitsOff.test.ts @@ -5,6 +5,8 @@ import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from '../substrate/substrate-api'; import { createCollectionExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -36,29 +38,28 @@ const chainAdminLimit = (api.consts.nft.collectionAdminsLimit as any).toNumber(); expect(chainAdminLimit).to.be.equal(5); - const changeAdminTx1 = api.tx.nft.addCollectionAdmin(collectionId, Eve.address); + const changeAdminTx1 = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Eve.address)); await submitTransactionAsync(Alice, changeAdminTx1); - const changeAdminTx2 = api.tx.nft.addCollectionAdmin(collectionId, Dave.address); + const changeAdminTx2 = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Dave.address)); await submitTransactionAsync(Alice, changeAdminTx2); - const changeAdminTx3 = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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, Ferdie.address); - const addAdmTwo = api.tx.nft.addCollectionAdmin(collectionId, Charlie.address); + 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(10000); - const changeAdminTx4 = api.tx.nft.addCollectionAdmin(collectionId, Alice.address); + await waitNewBlocks(2); + const changeAdminTx4 = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(Alice.address)); await expect(submitTransactionExpectFailAsync(Alice, changeAdminTx4)).to.be.rejected; const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId)); - expect(adminListAfterAddAdmin).to.be.contains(Eve.address); - expect(adminListAfterAddAdmin).to.be.contains(Ferdie.address); - expect(adminListAfterAddAdmin).not.to.be.contains(Alice.address); - await timeoutPromise(20000); + 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 waitNewBlocks(2); }); }); }); --- a/tests/src/collision-tests/adminRightsOff.test.ts +++ b/tests/src/collision-tests/adminRightsOff.test.ts @@ -6,6 +6,8 @@ import usingApi, { submitTransactionAsync } from '../substrate/substrate-api'; import { createCollectionExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -25,23 +27,22 @@ it('In the block, the collection admin adds a token or changes data, and the collection owner deprives the admin of rights ', async () => { await usingApi(async (api) => { const collectionId = await createCollectionExpectSuccess(); - const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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, Bob.address, args); - const removeAdm = api.tx.nft.removeCollectionAdmin(collectionId, Bob.address); + const addItemAdm = api.tx.nft.createMultipleItems(collectionId, normalizeAccountId(Bob.address), args); + const removeAdm = api.tx.nft.removeCollectionAdmin(collectionId, normalizeAccountId(Bob.address)); await Promise.all([ addItemAdm.signAndSend(Bob), removeAdm.signAndSend(Alice), ]); - await timeoutPromise(10000); + 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(Bob.address); - await timeoutPromise(20000); + expect(adminList).not.to.be.contains(normalizeAccountId(Bob.address)); + await waitNewBlocks(2); }); }); }); --- a/tests/src/collision-tests/setSponsorNewOwner.test.ts +++ b/tests/src/collision-tests/setSponsorNewOwner.test.ts @@ -4,7 +4,9 @@ import privateKey from '../substrate/privateKey'; import usingApi from '../substrate/substrate-api'; import { - createCollectionExpectSuccess, setCollectionSponsorExpectSuccess, + createCollectionExpectSuccess, + setCollectionSponsorExpectSuccess, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -27,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(10000); + 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(10000); + await waitNewBlocks(2); const collection: any = (await api.query.nft.collectionById(collectionId)).toJSON(); - expect(collection.Sponsorship.Confirmed).to.be.eq(Bob.address); + expect(collection.Sponsorship.confirmed).to.be.eq(Bob.address); expect(collection.Owner).to.be.eq(Ferdie.address); - await timeoutPromise(20000); + await waitNewBlocks(2); }); }); }); --- a/tests/src/collision-tests/sponsorPayments.test.ts +++ b/tests/src/collision-tests/sponsorPayments.test.ts @@ -10,6 +10,8 @@ createCollectionExpectSuccess, createItemExpectSuccess, setCollectionSponsorExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -29,15 +31,14 @@ it('Payment of commission if one block contains transactions for payment from the sponsor\'s balance and his (sponsor\'s) exclusion from the collection ', async () => { await usingApi(async (api) => { const collectionId = await createCollectionExpectSuccess(); - const changeAdminTxBob = api.tx.nft.addCollectionAdmin(collectionId, Bob.address); + 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'); const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]); - const sendItem = api.tx.nft.transfer(Alice.address, collectionId, itemId, 1); + const sendItem = api.tx.nft.transfer(normalizeAccountId(Alice.address), collectionId, itemId, 1); const revokeSponsor = api.tx.nft.removeCollectionSponsor(collectionId); await Promise.all([ sendItem.signAndSend(Bob), @@ -48,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); }); }); }); --- a/tests/src/collision-tests/tokenLimitsOff.test.ts +++ b/tests/src/collision-tests/tokenLimitsOff.test.ts @@ -9,6 +9,8 @@ createCollectionExpectSuccess, getCreateItemResult, setMintPermissionExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -56,23 +58,22 @@ const subTxTesult = getCreateItemResult(subTx); // tslint:disable-next-line:no-unused-expression expect(subTxTesult.success).to.be.true; - const timeoutPromise = (timeout: number) => new Promise((resolve) => setTimeout(resolve, timeout)); - await timeoutPromise(10000); + await waitNewBlocks(2); const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }]; const mintItemOne = api.tx.nft - .createMultipleItems(collectionId, Ferdie.address, args); + .createMultipleItems(collectionId, normalizeAccountId(Ferdie.address), args); const mintItemTwo = api.tx.nft - .createMultipleItems(collectionId, Bob.address, args); + .createMultipleItems(collectionId, normalizeAccountId(Bob.address), args); await Promise.all([ mintItemOne.signAndSend(Ferdie), mintItemTwo.signAndSend(Bob), ]); - await timeoutPromise(10000); + await waitNewBlocks(2); const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN; expect(itemsListIndexAfter.toNumber()).to.be.equal(3); // TokenLimit = 4. The first transaction is successful. The second should fail. - await timeoutPromise(10000); + await waitNewBlocks(2); }); }); }); --- a/tests/src/collision-tests/turnsOffMinting.test.ts +++ b/tests/src/collision-tests/turnsOffMinting.test.ts @@ -7,6 +7,8 @@ addToWhiteListExpectSuccess, createCollectionExpectSuccess, setMintPermissionExpectSuccess, + normalizeAccountId, + waitNewBlocks, } from '../util/helpers'; chai.use(chaiAsPromised); @@ -26,11 +28,10 @@ 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); - const mintItem = api.tx.nft.createItem(collectionId, Ferdie.address, 'NFT'); + const mintItem = api.tx.nft.createItem(collectionId, normalizeAccountId(Ferdie.address), 'NFT'); const offMinting = api.tx.nft.setMintPermission(collectionId, false); await Promise.all([ mintItem.signAndSend(Ferdie), @@ -40,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); }); }); }); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -1193,4 +1193,21 @@ export async function queryNftOwner(api: ApiPromise, collectionId: number, tokenId: number): Promise { 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 { + await usingApi(async (api) => { + const promise = new Promise(async (resolve) => { + + const unsubscribe = await api.rpc.chain.subscribeNewHeads(() => { + if (blocksCount > 0) { + blocksCount--; + } else { + unsubscribe(); + resolve(); + } + }); + }); + return promise; + }); +} -- gitstuff