From 844f42fc90274ecb102c09f568f1077d8e4e180f Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Tue, 19 Apr 2022 13:12:25 -0700 Subject: [PATCH] Avoid saturation in balance_to_unbond --- frame/nomination-pools/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/nomination-pools/src/lib.rs b/frame/nomination-pools/src/lib.rs index 4dc2535a20d5f..5b2c206d89031 100644 --- a/frame/nomination-pools/src/lib.rs +++ b/frame/nomination-pools/src/lib.rs @@ -215,7 +215,6 @@ //! after the election snapshot is taken it will benefit from the rewards of the next _2_ eras //! because it's vote weight will not be counted until the election snapshot in active era + 1. //! Related: -//! // _Note to maintainers_: In order to ensure the reward account never falls below the existential // deposit, at creation the reward account must be endowed with the existential deposit. All logic // for calculating rewards then does not see that existential deposit as part of the free balance. @@ -1766,14 +1765,15 @@ impl Pallet { current_points: BalanceOf, delegator_points: BalanceOf, ) -> BalanceOf { + let u256 = |x| T::BalanceToU256::convert(x); + let balance = |x| T::U256ToBalance::convert(x); if current_balance.is_zero() || current_points.is_zero() || delegator_points.is_zero() { // There is nothing to unbond return Zero::zero() } // Equivalent of (current_balance / current_points) * delegator_points - current_balance - .saturating_mul(delegator_points) + balance(u256(current_balance).saturating_mul(u256(delegator_points))) // We check for zero above .div(current_points) }