fn try_process_withdraw_queue(pool_info: &mut PoolInfo<T::AccountId, BalanceOf<T>>) {
			// The share price shouldn't change at any point in this function. So we can calculate
			// only once at the beginning.
			let price = match pool_info.share_price() {
				Some(price) => price,
				None => return,
			};

			while is_nondust_balance(pool_info.free_stake) {
				if let Some(mut withdraw) = pool_info.withdraw_queue.front().cloned() {
					// Must clear the pending reward before any stake change
					let info_key = (pool_info.pid, withdraw.user.clone());
					let mut user_info = match Self::pool_stakers(&info_key) {
						Some(user) => user,
						// Usually it shouldn't be the case but we still check as a safe-guard
						None => {
							pool_info.withdraw_queue.pop_front();
							continue;
						}
					};
					pool_info.settle_user_pending_reward(&mut user_info);
					// Try to fulfill the withdraw requests as much as possible
					let free_shares = if price == fp!(0) {
						withdraw.shares // 100% slashed
					} else {
						bdiv(pool_info.free_stake, &price)
					};
					// This is the shares to withdraw immedately. It should NOT contain any dust
					// because we ensure (1) `free_shares` is not dust earlier, and (2) the shares
					// in any withdraw request mustn't be dust when inserting and updating it.
					let withdrawing_shares = free_shares.min(withdraw.shares);
					debug_assert!(
						is_nondust_balance(withdrawing_shares),
						"withdrawing_shares must be positive"
					);
					// Actually remove the fulfilled withdraw request. Dust in the user shares is
					// considered but it in the request is ignored.
					Self::maybe_settle_slash(pool_info, &mut user_info);
					let (reduced, dust) = pool_info
						.remove_stake(&mut user_info, withdrawing_shares)
						.expect("Remove only what we have; qed.");
					let (shares, _) = extract_dust(withdraw.shares - withdrawing_shares);
					withdraw.shares = shares;
					// Withdraw the funds
					Self::ledger_reduce(&user_info.user, reduced, dust);
					Self::deposit_event(Event::<T>::Withdrawal(
						pool_info.pid,
						user_info.user.clone(),
						reduced,
					));
					// Update the pending reward after changing the staked amount
					pool_info.reset_pending_reward(&mut user_info);
					PoolStakers::<T>::insert(&info_key, &user_info);
					// Update if the withdraw is partially fulfilled, otherwise pop it out of the
					// queue
					if withdraw.shares == Zero::zero() {
						pool_info.withdraw_queue.pop_front();
					} else {
						*pool_info
							.withdraw_queue
							.front_mut()
							.expect("front exists as just checked; qed.") = withdraw;
					}
				} else {
					break;
				}
			}
		}
/// Tries to fulfill the withdraw queue with the newly freed stake
Tries to fulfill the withdraw queue with the newly freed stake
Tries to fulfill the withdraw queue with the newly freed stake
fn try_process_withdraw_queue(pool_info: &mut PoolInfo<T::AccountId, BalanceOf<T>>) {
			// The share price shouldn't change at any point in this function. So we can calculate
			// only once at the beginning.
			let price = match pool_info.share_price() {
				Some(price) => price,
				None => return,
			};
			while is_nondust_balance(pool_info.free_stake) {
				if let Some(mut withdraw) = pool_info.withdraw_queue.front().cloned() {
					// Must clear the pending reward before any stake change
					let info_key = (pool_info.pid, withdraw.user.clone());
					let mut user_info = match Self::pool_stakers(&info_key) {
						Some(user) => user,
						// Usually it shouldn't be the case but we still check as a safe-guard
						None => {
							pool_info.withdraw_queue.pop_front();
							continue;
						}
					};
					pool_info.settle_user_pending_reward(&mut user_info);
					// Try to fulfill the withdraw requests as much as possible
					let free_shares = if price == fp!(0) {
						withdraw.shares // 100% slashed
					} else {
						bdiv(pool_info.free_stake, &price)
					};
					// This is the shares to withdraw immedately. It should NOT contain any dust
					// because we ensure (1) `free_shares` is not dust earlier, and (2) the shares
					// in any withdraw request mustn't be dust when inserting and updating it.
					let withdrawing_shares = free_shares.min(withdraw.shares);
					debug_assert!(
						is_nondust_balance(withdrawing_shares),
						"withdrawing_shares must be positive"
					);
					// Actually remove the fulfilled withdraw request. Dust in the user shares is
					// considered but it in the request is ignored.
					Self::maybe_settle_slash(pool_info, &mut user_info);
					let (reduced, dust) = pool_info
						.remove_stake(&mut user_info, withdrawing_shares)
						.expect("Remove only what we have; qed.");
					let (shares, _) = extract_dust(withdraw.shares - withdrawing_shares);
					withdraw.shares = shares;
					// Withdraw the funds
					Self::ledger_reduce(&user_info.user, reduced, dust);
					Self::deposit_event(Event::<T>::Withdrawal(
						pool_info.pid,
						user_info.user.clone(),
						reduced,
					));
					// Update the pending reward after changing the staked amount
					pool_info.reset_pending_reward(&mut user_info);
					PoolStakers::<T>::insert(&info_key, &user_info);
					// Update if the withdraw is partially fulfilled, otherwise pop it out of the
					// queue
					if withdraw.shares == Zero::zero() {
						pool_info.withdraw_queue.pop_front();
					} else {
						*pool_info
							.withdraw_queue
							.front_mut()
							.expect("front exists as just checked; qed.") = withdraw;
					}
				} else {
					break;
				}
			}
		}
