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
86 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');86 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');
8787
88 // After unstake can stake again88 // After unstake can stake again
89 await helper.staking.unstake(staker);89 await helper.staking.unstakeAll(staker);
90 await helper.staking.stake(staker, 100n * nominal);90 await helper.staking.stake(staker, 100n * nominal);
91 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);91 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);
92 });92 });
109 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(1199n * nominal);109 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(1199n * nominal);
110110
111 // staker can unstake111 // staker can unstake
112 await helper.staking.unstake(staker);112 await helper.staking.unstakeAll(staker);
113 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1199n * nominal);113 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1199n * nominal);
114 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});114 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
115 await helper.wait.forParachainBlockNumber(pendingUnstake.block);115 await helper.wait.forParachainBlockNumber(pendingUnstake.block);
152 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];152 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];
153 const totalStakedBefore = await helper.staking.getTotalStaked();153 const totalStakedBefore = await helper.staking.getTotalStaked();
154 await helper.staking.stake(staker, 900n * nominal);154 await helper.staking.stake(staker, 900n * nominal);
155 await helper.staking.unstake(staker);155 await helper.staking.unstakeAll(staker);
156156
157 // Right after unstake tokens are still locked157 // Right after unstake tokens are still locked
158 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: 900n * nominal, reasons: 'All'}]);158 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: 900n * nominal, reasons: 'All'}]);
167 itSub('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async ({helper}) => {167 itSub('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async ({helper}) => {
168 const staker = accounts.pop()!;168 const staker = accounts.pop()!;
169 await helper.staking.stake(staker, 100n * nominal);169 await helper.staking.stake(staker, 100n * nominal);
170 await helper.staking.unstake(staker);170 await helper.staking.unstakeAll(staker);
171 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});171 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
172172
173 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n173 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n
197 expect(stakes[2].amount).to.equal(300n * nominal);197 expect(stakes[2].amount).to.equal(300n * nominal);
198198
199 // Can unstake multiple stakes199 // Can unstake multiple stakes
200 await helper.staking.unstake(staker);200 await helper.staking.unstakeAll(staker);
201 pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});201 pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
202 totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});202 totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
203 stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});203 stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
216 const staker = accounts.pop()!;216 const staker = accounts.pop()!;
217217
218 // unstake has no effect if no stakes at all218 // unstake has no effect if no stakes at all
219 await helper.staking.unstake(staker);219 await helper.staking.unstakeAll(staker);
220 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);220 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);
221 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper221 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper
222222
223 // TODO stake() unstake() waitUnstaked() unstake();223 // TODO stake() unstake() waitUnstaked() unstake();
224224
225 // can't unstake if there are only pendingUnstakes225 // can't unstake if there are only pendingUnstakes
226 await helper.staking.stake(staker, 100n * nominal);226 await helper.staking.stake(staker, 100n * nominal);
227 await helper.staking.unstake(staker);227 await helper.staking.unstakeAll(staker);
228 await helper.staking.unstake(staker);228 await helper.staking.unstakeAll(staker);
229229
230 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);230 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);
231 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);231 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);
234 itSub('should keep different unlocking block for each unlocking stake', async ({helper}) => {234 itSub('should keep different unlocking block for each unlocking stake', async ({helper}) => {
235 const staker = accounts.pop()!;235 const staker = accounts.pop()!;
236 await helper.staking.stake(staker, 100n * nominal);236 await helper.staking.stake(staker, 100n * nominal);
237 await helper.staking.unstake(staker);237 await helper.staking.unstakeAll(staker);
238 await helper.staking.stake(staker, 120n * nominal);238 await helper.staking.stake(staker, 120n * nominal);
239 await helper.staking.unstake(staker);239 await helper.staking.unstakeAll(staker);
240240
241 const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});241 const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
242 expect(unstakingPerBlock).has.length(2);242 expect(unstakingPerBlock).has.length(2);
248 const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!];248 const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!];
249249
250 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));250 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
251 await Promise.all(stakers.map(staker => helper.staking.unstake(staker)));251 await Promise.all(stakers.map(staker => helper.staking.unstakeAll(staker)));
252252
253 await Promise.all(stakers.map(async (staker) => {253 await Promise.all(stakers.map(async (staker) => {
254 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);254 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);
261 const stakers = await helper.arrange.createAccounts([200n,200n,200n,200n,200n,200n,200n,200n,200n,200n], donor);261 const stakers = await helper.arrange.createAccounts([200n,200n,200n,200n,200n,200n,200n,200n,200n,200n], donor);
262262
263 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));263 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
264 const unstakingResults = await Promise.allSettled(stakers.map(staker => helper.staking.unstake(staker)));264 const unstakingResults = await Promise.allSettled(stakers.map(staker => helper.staking.unstakeAll(staker)));
265265
266 const successfulUnstakes = unstakingResults.filter(result => result.status === 'fulfilled');266 const successfulUnstakes = unstakingResults.filter(result => result.status === 'fulfilled');
267 expect(successfulUnstakes).to.have.length(3);267 expect(successfulUnstakes).to.have.length(3);
597 const totalStakedAfter = await helper.staking.getTotalStaked();597 const totalStakedAfter = await helper.staking.getTotalStaked();
598 expect(totalStakedAfter).to.equal(totalStakedBefore + (100n * nominal) + totalPayout);598 expect(totalStakedAfter).to.equal(totalStakedBefore + (100n * nominal) + totalPayout);
599 // staker can unstake599 // staker can unstake
600 await helper.staking.unstake(staker);600 await helper.staking.unstakeAll(staker);
601 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal));601 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal));
602 });602 });
603603
651 await helper.staking.stake(staker, 100n * nominal);651 await helper.staking.stake(staker, 100n * nominal);
652 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});652 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
653 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);653 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
654 await helper.staking.unstake(staker);654 await helper.staking.unstakeAll(staker);
655655
656 // so he did not receive any rewards656 // so he did not receive any rewards
657 const totalBalanceBefore = await helper.balance.getSubstrate(staker.address);657 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
--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -561,7 +561,7 @@
           if (status === this.transactionStatus.SUCCESS) {
             this.logger.log(`${label} successful`);
             unsub();
-            resolve({result, status});
+            resolve({result, status, blockHash: result.status.asInBlock.toHuman()});
           } else if (status === this.transactionStatus.FAIL) {
             let moduleError = null;
 
@@ -2657,20 +2657,35 @@
   }
 
   /**
-   * Unstake tokens for App Promotion
+   * Unstake all staked tokens
    * @param signer keyring of signer
    * @param amountToUnstake amount of tokens to unstake
    * @param label extra label for log
-   * @returns block number where balances will be unlocked
+   * @returns block hash where unstake happened
    */
-  async unstake(signer: TSigner, label?: string): Promise<number> {
+  async unstakeAll(signer: TSigner, label?: string): Promise<string> {
     if(typeof label === 'undefined') label = `${signer.address}`;
-    const _unstakeResult = await this.helper.executeExtrinsic(
-      signer, 'api.tx.appPromotion.unstake',
+    const unstakeResult = await this.helper.executeExtrinsic(
+      signer, 'api.tx.appPromotion.unstakeAll',
       [], true,
     );
-    // TODO extract block number fron events
-    return 1;
+    return unstakeResult.blockHash;
+  }
+
+  /**
+   * Unstake the part of a staked tokens
+   * @param signer keyring of signer
+   * @param amount amount of tokens to unstake
+   * @param label extra label for log
+   * @returns block hash where unstake happened
+   */
+  async unstakePartial(signer: TSigner, amount: bigint, label?: string): Promise<string> {
+    if(typeof label === 'undefined') label = `${signer.address}`;
+    const unstakeResult = await this.helper.executeExtrinsic(
+      signer, 'api.tx.appPromotion.unstakePartial',
+      [amount], true,
+    );
+    return unstakeResult.blockHash;
   }
 
   /**