From 401c14702046b0c6a82cd1c718db0c806f644339 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 15 May 2020 17:05:27 +0200 Subject: [PATCH] New stake steps (#2754) * New stake steps * Controller jiggle * 2 step validation * Fatal errors on validation * Cleanups * Fix auto-select before rewards --- .../Account/InputValidationController.tsx | 18 +- .../src/Actions/Account/Nominate.tsx | 221 ++-------------- .../src/Actions/Account/SetSessionKey.tsx | 56 +--- .../src/Actions/Account/Validate.tsx | 61 +---- .../src/Actions/Account/index.tsx | 1 - .../page-staking/src/Actions/NewNominator.tsx | 129 +++++++++ .../page-staking/src/Actions/NewStake.tsx | 159 ------------ .../page-staking/src/Actions/NewStash.tsx | 60 +++++ .../page-staking/src/Actions/NewValidator.tsx | 130 ++++++++++ packages/page-staking/src/Actions/index.tsx | 17 +- .../src/Actions/partials/Bond.tsx | 142 ++++++++++ .../src/Actions/partials/Nominate.tsx | 245 ++++++++++++++++++ .../src/Actions/partials/SessionKey.tsx | 79 ++++++ .../src/Actions/partials/Validate.tsx | 76 ++++++ .../src/Actions/partials/types.ts | 25 ++ 15 files changed, 959 insertions(+), 460 deletions(-) create mode 100644 packages/page-staking/src/Actions/NewNominator.tsx delete mode 100644 packages/page-staking/src/Actions/NewStake.tsx create mode 100644 packages/page-staking/src/Actions/NewStash.tsx create mode 100644 packages/page-staking/src/Actions/NewValidator.tsx create mode 100644 packages/page-staking/src/Actions/partials/Bond.tsx create mode 100644 packages/page-staking/src/Actions/partials/Nominate.tsx create mode 100644 packages/page-staking/src/Actions/partials/SessionKey.tsx create mode 100644 packages/page-staking/src/Actions/partials/Validate.tsx create mode 100644 packages/page-staking/src/Actions/partials/types.ts diff --git a/packages/page-staking/src/Actions/Account/InputValidationController.tsx b/packages/page-staking/src/Actions/Account/InputValidationController.tsx index 34b6ae3fc3..3bc8cef8dc 100644 --- a/packages/page-staking/src/Actions/Account/InputValidationController.tsx +++ b/packages/page-staking/src/Actions/Account/InputValidationController.tsx @@ -15,7 +15,7 @@ interface Props { accountId: string | null; controllerId: string | null; defaultController?: string; - onError: (error: string | null) => void; + onError: (error: string | null, isFatal: boolean) => void; } function ValidateController ({ accountId, controllerId, defaultController, onError }: Props): React.ReactElement | null { @@ -33,24 +33,26 @@ function ValidateController ({ accountId, controllerId, defaultController, onErr ? value.unwrap().stash.toString() : null }); - const [error, setError] = useState(null); + const [{ error, isFatal }, setError] = useState<{ error: string | null; isFatal: boolean }>({ error: null, isFatal: false }); useEffect((): void => { // don't show an error if the selected controller is the default // this applies when changing controller if (defaultController !== controllerId) { let newError: string | null = null; + let isFatal = true; - if (controllerId === accountId) { - newError = t('Distinct stash and controller accounts are recommended to ensure fund security. You will be allowed to make the transaction, but take care to not tie up all funds, only use a portion of the available funds during this period.'); - } else if (bondedId) { + if (bondedId) { newError = t('A controller account should not map to another stash. This selected controller is a stash, controlled by {{bondedId}}', { replace: { bondedId } }); } else if (stashId) { newError = t('A controller account should not be set to manages multiple stashes. The selected controller is already controlling {{stashId}}', { replace: { stashId } }); + } else if (controllerId === accountId) { + newError = t('Distinct stash and controller accounts are recommended to ensure fund security. You will be allowed to make the transaction, but take care to not tie up all funds, only use a portion of the available funds during this period.'); + isFatal = false; } - onError(newError); - setError((error) => error !== newError ? newError : error); + onError(newError, isFatal); + setError((state) => state.error !== newError ? { error: newError, isFatal } : state); } }, [accountId, bondedId, controllerId, defaultController, onError, stashId, t]); @@ -59,7 +61,7 @@ function ValidateController ({ accountId, controllerId, defaultController, onErr } return ( -
+
{error}
); diff --git a/packages/page-staking/src/Actions/Account/Nominate.tsx b/packages/page-staking/src/Actions/Account/Nominate.tsx index f5382f5975..4397959608 100644 --- a/packages/page-staking/src/Actions/Account/Nominate.tsx +++ b/packages/page-staking/src/Actions/Account/Nominate.tsx @@ -2,238 +2,59 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { SubmittableExtrinsic } from '@polkadot/api/types'; import { SortedTargets } from '../../types'; -import React, { useCallback, useState } from 'react'; -import styled from 'styled-components'; -import { AddressMini, InputAddressMulti, InputAddress, Modal, Static, Toggle, TxButton } from '@polkadot/react-components'; -import { useFavorites } from '@polkadot/react-hooks'; +import React, { useState } from 'react'; +import { Modal, TxButton } from '@polkadot/react-components'; -import { MAX_NOMINATIONS, MAX_PAYOUTS, STORE_FAVS_BASE } from '../../constants'; import { useTranslation } from '../../translate'; +import NominatePartial from '../partials/Nominate'; interface Props { - className?: string; controllerId: string; next?: string[]; nominating?: string[]; onClose: () => void; stashId: string; targets: SortedTargets; - validators: string[]; + validators?: string[]; } -interface Selected { - isAutoSelect: boolean; - selected: string[]; -} - -function autoPick (targets: SortedTargets): string[] { - return (targets.validators || []).reduce((result: string[], { key, numNominators }): string[] => { - if (result.length < MAX_NOMINATIONS) { - if (numNominators && (numNominators < MAX_PAYOUTS)) { - result.push(key); - } - } - - return result; - }, []); -} - -function initialPick (targets: SortedTargets): Selected { - const selected = targets.validators?.length - ? autoPick(targets) - : []; - - return { - isAutoSelect: selected.length !== 0, - selected - }; -} - -function Nominate ({ className, controllerId, next, nominating, onClose, stashId, targets, validators }: Props): React.ReactElement | null { +function Nominate ({ controllerId, next, nominating, onClose, stashId, targets, validators }: Props): React.ReactElement | null { const { t } = useTranslation(); - const [favorites] = useFavorites(STORE_FAVS_BASE); - const [{ isAutoSelect, selected }, setSelected] = useState(initialPick(targets)); - const [available] = useState((): string[] => { - const shortlist = [ - // ensure that the favorite is included in the list of stashes - ...favorites.filter((acc) => (validators || []).includes(acc) || (next || []).includes(acc)), - // make sure the nominee is not in our favorites already - ...(nominating || []).filter((acc) => !favorites.includes(acc)) - ]; - - return shortlist - .concat(...(validators || []).filter((acc) => !shortlist.includes(acc))) - .concat(...(next || []).filter((acc) => !shortlist.includes(acc))); - }); - - const _setSelected = useCallback( - (selected: string[]) => setSelected(({ isAutoSelect }: Selected) => ({ - isAutoSelect, - selected - })), - [] - ); - - const _toggleAutoSelect = useCallback( - (isAutoSelect: boolean) => setSelected(() => ({ - isAutoSelect, - selected: isAutoSelect - ? autoPick(targets) - : [] - })), - [targets] - ); + const [tx, setTx] = useState | null>(null); return ( - - - - - - - -

{t('The stash that is to be affected. The transaction will be sent from the associated controller account.')}

-
-
- - - {isAutoSelect - ? ( - <> - ( - - )) - } - /> -
{t('The auto-selection is done on the current profitability of the validators taking your favorites into account. It is adjusted based on the commission and current range of backing for the validator. The calculation may and will change over time, so it is rather a selection based on the current state of the network, not a predictor of future profitability.')}
- - ) - : ( - - ) - } - -
- -

{t('Nominators can be selected automatically based on the current on-chain conditions or supplied manually as selected from the list of all currently available validators. In both cases, your favorites appear for the selection.')}

-

{t('Once transmitted the new selection will only take effect in 2 eras since the selection criteria for the next era was done at the end of the previous era. Until then, the nominations will show as inactive.')}

-
-
+ +
); } -export default React.memo(styled(Nominate)` - article.warning { - margin-top: 0; - } - - .auto--toggle { - margin: 0.5rem 0 0; - text-align: right; - width: 100%; - } - - .ui--Static .ui--AddressMini.padded { - padding-top: 0.5rem; - } - - .shortlist { - display: flex; - flex-wrap: wrap; - justify-content: center; - - .candidate { - border: 1px solid #eee; - border-radius: 0.25rem; - margin: 0.25rem; - padding-bottom: 0.25rem; - padding-right: 0.5rem; - position: relative; - - &::after { - content: ''; - position: absolute; - top: 0; - right: 0; - border-color: transparent; - border-style: solid; - border-radius: 0.25em; - border-width: 0.25em; - } - - &.isAye { - background: #fff; - border-color: #ccc; - } - - &.member::after { - border-color: green; - } - - &.runnerup::after { - border-color: steelblue; - } - - .ui--AddressMini-icon { - z-index: 1; - } - - .candidate-right { - text-align: right; - } - } - } -`); +export default React.memo(Nominate); diff --git a/packages/page-staking/src/Actions/Account/SetSessionKey.tsx b/packages/page-staking/src/Actions/Account/SetSessionKey.tsx index 520ed732dc..0c752d9528 100644 --- a/packages/page-staking/src/Actions/Account/SetSessionKey.tsx +++ b/packages/page-staking/src/Actions/Account/SetSessionKey.tsx @@ -2,10 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { SessionInfo } from '../partials/types'; + import React, { useState } from 'react'; -import { Input, InputAddress, Modal, TxButton } from '@polkadot/react-components'; +import { Modal, TxButton } from '@polkadot/react-components'; import { useTranslation } from '../../translate'; +import SessionKeyPartital from '../partials/SessionKey'; interface Props { controllerId: string; @@ -13,63 +16,32 @@ interface Props { stashId: string; } -const EMPTY_PROOF = new Uint8Array(); - function SetSessionKey ({ controllerId, onClose, stashId }: Props): React.ReactElement | null { const { t } = useTranslation(); - const [keys, setKeys] = useState(null); + const [{ sessionTx }, setTx] = useState({}); return ( - - - - - - - -

{t('The stash and controller pair. This transaction, setting the session keys, will be sent from the controller.')}

-
-
- - - - - -

{t('The hex output from author_rotateKeys, as executed on the validator node. The keys will show as pending until applied at the start of a new session.')}

-
-
+ +
diff --git a/packages/page-staking/src/Actions/Account/Validate.tsx b/packages/page-staking/src/Actions/Account/Validate.tsx index e564c51f0c..d793ab8629 100644 --- a/packages/page-staking/src/Actions/Account/Validate.tsx +++ b/packages/page-staking/src/Actions/Account/Validate.tsx @@ -2,11 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import BN from 'bn.js'; -import React, { useCallback, useState } from 'react'; -import { InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components'; +import { ValidateInfo } from '../partials/types'; + +import React, { useState } from 'react'; +import { Modal, TxButton } from '@polkadot/react-components'; import { useTranslation } from '../../translate'; +import ValidatePartial from '../partials/Validate'; interface Props { controllerId: string; @@ -14,67 +16,32 @@ interface Props { stashId: string; } -const COMM_MUL = new BN(10000000); -const MAX_COMM = new BN(100); -const ZERO = new BN(0); - function Validate ({ controllerId, onClose, stashId }: Props): React.ReactElement { const { t } = useTranslation(); - const [prefs, setPrefs] = useState({ commission: ZERO }); - - const _setCommission = useCallback( - (commission?: BN) => setPrefs({ commission: (commission || ZERO).mul(COMM_MUL) }), - [] - ); + const [{ validateTx }, setTx] = useState({}); return ( - - - - - - -

{t('The stash and controller pair. This transaction, managing preferences, will be sent from the controller.')}

-
-
- - - - - -

{t('The commission is deducted from all rewards before the remainder is split with nominators.')}

-
-
+
diff --git a/packages/page-staking/src/Actions/Account/index.tsx b/packages/page-staking/src/Actions/Account/index.tsx index 37a01279bc..21e9448cac 100644 --- a/packages/page-staking/src/Actions/Account/index.tsx +++ b/packages/page-staking/src/Actions/Account/index.tsx @@ -65,7 +65,6 @@ function Account ({ className, info: { controllerId, destination, destinationId, {isNominateOpen && controllerId && ( { + const { t } = useTranslation(); + const [isVisible, toggleVisible] = useToggle(); + const [{ bondOwnTx, bondTx, controllerId, controllerTx, stashId }, setBondInfo] = useState({}); + const [{ nominateTx }, setNominateInfo] = useState({}); + const [step, setStep] = useState(1); + + const _nextStep = useCallback( + () => setStep((step) => step + 1), + [] + ); + + const _prevStep = useCallback( + () => setStep((step) => step - 1), + [] + ); + + const _toggle = useCallback( + (): void => { + setBondInfo({}); + setNominateInfo({}); + setStep(1); + toggleVisible(); + }, + [toggleVisible] + ); + + return ( + <> +