git.delta.rocks / unique-network / refs/commits / f80a6562a804

difftreelog

Add more unstaking tests

Max Andreev2023-02-14parent: #59f9c8e.patch.diff
in: master

1 file changed

modifiedtests/src/sub/appPromotion/appPromotion.test.tsdiffbeforeafterboth
73 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);73 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);
74 });74 });
7575
76 [
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 }
8186
82 // can have 10 stakes87 // can have 10 stakes
83 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);
8590
86 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');
8792
88 // After unstake can stake again93 // After unstake can stake again
94
95 // CASE 1: unstakeAll
96 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: unstakePartial
103 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 });
93115
94 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 });
355
356 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);
361
362 // cannot usntake 300.00000...1
363 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);
365
366 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);
370
371 // 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 });
375
376 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);
380
381 // 0. Staker cannot unstake negative amount
382 await expect(helper.staking.unstakePartial(staker, -1n)).to.be.rejected;
383
384 // 1. Staker can unstake 0 wei
385 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);
389
390 // 2. Staker can unstake 1 wei
391 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);
399
400 // 3. Staker can unstake all but 1 wei
401 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 });
409
410 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);
414
415 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);
420
421 const [_unstake1, unstake2] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
422 await helper.wait.forParachainBlockNumber(unstake2.block);
423
424 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 });
334432
335 describe('collection sponsoring', () => {433 describe('collection sponsoring', () => {