From 7b360da193f4707c9719196a03290151d4ec6c35 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 11 Aug 2020 23:02:42 +0200 Subject: [PATCH] Slash enact countdown (#3393) * Slach enact * Update packages/page-staking/src/Slashes/Header.tsx --- packages/page-staking/src/Slashes/Header.tsx | 78 ++++++++++++++++++++ packages/page-staking/src/Slashes/index.tsx | 48 ++---------- packages/page-staking/src/Slashes/types.ts | 10 +++ 3 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 packages/page-staking/src/Slashes/Header.tsx diff --git a/packages/page-staking/src/Slashes/Header.tsx b/packages/page-staking/src/Slashes/Header.tsx new file mode 100644 index 0000000000..6e72644400 --- /dev/null +++ b/packages/page-staking/src/Slashes/Header.tsx @@ -0,0 +1,78 @@ +// Copyright 2017-2020 @polkadot/app-staking authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { DeriveSessionProgress } from '@polkadot/api-derive/types'; +import { SlashEra } from './types'; + +import BN from 'bn.js'; +import React, { useEffect, useState } from 'react'; +import { CardSummary, SummaryBox } from '@polkadot/react-components'; +import { useApi, useCall } from '@polkadot/react-hooks'; +import { FormatBalance } from '@polkadot/react-query'; +import { formatNumber } from '@polkadot/util'; + +import { useTranslation } from '../translate'; + +interface Props { + slash: SlashEra; +} + +function calcBlocks (slashDeferDuration: BN, era: BN, { activeEra, eraLength, eraProgress }: DeriveSessionProgress): [BN, BN] { + return [ + activeEra.sub(era).subn(1).mul(eraLength).add(eraProgress), + slashDeferDuration.mul(eraLength) + ]; +} + +function Header ({ slash: { era, nominators, reporters, total, validators } }: Props): React.ReactElement | null { + const { t } = useTranslation(); + const { api } = useApi(); + const sessionInfo = useCall(api.derive.session?.progress, []); + const [[blockProgress, blockEnd], setBlocks] = useState([new BN(0), new BN(0)]); + + useEffect((): void => { + sessionInfo && setBlocks( + calcBlocks(api.consts.staking.slashDeferDuration, era, sessionInfo) + ); + }, [api, era, sessionInfo]); + + return ( + + + +
+ ('validators')}> + {formatNumber(validators.length)} + + ('nominators')}> + {formatNumber(nominators.length)} + + ('reporters')}> + {formatNumber(reporters.length)} + +
+ {blockProgress.gtn(0) && ( +
+ ('defer')} + progress={{ + total: blockEnd, + value: blockProgress, + withTime: true + }} + /> +
+ )} +
+ ('total')}> + + +
+
+ + + ); +} + +export default React.memo(Header); diff --git a/packages/page-staking/src/Slashes/index.tsx b/packages/page-staking/src/Slashes/index.tsx index 433e04b62f..f81c6028a4 100644 --- a/packages/page-staking/src/Slashes/index.tsx +++ b/packages/page-staking/src/Slashes/index.tsx @@ -4,15 +4,14 @@ import { StakerState } from '@polkadot/react-hooks/types'; import { UnappliedSlash } from '@polkadot/types/interfaces'; -import { Slash } from './types'; +import { Slash, SlashEra } from './types'; import BN from 'bn.js'; import React, { useMemo } from 'react'; -import { CardSummary, SummaryBox, Table } from '@polkadot/react-components'; -import { FormatBalance } from '@polkadot/react-query'; -import { formatNumber } from '@polkadot/util'; +import { Table } from '@polkadot/react-components'; import { useTranslation } from '../translate'; +import Header from './Header'; import Row from './Row'; interface Props { @@ -20,16 +19,6 @@ interface Props { slashes: [BN, UnappliedSlash[]][]; } -interface SlashEra { - era: BN; - nominators: string[]; - payout: BN; - reporters: string[]; - slashes: Slash[]; - validators: string[]; - total: BN; -} - function calcSlashEras (slashes: [BN, UnappliedSlash[]][], ownStashes: StakerState[]): SlashEra[] { const slashEras: SlashEra[] = []; @@ -118,33 +107,12 @@ function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement - {rows.map(({ era, nominators, reporters, slashes, total, validators }): React.ReactNode => ( + {rows.map((slash): React.ReactNode => ( - - - +
{header.map(([label, className, colSpan = 1], index): React.ReactNode => ( - {slashes.map((slash, index): React.ReactNode => ( + {slash.slashes.map((slash, index): React.ReactNode => (
- -
- ('validators')}> - {formatNumber(validators.length)} - - ('nominators')}> - {formatNumber(nominators.length)} - - ('reporters')}> - {formatNumber(reporters.length)} - -
-
- ('total')}> - - -
-
-
))}