fn try_process_withdraw_queue ( pool_info : & mut PoolInfo < T :: AccountId , BalanceOf < T > > ) { // The share price shouldn't change at any point in this function. So we can calculate // only once at the beginning. let price = match pool_info . share_price ( ) { Some ( price ) => price , None => return , } ; while is_nondust_balance ( pool_info . free_stake ) { if let Some ( mut withdraw ) = pool_info . withdraw_queue . front ( ) . cloned ( ) { // Must clear the pending reward before any stake change let info_key = ( pool_info . pid , withdraw . user . clone ( ) ) ; let mut user_info = match Self :: pool_stakers ( & info_key ) { Some ( user ) => user , // Usually it shouldn't be the case but we still check as a safe-guard None => { pool_info . withdraw_queue . pop_front ( ) ; continue ; } } ; pool_info . settle_user_pending_reward ( & mut user_info ) ; // Try to fulfill the withdraw requests as much as possible let free_shares = if price == fp ! ( 0 ) { withdraw . shares // 100% slashed } else { bdiv ( pool_info . free_stake , & price ) } ; // This is the shares to withdraw immedately. It should NOT contain any dust // because we ensure (1) `free_shares` is not dust earlier, and (2) the shares // in any withdraw request mustn't be dust when inserting and updating it. let withdrawing_shares = free_shares . min ( withdraw . shares ) ; debug_assert ! ( is_nondust_balance ( withdrawing_shares ) " " ) ; // Actually remove the fulfilled withdraw request. Dust in the user shares is // considered but it in the request is ignored. Self :: maybe_settle_slash ( pool_info , & mut user_info ) ; let ( reduced , dust ) = pool_info . remove_stake ( & mut user_info , withdrawing_shares ) . expect ( " " ) ; let ( shares , _ ) = extract_dust ( withdraw . shares - withdrawing_shares ) ; withdraw . shares = shares ; // Withdraw the funds Self :: ledger_reduce ( & user_info . user , reduced , dust ) ; Self :: deposit_event ( Event :: < T > :: Withdrawal ( pool_info . pid , user_info . user . clone ( ) , reduced , ) ) ; // Update the pending reward after changing the staked amount pool_info . reset_pending_reward ( & mut user_info ) ; PoolStakers :: < T > :: insert ( & info_key , & user_info ) ; // Update if the withdraw is partially fulfilled, otherwise pop it out of the // queue if withdraw . shares == Zero :: zero ( ) { pool_info . withdraw_queue . pop_front ( ) ; } else { * pool_info . withdraw_queue . front_mut ( ) . expect ( " " ) = withdraw ; } } else { break ; } } }
Tries to fulfill the withdraw queue with the newly freed stake
Tries to fulfill the withdraw queue with the newly freed stake
pool_info
&mut PoolInfo<T::AccountId, BalanceOf<T>>
pool_info
&mut PoolInfo<T::AccountId, BalanceOf<T>>