From de3cc2908b884f5e10dec9ae42c6142af62eedba Mon Sep 17 00:00:00 2001 From: PraetorP Date: Fri, 02 Sep 2022 13:09:18 +0000 Subject: [PATCH] fix unstake --- --- a/pallets/app-promotion/src/lib.rs +++ b/pallets/app-promotion/src/lib.rs @@ -358,7 +358,7 @@ let block = >::block_number() + T::PendingInterval::get(); let mut pendings = >::get(block); - ensure!(pendings.is_full(), Error::::PendingForBlockOverflow); + ensure!(!pendings.is_full(), Error::::PendingForBlockOverflow); let mut total_stakes = 0u64; @@ -370,7 +370,7 @@ .sum(); if total_staked.is_zero() { - return Ok(None.into()); + return Ok(None.into()); // TO-DO } pendings @@ -387,7 +387,7 @@ TotalStaked::::get() .checked_sub(&total_staked) .ok_or(ArithmeticError::Underflow)?, - ); // when error we should recover initial stake state for the staker + ); StakesPerAccount::::remove(&staker_id); @@ -474,46 +474,46 @@ let next_recalc_block = current_recalc_block + T::RecalculationInterval::get(); let mut storage_iterator = Self::get_next_calculated_key() - .map_or(Staked::::iter().skip(0), |key| { - Staked::::iter_from(key).skip(1) + .map_or(Staked::::iter(), |key| { + Staked::::iter_from(key) }); NextCalculatedRecord::::set(None); { let mut stakers_number = stakers_number.unwrap_or(20); - let mut current_id = admin_id; + let mut last_id = admin_id; let mut income_acc = BalanceOf::::default(); - while let Some(((id, staked_block), (amount, next_recalc_block_for_stake))) = + while let Some(((current_id, staked_block), (amount, next_recalc_block_for_stake))) = storage_iterator.next() { - if current_id != id { + if last_id != current_id { if income_acc != BalanceOf::::default() { >::transfer( &T::TreasuryAccountId::get(), - ¤t_id, + &last_id, income_acc, ExistenceRequirement::KeepAlive, ) - .and_then(|_| Self::add_lock_balance(¤t_id, income_acc))?; + .and_then(|_| Self::add_lock_balance(&last_id, income_acc))?; Self::deposit_event(Event::StakingRecalculation( - current_id, amount, income_acc, + last_id, amount, income_acc, )); } if stakers_number == 0 { - NextCalculatedRecord::::set(Some((id, staked_block))); + NextCalculatedRecord::::set(Some((current_id, staked_block))); break; } stakers_number -= 1; income_acc = BalanceOf::::default(); - current_id = id; + last_id = current_id; }; if current_recalc_block >= next_recalc_block_for_stake { Self::recalculate_and_insert_stake( - ¤t_id, + &last_id, staked_block, next_recalc_block, amount, --- a/tests/src/app-promotion.test.ts +++ b/tests/src/app-promotion.test.ts @@ -51,14 +51,12 @@ if (!promotionStartBlock) { promotionStartBlock = (await helper.api!.query.parachainSystem.lastRelayChainBlockNumber()).toNumber(); } - await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.startAppPromotion(promotionStartBlock!))); accounts = await helper.arrange.createCrowd(100, 1000n, alice); // create accounts-pool to speed up tests }); }); after(async function () { await usingPlaygrounds(async (helper) => { - await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.stopAppPromotion())); }); }); @@ -241,7 +239,7 @@ it('should be possible for different accounts in one block', async () => { await usingPlaygrounds(async (helper) => { - const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!]; + 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))); -- gitstuff