From eba24d5ca46a68c982d2e5ce63c2fde69c5d4467 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 30 Aug 2020 16:30:52 +0200 Subject: [PATCH] Remove 64 fallback (#3575) * Remove 64 fallback * toString * Protection * No slice-and-dice when in-range * linting * Ref for labels --- .../src/Actions/Account/ListNominees.tsx | 3 +- .../page-staking/src/Actions/useInactives.ts | 6 +-- packages/page-staking/src/Ledgend.tsx | 3 +- packages/page-staking/src/MaxBadge.tsx | 6 +-- .../src/Overview/Address/StakeOther.tsx | 14 ++++-- packages/page-staking/src/Targets/index.tsx | 50 +++++++++---------- packages/page-staking/src/constants.ts | 4 -- 7 files changed, 40 insertions(+), 46 deletions(-) diff --git a/packages/page-staking/src/Actions/Account/ListNominees.tsx b/packages/page-staking/src/Actions/Account/ListNominees.tsx index 6916aa9a16..e9cebd6fa3 100644 --- a/packages/page-staking/src/Actions/Account/ListNominees.tsx +++ b/packages/page-staking/src/Actions/Account/ListNominees.tsx @@ -6,7 +6,6 @@ import React from 'react'; import { AddressMini, Expander } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; -import { MAX_NOM_PAYOUTS } from '../../constants'; import { useTranslation } from '../../translate'; import useInactives from '../useInactives'; @@ -20,7 +19,7 @@ function ListNominees ({ nominating, stashId }: Props): React.ReactElement diff --git a/packages/page-staking/src/Actions/useInactives.ts b/packages/page-staking/src/Actions/useInactives.ts index d5c0eb8438..1fea485ca4 100644 --- a/packages/page-staking/src/Actions/useInactives.ts +++ b/packages/page-staking/src/Actions/useInactives.ts @@ -10,8 +10,6 @@ import { ApiPromise } from '@polkadot/api'; import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks'; import { Option } from '@polkadot/types'; -import { MAX_NOM_PAYOUTS } from '../constants'; - interface Inactives { nomsActive?: string[]; nomsChilled?: string[]; @@ -21,7 +19,7 @@ interface Inactives { } function extractState (api: ApiPromise, stashId: string, slashes: Option[], nominees: string[], activeEra: EraIndex, submittedIn: EraIndex, exposures: Exposure[]): Inactives { - const max = (api.consts.staking?.maxNominatorRewardedPerValidator || MAX_NOM_PAYOUTS); + const max = api.consts.staking?.maxNominatorRewardedPerValidator; // chilled const nomsChilled = nominees.filter((_, index): boolean => { @@ -38,7 +36,7 @@ function extractState (api: ApiPromise, stashId: string, slashes: Option others.sort((a, b) => b.value.unwrap().cmp(a.value.unwrap()))) .map((others, index) => - max.gtn(others.map(({ who }) => who.toString()).indexOf(stashId)) + !max || max.gtn(others.map(({ who }) => who.toString()).indexOf(stashId)) ? null : nominees[index] ) diff --git a/packages/page-staking/src/Ledgend.tsx b/packages/page-staking/src/Ledgend.tsx index 1455377e6a..9d73a10265 100644 --- a/packages/page-staking/src/Ledgend.tsx +++ b/packages/page-staking/src/Ledgend.tsx @@ -7,7 +7,6 @@ import styled from 'styled-components'; import { Badge, Icon } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; -import { MAX_NOM_PAYOUTS } from './constants'; import { useTranslation } from './translate'; interface Props { @@ -51,7 +50,7 @@ function Ledgend ({ className }: Props): React.ReactElement { {t('Oversubscribed')} diff --git a/packages/page-staking/src/MaxBadge.tsx b/packages/page-staking/src/MaxBadge.tsx index 717b211876..2de9e9ba45 100644 --- a/packages/page-staking/src/MaxBadge.tsx +++ b/packages/page-staking/src/MaxBadge.tsx @@ -6,8 +6,6 @@ import React from 'react'; import { Badge } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; -import { MAX_NOM_PAYOUTS } from './constants'; - interface Props { numNominators?: number; } @@ -15,9 +13,9 @@ interface Props { function MaxBadge ({ numNominators }: Props): React.ReactElement | null { const { api } = useApi(); - const max = (api.consts.staking?.maxNominatorRewardedPerValidator || MAX_NOM_PAYOUTS); + const max = api.consts.staking?.maxNominatorRewardedPerValidator; - if (!numNominators || max.gten(numNominators)) { + if (!numNominators || !max || max.gten(numNominators)) { return null; } diff --git a/packages/page-staking/src/Overview/Address/StakeOther.tsx b/packages/page-staking/src/Overview/Address/StakeOther.tsx index fa1e1e5c54..5e82d11d48 100644 --- a/packages/page-staking/src/Overview/Address/StakeOther.tsx +++ b/packages/page-staking/src/Overview/Address/StakeOther.tsx @@ -9,21 +9,25 @@ import React, { useMemo } from 'react'; import { AddressMini, Expander } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; - -import { MAX_NOM_PAYOUTS } from '../../constants'; +import { BN_ZERO } from '@polkadot/util'; interface Props { stakeOther?: BN; nominators: [string, Balance][]; } -function StakeOther ({ nominators }: Props): React.ReactElement { +function StakeOther ({ nominators, stakeOther }: Props): React.ReactElement { const { api } = useApi(); const [rewarded, rewardedTotal, unrewarded, unrewardedTotal] = useMemo( (): [[string, Balance][], BN, [string, Balance][], BN] => { const sorted = nominators.sort((a, b) => b[1].cmp(a[1])); - const max = (api.consts.staking?.maxNominatorRewardedPerValidator || MAX_NOM_PAYOUTS).toNumber(); + const max = api.consts.staking?.maxNominatorRewardedPerValidator?.toNumber(); + + if (!max || sorted.length <= max) { + return [sorted, stakeOther || BN_ZERO, [], BN_ZERO]; + } + const rewarded = sorted.slice(0, max); const rewardedTotal = rewarded.reduce((total, [, value]) => total.iadd(value), new BN(0)); const unrewarded = sorted.slice(max); @@ -31,7 +35,7 @@ function StakeOther ({ nominators }: Props): React.ReactElement { return [rewarded, rewardedTotal, unrewarded, unrewardedTotal]; }, - [api, nominators] + [api, nominators, stakeOther] ); return ( diff --git a/packages/page-staking/src/Targets/index.tsx b/packages/page-staking/src/Targets/index.tsx index b1319d7f73..05bd2f4560 100644 --- a/packages/page-staking/src/Targets/index.tsx +++ b/packages/page-staking/src/Targets/index.tsx @@ -6,14 +6,14 @@ import { DeriveStakingOverview } from '@polkadot/api-derive/types'; import { StakerState } from '@polkadot/react-hooks/types'; import { SortedTargets, TargetSortBy, ValidatorInfo } from '../types'; -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import styled from 'styled-components'; import { Button, Icon, InputBalance, Table, Toggle } from '@polkadot/react-components'; import { useApi, useAvailableSlashes } from '@polkadot/react-hooks'; import ElectionBanner from '../ElectionBanner'; import Filtering from '../Filtering'; -import { MAX_NOMINATIONS, MAX_NOM_PAYOUTS } from '../constants'; +import { MAX_NOMINATIONS } from '../constants'; import { useTranslation } from '../translate'; import Nominate from './Nominate'; import Summary from './Summary'; @@ -109,41 +109,41 @@ function Targets ({ className = '', isInElection, ownStashes, targets: { avgStak ); const _selectProfitable = useCallback( - () => setSelected( - (validators || []).reduce((result: string[], { hasIdentity, isElected, isFavorite, key, numNominators, rewardPayout }): string[] => { - if ((api.consts.staking?.maxNominatorRewardedPerValidator || MAX_NOM_PAYOUTS).gtn(numNominators) && (result.length < MAX_NOMINATIONS) && (hasIdentity || !withIdentity) && (isElected || isFavorite) && !rewardPayout.isZero()) { + () => setSelected((): string[] => { + const max = api.consts.staking?.maxNominatorRewardedPerValidator; + + return (validators || []).reduce((result: string[], { hasIdentity, isElected, isFavorite, key, numNominators, rewardPayout }): string[] => { + if ((result.length < MAX_NOMINATIONS) && (hasIdentity || !withIdentity) && (isElected || isFavorite) && !rewardPayout.isZero() && (!max || max.gtn(numNominators))) { result.push(key); } return result; - }, []) - ), + }, []); + }), [api, validators, withIdentity] ); - const labels = useMemo( - (): Record => ({ - rankBondOther: t('other stake'), - rankBondOwn: t('own stake'), - rankBondTotal: t('total stake'), - rankComm: t('comm.'), - rankNumNominators: t('nominators'), - rankOverall: t('profit/era') - }), - [t] - ); + const labelsRef = useRef({ + rankBondOther: t('other stake'), + rankBondOwn: t('own stake'), + rankBondTotal: t('total stake'), + rankComm: t('comm.'), + rankNumNominators: t('nominators'), + rankOverall: t('profit/era') + }); const header = useMemo(() => [ [t('validators'), 'start', 3], - ...['rankNumNominators', 'rankComm', 'rankBondTotal', 'rankBondOwn', 'rankBondOther', 'rankOverall'].map((header) => [ - <>{labels[header]}, - `${sorted ? `isClickable ${sortBy === header ? 'highlight--border' : ''} number` : 'number'} ${CLASSES[header] || ''}`, - 1, - (): void => _sort(header as 'rankComm') - ]), + ...(['rankNumNominators', 'rankComm', 'rankBondTotal', 'rankBondOwn', 'rankBondOther', 'rankOverall'] as (keyof typeof labelsRef.current)[]) + .map((header) => [ + <>{labelsRef.current[header]}, + `${sorted ? `isClickable ${sortBy === header ? 'highlight--border' : ''} number` : 'number'} ${CLASSES[header] || ''}`, + 1, + () => _sort(header as 'rankComm') + ]), [], [] - ], [_sort, labels, sortBy, sorted, sortFromMax, t]); + ], [_sort, labelsRef, sortBy, sorted, sortFromMax, t]); const filter = useMemo(() => ( sorted && ( diff --git a/packages/page-staking/src/constants.ts b/packages/page-staking/src/constants.ts index 8e48eb4eab..46f86d0d9a 100644 --- a/packages/page-staking/src/constants.ts +++ b/packages/page-staking/src/constants.ts @@ -2,10 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import BN from 'bn.js'; - export const MAX_NOMINATIONS = 16; -export const MAX_NOM_PAYOUTS = new BN(64); - export const STORE_FAVS_BASE = 'staking:favorites';