diff --git a/packages/page-staking/src/Actions/Pool/Account.tsx b/packages/page-staking/src/Actions/Pool/Account.tsx index 3d06f3ece8..fc6a648242 100644 --- a/packages/page-staking/src/Actions/Pool/Account.tsx +++ b/packages/page-staking/src/Actions/Pool/Account.tsx @@ -5,7 +5,6 @@ import type { DeriveSessionProgress, DeriveUnlocking } from '@polkadot/api-deriv import type { PalletNominationPoolsPoolMember, PalletNominationPoolsPoolRoles } from '@polkadot/types/lookup'; import type { PoolInfo } from '../../Pools/types'; import type { SortedTargets } from '../../types'; -import type { PalletNominationPoolsPoolMemberV0 } from './types'; import React, { useCallback, useContext, useMemo } from 'react'; @@ -42,7 +41,7 @@ function extractRoles (accountId: string, { nominator, root }: PalletNominationP }; } -function calcUnbonding (accountId: string, stashId: string, { activeEra }: DeriveSessionProgress, { unbondingEras }: PalletNominationPoolsPoolMemberV0 | PalletNominationPoolsPoolMember): { accountId: string, controllerId: string, redeemable: BN, stashId: string, unlocking: DeriveUnlocking[] } { +function calcUnbonding (accountId: string, stashId: string, { activeEra }: DeriveSessionProgress, { unbondingEras }: PalletNominationPoolsPoolMember): { accountId: string, controllerId: string, redeemable: BN, stashId: string, unlocking: DeriveUnlocking[] } { const unlocking: DeriveUnlocking[] = []; const redeemable = new BN(0); @@ -63,7 +62,7 @@ function calcUnbonding (accountId: string, stashId: string, { activeEra }: Deriv }; } -function Pool ({ accountId, className, info: { bonded: { points, roles }, metadata, nominating, reward, rewardClaimable, stashId }, isFirst, poolId, sessionProgress, targets }: Props): React.ReactElement { +function Pool ({ accountId, className, info: { bonded: { roles }, metadata, nominating, stashId }, isFirst, poolId, sessionProgress, targets }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const spanCount = useSlashingSpans(stashId); @@ -71,7 +70,7 @@ function Pool ({ accountId, className, info: { bonded: { points, roles }, metada const [isBondOpen, toggleBond] = useToggle(); const [isNominateOpen, toggleNominate] = useToggle(); const [isUnbondOpen, toggleUnbond] = useToggle(); - const accInfo = useAccountInfo(accountId, reward, points, rewardClaimable); + const accInfo = useAccountInfo(accountId); const stakingInfo = useMemo( () => sessionProgress && accInfo && accInfo.member.unbondingEras && !accInfo.member.unbondingEras.isEmpty diff --git a/packages/page-staking/src/Actions/Pool/is.ts b/packages/page-staking/src/Actions/Pool/is.ts deleted file mode 100644 index d64bfa1ccc..0000000000 --- a/packages/page-staking/src/Actions/Pool/is.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2022 @polkadot/app-staking authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { PalletNominationPoolsPoolMember, PalletNominationPoolsRewardPool } from '@polkadot/types/lookup'; -import type { PalletNominationPoolsPoolMemberV0, PalletNominationPoolsRewardPoolV0 } from './types'; - -export function isPrevMember (member?: PalletNominationPoolsPoolMember | PalletNominationPoolsPoolMemberV0 | null): member is PalletNominationPoolsPoolMemberV0 { - return !!(member && (member as PalletNominationPoolsPoolMemberV0).rewardPoolTotalEarnings); -} - -export function isPrevPool (pool?: PalletNominationPoolsRewardPool | PalletNominationPoolsRewardPoolV0 | null): pool is PalletNominationPoolsRewardPoolV0 { - return !!(pool && (pool as PalletNominationPoolsRewardPoolV0).totalEarnings); -} diff --git a/packages/page-staking/src/Actions/Pool/types.ts b/packages/page-staking/src/Actions/Pool/types.ts index b6401f0fd0..117ad3dff6 100644 --- a/packages/page-staking/src/Actions/Pool/types.ts +++ b/packages/page-staking/src/Actions/Pool/types.ts @@ -1,25 +1,10 @@ // Copyright 2017-2022 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { BTreeMap, u32, u128, U256 } from '@polkadot/types'; -// import type { PalletNominationPoolsPoolMember } from '@polkadot/types/lookup'; +import type { PalletNominationPoolsPoolMember } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; -export interface PalletNominationPoolsPoolMemberV0 { - readonly poolId: u32; - readonly points: u128; - readonly rewardPoolTotalEarnings: u128; - readonly unbondingEras: BTreeMap; -} - -export interface PalletNominationPoolsRewardPoolV0 { - readonly balance: u128; - readonly totalEarnings: u128; - readonly points: U256; -} - export interface AccountInfo { claimable: BN; - // TODO current version rewards - member: PalletNominationPoolsPoolMemberV0; + member: PalletNominationPoolsPoolMember; } diff --git a/packages/page-staking/src/Actions/Pool/useAccountInfo.ts b/packages/page-staking/src/Actions/Pool/useAccountInfo.ts index 97a5e57de7..5769e6baeb 100644 --- a/packages/page-staking/src/Actions/Pool/useAccountInfo.ts +++ b/packages/page-staking/src/Actions/Pool/useAccountInfo.ts @@ -2,53 +2,35 @@ // SPDX-License-Identifier: Apache-2.0 import type { Option } from '@polkadot/types'; -import type { PalletNominationPoolsPoolMember, PalletNominationPoolsRewardPool } from '@polkadot/types/lookup'; -import type { BN } from '@polkadot/util'; -import type { AccountInfo, PalletNominationPoolsPoolMemberV0, PalletNominationPoolsRewardPoolV0 } from './types'; +import type { PalletNominationPoolsPoolMember } from '@polkadot/types/lookup'; +import type { AccountInfo } from './types'; -import { useMemo } from 'react'; +import { useEffect, useState } from 'react'; -import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; -import { BN_ZERO, bnMax } from '@polkadot/util'; - -import { isPrevMember, isPrevPool } from './is'; +import { createNamedHook, useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks'; const OPT_DEL = { - transform: (opt: Option): PalletNominationPoolsPoolMember | PalletNominationPoolsPoolMemberV0 | null => + transform: (opt: Option): PalletNominationPoolsPoolMember | null => opt.unwrapOr(null) }; -function createInfoPrev (member: PalletNominationPoolsPoolMemberV0, rewardPool: PalletNominationPoolsRewardPoolV0, poolPoints: BN, currentBalance: BN): AccountInfo { - const lastTotalEarnings = rewardPool.totalEarnings; - const currTotalEarnings = bnMax(BN_ZERO, currentBalance.sub(rewardPool.balance)).add(rewardPool.totalEarnings); - const newEarnings = bnMax(BN_ZERO, currTotalEarnings.sub(lastTotalEarnings)); - const newPoints = poolPoints.mul(newEarnings); - const currentPoints = rewardPool.points.add(newPoints); - const newEarningsSinceLastClaim = bnMax(BN_ZERO, currTotalEarnings.sub(member.rewardPoolTotalEarnings)); - const delegatorVirtualPoints = member.points.mul(newEarningsSinceLastClaim); - - return { - claimable: delegatorVirtualPoints.isZero() || currentPoints.isZero() || currentBalance.isZero() - ? BN_ZERO - : delegatorVirtualPoints.mul(currentBalance).div(currentPoints), - member - }; -} - -function useAccountInfoImpl (accountId: string, rewardPool: PalletNominationPoolsRewardPool, poolPoints: BN, rewardClaimable: BN): AccountInfo | undefined | null { +function useAccountInfoImpl (accountId: string): AccountInfo | null { const { api } = useApi(); + const isMountedRef = useIsMountedRef(); + const [state, setState] = useState(null); const member = useCall(api.query.nominationPools.poolMembers, [accountId], OPT_DEL); - return useMemo( - () => isPrevPool(rewardPool) - ? member && ( - isPrevMember(member) - ? createInfoPrev(member, rewardPool, poolPoints, rewardClaimable) - : null - ) - : null, - [member, poolPoints, rewardPool, rewardClaimable] - ); + useEffect((): void => { + member && api.call.nominationPoolsApi && + api.call.nominationPoolsApi + .pendingRewards(accountId) + .then((claimable) => + isMountedRef.current && setState({ claimable, member }) + ) + .catch(console.error); + }, [accountId, member, api, isMountedRef]); + + return state; } export default createNamedHook('useAccountInfo', useAccountInfoImpl);