diff --git a/packages/page-council/src/Motions/Slashing.tsx b/packages/page-council/src/Motions/Slashing.tsx index fe5deeb17f..7c21130439 100644 --- a/packages/page-council/src/Motions/Slashing.tsx +++ b/packages/page-council/src/Motions/Slashing.tsx @@ -6,11 +6,10 @@ import { SubmittableExtrinsic } from '@polkadot/api/types'; import React, { useEffect, useState } from 'react'; import { Button, Dropdown, Input, InputAddress, Modal, TxButton } from '@polkadot/react-components'; -import { useApi, useToggle } from '@polkadot/react-hooks'; +import { useApi, useAvailableSlashes, useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; import { getSlashThreshold } from './thresholds'; -import useAvailableSlashes from './useAvailableSlashes'; interface Props { className?: string; diff --git a/packages/page-staking/src/Actions/Account/ListNominees.tsx b/packages/page-staking/src/Actions/Account/ListNominees.tsx index 9f032e4a03..67280c7ff2 100644 --- a/packages/page-staking/src/Actions/Account/ListNominees.tsx +++ b/packages/page-staking/src/Actions/Account/ListNominees.tsx @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { Slash } from '../types'; + import React from 'react'; import { AddressMini, Expander } from '@polkadot/react-components'; @@ -11,11 +13,12 @@ import useInactives from '../useInactives'; interface Props { nominating?: string[]; stashId: string; + slashes: Slash[]; } -function ListNominees ({ nominating, stashId }: Props): React.ReactElement { +function ListNominees ({ nominating, slashes, stashId }: Props): React.ReactElement { const { t } = useTranslation(); - const { nomsActive, nomsInactive, nomsWaiting } = useInactives(stashId, nominating); + const { nomsActive, nomsChilled, nomsInactive, nomsWaiting } = useInactives(stashId, slashes, nominating); return ( <> @@ -30,6 +33,17 @@ function ListNominees ({ nominating, stashId }: Props): React.ReactElement )} + {nomsChilled && nomsChilled.length !== 0 && ( + ('Chilled nominations ({{count}})', { replace: { count: nomsChilled.length } })}> + {nomsChilled.map((nomineeId, index): React.ReactNode => ( + + ))} + + )} {nomsInactive && nomsInactive.length !== 0 && ( ('Inactive nominations ({{count}})', { replace: { count: nomsInactive.length } })}> {nomsInactive.map((nomineeId, index): React.ReactNode => ( diff --git a/packages/page-staking/src/Actions/Account/Nominate.tsx b/packages/page-staking/src/Actions/Account/Nominate.tsx index 5fd794bef2..ae7c13f376 100644 --- a/packages/page-staking/src/Actions/Account/Nominate.tsx +++ b/packages/page-staking/src/Actions/Account/Nominate.tsx @@ -6,12 +6,14 @@ import { NominateInfo } from '../partials/types'; import { SortedTargets } from '../../types'; import React, { useState } from 'react'; +import styled from 'styled-components'; import { Modal, TxButton } from '@polkadot/react-components'; import { useTranslation } from '../../translate'; import NominatePartial from '../partials/Nominate'; interface Props { + className?: string; controllerId: string; next?: string[]; nominating?: string[]; @@ -21,17 +23,19 @@ interface Props { validators?: string[]; } -function Nominate ({ controllerId, next, nominating, onClose, stashId, targets, validators }: Props): React.ReactElement | null { +function Nominate ({ className = '', controllerId, next, nominating, onClose, stashId, targets, validators }: Props): React.ReactElement | null { const { t } = useTranslation(); const [{ nominateTx }, setTx] = useState({}); return ( ('Nominate Validators')} size='large' > { +function Account ({ allSlashes, className = '', info: { controllerId, destination, destinationId, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, next, targets, validators }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { queueExtrinsic } = useContext(StatusContext); @@ -57,6 +60,7 @@ function Account ({ className = '', info: { controllerId, destination, destinati const [isUnbondOpen, toggleUnbond] = useToggle(); const [isValidateOpen, toggleValidate] = useToggle(); const [hasBonded, setHasBonded] = useState(false); + const [slashes, setSlashes] = useState([]); useEffect((): void => { stakingAccount?.stakingLedger && setHasBonded( @@ -64,6 +68,26 @@ function Account ({ className = '', info: { controllerId, destination, destinati ); }, [stakingAccount]); + useEffect((): void => { + allSlashes && setSlashes( + allSlashes + .map(([era, all]) => { + const slashes = all + .filter(({ others, validator }) => validator.eq(stashId) || others.some(([nominatorId]) => nominatorId.eq(stashId))) + .map(({ others, own, payout, reporters, validator }) => ({ + isValidator: validator.eq(stashId), others, own, payout, reporters, validator + })); + + return { + era, + isValidator: slashes.some(({ isValidator }) => isValidator), + slashes + }; + }) + .filter(({ slashes }) => slashes.length) + ); + }, [allSlashes, stashId]); + const withdrawFunds = useCallback( () => { queueExtrinsic({ @@ -80,6 +104,19 @@ function Account ({ className = '', info: { controllerId, destination, destinati return ( + + {!isStashNominating && slashes.length !== 0 && ( + ('Slashed in era {{eras}}', { + replace: { + eras: slashes.map(({ era }) => formatNumber(era)).join(', ') + } + })} + icon='skull-crossbones' + /> + )} + {isBondExtraOpen && ( @@ -167,6 +204,7 @@ function Account ({ className = '', info: { controllerId, destination, destinati {isStashNominating && ( )} diff --git a/packages/page-staking/src/Actions/index.tsx b/packages/page-staking/src/Actions/index.tsx index 0b7cc087cd..7fe9fcd74b 100644 --- a/packages/page-staking/src/Actions/index.tsx +++ b/packages/page-staking/src/Actions/index.tsx @@ -2,16 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ActiveEraInfo, EraIndex } from '@polkadot/types/interfaces'; import { StakerState } from '@polkadot/react-hooks/types'; import { SortedTargets } from '../types'; import BN from 'bn.js'; import React, { useEffect, useMemo, useState } from 'react'; import { Button, Table } from '@polkadot/react-components'; -import { useCall, useApi } from '@polkadot/react-hooks'; +import { useAvailableSlashes } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; -import { Option } from '@polkadot/types'; import { BN_ZERO } from '@polkadot/util'; import ElectionBanner from '../ElectionBanner'; @@ -52,10 +50,7 @@ function extractState (ownStashes: StakerState[]): State { function Actions ({ className = '', isInElection, next, ownStashes, targets, validators }: Props): React.ReactElement { const { t } = useTranslation(); - const { api } = useApi(); - const activeEra = useCall(api.query.staking?.activeEra, [], { - transform: (activeEra: Option) => activeEra.unwrapOr({ index: undefined }).index - }); + const allSlashes = useAvailableSlashes(); const [{ bondedTotal, foundStashes }, setState] = useState({}); useEffect((): void => { @@ -63,7 +58,7 @@ function Actions ({ className = '', isInElection, next, ownStashes, targets, val }, [ownStashes]); const header = useMemo(() => [ - [t('stashes'), 'start'], + [t('stashes'), 'start', 2], [t('controller'), 'address'], [t('rewards'), 'number ui--media-1200'], [t('bonded'), 'number'], @@ -72,7 +67,7 @@ function Actions ({ className = '', isInElection, next, ownStashes, targets, val const footer = useMemo(() => ( - + {bondedTotal && } @@ -100,7 +95,7 @@ function Actions ({ className = '', isInElection, next, ownStashes, targets, val > {foundStashes?.map((info): React.ReactNode => ( (initialPick(targets)); + const [{ isAutoSelect, selected }, setSelected] = useState( + // nominating?.length + // ? { isAutoSelect: false, selected: nominating } + // : initialPick(targets) + initialPick(targets) + ); const [available] = useState((): string[] => { const shortlist = [ // ensure that the favorite is included in the list of stashes diff --git a/packages/page-staking/src/Actions/types.ts b/packages/page-staking/src/Actions/types.ts index d2170723ec..adb761161f 100644 --- a/packages/page-staking/src/Actions/types.ts +++ b/packages/page-staking/src/Actions/types.ts @@ -2,7 +2,26 @@ // 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, UnappliedSlashOther } from '@polkadot/types/interfaces'; + +import BN from 'bn.js'; + export interface AmountValidateState { error: string | null; warning: string | null; } + +interface Unapplied { + isValidator: boolean; + others: UnappliedSlashOther[]; + own: Balance; + payout: Balance; + reporters: AccountId[]; + validator: AccountId; +} + +export interface Slash { + era: BN; + isValidator: boolean; + slashes: Unapplied[]; +} diff --git a/packages/page-staking/src/Actions/useInactives.ts b/packages/page-staking/src/Actions/useInactives.ts index 0add96f71c..5eb786b9fb 100644 --- a/packages/page-staking/src/Actions/useInactives.ts +++ b/packages/page-staking/src/Actions/useInactives.ts @@ -4,6 +4,7 @@ import { DeriveSessionIndexes } from '@polkadot/api-derive/types'; import { EraIndex, Exposure, Nominations } from '@polkadot/types/interfaces'; +import { Slash } from './types'; import { useEffect, useState } from 'react'; import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks'; @@ -11,11 +12,25 @@ import { Option } from '@polkadot/types'; interface Inactives { nomsActive?: string[]; + nomsChilled?: string[]; nomsInactive?: string[]; nomsWaiting?: string[]; } -function extractState (stashId: string, nominees: string[], activeEra: EraIndex, submittedIn: EraIndex, exposures: Exposure[]): Inactives { +function extractState (stashId: string, slashes: Slash[], nominees: string[], activeEra: EraIndex, submittedIn: EraIndex, exposures: Exposure[]): Inactives { + // chilled + const nomsChilled = nominees.filter((nominee) => + slashes.some(({ era, slashes }) => + submittedIn.lt(era) && ( + slashes + .filter(({ validator }) => validator.eq(nominee)) + .some(({ others }) => + others.some(([accountId]) => accountId.eq(stashId)) + ) + ) + ) + ); + // first a blanket find of nominations not in the active set let nomsInactive = exposures .map((exposure, index) => @@ -32,22 +47,24 @@ function extractState (stashId: string, nominees: string[], activeEra: EraIndex, ? nominees[index] : null ) - .filter((waitingId): waitingId is string => !!waitingId); + .filter((waitingId): waitingId is string => !!waitingId) + .filter((nominee) => !nomsChilled.includes(nominee)); // filter based on all inactives - const nomsActive = nominees.filter((nominee) => !nomsInactive.includes(nominee)); + const nomsActive = nominees.filter((nominee) => !nomsInactive.includes(nominee) && !nomsChilled.includes(nominee)); // inactive also contains waiting, remove those - nomsInactive = nomsInactive.filter((inactiveId) => !nomsWaiting.includes(inactiveId)); + nomsInactive = nomsInactive.filter((nominee) => !nomsWaiting.includes(nominee) && !nomsChilled.includes(nominee)); return { nomsActive, + nomsChilled, nomsInactive, nomsWaiting }; } -export default function useInactives (stashId: string, nominees?: string[]): Inactives { +export default function useInactives (stashId: string, slashes: Slash[], nominees?: string[]): Inactives { const { api } = useApi(); const mountedRef = useIsMountedRef(); const [state, setState] = useState({}); @@ -66,7 +83,7 @@ export default function useInactives (stashId: string, nominees?: string[]): Ina ), ([optNominators, ...exposures]: [Option, ...Exposure[]]): void => { mountedRef.current && setState( - extractState(stashId, nominees, indexes.activeEra, optNominators.unwrapOrDefault().submittedIn, exposures) + extractState(stashId, slashes, nominees, indexes.activeEra, optNominators.unwrapOrDefault().submittedIn, exposures) ); } ) @@ -78,7 +95,7 @@ export default function useInactives (stashId: string, nominees?: string[]): Ina return (): void => { unsub && unsub(); }; - }, [api, indexes, mountedRef, nominees, stashId]); + }, [api, indexes, mountedRef, nominees, slashes, stashId]); return state; } diff --git a/packages/page-staking/src/Targets/Validator.tsx b/packages/page-staking/src/Targets/Validator.tsx index f5a0219ced..4324d5b00a 100644 --- a/packages/page-staking/src/Targets/Validator.tsx +++ b/packages/page-staking/src/Targets/Validator.tsx @@ -3,8 +3,10 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { DeriveAccountInfo } from '@polkadot/api-derive/types'; +import { UnappliedSlash } from '@polkadot/types/interfaces'; import { ValidatorInfo } from '../types'; +import BN from 'bn.js'; import React, { useCallback, useEffect, useState } from 'react'; import { ApiPromise } from '@polkadot/api'; import { AddressSmall, Badge, Checkbox, Icon } from '@polkadot/react-components'; @@ -15,9 +17,11 @@ import keyring from '@polkadot/ui-keyring'; import MaxBadge from '../MaxBadge'; import Favorite from '../Overview/Address/Favorite'; +import { useTranslation } from '../translate'; import { checkVisibility } from '../util'; interface Props { + allSlashes?: [BN, UnappliedSlash[]][]; canSelect: boolean; filterName: string; info: ValidatorInfo; @@ -29,6 +33,11 @@ interface Props { withIdentity: boolean; } +interface Slash { + era: BN; + slashes: UnappliedSlash[]; +} + function checkIdentity (api: ApiPromise, accountInfo: DeriveAccountInfo): boolean { let hasIdentity = false; @@ -49,10 +58,12 @@ function checkIdentity (api: ApiPromise, accountInfo: DeriveAccountInfo): boolea return hasIdentity; } -function Validator ({ canSelect, filterName, info, isNominated, isSelected, toggleFavorite, toggleSelected, withElected, withIdentity }: Props): React.ReactElement | null { +function Validator ({ allSlashes, canSelect, filterName, info, isNominated, isSelected, toggleFavorite, toggleSelected, withElected, withIdentity }: Props): React.ReactElement | null { + const { t } = useTranslation(); const { api } = useApi(); const accountInfo = useCall(api.derive.accounts.info, [info.accountId]); const [isVisible, setVisibility] = useState(true); + const [slashes, setSlashes] = useState([]); useEffect((): void => { if (accountInfo) { @@ -61,6 +72,17 @@ function Validator ({ canSelect, filterName, info, isNominated, isSelected, togg } }, [accountInfo, api, filterName, info, withIdentity]); + useEffect((): void => { + allSlashes && setSlashes( + allSlashes + .map(([era, slashes]) => ({ + era, + slashes: slashes.filter(({ validator }) => validator.eq(info.accountId)) + })) + .filter(({ slashes }) => slashes.length) + ); + }, [allSlashes, info]); + const _onQueryStats = useCallback( (): void => { window.location.hash = `/staking/query/${info.key}`; @@ -106,6 +128,17 @@ function Validator ({ canSelect, filterName, info, isNominated, isSelected, togg : } + {slashes.length !== 0 && ( + ('Slashed in era {{eras}}', { + replace: { + eras: slashes.map(({ era }) => formatNumber(era)).join(', ') + } + })} + icon='skull-crossbones' + /> + )} {formatNumber(rankOverall)} diff --git a/packages/page-staking/src/Targets/index.tsx b/packages/page-staking/src/Targets/index.tsx index fafd472bdc..5e0f903d22 100644 --- a/packages/page-staking/src/Targets/index.tsx +++ b/packages/page-staking/src/Targets/index.tsx @@ -9,6 +9,7 @@ import { SortedTargets, TargetSortBy, ValidatorInfo } from '../types'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; import { Button, Icon, InputBalance, Table, Toggle } from '@polkadot/react-components'; +import { useAvailableSlashes } from '@polkadot/react-hooks'; import ElectionBanner from '../ElectionBanner'; import Filtering from '../Filtering'; @@ -50,6 +51,7 @@ function sort (sortBy: TargetSortBy, sortFromMax: boolean, validators: Validator function Targets ({ className = '', isInElection, ownStashes, targets: { calcWith, lastReward, nominators, setCalcWith, totalStaked, validators }, toggleFavorite }: Props): React.ReactElement { const { t } = useTranslation(); + const allSlashes = useAvailableSlashes(); const ownNominators = useOwnNominators(ownStashes); const [selected, setSelected] = useState([]); const [sorted, setSorted] = useState(); @@ -197,6 +199,7 @@ function Targets ({ className = '', isInElection, ownStashes, targets: { calcWit > {validators && sorted && (validators.length === sorted.length) && sorted.map((index): React.ReactNode =>