From 0c093d5c3f30a379f396d0a98881be71262e3c19 Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Wed, 14 Sep 2022 19:39:51 +0000 Subject: [PATCH] use itSub --- --- a/tests/src/app-promotion.test.ts +++ b/tests/src/app-promotion.test.ts @@ -23,7 +23,7 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; -import {usingPlaygrounds} from './util/playgrounds'; +import {itSub, usingPlaygrounds} from './util/playgrounds'; import {encodeAddress} from '@polkadot/util-crypto'; import {stringToU8a} from '@polkadot/util'; @@ -59,78 +59,70 @@ await beforeEach(this); }); - it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => { - await usingPlaygrounds(async (helper) => { - const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; - const totalStakedBefore = await helper.staking.getTotalStaked(); + itSub('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async ({helper}) => { + const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; + const totalStakedBefore = await helper.staking.getTotalStaked(); - // Minimum stake amount is 100: - await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected; - await helper.staking.stake(staker, 100n * nominal); + // Minimum stake amount is 100: + await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected; + await helper.staking.stake(staker, 100n * nominal); - // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n... - // ...so he can not transfer 900 - expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n}); - await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected; - - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal); - expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); - // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? - expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased + // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n... + // ...so he can not transfer 900 + expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n}); + await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected; + + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); + // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? + expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased - - await helper.staking.stake(staker, 200n * nominal); - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal); - const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal); - expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal); - }); + + await helper.staking.stake(staker, 200n * nominal); + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal); + const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal); + expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal); }); - it('should allow to create maximum 10 stakes for account', async () => { - await usingPlaygrounds(async (helper) => { - const [staker] = await helper.arrange.createAccounts([2000n], alice); - for (let i = 0; i < 10; i++) { - await helper.staking.stake(staker, 100n * nominal); - } + itSub('should allow to create maximum 10 stakes for account', async ({helper}) => { + const [staker] = await helper.arrange.createAccounts([2000n], alice); + for (let i = 0; i < 10; i++) { + await helper.staking.stake(staker, 100n * nominal); + } - // can have 10 stakes - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal); - expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10); + // can have 10 stakes + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal); + expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10); - await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejected; + await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejected; - // After unstake can stake again - await helper.staking.unstake(staker); - await helper.staking.stake(staker, 100n * nominal); - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal); - }); + // After unstake can stake again + await helper.staking.unstake(staker); + await helper.staking.stake(staker, 100n * nominal); + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal); }); - it('should reject transaction if stake amount is more than total free balance minus frozen', async () => { - await usingPlaygrounds(async helper => { - const staker = accounts.pop()!; + itSub('should reject transaction if stake amount is more than total free balance minus frozen', async ({helper}) => { + const staker = accounts.pop()!; - // Can't stake full balance because Alice needs to pay some fee - await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.eventually.rejected; - await helper.staking.stake(staker, 500n * nominal); + // Can't stake full balance because Alice needs to pay some fee + await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.eventually.rejected; + await helper.staking.stake(staker, 500n * nominal); - // Can't stake 500 tkn because Alice has Less than 500 transferable; - await expect(helper.staking.stake(staker, 500n * nominal)).to.be.eventually.rejected; - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal); - }); + // Can't stake 500 tkn because Alice has Less than 500 transferable; + await expect(helper.staking.stake(staker, 500n * nominal)).to.be.eventually.rejected; + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal); }); - it('for different accounts in one block is possible', async () => { - await usingPlaygrounds(async helper => { - const crowd = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!]; + itSub('for different accounts in one block is possible', async ({helper}) => { + const crowd = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!]; - const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal)); - await expect(Promise.all(crowdStartsToStake)).to.be.eventually.fulfilled; + const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal)); + await expect(Promise.all(crowdStartsToStake)).to.be.eventually.fulfilled; - const crowdStakes = await Promise.all(crowd.map(address => helper.staking.getTotalStaked({Substrate: address.address}))); - expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]); - }); + const crowdStakes = await Promise.all(crowd.map(address => helper.staking.getTotalStaked({Substrate: address.address}))); + expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]); }); }); @@ -139,122 +131,110 @@ await beforeEach(this); }); - it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => { - await usingPlaygrounds(async helper => { - const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; - const totalStakedBefore = await helper.staking.getTotalStaked(); - await helper.staking.stake(staker, 900n * nominal); - await helper.staking.unstake(staker); + itSub('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async ({helper}) => { + const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; + const totalStakedBefore = await helper.staking.getTotalStaked(); + await helper.staking.stake(staker, 900n * nominal); + await helper.staking.unstake(staker); - // Right after unstake balance is reserved - // Staker can not transfer - expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 900n * nominal, miscFrozen: 0n, feeFrozen: 0n}); - await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected; - expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(900n * nominal); - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); - expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore); - }); + // Right after unstake balance is reserved + // Staker can not transfer + expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 900n * nominal, miscFrozen: 0n, feeFrozen: 0n}); + await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected; + expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(900n * nominal); + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); + expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore); }); - it('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async () => { - await usingPlaygrounds(async (helper) => { - const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); - await helper.staking.unstake(staker); - const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); + itSub('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async ({helper}) => { + const staker = accounts.pop()!; + await helper.staking.stake(staker, 100n * nominal); + await helper.staking.unstake(staker); + const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); - // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n - await helper.wait.forParachainBlockNumber(pendingUnstake.block); - expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n}); - expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); + // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n + await helper.wait.forParachainBlockNumber(pendingUnstake.block); + expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n}); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); - // staker can transfer: - await helper.balance.transferToSubstrate(staker, alice.address, 998n * nominal); - expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n); - }); + // staker can transfer: + await helper.balance.transferToSubstrate(staker, alice.address, 998n * nominal); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n); }); - it('should successfully unstake multiple stakes', async () => { - await usingPlaygrounds(async helper => { - const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); - await helper.staking.stake(staker, 200n * nominal); - await helper.staking.stake(staker, 300n * nominal); + itSub('should successfully unstake multiple stakes', async ({helper}) => { + const staker = accounts.pop()!; + await helper.staking.stake(staker, 100n * nominal); + await helper.staking.stake(staker, 200n * nominal); + await helper.staking.stake(staker, 300n * nominal); - // staked: [100, 200, 300]; unstaked: 0 - let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address}); - let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); - let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(totalPendingUnstake).to.be.deep.equal(0n); - expect(pendingUnstake).to.be.deep.equal([]); - expect(stakes[0].amount).to.equal(100n * nominal); - expect(stakes[1].amount).to.equal(200n * nominal); - expect(stakes[2].amount).to.equal(300n * nominal); + // staked: [100, 200, 300]; unstaked: 0 + let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address}); + let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); + let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(totalPendingUnstake).to.be.deep.equal(0n); + expect(pendingUnstake).to.be.deep.equal([]); + expect(stakes[0].amount).to.equal(100n * nominal); + expect(stakes[1].amount).to.equal(200n * nominal); + expect(stakes[2].amount).to.equal(300n * nominal); - // Can unstake multiple stakes - await helper.staking.unstake(staker); - pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); - totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address}); - stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(totalPendingUnstake).to.be.equal(600n * nominal); - expect(stakes).to.be.deep.equal([]); - expect(pendingUnstake[0].amount).to.equal(600n * nominal); + // Can unstake multiple stakes + await helper.staking.unstake(staker); + pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); + totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address}); + stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(totalPendingUnstake).to.be.equal(600n * nominal); + expect(stakes).to.be.deep.equal([]); + expect(pendingUnstake[0].amount).to.equal(600n * nominal); - expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n}); - await helper.wait.forParachainBlockNumber(pendingUnstake[0].block); - expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n}); - expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); - }); + expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n}); + await helper.wait.forParachainBlockNumber(pendingUnstake[0].block); + expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n}); + expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); }); - it('should not have any effects if no active stakes', async () => { - await usingPlaygrounds(async (helper) => { - const staker = accounts.pop()!; + itSub('should not have any effects if no active stakes', async ({helper}) => { + const staker = accounts.pop()!; - // unstake has no effect if no stakes at all - await helper.staking.unstake(staker); - expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n); - expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper + // unstake has no effect if no stakes at all + await helper.staking.unstake(staker); + expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper - // TODO stake() unstake() waitUnstaked() unstake(); + // TODO stake() unstake() waitUnstaked() unstake(); - // can't unstake if there are only pendingUnstakes - await helper.staking.stake(staker, 100n * nominal); - await helper.staking.unstake(staker); - await helper.staking.unstake(staker); + // can't unstake if there are only pendingUnstakes + await helper.staking.stake(staker, 100n * nominal); + await helper.staking.unstake(staker); + await helper.staking.unstake(staker); - expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal); - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); - }); + expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal); + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); }); - it('should keep different unlocking block for each unlocking stake', async () => { - await usingPlaygrounds(async (helper) => { - const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); - await helper.staking.unstake(staker); - await helper.staking.stake(staker, 120n * nominal); - await helper.staking.unstake(staker); + itSub('should keep different unlocking block for each unlocking stake', async ({helper}) => { + const staker = accounts.pop()!; + await helper.staking.stake(staker, 100n * nominal); + await helper.staking.unstake(staker); + await helper.staking.stake(staker, 120n * nominal); + await helper.staking.unstake(staker); - const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); - expect(unstakingPerBlock).has.length(2); - expect(unstakingPerBlock[0].amount).to.equal(100n * nominal); - expect(unstakingPerBlock[1].amount).to.equal(120n * nominal); - }); + const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}); + expect(unstakingPerBlock).has.length(2); + expect(unstakingPerBlock[0].amount).to.equal(100n * nominal); + expect(unstakingPerBlock[1].amount).to.equal(120n * nominal); }); - it('should be possible for different accounts in one block', async () => { - await usingPlaygrounds(async (helper) => { - const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; + itSub('should be possible for different accounts in one block', async ({helper}) => { + const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; - await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal))); - await Promise.all(stakers.map(staker => helper.staking.unstake(staker))); + await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal))); + await Promise.all(stakers.map(staker => helper.staking.unstake(staker))); - await Promise.all(stakers.map(async (staker) => { - expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal); - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); - })); - }); + await Promise.all(stakers.map(async (staker) => { + expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal); + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); + })); }); }); @@ -263,45 +243,39 @@ await beforeEach(this); }); - it('can be set by sudo only', async () => { - await usingPlaygrounds(async (helper) => { - const nonAdmin = accounts.pop()!; - // nonAdmin can not set admin not from himself nor as a sudo - await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address}))).to.be.eventually.rejected; - await expect(helper.signTransaction(nonAdmin, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address})))).to.be.eventually.rejected; + itSub('can be set by sudo only', async ({helper}) => { + const nonAdmin = accounts.pop()!; + // nonAdmin can not set admin not from himself nor as a sudo + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address}))).to.be.eventually.rejected; + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address})))).to.be.eventually.rejected; - // Alice can - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; - }); + // Alice can + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; }); - it('can be any valid CrossAccountId', async () => { + itSub('can be any valid CrossAccountId', async ({helper}) => { // We are not going to set an eth address as a sponsor, // but we do want to check, it doesn't break anything; - await usingPlaygrounds(async (helper) => { - const account = accounts.pop()!; - const ethAccount = helper.address.substrateToEth(account.address); - // Alice sets Ethereum address as a sudo. Then Substrate address back... - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Ethereum: ethAccount})))).to.be.eventually.fulfilled; - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; + const account = accounts.pop()!; + const ethAccount = helper.address.substrateToEth(account.address); + // Alice sets Ethereum address as a sudo. Then Substrate address back... + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Ethereum: ethAccount})))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; - // ...It doesn't break anything; - const collection = await helper.nft.mintCollection(account, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(account, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; - }); + // ...It doesn't break anything; + const collection = await helper.nft.mintCollection(account, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + await expect(helper.signTransaction(account, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; }); - it('can be reassigned', async () => { - await usingPlaygrounds(async (helper) => { - const [oldAdmin, newAdmin, collectionOwner] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + itSub('can be reassigned', async ({helper}) => { + const [oldAdmin, newAdmin, collectionOwner] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled; - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(newAdmin))))).to.be.eventually.fulfilled; - await expect(helper.signTransaction(oldAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(newAdmin))))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(oldAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; - await expect(helper.signTransaction(newAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled; - }); + await expect(helper.signTransaction(newAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled; }); }); @@ -314,99 +288,87 @@ }); }); - it('should actually sponsor transactions', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, tokenSender, receiver] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}}); - const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address}); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId)); - const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress); + itSub('should actually sponsor transactions', async ({helper}) => { + const [collectionOwner, tokenSender, receiver] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}}); + const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address}); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId)); + const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress); - await token.transfer(tokenSender, {Substrate: receiver.address}); - expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address}); - const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress); + await token.transfer(tokenSender, {Substrate: receiver.address}); + expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address}); + const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress); - // senders balance the same, transaction has sponsored - expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal); - expect (palletBalanceBefore > palletBalanceAfter).to.be.true; - }); + // senders balance the same, transaction has sponsored + expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal); + expect (palletBalanceBefore > palletBalanceAfter).to.be.true; }); - it('can not be set by non admin', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; + itSub('can not be set by non admin', async ({helper}) => { + const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; - expect((await collection.getData())?.raw.sponsorship).to.equal('Disabled'); - }); + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; + expect((await collection.getData())?.raw.sponsorship).to.equal('Disabled'); }); - it('should set pallet address as confirmed admin', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, oldSponsor] = [accounts.pop()!, accounts.pop()!]; + itSub('should set pallet address as confirmed admin', async ({helper}) => { + const [collectionOwner, oldSponsor] = [accounts.pop()!, accounts.pop()!]; - // Can set sponsoring for collection without sponsor - const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithoutSponsor.collectionId))).to.be.eventually.fulfilled; - expect((await collectionWithoutSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); + // Can set sponsoring for collection without sponsor + const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'}); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithoutSponsor.collectionId))).to.be.eventually.fulfilled; + expect((await collectionWithoutSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); - // Can set sponsoring for collection with unconfirmed sponsor - const collectionWithUnconfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Unconfirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address}); - expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: oldSponsor.address}); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithUnconfirmedSponsor.collectionId))).to.be.eventually.fulfilled; - expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); + // Can set sponsoring for collection with unconfirmed sponsor + const collectionWithUnconfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Unconfirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address}); + expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: oldSponsor.address}); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithUnconfirmedSponsor.collectionId))).to.be.eventually.fulfilled; + expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); - // Can set sponsoring for collection with confirmed sponsor - const collectionWithConfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Confirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address}); - await collectionWithConfirmedSponsor.confirmSponsorship(oldSponsor); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithConfirmedSponsor.collectionId))).to.be.eventually.fulfilled; - expect((await collectionWithConfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); - }); + // Can set sponsoring for collection with confirmed sponsor + const collectionWithConfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Confirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address}); + await collectionWithConfirmedSponsor.confirmSponsorship(oldSponsor); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithConfirmedSponsor.collectionId))).to.be.eventually.fulfilled; + expect((await collectionWithConfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); }); - it('can be overwritten by collection owner', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, newSponsor] = [accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - const collectionId = collection.collectionId; + itSub('can be overwritten by collection owner', async ({helper}) => { + const [collectionOwner, newSponsor] = [accounts.pop()!, accounts.pop()!]; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + const collectionId = collection.collectionId; - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionId))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionId))).to.be.eventually.fulfilled; - // Collection limits still can be changed by the owner - expect(await collection.setLimits(collectionOwner, {sponsorTransferTimeout: 0})).to.be.true; - expect((await collection.getData())?.raw.limits.sponsorTransferTimeout).to.be.equal(0); - expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); + // Collection limits still can be changed by the owner + expect(await collection.setLimits(collectionOwner, {sponsorTransferTimeout: 0})).to.be.true; + expect((await collection.getData())?.raw.limits.sponsorTransferTimeout).to.be.equal(0); + expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); - // Collection sponsor can be changed too - expect((await collection.setSponsor(collectionOwner, newSponsor.address))).to.be.true; - expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: newSponsor.address}); - }); + // Collection sponsor can be changed too + expect((await collection.setSponsor(collectionOwner, newSponsor.address))).to.be.true; + expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: newSponsor.address}); }); - it('should not overwrite collection limits set by the owner earlier', async () => { - await usingPlaygrounds(async (helper) => { - const limits = {ownerCanDestroy: true, ownerCanTransfer: true, sponsorTransferTimeout: 0}; - const collectionWithLimits = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits}); + itSub('should not overwrite collection limits set by the owner earlier', async ({helper}) => { + const limits = {ownerCanDestroy: true, ownerCanTransfer: true, sponsorTransferTimeout: 0}; + const collectionWithLimits = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits}); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithLimits.collectionId))).to.be.eventually.fulfilled; - expect((await collectionWithLimits.getData())?.raw.limits).to.be.deep.contain(limits); - }); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithLimits.collectionId))).to.be.eventually.fulfilled; + expect((await collectionWithLimits.getData())?.raw.limits).to.be.deep.contain(limits); }); - it('should reject transaction if collection doesn\'t exist', async () => { - await usingPlaygrounds(async (helper) => { - const collectionOwner = accounts.pop()!; + itSub('should reject transaction if collection doesn\'t exist', async ({helper}) => { + const collectionOwner = accounts.pop()!; - // collection has never existed - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(999999999))).to.be.eventually.rejected; - // collection has been burned - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await collection.burn(collectionOwner); + // collection has never existed + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(999999999))).to.be.eventually.rejected; + // collection has been burned + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + await collection.burn(collectionOwner); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; - }); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; }); }); @@ -415,58 +377,50 @@ await beforeEach(this); }); - it('can not be called by non-admin', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + itSub('can not be called by non-admin', async ({helper}) => { + const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled; - await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; - expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); - }); + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; + expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress}); }); - it('should set sponsoring as disabled', async () => { - await usingPlaygrounds(async (helper) => { - const [collectionOwner, recepient] = [accounts.pop()!, accounts.pop()!]; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}}); - const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address}); + itSub('should set sponsoring as disabled', async ({helper}) => { + const [collectionOwner, recepient] = [accounts.pop()!, accounts.pop()!]; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}}); + const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address}); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId)); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId)); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId)); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId)); - expect((await collection.getData())?.raw.sponsorship).to.be.equal('Disabled'); + expect((await collection.getData())?.raw.sponsorship).to.be.equal('Disabled'); - // Transactions are not sponsored anymore: - const ownerBalanceBefore = await helper.balance.getSubstrate(collectionOwner.address); - await token.transfer(collectionOwner, {Substrate: recepient.address}); - const ownerBalanceAfter = await helper.balance.getSubstrate(collectionOwner.address); - expect(ownerBalanceAfter < ownerBalanceBefore).to.be.equal(true); - }); + // Transactions are not sponsored anymore: + const ownerBalanceBefore = await helper.balance.getSubstrate(collectionOwner.address); + await token.transfer(collectionOwner, {Substrate: recepient.address}); + const ownerBalanceAfter = await helper.balance.getSubstrate(collectionOwner.address); + expect(ownerBalanceAfter < ownerBalanceBefore).to.be.equal(true); }); - it('should not affect collection which is not sponsored by pallete', async () => { - await usingPlaygrounds(async (helper) => { - const collectionOwner = accounts.pop()!; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address}); - await collection.confirmSponsorship(collectionOwner); + itSub('should not affect collection which is not sponsored by pallete', async ({helper}) => { + const collectionOwner = accounts.pop()!; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address}); + await collection.confirmSponsorship(collectionOwner); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; - expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: collectionOwner.address}); - }); + expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: collectionOwner.address}); }); - it('should reject transaction if collection does not exist', async () => { - await usingPlaygrounds(async (helper) => { - const collectionOwner = accounts.pop()!; - const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + itSub('should reject transaction if collection does not exist', async ({helper}) => { + const collectionOwner = accounts.pop()!; + const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await collection.burn(collectionOwner); - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; - await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(999999999))).to.be.eventually.rejected; - }); + await collection.burn(collectionOwner); + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected; + await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(999999999))).to.be.eventually.rejected; }); }); @@ -657,143 +611,127 @@ await beforeEach(this); }); - it('can not be called by non admin', async () => { - await usingPlaygrounds(async (helper) => { - const nonAdmin = accounts.pop()!; - await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.payoutStakers(100))).to.be.rejected; - }); + itSub('can not be called by non admin', async ({helper}) => { + const nonAdmin = accounts.pop()!; + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.payoutStakers(100))).to.be.rejected; }); - it('should increase total staked', async () => { - await usingPlaygrounds(async (helper) => { - const staker = accounts.pop()!; - const totalStakedBefore = await helper.staking.getTotalStaked(); - await helper.staking.stake(staker, 100n * nominal); + itSub('should increase total staked', async ({helper}) => { + const staker = accounts.pop()!; + const totalStakedBefore = await helper.staking.getTotalStaked(); + await helper.staking.stake(staker, 100n * nominal); - // Wait for rewards and pay - const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block)); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + // Wait for rewards and pay + const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block)); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - const totalStakedAfter = await helper.staking.getTotalStaked(); - expect(totalStakedAfter >= totalStakedBefore + calculateIncome(100n * nominal, 10n)).to.be.true; + const totalStakedAfter = await helper.staking.getTotalStaked(); + expect(totalStakedAfter >= totalStakedBefore + calculateIncome(100n * nominal, 10n)).to.be.true; - // staker can unstake - await helper.staking.unstake(staker); - expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal, 10n)); - }); + // staker can unstake + await helper.staking.unstake(staker); + expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal, 10n)); }); - it('should credit 0.05% for staking period', async () => { - await usingPlaygrounds(async helper => { - const staker = accounts.pop()!; + itSub('should credit 0.05% for staking period', async ({helper}) => { + const staker = accounts.pop()!; - await waitPromotionPeriodDoesntEnd(helper); + await waitPromotionPeriodDoesntEnd(helper); - await helper.staking.stake(staker, 100n * nominal); - await helper.staking.stake(staker, 200n * nominal); + await helper.staking.stake(staker, 100n * nominal); + await helper.staking.stake(staker, 200n * nominal); - // wait rewards are available: - const [_, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block)); + // wait rewards are available: + const [_, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block)); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal, 10n)); - expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal, 10n)); - }); + const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal, 10n)); + expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal, 10n)); }); - it('shoud be paid for more than one period if payments was missed', async () => { - await usingPlaygrounds(async (helper) => { - const staker = accounts.pop()!; + itSub('shoud be paid for more than one period if payments was missed', async ({helper}) => { + const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); - // wait for two rewards are available: - let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); + await helper.staking.stake(staker, 100n * nominal); + // wait for two rewards are available: + let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2); - expect(stake.amount).to.be.equal(frozenBalanceShouldBe); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2); + expect(stake.amount).to.be.equal(frozenBalanceShouldBe); - const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address); + const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address); - expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe}); - }); + expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe}); }); - it('should not be credited for unstaked (reserved) balance', async () => { - await usingPlaygrounds(async helper => { - // staker unstakes before rewards has been payed - const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); - const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); - await helper.staking.unstake(staker); + itSub('should not be credited for unstaked (reserved) balance', async ({helper}) => { + // staker unstakes before rewards has been payed + const staker = accounts.pop()!; + await helper.staking.stake(staker, 100n * nominal); + const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); + await helper.staking.unstake(staker); - // so he did not receive any rewards - const totalBalanceBefore = await helper.balance.getSubstrate(staker.address); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - const totalBalanceAfter = await helper.balance.getSubstrate(staker.address); + // so he did not receive any rewards + const totalBalanceBefore = await helper.balance.getSubstrate(staker.address); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + const totalBalanceAfter = await helper.balance.getSubstrate(staker.address); - expect(totalBalanceBefore).to.be.equal(totalBalanceAfter); - }); + expect(totalBalanceBefore).to.be.equal(totalBalanceAfter); }); - it('should bring compound interest', async () => { - await usingPlaygrounds(async helper => { - const staker = accounts.pop()!; + itSub('should bring compound interest', async ({helper}) => { + const staker = accounts.pop()!; - await helper.staking.stake(staker, 100n * nominal); + await helper.staking.stake(staker, 100n * nominal); - let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block)); + let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block)); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n)); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n)); - await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); - await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); - expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n, 2)); - }); + await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD); + await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); + [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); + expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n, 2)); }); - it.skip('can be paid 1000 rewards in a time', async () => { + itSub.skip('can be paid 1000 rewards in a time', async ({helper}) => { // all other stakes should be unstaked - await usingPlaygrounds(async (helper) => { - const oneHundredStakers = await helper.arrange.createCrowd(100, 1050n, alice); + const oneHundredStakers = await helper.arrange.createCrowd(100, 1050n, alice); - // stakers stakes 10 times each - for (let i = 0; i < 10; i++) { - await Promise.all(oneHundredStakers.map(staker => helper.staking.stake(staker, 100n * nominal))); - } - await helper.wait.newBlocks(40); - const result = await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); - }); + // stakers stakes 10 times each + for (let i = 0; i < 10; i++) { + await Promise.all(oneHundredStakers.map(staker => helper.staking.stake(staker, 100n * nominal))); + } + await helper.wait.newBlocks(40); + const result = await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100)); }); - it.skip('can handle 40.000 rewards', async () => { - await usingPlaygrounds(async (helper) => { - const [donor] = await helper.arrange.createAccounts([7_000_000n], alice); - const crowdStakes = async () => { - // each account in the crowd stakes 2 times - const crowd = await helper.arrange.createCrowd(500, 300n, donor); - await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal))); - await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal))); - // - }; + itSub.skip('can handle 40.000 rewards', async ({helper}) => { + const [donor] = await helper.arrange.createAccounts([7_000_000n], alice); + const crowdStakes = async () => { + // each account in the crowd stakes 2 times + const crowd = await helper.arrange.createCrowd(500, 300n, donor); + await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal))); + await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal))); + // + }; - for (let i = 0; i < 40; i++) { - await crowdStakes(); - } + for (let i = 0; i < 40; i++) { + await crowdStakes(); + } - // TODO pay rewards for some period - }); + // TODO pay rewards for some period }); }); -- gitstuff