Retrieve pool pending via runtime call (#8083)
* Retrieve pool pending via call * Remove is * mountedRef
This commit is contained in:
parent
84849fb8e2
commit
04e5f4d23b
@ -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<Props> {
|
||||
function Pool ({ accountId, className, info: { bonded: { roles }, metadata, nominating, stashId }, isFirst, poolId, sessionProgress, targets }: Props): React.ReactElement<Props> {
|
||||
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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -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<u32, u128>;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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>): PalletNominationPoolsPoolMember | PalletNominationPoolsPoolMemberV0 | null =>
|
||||
transform: (opt: Option<PalletNominationPoolsPoolMember>): 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<AccountInfo | null>(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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user