From 797651782deb8ee282fcd8f58e756ea6f01231b0 Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Mon, 13 Feb 2023 11:14:20 +0000 Subject: [PATCH] Add new helpers for new unstaking api --- --- 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); --- a/tests/src/util/playgrounds/types.ts +++ b/tests/src/util/playgrounds/types.ts @@ -20,6 +20,7 @@ event: IEvent; }[]; }, + blockHash: string, moduleError?: string; } --- 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 { + async unstakeAll(signer: TSigner, label?: string): Promise { 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 { + if(typeof label === 'undefined') label = `${signer.address}`; + const unstakeResult = await this.helper.executeExtrinsic( + signer, 'api.tx.appPromotion.unstakePartial', + [amount], true, + ); + return unstakeResult.blockHash; } /** -- gitstuff