From 881c020b5b69a3185317e6424298289c4bf85e4e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 11 Aug 2020 19:42:01 +0200 Subject: [PATCH] Reformat unapplied tables (#3390) * Reformat unapplied tables * Payout total --- packages/page-staking/src/Slashes/Row.tsx | 9 +- packages/page-staking/src/Slashes/index.tsx | 106 ++++++++++++++------ packages/page-staking/src/Slashes/types.ts | 1 - 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/packages/page-staking/src/Slashes/Row.tsx b/packages/page-staking/src/Slashes/Row.tsx index d4c042d0b9..4086b16eec 100644 --- a/packages/page-staking/src/Slashes/Row.tsx +++ b/packages/page-staking/src/Slashes/Row.tsx @@ -13,17 +13,13 @@ import { useTranslation } from '../translate'; interface Props { slash: Slash; - withEra: boolean; } -function Row ({ slash: { cumulative, era, slash: { others, own, payout, reporters, validator }, total, totalOther }, withEra }: Props): React.ReactElement { +function Row ({ slash: { slash: { others, own, payout, reporters, validator }, total, totalOther } }: Props): React.ReactElement { const { t } = useTranslation(); return ( - -

{withEra ? formatNumber(era) : <> }

- @@ -61,9 +57,6 @@ function Row ({ slash: { cumulative, era, slash: { others, own, payout, reporter - - - ); } diff --git a/packages/page-staking/src/Slashes/index.tsx b/packages/page-staking/src/Slashes/index.tsx index a4eedb8a2f..3c3e9696e1 100644 --- a/packages/page-staking/src/Slashes/index.tsx +++ b/packages/page-staking/src/Slashes/index.tsx @@ -8,6 +8,8 @@ import { Slash } from './types'; import BN from 'bn.js'; import React, { useMemo } from 'react'; import { Table } from '@polkadot/react-components'; +import { FormatBalance } from '@polkadot/react-query'; +import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; import Row from './Row'; @@ -16,58 +18,106 @@ interface Props { slashes: [BN, UnappliedSlash[]][]; } +interface SlashEra { + era: BN; + nominators: string[]; + payout: BN; + slashes: Slash[]; + validators: string[]; + total: BN; +} + function Slashes ({ slashes }: Props): React.ReactElement | null { const { t } = useTranslation(); const header = useMemo(() => [ - [t('unapplied slashes'), 'start', 3], + [], [t('own')], [t('other')], [t('total')], - [t('reporters'), 'start'], - [t('payout')], - [t('era cumulative')] + [t('reporters'), 'address'], + [t('payout')] ], [t]); - const rows = useMemo((): Slash[] => { - const rows = slashes + const rows = useMemo((): SlashEra[] => { + const slashEras: SlashEra[] = []; + + slashes .reduce((rows: Slash[], [era, slashes]): Slash[] => { return slashes.reduce((rows: Slash[], slash): Slash[] => { const totalOther = slash.others.reduce((total: BN, [, value]): BN => { return total.add(value); }, new BN(0)); - const total = slash.own.add(totalOther); - - rows.push({ cumulative: total, era, slash, total, totalOther }); + rows.push({ era, slash, total: slash.own.add(totalOther), totalOther }); return rows; }, rows); }, []) - .sort((a, b) => b.era.cmp(a.era)); + .forEach((slash): void => { + let slashEra = slashEras.find(({ era }) => era.eq(slash.era)); - rows.forEach((row, index): void => { - const prev = rows[index - 1]; + if (!slashEra) { + slashEra = { + era: slash.era, + nominators: [], + payout: new BN(0), + slashes: [], + total: new BN(0), + validators: [] + }; + slashEras.push(slashEra); + } - if (prev && prev.era.eq(row.era)) { - row.cumulative = row.cumulative.add(prev.cumulative); - } - }); + slashEra.payout.iadd(slash.slash.payout); + slashEra.total.iadd(slash.total); + slashEra.slashes.push(slash); - return rows; + const validatorId = slash.slash.validator.toString(); + + if (!slashEra.validators.includes(validatorId)) { + slashEra.validators.push(validatorId); + } + + slash.slash.others.forEach(([accountId]): void => { + const nominatorId = accountId.toString(); + + if (slashEra && !slashEra.nominators.includes(nominatorId)) { + slashEra.nominators.push(nominatorId); + } + }); + }); + + return slashEras.sort((a, b) => b.era.cmp(a.era)); }, [slashes]); + if (!rows.length) { + return
{t('There are no unapplied/pending slashes')}
; + } + return ( - ('There an no unapplied/pending slashes')} - header={header} - > - {rows.map((slash, index): React.ReactNode => ( - + <> + {rows.map(({ era, payout, slashes, total, validators }): React.ReactNode => ( +
+ + + + + } + header={[[t('era {{era}}/unapplied', { replace: { era: era.toString() } }), 'start']].concat(header)} + key={era.toString()} + > + {slashes.map((slash, index): React.ReactNode => ( + + ))} +
{formatNumber(validators.length)} + +
))} - + ); } diff --git a/packages/page-staking/src/Slashes/types.ts b/packages/page-staking/src/Slashes/types.ts index 47c96a8827..f260fca6d6 100644 --- a/packages/page-staking/src/Slashes/types.ts +++ b/packages/page-staking/src/Slashes/types.ts @@ -7,7 +7,6 @@ import { UnappliedSlash } from '@polkadot/types/interfaces'; import BN from 'bn.js'; export interface Slash { - cumulative: BN; era: BN; slash: UnappliedSlash; total: BN;