Slash enact countdown (#3393)

* Slach enact

* Update packages/page-staking/src/Slashes/Header.tsx
This commit is contained in:
Jaco Greeff 2020-08-11 23:02:42 +02:00 committed by GitHub
parent c1df9f93f0
commit 7b360da193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 40 deletions

View File

@ -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<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const sessionInfo = useCall<DeriveSessionProgress>(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 (
<tr>
<td colSpan={8}>
<SummaryBox isSmall>
<section>
<CardSummary label={t<string>('validators')}>
{formatNumber(validators.length)}
</CardSummary>
<CardSummary label={t<string>('nominators')}>
{formatNumber(nominators.length)}
</CardSummary>
<CardSummary label={t<string>('reporters')}>
{formatNumber(reporters.length)}
</CardSummary>
</section>
{blockProgress.gtn(0) && (
<section>
<CardSummary
label={t<string>('defer')}
progress={{
total: blockEnd,
value: blockProgress,
withTime: true
}}
/>
</section>
)}
<section>
<CardSummary label={t<string>('total')}>
<FormatBalance value={total} />
</CardSummary>
</section>
</SummaryBox>
</td>
</tr>
);
}
export default React.memo(Header);

View File

@ -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<Props
return (
<>
{rows.map(({ era, nominators, reporters, slashes, total, validators }): React.ReactNode => (
{rows.map((slash): React.ReactNode => (
<Table
header={[[t('era {{era}}/unapplied', { replace: { era: era.toString() } }), 'start', 6]]}
key={era.toString()}
header={[[t('era {{era}}/unapplied', { replace: { era: slash.era.toString() } }), 'start', 6]]}
key={slash.era.toString()}
>
<tr>
<td colSpan={8}>
<SummaryBox isSmall>
<section>
<CardSummary label={t<string>('validators')}>
{formatNumber(validators.length)}
</CardSummary>
<CardSummary label={t<string>('nominators')}>
{formatNumber(nominators.length)}
</CardSummary>
<CardSummary label={t<string>('reporters')}>
{formatNumber(reporters.length)}
</CardSummary>
</section>
<section>
<CardSummary label={t<string>('total')}>
<FormatBalance value={total} />
</CardSummary>
</section>
</SummaryBox>
</td>
</tr>
<Header slash={slash} />
<tr>
{header.map(([label, className, colSpan = 1], index): React.ReactNode => (
<td
@ -156,7 +124,7 @@ function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement<Props
</td>
))}
</tr>
{slashes.map((slash, index): React.ReactNode => (
{slash.slashes.map((slash, index): React.ReactNode => (
<Row
key={index}
slash={slash}

View File

@ -13,3 +13,13 @@ export interface Slash {
total: BN;
totalOther: BN;
}
export interface SlashEra {
era: BN;
nominators: string[];
payout: BN;
reporters: string[];
slashes: Slash[];
validators: string[];
total: BN;
}