diff --git a/packages/page-staking/src/useCounter.ts b/packages/page-staking/src/useCounter.ts index bd1786d1d4..b6f56cc921 100644 --- a/packages/page-staking/src/useCounter.ts +++ b/packages/page-staking/src/useCounter.ts @@ -5,7 +5,7 @@ import { useOwnEraRewards } from '@polkadot/react-hooks'; export default function useCounter (): number { - const { rewardCount } = useOwnEraRewards(); + const { rewardCount } = useOwnEraRewards(true); return rewardCount; } diff --git a/packages/react-hooks/src/useOwnEraRewards.ts b/packages/react-hooks/src/useOwnEraRewards.ts index f9572ac2e7..81d07bcd82 100644 --- a/packages/react-hooks/src/useOwnEraRewards.ts +++ b/packages/react-hooks/src/useOwnEraRewards.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { DerivedStakingQuery, DeriveStakerReward } from '@polkadot/api-derive/types'; +import { DeriveSessionIndexes, DerivedStakingQuery, DeriveStakerReward } from '@polkadot/api-derive/types'; import { EraIndex } from '@polkadot/types/interfaces'; import { useEffect, useState } from 'react'; @@ -17,24 +17,33 @@ interface OwnRewards { rewardCount: number; } -function useNextPayouts (): [string, EraIndex][] | undefined { - const { api } = useApi(); +function useNextPayouts (onlyLatest?: boolean): [string, BN][] | undefined { + const { api, isApiReady } = useApi(); const stashIds = useOwnStashIds(); - const allInfo = useCall(stashIds && api.derive.staking?.queryMulti, stashIds); - const [nextPayouts, setNextPayouts] = useState<[string, EraIndex][] | undefined>(); + const allInfo = useCall(isApiReady && stashIds && api.derive.staking?.queryMulti, stashIds); + const indexes = useCall(isApiReady && api.derive.session.indexes, []); + const [nextPayouts, setNextPayouts] = useState<[string, BN][] | undefined>(); useEffect((): void => { - stashIds && allInfo && setNextPayouts( - allInfo - .map(({ stakingLedger }, index) => [stashIds[index], stakingLedger?.lastReward?.unwrapOr(new BN(-1)).addn(1)]) - .filter((value): value is [string, EraIndex] => !!value[1]) - ); - }, [allInfo, stashIds]); + if (stashIds) { + if (onlyLatest) { + indexes && setNextPayouts( + stashIds.map((stashId) => [stashId, indexes.activeEra.subn(1)]) + ); + } else if (allInfo) { + setNextPayouts( + allInfo + .map(({ stakingLedger }, index) => [stashIds[index], stakingLedger?.lastReward?.unwrapOr(new BN(-1)).addn(1)]) + .filter((value): value is [string, EraIndex] => !!value[1]) + ); + } + } + }, [allInfo, indexes, onlyLatest, stashIds]); return nextPayouts; } -function getRewards ([thesePayouts, theseRewards]: [[string, EraIndex][], DeriveStakerReward[][]], nextPayouts: [string, EraIndex][]): OwnRewards { +function getRewards ([thesePayouts, theseRewards]: [[string, EraIndex][], DeriveStakerReward[][]], nextPayouts: [string, BN][]): OwnRewards { const allRewards = theseRewards.reduce((result: Record, rewards, index): Record => { const [stashId] = thesePayouts[index]; const nextPayout = nextPayouts.find(([thisId]) => thisId === stashId); @@ -52,9 +61,9 @@ function getRewards ([thesePayouts, theseRewards]: [[string, EraIndex][], Derive }; } -export default function useOwnEraRewards (): OwnRewards { +export default function useOwnEraRewards (onlyLatest?: boolean): OwnRewards { const { api } = useApi(); - const nextPayouts = useNextPayouts(); + const nextPayouts = useNextPayouts(onlyLatest); const available = useCall<[[string, EraIndex][], DeriveStakerReward[][]]>(nextPayouts && api.derive.staking?.stakerRewardsMulti as any, nextPayouts, { withParams: true }); const [state, setState] = useState({ rewardCount: 0 });