git.delta.rocks / unique-network / refs/commits / 797651782deb

difftreelog

Add new helpers for new unstaking api

Max Andreev2023-02-13parent: #9935321.patch.diff
in: master

3 files changed

modifiedtests/src/sub/appPromotion/appPromotion.test.tsdiffbeforeafterboth
--- a/tests/src/sub/appPromotion/appPromotion.test.ts
+++ b/tests/src/sub/appPromotion/appPromotion.test.ts
@@ -86,7 +86,7 @@
       await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');
 
       // After unstake can stake again
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(staker);
       await helper.staking.stake(staker, 100n * nominal);
       expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);
     });
@@ -109,7 +109,7 @@
       expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(1199n * nominal);
 
       // staker can unstake
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(staker);
       expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1199n * nominal);
       const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
       await helper.wait.forParachainBlockNumber(pendingUnstake.block);
@@ -152,7 +152,7 @@
       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);
+      await helper.staking.unstakeAll(staker);
 
       // Right after unstake tokens are still locked
       expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: 900n * nominal, reasons: 'All'}]);
@@ -167,7 +167,7 @@
     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);
+      await helper.staking.unstakeAll(staker);
       const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
 
       // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n
@@ -197,7 +197,7 @@
       expect(stakes[2].amount).to.equal(300n * nominal);
 
       // Can unstake multiple stakes
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(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});
@@ -216,7 +216,7 @@
       const staker = accounts.pop()!;
 
       // unstake has no effect if no stakes at all
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(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
 
@@ -224,8 +224,8 @@
 
       // 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);
+      await helper.staking.unstakeAll(staker);
+      await helper.staking.unstakeAll(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);
@@ -234,9 +234,9 @@
     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.unstakeAll(staker);
       await helper.staking.stake(staker, 120n * nominal);
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(staker);
 
       const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
       expect(unstakingPerBlock).has.length(2);
@@ -248,7 +248,7 @@
       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.unstakeAll(staker)));
 
       await Promise.all(stakers.map(async (staker) => {
         expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);
@@ -261,7 +261,7 @@
         const stakers = await helper.arrange.createAccounts([200n,200n,200n,200n,200n,200n,200n,200n,200n,200n], donor);
 
         await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
-        const unstakingResults = await Promise.allSettled(stakers.map(staker => helper.staking.unstake(staker)));
+        const unstakingResults = await Promise.allSettled(stakers.map(staker => helper.staking.unstakeAll(staker)));
 
         const successfulUnstakes = unstakingResults.filter(result => result.status === 'fulfilled');
         expect(successfulUnstakes).to.have.length(3);
@@ -597,7 +597,7 @@
       const totalStakedAfter = await helper.staking.getTotalStaked();
       expect(totalStakedAfter).to.equal(totalStakedBefore + (100n * nominal) + totalPayout);
       // staker can unstake
-      await helper.staking.unstake(staker);
+      await helper.staking.unstakeAll(staker);
       expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal));
     });
 
@@ -651,7 +651,7 @@
       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);
+      await helper.staking.unstakeAll(staker);
 
       // so he did not receive any rewards
       const totalBalanceBefore = await helper.balance.getSubstrate(staker.address);
modifiedtests/src/util/playgrounds/types.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/types.ts
+++ b/tests/src/util/playgrounds/types.ts
@@ -20,6 +20,7 @@
         event: IEvent;
       }[];
   },
+  blockHash: string,
   moduleError?: string;
 }
 
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
561 if (status === this.transactionStatus.SUCCESS) {561 if (status === this.transactionStatus.SUCCESS) {
562 this.logger.log(`${label} successful`);562 this.logger.log(`${label} successful`);
563 unsub();563 unsub();
564 resolve({result, status});564 resolve({result, status, blockHash: result.status.asInBlock.toHuman()});
565 } else if (status === this.transactionStatus.FAIL) {565 } else if (status === this.transactionStatus.FAIL) {
566 let moduleError = null;566 let moduleError = null;
567567
2656 return true;2656 return true;
2657 }2657 }
26582658
2659 /**2659 /**
2660 * Unstake tokens for App Promotion2660 * Unstake all staked tokens
2661 * @param signer keyring of signer2661 * @param signer keyring of signer
2662 * @param amountToUnstake amount of tokens to unstake2662 * @param amountToUnstake amount of tokens to unstake
2663 * @param label extra label for log2663 * @param label extra label for log
2664 * @returns block number where balances will be unlocked2664 * @returns block hash where unstake happened
2665 */2665 */
2666 async unstake(signer: TSigner, label?: string): Promise<number> {2666 async unstakeAll(signer: TSigner, label?: string): Promise<string> {
2667 if(typeof label === 'undefined') label = `${signer.address}`;2667 if(typeof label === 'undefined') label = `${signer.address}`;
2668 const _unstakeResult = await this.helper.executeExtrinsic(2668 const unstakeResult = await this.helper.executeExtrinsic(
2669 signer, 'api.tx.appPromotion.unstake',2669 signer, 'api.tx.appPromotion.unstakeAll',
2670 [], true,2670 [], true,
2671 );2671 );
2672 // TODO extract block number fron events
2673 return 1;2672 return unstakeResult.blockHash;
2674 }2673 }
2674
2675 /**
2676 * Unstake the part of a staked tokens
2677 * @param signer keyring of signer
2678 * @param amount amount of tokens to unstake
2679 * @param label extra label for log
2680 * @returns block hash where unstake happened
2681 */
2682 async unstakePartial(signer: TSigner, amount: bigint, label?: string): Promise<string> {
2683 if(typeof label === 'undefined') label = `${signer.address}`;
2684 const unstakeResult = await this.helper.executeExtrinsic(
2685 signer, 'api.tx.appPromotion.unstakePartial',
2686 [amount], true,
2687 );
2688 return unstakeResult.blockHash;
2689 }
26752690
2676 /**2691 /**
2677 * Get total staked amount for address2692 * Get total staked amount for address