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
20 event: IEvent;20 event: IEvent;
21 }[];21 }[];
22 },22 },
23 blockHash: string,
23 moduleError?: string;24 moduleError?: string;
24}25}
2526
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