From 06a6d1cc2547da5e4ad49b0dd60d9a25ff79eff5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 28 Mar 2020 08:47:23 +0100 Subject: [PATCH] Cleanup staking list state (#2470) * Cleanup staking list state * Cleanup conversions --- .../src/Overview/Address/StakeOther.tsx | 6 ++-- .../src/Overview/Address/index.tsx | 35 ++++++++++--------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/packages/page-staking/src/Overview/Address/StakeOther.tsx b/packages/page-staking/src/Overview/Address/StakeOther.tsx index 7db0dbfa6e..684994d76f 100644 --- a/packages/page-staking/src/Overview/Address/StakeOther.tsx +++ b/packages/page-staking/src/Overview/Address/StakeOther.tsx @@ -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 { AccountId, Balance } from '@polkadot/types/interfaces'; +import { Balance } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import React from 'react'; @@ -14,7 +14,7 @@ import { useTranslation } from '../../translate'; interface Props { stakeOther?: BN; - nominators: [AccountId, Balance][]; + nominators: [string, Balance][]; } function StakeOther ({ nominators, stakeOther }: Props): React.ReactElement { @@ -33,7 +33,7 @@ function StakeOther ({ nominators, stakeOther }: Props): React.ReactElement diff --git a/packages/page-staking/src/Overview/Address/index.tsx b/packages/page-staking/src/Overview/Address/index.tsx index 66fc696e5d..77e2d3cc37 100644 --- a/packages/page-staking/src/Overview/Address/index.tsx +++ b/packages/page-staking/src/Overview/Address/index.tsx @@ -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 { AccountId, Balance } from '@polkadot/types/interfaces'; +import { Balance } from '@polkadot/types/interfaces'; import { DeriveAccountInfo, DeriveStakingQuery } from '@polkadot/api-derive/types'; import BN from 'bn.js'; @@ -34,35 +34,36 @@ interface Props { points?: false | number; setNominators?: false | ((nominators: string[]) => void); toggleFavorite: (accountId: string) => void; - withNominations?: boolean; } interface StakingState { commission?: string; - controllerId?: string; - nominators: [AccountId, Balance][]; - sessionId?: string; + nominators: [string, Balance][]; stakeTotal?: BN; stakeOther?: BN; stakeOwn?: BN; } -function expandInfo ({ controllerId, exposure, nextSessionIds, validatorPrefs }: DeriveStakingQuery, withNominations = true): StakingState { - const nominators = withNominations && exposure - ? exposure.others.map(({ who, value }): [AccountId, Balance] => [who, value.unwrap()]) - : []; - const stakeTotal = (exposure && !exposure.total.isEmpty && exposure.total.unwrap()) || undefined; - const stakeOwn = (exposure && !exposure.own.isEmpty && exposure.own.unwrap()) || undefined; - const stakeOther = (stakeTotal && stakeOwn) ? stakeTotal.sub(stakeOwn) : undefined; +function expandInfo ({ exposure, validatorPrefs }: DeriveStakingQuery): StakingState { + let nominators: [string, Balance][] = []; + let stakeTotal: BN | undefined; + let stakeOther: BN | undefined; + let stakeOwn: BN | undefined; + + if (exposure) { + nominators = exposure.others.map(({ who, value }): [string, Balance] => [who.toString(), value.unwrap()]); + stakeTotal = exposure.total.unwrap(); + stakeOwn = exposure.own.unwrap(); + stakeOther = stakeTotal.sub(stakeOwn); + } + const commission = validatorPrefs?.commission?.unwrap(); return { commission: commission ? `${(commission.toNumber() / 10_000_000).toFixed(2)}%` : undefined, - controllerId: controllerId?.toString(), nominators, - sessionId: nextSessionIds && nextSessionIds[0]?.toString(), stakeOther, stakeOwn, stakeTotal @@ -100,7 +101,7 @@ function checkVisibility (api: ApiPromise, address: string, filterName: string, return isVisible; } -function Address ({ address, className, filterName, hasQueries, isAuthor, isElected, isFavorite, isMain, lastBlock, onlineCount, onlineMessage, points, setNominators, toggleFavorite, withNominations }: Props): React.ReactElement | null { +function Address ({ address, className, filterName, hasQueries, isAuthor, isElected, isFavorite, isMain, lastBlock, onlineCount, onlineMessage, points, setNominators, toggleFavorite }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const info = useCall(api.derive.accounts.info as any, [address]); @@ -110,12 +111,12 @@ function Address ({ address, className, filterName, hasQueries, isAuthor, isElec useEffect((): void => { if (stakingInfo) { - const info = expandInfo(stakingInfo, withNominations); + const info = expandInfo(stakingInfo); setNominators && setNominators(info.nominators.map(([who]): string => who.toString())); setStakingState(info); } - }, [setNominators, stakingInfo, withNominations]); + }, [setNominators, stakingInfo]); useEffect((): void => { setIsVisible(