From 2bef228fd0d537802cff28efc8dc69664446a81c Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 15 Dec 2022 15:57:51 +0200 Subject: [PATCH] Chilled filtering adjustments (no-auto, recent Substrate) (#8551) --- .../page-staking/src/Actions/useInactives.ts | 28 +++++++++++-------- .../src/Validators/Address/NominatedBy.tsx | 16 +++++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/page-staking/src/Actions/useInactives.ts b/packages/page-staking/src/Actions/useInactives.ts index 11f146417e..72c89a1b91 100644 --- a/packages/page-staking/src/Actions/useInactives.ts +++ b/packages/page-staking/src/Actions/useInactives.ts @@ -24,21 +24,25 @@ function extractState (api: ApiPromise, stashId: string, slashes: Option { - if (slashes[index].isNone) { - return false; - } - - const { lastNonzeroSlash } = slashes[index].unwrap(); - - // to be chilled, we have a slash era and it is later than the submission era - // (if submitted in the same, the nomination will only take effect after the era) - return !lastNonzeroSlash.isZero() && lastNonzeroSlash.gt(submittedIn); - }); + // NOTE With the introduction of the SlashReported event, + // nominators are not auto-chilled on validator slash + const nomsChilled = !api.events.staking.SlashReported + ? nominees.filter((_, index) => + slashes[index].isNone + ? false + // to be chilled, we have a slash era and it is later than the submission era + // (if submitted in the same, the nomination will only take effect after the era) + : slashes[index].unwrap().lastNonzeroSlash.gt(submittedIn) + ) + : []; // all nominations that are oversubscribed const nomsOver = exposures - .map(({ others }) => others.sort((a, b) => (b.value?.unwrap() || BN_ZERO).cmp(a.value?.unwrap() || BN_ZERO))) + .map(({ others }) => + others.sort((a, b) => + (b.value?.unwrap() || BN_ZERO).cmp(a.value?.unwrap() || BN_ZERO) + ) + ) .map((others, index) => !max || max.gtn(others.map(({ who }) => who.toString()).indexOf(stashId)) ? null diff --git a/packages/page-staking/src/Validators/Address/NominatedBy.tsx b/packages/page-staking/src/Validators/Address/NominatedBy.tsx index 670c08af0e..fddc6ad919 100644 --- a/packages/page-staking/src/Validators/Address/NominatedBy.tsx +++ b/packages/page-staking/src/Validators/Address/NominatedBy.tsx @@ -1,12 +1,14 @@ // Copyright 2017-2022 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { ApiPromise } from '@polkadot/api'; import type { SlashingSpans } from '@polkadot/types/interfaces'; import type { NominatedBy as NominatedByType } from '../../types'; import React, { useMemo } from 'react'; import { AddressMini, ExpanderScroll } from '@polkadot/react-components'; +import { useApi } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../../translate'; @@ -35,12 +37,13 @@ function extractFunction (all: string[]): null | [number, () => React.ReactNode[ : null; } -function extractChilled (nominators: NominatedByType[] = [], slashingSpans?: SlashingSpans | null): Chilled { - const chilled = slashingSpans +function extractChilled (api: ApiPromise, nominators: NominatedByType[] = [], slashingSpans?: SlashingSpans | null): Chilled { + // NOTE With the introduction of the SlashReported event, + // nominators are not auto-chilled on validator slash + const chilled = slashingSpans && !api.events.staking.SlashReported ? nominators .filter(({ submittedIn }) => - !slashingSpans.lastNonzeroSlash.isZero() && - slashingSpans.lastNonzeroSlash.gte(submittedIn) + slashingSpans.lastNonzeroSlash.gt(submittedIn) ) .map(({ nominatorId }) => nominatorId) : []; @@ -57,10 +60,11 @@ function extractChilled (nominators: NominatedByType[] = [], slashingSpans?: Sla function NominatedBy ({ nominators, slashingSpans }: Props): React.ReactElement { const { t } = useTranslation(); + const { api } = useApi(); const { active, chilled } = useMemo( - () => extractChilled(nominators, slashingSpans), - [nominators, slashingSpans] + () => extractChilled(api, nominators, slashingSpans), + [api, nominators, slashingSpans] ); return (