difftreelog
Add more unstaking tests
in: master
1 file changed
tests/src/sub/appPromotion/appPromotion.test.tsdiffbeforeafterboth73 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);73 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);74 });74 });757576 [77 {unstake: 'unstakeAll' as const},78 {unstake: 'unstakePartial' as const},79 ].map(testCase => {76 itSub('should allow to create maximum 10 stakes for account', async ({helper}) => {80 itSub('should allow to create maximum 10 stakes for account', async ({helper}) => {77 const [staker] = await helper.arrange.createAccounts([2000n], donor);81 const [staker] = await helper.arrange.createAccounts([2000n], donor);82 const ONE_STAKE = 100n * nominal;78 for (let i = 0; i < 10; i++) {83 for (let i = 0; i < 10; i++) {79 await helper.staking.stake(staker, 100n * nominal);84 await helper.staking.stake(staker, ONE_STAKE);80 }85 }818682 // can have 10 stakes87 // can have 10 stakes83 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal);88 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal);84 expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10);89 expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10);859086 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');91 await expect(helper.staking.stake(staker, ONE_STAKE)).to.be.rejectedWith('appPromotion.NoPermission');879288 // After unstake can stake again93 // After unstake can stake again9495 // CASE 1: unstakeAll96 if (testCase.unstake === 'unstakeAll') {89 await helper.staking.unstakeAll(staker);97 await helper.staking.unstakeAll(staker);98 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);90 await helper.staking.stake(staker, 100n * nominal);99 await helper.staking.stake(staker, 100n * nominal);91 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);100 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);101 }102 // CASE 2: unstakePartial103 else {104 await helper.staking.unstakePartial(staker, ONE_STAKE);105 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(9);106 await helper.staking.stake(staker, 100n * nominal);107 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(10);108 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');109 await helper.staking.unstakePartial(staker, 150n * nominal);110 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(9);111 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(850n * nominal);112 }92 });113 });114 });9311594 itSub('should allow to stake() if balance is locked with different id', async ({helper}) => {116 itSub('should allow to stake() if balance is locked with different id', async ({helper}) => {95 const staker = accounts.pop()!;117 const staker = accounts.pop()!;331 }353 }332 });354 });355356 itSub('Cannot partially unstake more than staked', async ({helper}) => {357 const staker = accounts.pop()!;358 // Staker stakes 300:359 await helper.staking.stake(staker, 100n * nominal);360 await helper.staking.stake(staker, 200n * nominal);361362 // cannot usntake 300.00000...1363 await expect(helper.staking.unstakePartial(staker, 300n * nominal + 1n)).to.be.rejectedWith('Arithmetic: Underflow');364 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(2);365366 await helper.staking.unstakePartial(staker, 150n * nominal);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');369 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);370371 // nothing broken, can unstake full amount:372 await helper.staking.unstakePartial(staker, 150n * nominal);373 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(0);374 });375376 itSub('Can partially unstake arbitrary amount', async ({helper}) => {377 const staker = accounts.pop()!;378 await helper.staking.stake(staker, 100n * nominal);379 await helper.staking.stake(staker, 200n * nominal);380381 // 0. Staker cannot unstake negative amount382 await expect(helper.staking.unstakePartial(staker, -1n)).to.be.rejected;383384 // 1. Staker can unstake 0 wei385 await helper.staking.unstakePartial(staker, 0n);386 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);387 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(300n * nominal);388 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(0n);389390 // 2. Staker can unstake 1 wei391 await helper.staking.unstakePartial(staker, 1n);392 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);393 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(300n * nominal - 1n);394 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1n);395 // 2.1 The oldest stake decreased:396 let [stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});397 expect(stake1.amount).to.eq(100n * nominal - 1n);398 expect(stake2.amount).to.eq(200n * nominal);399400 // 3. Staker can unstake all but 1 wei401 await helper.staking.unstakePartial(staker, 100n * nominal - 2n);402 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);403 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(200n * nominal + 1n);404 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(100n * nominal - 1n);405 [stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});406 expect(stake1.amount).to.eq(1n);407 expect(stake2.amount).to.eq(200n * nominal);408 });409410 itSub('can mix different type of unstakes', async ({helper}) => {411 const staker = accounts.pop()!;412 await helper.staking.stake(staker, 100n * nominal);413 await helper.staking.stake(staker, 200n * nominal);414415 await helper.staking.unstakePartial(staker, 50n * nominal);416 await helper.staking.unstakeAll(staker);417 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);418 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(0n);419 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(300n * nominal);420421 const [_unstake1, unstake2] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});422 await helper.wait.forParachainBlockNumber(unstake2.block);423424 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([]);425 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});426 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.eq(999n);427 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(0n);428 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(0n);429 expect(await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).to.deep.eq([]);430 });333 });431 });334432335 describe('collection sponsoring', () => {433 describe('collection sponsoring', () => {