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

Fix nomination pools pending rewards RPC #12095

Merged
merged 5 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ impl_runtime_apis! {

impl pallet_nomination_pools_runtime_api::NominationPoolsApi<Block, AccountId, Balance> for Runtime {
fn pending_rewards(member_account: AccountId) -> Balance {
NominationPools::pending_rewards(member_account)
NominationPools::pending_rewards(member_account).unwrap_or_default()
}
}

Expand Down
16 changes: 10 additions & 6 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2157,15 +2157,19 @@ impl<T: Config> Pallet<T> {
/// Returns the pending rewards for the specified `member_account`.
///
/// In the case of error the function returns balance of zero.
pub fn pending_rewards(member_account: T::AccountId) -> BalanceOf<T> {
pub fn pending_rewards(member_account: T::AccountId) -> Option<BalanceOf<T>> {
if let Some(pool_member) = PoolMembers::<T>::get(member_account) {
if let Some(reward_pool) = RewardPools::<T>::get(pool_member.pool_id) {
return pool_member
.pending_rewards(reward_pool.last_recorded_reward_counter())
.unwrap_or_default()
if let Some((reward_pool, bonded_pool)) = RewardPools::<T>::get(pool_member.pool_id)
.zip(BondedPools::<T>::get(pool_member.pool_id))
{
let current_reward_counter = reward_pool
.current_reward_counter(pool_member.pool_id, bonded_pool.points)
.ok()?;
return Some(pool_member.pending_rewards(current_reward_counter).unwrap_or_default())
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
}
}
BalanceOf::<T>::default()

None
}

/// The amount of bond that MUST REMAIN IN BONDED in ALL POOLS.
Expand Down
53 changes: 53 additions & 0 deletions frame/nomination-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,59 @@ mod claim_payout {
});
}

#[test]
fn pending_rewards_per_member_works() {
ExtBuilder::default().build_and_execute(|| {
let ed = Balances::minimum_balance();

assert_eq!(Pools::pending_rewards(10), Some(0));
Balances::mutate_account(&default_reward_account(), |f| f.free += 30).unwrap();
assert_eq!(Pools::pending_rewards(10), Some(30));
assert_eq!(Pools::pending_rewards(20), None);

Balances::make_free_balance_be(&20, ed + 10);
assert_ok!(Pools::join(Origin::signed(20), 10, 1));

assert_eq!(Pools::pending_rewards(10), Some(30));
assert_eq!(Pools::pending_rewards(20), Some(0));

Balances::mutate_account(&default_reward_account(), |f| f.free += 100).unwrap();

assert_eq!(Pools::pending_rewards(10), Some(30 + 50));
assert_eq!(Pools::pending_rewards(20), Some(50));
assert_eq!(Pools::pending_rewards(30), None);

Balances::make_free_balance_be(&30, ed + 10);
assert_ok!(Pools::join(Origin::signed(30), 10, 1));

assert_eq!(Pools::pending_rewards(10), Some(30 + 50));
assert_eq!(Pools::pending_rewards(20), Some(50));
assert_eq!(Pools::pending_rewards(30), Some(0));

Balances::mutate_account(&default_reward_account(), |f| f.free += 60).unwrap();

assert_eq!(Pools::pending_rewards(10), Some(30 + 50 + 20));
assert_eq!(Pools::pending_rewards(20), Some(50 + 20));
assert_eq!(Pools::pending_rewards(30), Some(20));

// 10 should claim 10, 20 should claim nothing.
assert_ok!(Pools::claim_payout(Origin::signed(10)));
assert_eq!(Pools::pending_rewards(10), Some(0));
assert_eq!(Pools::pending_rewards(20), Some(50 + 20));
assert_eq!(Pools::pending_rewards(30), Some(20));

assert_ok!(Pools::claim_payout(Origin::signed(20)));
assert_eq!(Pools::pending_rewards(10), Some(0));
assert_eq!(Pools::pending_rewards(20), Some(0));
assert_eq!(Pools::pending_rewards(30), Some(20));

assert_ok!(Pools::claim_payout(Origin::signed(30)));
assert_eq!(Pools::pending_rewards(10), Some(0));
assert_eq!(Pools::pending_rewards(20), Some(0));
assert_eq!(Pools::pending_rewards(30), Some(0));
});
}

#[test]
fn rewards_distribution_is_fair_bond_extra() {
ExtBuilder::default().build_and_execute(|| {
Expand Down