diff --git a/packages/app-staking/src/Account/Bond.tsx b/packages/app-staking/src/Account/Bond.tsx index 4b483513d3..a9ea0aca39 100644 --- a/packages/app-staking/src/Account/Bond.tsx +++ b/packages/app-staking/src/Account/Bond.tsx @@ -9,19 +9,19 @@ import React from 'react'; import { Button, InputAddress, InputBalance, Modal, TxButton, Dropdown } from '@polkadot/ui-app'; import translate from '../translate'; +import ValidateController from './ValidateController'; type Props = I18nProps & { accountId: string, - bondedId: string | null, + controllerId: string | null, isOpen: boolean, onClose: () => void }; type State = { bondValue?: BN, - controllerId: string | null, - destination: number, - isValidController: boolean + controllerId: string, + destination: number }; const stashOptions = [ @@ -31,16 +31,24 @@ const stashOptions = [ ]; class Bond extends React.PureComponent { - state: State = { - controllerId: null, - destination: 0, - isValidController: false - }; + state: State; + + constructor (props: Props) { + super(props); + + const { accountId, controllerId } = this.props; + + this.state = { + controllerId: controllerId || accountId, + destination: 0 + }; + } render () { const { accountId, isOpen, onClose, t } = this.props; - const { bondValue, controllerId, destination, isValidController } = this.state; - const canSubmit = isValidController && !!bondValue && bondValue.gtn(0) && controllerId; + const { bondValue, controllerId, destination } = this.state; + const hasValue = !!bondValue && bondValue.gtn(0); + const canSubmit = hasValue && !!controllerId; if (!isOpen) { return null; @@ -78,8 +86,9 @@ class Bond extends React.PureComponent { } private renderContent () { - const { accountId, bondedId, t } = this.props; - const { controllerId, destination, isValidController } = this.state; + const { accountId, t } = this.props; + const { controllerId, bondValue, destination } = this.state; + const hasValue = !!bondValue && bondValue.gtn(0); return ( <> @@ -95,23 +104,20 @@ class Bond extends React.PureComponent { /> - { - isValidController - ? null - : ( -
{t('Select a controller account which is not the same as you stash account. Controllers are responsible for making any actions to contol the bonded funds.')}
- ) - } + @@ -130,12 +136,7 @@ class Bond extends React.PureComponent { } private onChangeController = (controllerId: string) => { - const { accountId } = this.props; - - this.setState({ - controllerId, - isValidController: controllerId !== accountId - }); + this.setState({ controllerId }); } private onChangeDestination = (destination: number) => { diff --git a/packages/app-staking/src/Account/Nominating.tsx b/packages/app-staking/src/Account/Nominating.tsx index 0770726240..2a70f20a31 100644 --- a/packages/app-staking/src/Account/Nominating.tsx +++ b/packages/app-staking/src/Account/Nominating.tsx @@ -14,9 +14,8 @@ type Props = I18nProps & { accountId: string, isOpen: boolean, onClose: () => void, - intentions: Array, stashId: string, - targets: Array + stashOptions: Array }; type State = { @@ -76,7 +75,7 @@ class Nominating extends React.PureComponent { } renderContent () { - const { accountId, stashId, t, targets } = this.props; + const { accountId, stashId, stashOptions, t } = this.props; return ( <> @@ -102,7 +101,7 @@ class Nominating extends React.PureComponent { help={t('Stash accounts that are to be nominated. Block rewards are split between validators and nominators')} label={t('nominate the following addresses')} onChangeMulti={this.onChangeNominees} - options={targets} + options={stashOptions} placeholder={t('select accounts(s) nominate')} type='account' /> diff --git a/packages/app-staking/src/Account/ValidateController.tsx b/packages/app-staking/src/Account/ValidateController.tsx new file mode 100644 index 0000000000..31c375d90d --- /dev/null +++ b/packages/app-staking/src/Account/ValidateController.tsx @@ -0,0 +1,70 @@ +// Copyright 2017-2019 @polkadot/ui-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 { I18nProps } from '@polkadot/ui-app/types'; + +import React from 'react'; +import { Icon } from '@polkadot/ui-app';import { AccountId, Option, StakingLedger } from '@polkadot/types'; +import { withCalls } from '@polkadot/ui-api'; + +import translate from '../translate'; + +type Props = I18nProps & { + accountId: string, + controllerId: string, + staking_bonded?: Option, + staking_ledger?: Option +}; + +type State = { + bondedId: string | null, + stashId: string | null +}; + +class ValidateController extends React.PureComponent { + state: State = { + bondedId: null, + stashId: null + }; + + static getDerivedStateFromProps ({ staking_bonded, staking_ledger }: Props): State { + return { + bondedId: staking_bonded && staking_bonded.isSome + ? staking_bonded.unwrap().toString() + : null, + stashId: staking_ledger && staking_ledger.isSome + ? staking_ledger.unwrap().stash.toString() + : null + }; + } + + render () { + const { accountId, controllerId, t } = this.props; + const { bondedId, stashId } = this.state; + let error; + + if (controllerId === accountId) { + error = t('A controller account which is not the same as your selected account is recommended'); + } else if (bondedId) { + error = t('A controller account should not map to another stash. This selected controller is a stash, controlled by {{bondedId}}', { replace: { bondedId } }); + } else if (stashId) { + error = t('A controller account should not be set to manages multiple stashes. The selected controller is already controlling {{stashId}}', { replace: { stashId } }); + } else { + return null; + } + + return ( +
+
{error}
+
+ ); + } +} + +export default translate( + withCalls( + ['query.staking.bonded', { paramName: 'controllerId' }], + ['query.staking.ledger', { paramName: 'controllerId' }] + )(ValidateController) +); diff --git a/packages/app-staking/src/Account/index.tsx b/packages/app-staking/src/Account/index.tsx index 8d915ca415..903b15f30e 100644 --- a/packages/app-staking/src/Account/index.tsx +++ b/packages/app-staking/src/Account/index.tsx @@ -24,7 +24,7 @@ type Props = ApiProps & I18nProps & { accountId: string, balances: DerivedBalancesMap, balanceArray: (_address: AccountId | string) => Array | undefined, - intentions: Array, + controllers: Array, isValidator: boolean, name: string, nominators: Nominators, @@ -34,38 +34,39 @@ type Props = ApiProps & I18nProps & { staking_ledger?: Option, staking_stakers?: Exposure, staking_validators?: [ValidatorPrefs], - targets: Array, + stashes: Array, + stashOptions: Array, validators: Array }; type State = { + controllerId: string | null, isBondOpen: boolean, isBondExtraOpen: boolean, isNominateOpen: boolean, isSessionKeyOpen: boolean, isValidatingOpen: boolean, isUnbondOpen: boolean, - bondedId: string | null, sessionId: string | null, stashId: string | null }; class Account extends React.PureComponent { state: State = { + controllerId: null, isBondOpen: false, isBondExtraOpen: false, isSessionKeyOpen: false, isNominateOpen: false, isValidatingOpen: false, isUnbondOpen: false, - bondedId: null, sessionId: null, stashId: null }; static getDerivedStateFromProps ({ session_nextKeyFor, staking_bonded, staking_ledger }: Props, state: State): Partial { return { - bondedId: staking_bonded && staking_bonded.isSome + controllerId: staking_bonded && staking_bonded.isSome ? staking_bonded.unwrap().toString() : null, sessionId: session_nextKeyFor && session_nextKeyFor.isSome @@ -98,7 +99,7 @@ class Account extends React.PureComponent { >
{this.renderButtons()} - {this.renderBondedId()} + {this.renderControllerId()} {this.renderStashId()} {this.renderSessionId()} {this.renderNominee()} @@ -111,12 +112,12 @@ class Account extends React.PureComponent { private renderBond () { const { accountId } = this.props; - const { bondedId, isBondOpen } = this.state; + const { controllerId, isBondOpen } = this.state; return ( @@ -244,11 +245,11 @@ class Account extends React.PureComponent { // ); } - private renderBondedId () { + private renderControllerId () { const { recentlyOffline, t } = this.props; - const { bondedId } = this.state; + const { controllerId } = this.state; - if (!bondedId) { + if (!controllerId) { return null; } @@ -256,8 +257,8 @@ class Account extends React.PureComponent {
); @@ -301,7 +302,7 @@ class Account extends React.PureComponent { } private renderNominating () { - const { accountId, intentions, targets } = this.props; + const { accountId, stashOptions } = this.props; const { isNominateOpen, stashId } = this.state; if (!stashId) { @@ -313,20 +314,19 @@ class Account extends React.PureComponent { accountId={accountId} isOpen={isNominateOpen} onClose={this.toggleNominate} - intentions={intentions} stashId={stashId} - targets={targets} + stashOptions={stashOptions} /> ); } private renderButtons () { - const { accountId, intentions, t } = this.props; - const { sessionId, bondedId, stashId } = this.state; + const { accountId, controllers, t } = this.props; + const { sessionId, controllerId, stashId } = this.state; const buttons = []; if (!stashId) { - if (!bondedId) { + if (!controllerId) { buttons.push(