Add bond fund warning (#1832)
This commit is contained in:
parent
27d3fd8e31
commit
2970b00863
@ -18,6 +18,7 @@ import { bnMax } from '@polkadot/util';
|
||||
|
||||
import translate from '../../translate';
|
||||
import detectUnsafe from '../../unsafeChains';
|
||||
import ValidateAmount from './InputValidateAmount';
|
||||
|
||||
interface Props extends I18nProps, ApiProps, CalculateBalanceProps {
|
||||
controllerId: string;
|
||||
@ -27,6 +28,7 @@ interface Props extends I18nProps, ApiProps, CalculateBalanceProps {
|
||||
}
|
||||
|
||||
interface State {
|
||||
amountError: string | null;
|
||||
extrinsic: SubmittableExtrinsic | null;
|
||||
maxAdditional?: BN;
|
||||
maxBalance?: BN;
|
||||
@ -36,6 +38,7 @@ const ZERO = new BN(0);
|
||||
|
||||
class BondExtra extends TxComponent<Props, State> {
|
||||
public state: State = {
|
||||
amountError: null,
|
||||
extrinsic: null
|
||||
};
|
||||
|
||||
@ -53,9 +56,9 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
}
|
||||
|
||||
public render (): React.ReactNode {
|
||||
const { balances_all = ZERO_BALANCE, isOpen, onClose, stashId, t } = this.props;
|
||||
const { extrinsic, maxAdditional, maxBalance = balances_all.freeBalance } = this.state;
|
||||
const canSubmit = !!maxAdditional && maxAdditional.gtn(0) && maxAdditional.lte(maxBalance);
|
||||
const { isOpen, onClose, stashId, t } = this.props;
|
||||
const { extrinsic, maxAdditional } = this.state;
|
||||
const canSubmit = !!maxAdditional && maxAdditional.gtn(0);
|
||||
|
||||
if (!isOpen) {
|
||||
return null;
|
||||
@ -96,7 +99,7 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
|
||||
private renderContent (): React.ReactNode {
|
||||
const { stashId, systemChain, t } = this.props;
|
||||
const { maxBalance } = this.state;
|
||||
const { amountError, maxAdditional, maxBalance } = this.state;
|
||||
const transferrable = <span className='label'>{t('transferrable ')}</span>;
|
||||
const isUnsafeChain = detectUnsafe(systemChain);
|
||||
|
||||
@ -117,12 +120,18 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
autoFocus
|
||||
className='medium'
|
||||
help={t('Amount to add to the currently bonded funds. This is adjusted using the available funds on the account.')}
|
||||
isError={!!amountError || !maxAdditional || maxAdditional.eqn(0)}
|
||||
label={t('additional bonded funds')}
|
||||
maxValue={maxBalance}
|
||||
onChange={this.onChangeValue}
|
||||
onEnter={this.sendTx}
|
||||
withMax={!isUnsafeChain}
|
||||
/>
|
||||
<ValidateAmount
|
||||
accountId={stashId}
|
||||
onError={this.setAmountError}
|
||||
value={maxAdditional}
|
||||
/>
|
||||
</Modal.Content>
|
||||
</>
|
||||
);
|
||||
@ -131,12 +140,13 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
private nextState (newState: Partial<State>): void {
|
||||
this.setState((prevState: State): State => {
|
||||
const { api } = this.props;
|
||||
const { maxAdditional = prevState.maxAdditional, maxBalance = prevState.maxBalance } = newState;
|
||||
const { amountError = prevState.amountError, maxAdditional = prevState.maxAdditional, maxBalance = prevState.maxBalance } = newState;
|
||||
const extrinsic = (maxAdditional && maxAdditional.gte(ZERO))
|
||||
? api.tx.staking.bondExtra(maxAdditional)
|
||||
: null;
|
||||
|
||||
return {
|
||||
amountError,
|
||||
extrinsic,
|
||||
maxAdditional,
|
||||
maxBalance
|
||||
@ -177,6 +187,10 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
private onChangeValue = (maxAdditional?: BN): void => {
|
||||
this.nextState({ maxAdditional });
|
||||
}
|
||||
|
||||
private setAmountError = (amountError: string | null): void => {
|
||||
this.setState({ amountError });
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
@ -184,7 +198,6 @@ export default withMulti(
|
||||
translate,
|
||||
withApi,
|
||||
withCalls<Props>(
|
||||
'derive.balances.fees',
|
||||
['derive.balances.all', { paramName: 'stashId' }]
|
||||
'derive.balances.fees'
|
||||
)
|
||||
);
|
||||
|
||||
@ -0,0 +1,61 @@
|
||||
// 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 { DerivedBalances } from '@polkadot/api-derive/types';
|
||||
import { ApiProps } from '@polkadot/react-api/types';
|
||||
import { I18nProps } from '@polkadot/react-components/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Icon } from '@polkadot/react-components';
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
|
||||
import translate from '../../translate';
|
||||
|
||||
interface Props extends ApiProps, I18nProps {
|
||||
allBalances?: DerivedBalances;
|
||||
accountId: string | null;
|
||||
onError: (error: string | null) => void;
|
||||
value?: BN | null;
|
||||
}
|
||||
|
||||
function ValidateAmount ({ allBalances, onError, value, t }: Props): React.ReactElement<Props> | null {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect((): void => {
|
||||
// don't show an error if the selected controller is the default
|
||||
// this applies when changing controller
|
||||
if (allBalances && value) {
|
||||
let newError: string | null = null;
|
||||
|
||||
if (value.gt(allBalances.freeBalance)) {
|
||||
newError = t('The specified value is greater than your free balance. The node will bond the maximum amount available.');
|
||||
}
|
||||
|
||||
if (error !== newError) {
|
||||
onError(newError);
|
||||
setError(newError);
|
||||
}
|
||||
}
|
||||
}, [allBalances, value]);
|
||||
|
||||
if (!error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<article className='warning'>
|
||||
<div><Icon name='warning sign' />{error}</div>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
export default translate(
|
||||
withCalls<Props>(
|
||||
['derive.balances.all', {
|
||||
paramName: 'accountId',
|
||||
propName: 'allBalances'
|
||||
}]
|
||||
)(ValidateAmount)
|
||||
);
|
||||
@ -14,6 +14,7 @@ import { withApi, withMulti } from '@polkadot/react-api';
|
||||
|
||||
import translate from '../translate';
|
||||
import detectUnsafe from '../unsafeChains';
|
||||
import InputValidateAmount from './Account/InputValidateAmount';
|
||||
import InputValidationController from './Account/InputValidationController';
|
||||
import { rewardDestinationOptions } from './constants';
|
||||
|
||||
@ -22,6 +23,7 @@ interface Props extends ApiProps, I18nProps, CalculateBalanceProps {
|
||||
}
|
||||
|
||||
interface State {
|
||||
amountError: string | null;
|
||||
bondValue?: BN;
|
||||
controllerError: string | null;
|
||||
controllerId: string | null;
|
||||
@ -37,6 +39,7 @@ class NewStake extends TxComponent<Props, State> {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
amountError: null,
|
||||
controllerError: null,
|
||||
controllerId: null,
|
||||
destination: 0,
|
||||
@ -47,7 +50,7 @@ class NewStake extends TxComponent<Props, State> {
|
||||
|
||||
public render (): React.ReactNode {
|
||||
const { onClose, systemChain, t } = this.props;
|
||||
const { bondValue, controllerError, controllerId, destination, extrinsic, stashId } = this.state;
|
||||
const { amountError, bondValue, controllerError, controllerId, destination, extrinsic, stashId } = this.state;
|
||||
const hasValue = !!bondValue && bondValue.gtn(0);
|
||||
const isUnsafeChain = detectUnsafe(systemChain);
|
||||
const canSubmit = (hasValue && (isUnsafeChain || (!controllerError && !!controllerId)));
|
||||
@ -92,13 +95,18 @@ class NewStake extends TxComponent<Props, State> {
|
||||
destination={destination}
|
||||
extrinsicProp={'staking.bond'}
|
||||
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}
|
||||
isError={!hasValue || !!amountError}
|
||||
label={t('value bonded')}
|
||||
onChange={this.onChangeValue}
|
||||
onEnter={this.sendTx}
|
||||
stashId={stashId}
|
||||
withMax={!isUnsafeChain}
|
||||
/>
|
||||
<InputValidateAmount
|
||||
accountId={stashId}
|
||||
onError={this.onAmountError}
|
||||
value={bondValue}
|
||||
/>
|
||||
<Dropdown
|
||||
className='medium'
|
||||
defaultValue={0}
|
||||
@ -137,12 +145,13 @@ class NewStake extends TxComponent<Props, State> {
|
||||
private nextState (newState: Partial<State>): void {
|
||||
this.setState((prevState: State): State => {
|
||||
const { api } = this.props;
|
||||
const { bondValue = prevState.bondValue, controllerError = prevState.controllerError, controllerId = prevState.controllerId, destination = prevState.destination, stashId = prevState.stashId } = newState;
|
||||
const { amountError = prevState.amountError, bondValue = prevState.bondValue, controllerError = prevState.controllerError, controllerId = prevState.controllerId, destination = prevState.destination, stashId = prevState.stashId } = newState;
|
||||
const extrinsic = (bondValue && controllerId)
|
||||
? api.tx.staking.bond(controllerId, bondValue, destination)
|
||||
: null;
|
||||
|
||||
return {
|
||||
amountError,
|
||||
bondValue,
|
||||
controllerError,
|
||||
controllerId,
|
||||
@ -153,6 +162,10 @@ class NewStake extends TxComponent<Props, State> {
|
||||
});
|
||||
}
|
||||
|
||||
private onAmountError = (amountError: string | null): void => {
|
||||
this.nextState({ amountError });
|
||||
}
|
||||
|
||||
private onChangeController = (controllerId: string | null): void => {
|
||||
this.nextState({ controllerId });
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user