Additional set controller checks (#963)

* Cleanup naming (contreoller, stashes)

* Warning for controllers & stashes

* Linting

* Adjust popup to align with react-tooltip colors
This commit is contained in:
Jaco Greeff 2019-04-11 12:39:31 +02:00 committed by GitHub
parent c2ba1b9df8
commit e20933e68e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 162 additions and 91 deletions

View File

@ -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<Props, State> {
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<Props, State> {
}
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<Props, State> {
/>
<InputAddress
className='medium'
defaultValue={bondedId}
help={t('The controller is the account that will be used to control any nominating or validating actions')}
help={t('The controller is the account that will be used to control any nominating or validating actions. Should not match another stash or controller.')}
label={t('controller account')}
onChange={this.onChangeController}
value={controllerId}
/>
{
isValidController
? null
: (
<article className='error'>{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.')}</article>
)
}
<ValidateController
accountId={accountId}
controllerId={controllerId}
/>
<InputBalance
autoFocus
className='medium'
help={t('The total amount of the stash balance that will be at stake in any forthcoming rounds (should be less than the total amount available)')}
isError={!hasValue}
label={t('value bonded')}
onChange={this.onChangeValue}
/>
@ -130,12 +136,7 @@ class Bond extends React.PureComponent<Props, State> {
}
private onChangeController = (controllerId: string) => {
const { accountId } = this.props;
this.setState({
controllerId,
isValidController: controllerId !== accountId
});
this.setState({ controllerId });
}
private onChangeDestination = (destination: number) => {

View File

@ -14,9 +14,8 @@ type Props = I18nProps & {
accountId: string,
isOpen: boolean,
onClose: () => void,
intentions: Array<string>,
stashId: string,
targets: Array<KeyringSectionOption>
stashOptions: Array<KeyringSectionOption>
};
type State = {
@ -76,7 +75,7 @@ class Nominating extends React.PureComponent<Props, State> {
}
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<Props, State> {
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'
/>

View File

@ -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<AccountId>,
staking_ledger?: Option<StakingLedger>
};
type State = {
bondedId: string | null,
stashId: string | null
};
class ValidateController extends React.PureComponent<Props, State> {
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 (
<article className='warning'>
<div><Icon name='warning sign' />{error}</div>
</article>
);
}
}
export default translate(
withCalls<Props>(
['query.staking.bonded', { paramName: 'controllerId' }],
['query.staking.ledger', { paramName: 'controllerId' }]
)(ValidateController)
);

View File

@ -24,7 +24,7 @@ type Props = ApiProps & I18nProps & {
accountId: string,
balances: DerivedBalancesMap,
balanceArray: (_address: AccountId | string) => Array<Balance> | undefined,
intentions: Array<string>,
controllers: Array<string>,
isValidator: boolean,
name: string,
nominators: Nominators,
@ -34,38 +34,39 @@ type Props = ApiProps & I18nProps & {
staking_ledger?: Option<StakingLedger>,
staking_stakers?: Exposure,
staking_validators?: [ValidatorPrefs],
targets: Array<KeyringSectionOption>,
stashes: Array<string>,
stashOptions: Array<KeyringSectionOption>,
validators: Array<string>
};
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<Props, State> {
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<State> {
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<Props, State> {
>
<div className='staking--Account-expand'>
{this.renderButtons()}
{this.renderBondedId()}
{this.renderControllerId()}
{this.renderStashId()}
{this.renderSessionId()}
{this.renderNominee()}
@ -111,12 +112,12 @@ class Account extends React.PureComponent<Props, State> {
private renderBond () {
const { accountId } = this.props;
const { bondedId, isBondOpen } = this.state;
const { controllerId, isBondOpen } = this.state;
return (
<Bond
accountId={accountId}
bondedId={bondedId}
controllerId={controllerId}
isOpen={isBondOpen}
onClose={this.toggleBond}
/>
@ -244,11 +245,11 @@ class Account extends React.PureComponent<Props, State> {
// );
}
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<Props, State> {
<div className='staking--Account-detail'>
<label className='staking--label'>{t('controller')}</label>
<AddressMini
value={bondedId}
offlineStatus={recentlyOffline[bondedId]}
value={controllerId}
offlineStatus={recentlyOffline[controllerId]}
/>
</div>
);
@ -301,7 +302,7 @@ class Account extends React.PureComponent<Props, State> {
}
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<Props, State> {
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(
<Button
isPrimary
@ -348,7 +348,7 @@ class Account extends React.PureComponent<Props, State> {
} else {
const nominees = this.getNominees();
const isNominating = nominees && nominees.length;
const isValidating = intentions.indexOf(accountId) !== -1;
const isValidating = controllers.indexOf(accountId) !== -1;
if (isValidating || isNominating) {
buttons.push(

View File

@ -30,7 +30,7 @@ type Props = I18nProps & {
};
type State = {
bondedId: string,
controllerId: string,
stashActive: string | null,
stashTotal: string | null,
stashId: string | null,
@ -45,7 +45,7 @@ class Address extends React.PureComponent<Props, State> {
super(props);
this.state = {
bondedId: props.address,
controllerId: props.address,
sessionId: null,
stashActive: null,
stashTotal: null,
@ -59,8 +59,8 @@ class Address extends React.PureComponent<Props, State> {
? staking_ledger.unwrapOr(null)
: null;
return {
bondedId: !staking_bonded || staking_bonded.isNone
? prevState.bondedId
controllerId: !staking_bonded || staking_bonded.isNone
? prevState.controllerId
: staking_bonded.unwrap().toString(),
sessionId: !session_nextKeyFor || session_nextKeyFor.isNone
? prevState.sessionId
@ -79,11 +79,11 @@ class Address extends React.PureComponent<Props, State> {
render () {
const { address, lastAuthor, lastBlock } = this.props;
const { bondedId, stashActive, stashId } = this.state;
const isAuthor = [address, bondedId, stashId].includes(lastAuthor);
const { controllerId, stashActive, stashId } = this.state;
const isAuthor = [address, controllerId, stashId].includes(lastAuthor);
return (
<article key={stashId || bondedId}>
<article key={stashId || controllerId}>
<AddressRow
extraInfo={stashActive ? `bonded ${stashActive}` : undefined}
name={this.getDisplayName()}
@ -125,12 +125,12 @@ class Address extends React.PureComponent<Props, State> {
private renderKeys () {
const { t } = this.props;
const { bondedId, sessionId } = this.state;
const isSame = bondedId === sessionId;
const { controllerId, sessionId } = this.state;
const isSame = controllerId === sessionId;
return (
<div className='staking--accounts-info'>
{bondedId
{controllerId
? (
<div>
<label className='staking--label'>{
@ -138,7 +138,7 @@ class Address extends React.PureComponent<Props, State> {
? t('controller/session')
: t('controller')
}</label>
<AddressMini value={bondedId} />
<AddressMini value={controllerId} />
</div>
)
: null

View File

@ -15,7 +15,7 @@ import translate from '../translate';
type Props = I18nProps & {
balances: DerivedBalancesMap,
intentions: Array<string>,
controllers: Array<string>,
lastAuthor?: string,
lastBlock: string,
lastLengthChange?: BN,
@ -25,9 +25,9 @@ type Props = I18nProps & {
class Summary extends React.PureComponent<Props> {
render () {
const { className, intentions, lastAuthor, lastBlock, style, t, staking_validatorCount, validators } = this.props;
const waiting = intentions.length > validators.length
? (intentions.length - validators.length)
const { className, controllers, lastAuthor, lastBlock, style, t, staking_validatorCount, validators } = this.props;
const waiting = controllers.length > validators.length
? (controllers.length - validators.length)
: 0;
return (

View File

@ -23,9 +23,9 @@ const ZERO = new Balance(0);
class Overview extends React.PureComponent<Props> {
render () {
const { balances, balanceArray, chain_subscribeNewHead, intentions, nominators, recentlyOffline, validators } = this.props;
const intentionsSorted = this.sortByBalance(
intentions.filter((address) =>
const { balances, balanceArray, chain_subscribeNewHead, controllers, nominators, recentlyOffline, validators } = this.props;
const nextSorted = this.sortByBalance(
controllers.filter((address) =>
!validators.includes(address)
)
);
@ -43,7 +43,7 @@ class Overview extends React.PureComponent<Props> {
<div className='staking--Overview'>
<Summary
balances={balances}
intentions={intentions}
controllers={controllers}
lastBlock={lastBlock}
lastAuthor={lastAuthor}
validators={validators}
@ -54,7 +54,7 @@ class Overview extends React.PureComponent<Props> {
current={validatorsSorted}
lastBlock={lastBlock}
lastAuthor={lastAuthor}
next={intentionsSorted}
next={nextSorted}
nominators={nominators}
recentlyOffline={recentlyOffline}
/>

View File

@ -17,7 +17,7 @@ type Props = I18nProps & ComponentProps;
class StakeList extends React.PureComponent<Props> {
render () {
const { balances, balanceArray, intentions, nominators, recentlyOffline, validators } = this.props;
const { balances, balanceArray, controllers, nominators, recentlyOffline, stashes, validators } = this.props;
const accounts = keyring.getAccounts();
return (
@ -31,13 +31,14 @@ class StakeList extends React.PureComponent<Props> {
accountId={address}
balances={balances}
balanceArray={balanceArray}
intentions={intentions}
controllers={controllers}
isValidator={validators.includes(address)}
key={address}
name={name}
nominators={nominators}
recentlyOffline={recentlyOffline}
targets={this.getTargetOptions()}
stashes={stashes}
stashOptions={this.getStashOptions()}
validators={validators}
/>
);
@ -59,10 +60,10 @@ class StakeList extends React.PureComponent<Props> {
);
}
private getTargetOptions (): Array<KeyringSectionOption> {
const { targets } = this.props;
private getStashOptions (): Array<KeyringSectionOption> {
const { stashes } = this.props;
return targets.map((stashId) => {
return stashes.map((stashId) => {
const pair = keyring.getAccount(stashId).isValid()
? keyring.getAccount(stashId)
: keyring.getAddress(stashId);

View File

@ -33,11 +33,11 @@ type Props = AppProps & ApiProps & I18nProps & {
};
type State = {
intentions: Array<string>,
controllers: Array<string>,
nominators: Nominators,
recentlyOffline: RecentlyOfflineMap,
stashes: Array<string>,
tabs: Array<TabItem>,
targets: Array<string>,
validators: Array<string>
};
@ -50,9 +50,10 @@ class App extends React.PureComponent<Props, State> {
const { t } = props;
this.state = {
intentions: [],
controllers: [],
nominators: {},
recentlyOffline: {},
stashes: [],
tabs: [
{
name: 'overview',
@ -63,14 +64,13 @@ class App extends React.PureComponent<Props, State> {
text: t('Account Actions')
}
],
targets: [],
validators: []
};
}
static getDerivedStateFromProps ({ staking_controllers = [[], []], session_validators = [], staking_nominators = [[], []], staking_recentlyOffline = [] }: Props): State {
return {
intentions: staking_controllers[1].filter((optId) => optId.isSome).map((accountId) =>
controllers: staking_controllers[1].filter((optId) => optId.isSome).map((accountId) =>
accountId.unwrap().toString()
),
nominators: staking_nominators[0].reduce((result, accountId, index) => {
@ -80,7 +80,7 @@ class App extends React.PureComponent<Props, State> {
return result;
}, {} as Nominators),
targets: staking_controllers[0].map((accountId) => accountId.toString()),
stashes: staking_controllers[0].map((accountId) => accountId.toString()),
validators: session_validators.map((authorityId) =>
authorityId.toString()
),
@ -129,17 +129,17 @@ class App extends React.PureComponent<Props, State> {
private renderComponent (Component: React.ComponentType<ComponentProps>) {
return (): React.ReactNode => {
const { intentions, nominators, recentlyOffline, targets, validators } = this.state;
const { controllers, nominators, recentlyOffline, stashes, validators } = this.state;
const { balances = {} } = this.props;
return (
<Component
balances={balances}
balanceArray={this.balanceArray}
intentions={intentions}
controllers={controllers}
nominators={nominators}
recentlyOffline={recentlyOffline}
targets={targets}
stashes={stashes}
validators={validators}
/>
);

View File

@ -14,10 +14,10 @@ export type Nominators = {
export type ComponentProps = {
balances: DerivedBalancesMap,
balanceArray: (_address: AccountId | string) => Array<Balance> | undefined,
intentions: Array<string>,
controllers: Array<string>,
nominators: Nominators,
recentlyOffline: RecentlyOfflineMap,
targets: Array<string>,
stashes: Array<string>,
validators: Array<string>
};

View File

@ -54,9 +54,9 @@ const Wrapper = styled.div`
z-index: 1;
.help-hover {
background: #4e4e4e;
background: #222;
border-radius: 0.25rem;
color: #eee;
color: #fff;
display: none;
padding: 0.5rem 1rem;
position: absolute;