Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Upstream.into()
Browse files Browse the repository at this point in the history
  • Loading branch information
kianenigma committed Apr 20, 2022
2 parents 1e4f7fa + 844f42f commit 4ef56ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,14 +1953,15 @@ impl<T: Config> Pallet<T> {
current_points: BalanceOf<T>,
points: BalanceOf<T>,
) -> BalanceOf<T> {
let u256 = |x| T::BalanceToU256::convert(x);
let balance = |x| T::U256ToBalance::convert(x);
if current_balance.is_zero() || current_points.is_zero() || points.is_zero() {
// There is nothing to unbond
return Zero::zero()
}

// Equivalent of (current_balance / current_points) * points
current_balance
.saturating_mul(points)
balance(u256(current_balance).saturating_mul(u256(points)))
// We check for zero above
.div(current_points)
}
Expand Down
25 changes: 24 additions & 1 deletion frame/nomination-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1828,9 +1828,9 @@ mod unbond {
})
}

// depositor can unbond inly up to `MinCreateBond`.
#[test]
fn depositor_permissioned_partial_unbond() {
// Scenarios where non-admin accounts can unbond others
ExtBuilder::default()
.ed(1)
.add_delegators(vec![(100, 100)])
Expand All @@ -1852,6 +1852,29 @@ mod unbond {
);
});
}

// same as above, but the pool is slashed and therefore the depositor cannot partially unbond.
#[test]
fn depositor_permissioned_partial_unbond_slashed() {
ExtBuilder::default()
.ed(1)
.add_delegators(vec![(100, 100)])
.build_and_execute(|| {
// given
assert_eq!(MinCreateBond::<Runtime>::get(), 2);
assert_eq!(Delegators::<Runtime>::get(10).unwrap().active_points(), 10);
assert_eq!(Delegators::<Runtime>::get(10).unwrap().unbonding_points(), 0);

// slash the default pool
StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 5);

// cannot unbond even 7, because the value of shares is now less.
assert_noop!(
Pools::unbond(Origin::signed(10), 10, 7),
Error::<Runtime>::NotOnlyDelegator
);
});
}
}

mod pool_withdraw_unbonded {
Expand Down

0 comments on commit 4ef56ae

Please sign in to comment.