From e27058540fe06ca4de5ed99d230268a7e5e4a6d2 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sun, 10 Apr 2022 07:52:10 +0200 Subject: [PATCH] Pass through minComm to validate (#7343) --- .../src/Actions/Account/Validate.tsx | 5 ++++- .../src/Actions/Account/index.tsx | 4 +++- .../page-staking/src/Actions/NewValidator.tsx | 5 ++++- packages/page-staking/src/Actions/index.tsx | 5 ++++- .../src/Actions/partials/Validate.tsx | 21 ++++++++++++------- packages/page-staking/src/index.tsx | 1 + 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/packages/page-staking/src/Actions/Account/Validate.tsx b/packages/page-staking/src/Actions/Account/Validate.tsx index 42ae5b2b24..4fc780e1bc 100644 --- a/packages/page-staking/src/Actions/Account/Validate.tsx +++ b/packages/page-staking/src/Actions/Account/Validate.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2022 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { BN } from '@polkadot/util'; import type { ValidateInfo } from '../partials/types'; import React, { useState } from 'react'; @@ -12,11 +13,12 @@ import ValidatePartial from '../partials/Validate'; interface Props { controllerId: string; + minCommission?: BN; onClose: () => void; stashId: string; } -function Validate ({ controllerId, onClose, stashId }: Props): React.ReactElement { +function Validate ({ controllerId, minCommission, onClose, stashId }: Props): React.ReactElement { const { t } = useTranslation(); const [{ validateTx }, setTx] = useState({}); @@ -29,6 +31,7 @@ function Validate ({ controllerId, onClose, stashId }: Props): React.ReactElemen void; stashId: string; @@ -70,7 +71,7 @@ function useStashCalls (api: ApiPromise, stashId: string) { return { balancesAll, spanCount, stakingAccount }; } -function Account ({ allSlashes, className = '', info: { controllerId, destination, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, isSelectable, onSelect, targets }: Props): React.ReactElement { +function Account ({ allSlashes, className = '', info: { controllerId, destination, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, isSelectable, minCommission, onSelect, targets }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { queueExtrinsic } = useContext(StatusContext); @@ -199,6 +200,7 @@ function Account ({ allSlashes, className = '', info: { controllerId, destinatio {isValidateOpen && controllerId && ( diff --git a/packages/page-staking/src/Actions/NewValidator.tsx b/packages/page-staking/src/Actions/NewValidator.tsx index ca0069b332..6bc42d1fd0 100644 --- a/packages/page-staking/src/Actions/NewValidator.tsx +++ b/packages/page-staking/src/Actions/NewValidator.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2022 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { BN } from '@polkadot/util'; import type { SortedTargets } from '../types'; import type { BondInfo, SessionInfo, ValidateInfo } from './partials/types'; @@ -17,12 +18,13 @@ import ValidatePartial from './partials/Validate'; interface Props { isInElection?: boolean; + minCommission?: BN; targets: SortedTargets; } const NUM_STEPS = 2; -function NewValidator ({ isInElection, targets }: Props): React.ReactElement { +function NewValidator ({ isInElection, minCommission, targets }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [isVisible, toggleVisible] = useToggle(); @@ -90,6 +92,7 @@ function NewValidator ({ isInElection, targets }: Props): React.ReactElement diff --git a/packages/page-staking/src/Actions/index.tsx b/packages/page-staking/src/Actions/index.tsx index c50cd27b54..25d2b85017 100644 --- a/packages/page-staking/src/Actions/index.tsx +++ b/packages/page-staking/src/Actions/index.tsx @@ -23,6 +23,7 @@ import NewValidator from './NewValidator'; interface Props { className?: string; isInElection?: boolean; + minCommission?: BN; ownStashes?: StakerState[]; next?: string[]; validators?: string[]; @@ -111,7 +112,7 @@ function formatTotal (typeIndex: number, state: State): React.ReactNode { return value && ; } -function Actions ({ className = '', isInElection, ownStashes, targets }: Props): React.ReactElement { +function Actions ({ className = '', isInElection, minCommission, ownStashes, targets }: Props): React.ReactElement { const { t } = useTranslation(); const allSlashes = useAvailableSlashes(); const [typeIndex, setTypeIndex] = useState(0); @@ -181,6 +182,7 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props): /> @@ -198,6 +200,7 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props): isDisabled={isInElection} isSelectable={isSelectable} key={info.stashId} + minCommission={minCommission} onSelect={onSelectStash} targets={targets} /> diff --git a/packages/page-staking/src/Actions/partials/Validate.tsx b/packages/page-staking/src/Actions/partials/Validate.tsx index a7d1fd5a83..b139dfc7c1 100644 --- a/packages/page-staking/src/Actions/partials/Validate.tsx +++ b/packages/page-staking/src/Actions/partials/Validate.tsx @@ -3,17 +3,18 @@ import type { ValidateInfo } from './types'; -import React, { useCallback, useEffect, useRef, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Dropdown, InputAddress, InputNumber, MarkError, Modal } from '@polkadot/react-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; -import { BN, BN_HUNDRED as MAX_COMM, BN_ONE, isFunction } from '@polkadot/util'; +import { useApi } from '@polkadot/react-hooks'; +import { BN, BN_HUNDRED as MAX_COMM, BN_ONE, bnMax, isFunction } from '@polkadot/util'; import { useTranslation } from '../../translate'; interface Props { className?: string; controllerId: string; + minCommission?: BN; onChange: (info: ValidateInfo) => void; stashId: string; withFocus?: boolean; @@ -22,12 +23,17 @@ interface Props { const COMM_MUL = new BN(1e7); -function Validate ({ className = '', controllerId, onChange, stashId, withFocus, withSenders }: Props): React.ReactElement { +function Validate ({ className = '', controllerId, minCommission, onChange, stashId, withFocus, withSenders }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const minComm = useCall(api.query.staking.minCommission); const [commission, setCommission] = useState(BN_ONE); const [allowNoms, setAllowNoms] = useState(true); + const defaultComm = useMemo( + () => minCommission + ? bnMax(minCommission.div(COMM_MUL), BN_ONE) + : BN_ONE, + [minCommission] + ); const blockedOptions = useRef([ { text: t('Yes, allow nominations'), value: true }, @@ -56,7 +62,7 @@ function Validate ({ className = '', controllerId, onChange, stashId, withFocus, [] ); - const commErr = !!minComm && commission.lt(minComm); + const commErr = !!minCommission && commission.lt(minCommission); return (
@@ -77,6 +83,7 @@ function Validate ({ className = '', controllerId, onChange, stashId, withFocus, ('The commission is deducted from all rewards before the remainder is split with nominators.')}> ('The percentage reward (0-100) that should be applied for the validator')} isError={commErr} isZeroable @@ -85,7 +92,7 @@ function Validate ({ className = '', controllerId, onChange, stashId, withFocus, onChange={_setCommission} /> {commErr && ( - ('The commission is below the on-chain minimum of {{p}}%', { replace: { p: (minComm.mul(MAX_COMM).div(COMM_MUL).toNumber() / 100).toFixed(2) } })} /> + ('The commission is below the on-chain minimum of {{p}}%', { replace: { p: (minCommission.mul(MAX_COMM).div(COMM_MUL).toNumber() / 100).toFixed(2) } })} /> )} {isFunction(api.tx.staking.kick) && ( diff --git a/packages/page-staking/src/index.tsx b/packages/page-staking/src/index.tsx index b60f1ad980..80ae353ecd 100644 --- a/packages/page-staking/src/index.tsx +++ b/packages/page-staking/src/index.tsx @@ -190,6 +190,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement