From c40afb38dcae4d2644a475c1e640a6e5725eaa08 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 11 Nov 2024 19:44:02 -0500 Subject: [PATCH] Fix isClaimed for edgecase with non validator Ids --- .../api-derive/src/staking/stakerRewards.ts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/api-derive/src/staking/stakerRewards.ts b/packages/api-derive/src/staking/stakerRewards.ts index d575d8f1c5d..2a811089224 100644 --- a/packages/api-derive/src/staking/stakerRewards.ts +++ b/packages/api-derive/src/staking/stakerRewards.ts @@ -84,6 +84,8 @@ function parseRewards (api: DeriveApi, stashId: AccountId, [erasPoints, erasPref return { era, eraReward, + // This might not always be accurate as you need validator account information in order to see if the rewards have been claimed. + // This is possibly adjusted in `filterRewards` when need be. isClaimed: claimedRewardsEras.some((c) => c.eq(era)), isEmpty, isValidator, @@ -151,12 +153,26 @@ function filterRewards (eras: EraIndex[], valInfo: [string, DeriveStakingQuery][ return true; }) .filter(({ validators }) => Object.keys(validators).length !== 0) - .map((reward) => - objectSpread({}, reward, { - isClaimed: reward.isClaimed, + .map((reward) => { + let isClaimed = reward.isClaimed; + const valKeys = Object.keys(reward.validators); + + if (!reward.isClaimed && valKeys.length) { + for (const key of valKeys) { + const info = queryValidators.find((i) => i.accountId.toString() === key); + + if (info) { + isClaimed = info.claimedRewardsEras.toArray().some((era) => era.eq(reward.era)); + break; + } + } + } + + return objectSpread({}, reward, { + isClaimed, nominators: reward.nominating.filter((n) => reward.validators[n.validatorId]) - }) - ); + }); + }); } export function _stakerRewardsEras (instanceId: string, api: DeriveApi): (eras: EraIndex[], withActive?: boolean) => Observable {