git.delta.rocks / unique-network / refs/commits / 500168dd283f

difftreelog

Add tests for trash-stakes rewards

Max Andreev2023-02-14parent: #9482c4a.patch.diff
in: master

1 file changed

modifiedtests/src/sub/appPromotion/appPromotion.test.tsdiffbeforeafterboth
360 await helper.staking.stake(staker, 200n * nominal);360 await helper.staking.stake(staker, 200n * nominal);
361361
362 // cannot usntake 300.00000...1362 // cannot usntake 300.00000...1
363 await expect(helper.staking.unstakePartial(staker, 300n * nominal + 1n)).to.be.rejectedWith('Arithmetic: Underflow');363 await expect(helper.staking.unstakePartial(staker, 300n * nominal + 1n)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');
364 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(2);364 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(2);
365365
366 await helper.staking.unstakePartial(staker, 150n * nominal);366 await helper.staking.unstakePartial(staker, 150n * nominal);
367 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);367 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);
368 await expect(helper.staking.unstakePartial(staker, 150n * nominal + 1n)).to.be.rejectedWith('Arithmetic: Underflow');368 await expect(helper.staking.unstakePartial(staker, 150n * nominal + 1n)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');
369 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);369 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);
370370
371 // nothing broken, can unstake full amount:371 // nothing broken, can unstake full amount:
840 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 2));840 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 2));
841 });841 });
842842
843 itSub.skip('can be paid 1000 rewards in a time', async ({helper}) => {843 itSub('can calculate reward for tiny stake', async ({helper}) => {
844 // all other stakes should be unstaked
845 const oneHundredStakers = await helper.arrange.createCrowd(100, 1050n, donor);844 const staker = accounts.pop()!;
846845 await helper.staking.stake(staker, 100n * nominal);
847 // stakers stakes 10 times each
848 for (let i = 0; i < 10; i++) {
849 await Promise.all(oneHundredStakers.map(staker => helper.staking.stake(staker, 100n * nominal)));846 await helper.staking.stake(staker, 100n * nominal);
850 }847 await helper.staking.unstakePartial(staker, 100n * nominal - 1n);
848
849 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
851 await helper.wait.newBlocks(40);850 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));
851
852 await helper.admin.payoutStakers(palletAdmin, 100);852 const payouts = await helper.admin.payoutStakers(palletAdmin, 100);
853 const stakerPayout = payouts.find(p => p.staker === staker.address);
854 expect(stakerPayout!.stake).to.eq(100n * nominal + 1n);
853 });855 });
854856
855 itSub.skip('can handle 40.000 rewards', async ({helper}) => {857 itSub('can eventually pay all rewards', async ({helper}) => {
856 const crowdStakes = async () => {858 const stakers = await helper.arrange.createAccounts(new Array(100).fill(200n), donor);
857 // each account in the crowd stakes 2 times
858 const crowd = await helper.arrange.createCrowd(500, 300n, donor);
859 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));859 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
860
861 let unstakingTxs = [];
862 for (const staker of stakers) {
863 if (unstakingTxs.length == 3) {
864 await Promise.all(unstakingTxs);
865 unstakingTxs = [];
866 }
860 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));867 unstakingTxs.push(helper.staking.unstakePartial(staker, 100n * nominal - 1n));
861 //868 }
869
862 };870 const [staker] = await helper.arrange.createAccounts([1000n], donor);
863
864 for (let i = 0; i < 40; i++) {871 await helper.staking.stake(staker, 100n * nominal);
872 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
873 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));
874
875 let payouts;
876 do {
865 await crowdStakes();877 payouts = await helper.admin.payoutStakers(palletAdmin, 20);
866 }878 } while (payouts.length !== 0);
867
868 // TODO pay rewards for some period
869 });879 });
870 });880 });
871});881});