diff --git a/packages/app-staking/src/Actions/Account/BondExtra.tsx b/packages/app-staking/src/Actions/Account/BondExtra.tsx index 57c6a7905f..31e5e46228 100644 --- a/packages/app-staking/src/Actions/Account/BondExtra.tsx +++ b/packages/app-staking/src/Actions/Account/BondExtra.tsx @@ -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 { public state: State = { + amountError: null, extrinsic: null }; @@ -53,9 +56,9 @@ class BondExtra extends TxComponent { } 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 { private renderContent (): React.ReactNode { const { stashId, systemChain, t } = this.props; - const { maxBalance } = this.state; + const { amountError, maxAdditional, maxBalance } = this.state; const transferrable = {t('transferrable ')}; const isUnsafeChain = detectUnsafe(systemChain); @@ -117,12 +120,18 @@ class BondExtra extends TxComponent { 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} /> + ); @@ -131,12 +140,13 @@ class BondExtra extends TxComponent { private nextState (newState: Partial): 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 { 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( - 'derive.balances.fees', - ['derive.balances.all', { paramName: 'stashId' }] + 'derive.balances.fees' ) ); diff --git a/packages/app-staking/src/Actions/Account/InputValidateAmount.tsx b/packages/app-staking/src/Actions/Account/InputValidateAmount.tsx new file mode 100644 index 0000000000..595151cc47 --- /dev/null +++ b/packages/app-staking/src/Actions/Account/InputValidateAmount.tsx @@ -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 | null { + const [error, setError] = useState(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 ( +
+
{error}
+
+ ); +} + +export default translate( + withCalls( + ['derive.balances.all', { + paramName: 'accountId', + propName: 'allBalances' + }] + )(ValidateAmount) +); diff --git a/packages/app-staking/src/Actions/NewStake.tsx b/packages/app-staking/src/Actions/NewStake.tsx index bc3389c68f..b46a40fae7 100644 --- a/packages/app-staking/src/Actions/NewStake.tsx +++ b/packages/app-staking/src/Actions/NewStake.tsx @@ -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 { super(props); this.state = { + amountError: null, controllerError: null, controllerId: null, destination: 0, @@ -47,7 +50,7 @@ class NewStake extends TxComponent { 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 { 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} /> + { private nextState (newState: Partial): 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 { }); } + private onAmountError = (amountError: string | null): void => { + this.nextState({ amountError }); + } + private onChangeController = (controllerId: string | null): void => { this.nextState({ controllerId }); }