From f415732fe779d98cdb7f7a0c8ef256db03d53eb4 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 1 Nov 2019 15:19:44 +0100 Subject: [PATCH] Check freeBalance with bondExtra, not availableBalance (#1825) --- .../app-staking/src/Actions/Account/BondExtra.tsx | 13 ++++++------- packages/app-staking/src/Actions/Account/index.tsx | 2 +- packages/app-staking/src/types.ts | 3 --- .../react-components/src/InputBalanceBonded.tsx | 8 +++----- packages/react-signer/src/Checks/constants.ts | 2 +- packages/react-signer/src/Checks/index.tsx | 10 ++++------ 6 files changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/app-staking/src/Actions/Account/BondExtra.tsx b/packages/app-staking/src/Actions/Account/BondExtra.tsx index c885954259..0ef797b1f6 100644 --- a/packages/app-staking/src/Actions/Account/BondExtra.tsx +++ b/packages/app-staking/src/Actions/Account/BondExtra.tsx @@ -54,7 +54,7 @@ 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.availableBalance } = this.state; + const { extrinsic, maxAdditional, maxBalance = balances_all.freeBalance } = this.state; const canSubmit = !!maxAdditional && maxAdditional.gtn(0) && maxAdditional.lte(maxBalance); if (!isOpen) { @@ -145,11 +145,11 @@ class BondExtra extends TxComponent { } private setMaxBalance = (): void => { - const { api, system_accountNonce = ZERO, balances_fees = ZERO_FEES, balances_all = ZERO_BALANCE } = this.props; + const { api, balances_fees = ZERO_FEES, balances_all = ZERO_BALANCE } = this.props; const { maxAdditional } = this.state; const { transactionBaseFee, transactionByteFee } = balances_fees; - const { availableBalance } = balances_all; + const { accountNonce, freeBalance } = balances_all; let prevMax = new BN(0); let maxBalance = new BN(1); @@ -161,10 +161,10 @@ class BondExtra extends TxComponent { ? api.tx.staking.bondExtra(maxAdditional) : null; - const txLength = calcTxLength(extrinsic, system_accountNonce); + const txLength = calcTxLength(extrinsic, accountNonce); const fees = transactionBaseFee.add(transactionByteFee.mul(txLength)); - maxBalance = bnMax(availableBalance.sub(fees), ZERO); + maxBalance = bnMax(freeBalance.sub(fees), ZERO); } this.nextState({ @@ -185,7 +185,6 @@ export default withMulti( withApi, withCalls( 'derive.balances.fees', - ['derive.balances.all', { paramName: 'stashId' }], - ['query.system.accountNonce', { paramName: 'stashId' }] + ['derive.balances.all', { paramName: 'stashId' }] ) ); diff --git a/packages/app-staking/src/Actions/Account/index.tsx b/packages/app-staking/src/Actions/Account/index.tsx index 461b6ac37f..636d41a0f1 100644 --- a/packages/app-staking/src/Actions/Account/index.tsx +++ b/packages/app-staking/src/Actions/Account/index.tsx @@ -375,7 +375,7 @@ class Account extends React.PureComponent { // only show a "Bond Additional" button if this stash account actually doesn't bond everything already // staking_ledger.total gives the total amount that can be slashed (any active amount + what is being unlocked) - const canBondExtra = balances_all && balances_all.availableBalance.gtn(0); + const canBondExtra = balances_all && balances_all.freeBalance.gtn(0); return ( ; export interface ComponentProps { @@ -23,7 +21,6 @@ export interface ComponentProps { export interface CalculateBalanceProps { balances_fees?: DerivedFees; balances_all?: DerivedBalances; - system_accountNonce?: BN; } export type AccountFilter = 'all' | 'controller' | 'session' | 'stash' | 'unbonded'; diff --git a/packages/react-components/src/InputBalanceBonded.tsx b/packages/react-components/src/InputBalanceBonded.tsx index a06e33e6b5..97993c47c6 100644 --- a/packages/react-components/src/InputBalanceBonded.tsx +++ b/packages/react-components/src/InputBalanceBonded.tsx @@ -33,7 +33,6 @@ interface Props extends BareProps, ApiProps { onEnter?: () => void; placeholder?: string; stashId: string; - system_accountNonce?: BN; value?: BN | string; withEllipsis?: boolean; withLabel?: boolean; @@ -104,7 +103,7 @@ class InputBalanceBonded extends React.PureComponent { } private setMaxBalance = (): void => { - const { api, balances_fees = ZERO_FEES, balances_all = ZERO_BALANCE, controllerId, destination, extrinsicProp, system_accountNonce = ZERO } = this.props; + const { api, balances_fees = ZERO_FEES, balances_all = ZERO_BALANCE, controllerId, destination, extrinsicProp } = this.props; const { transactionBaseFee, transactionByteFee } = balances_fees; const { freeBalance } = balances_all; let prevMax = new BN(0); @@ -124,7 +123,7 @@ class InputBalanceBonded extends React.PureComponent { extrinsic = api.tx.staking.bonExtra(prevMax); } - const txLength = calcTxLength(extrinsic, system_accountNonce); + const txLength = calcTxLength(extrinsic, balances_all.accountNonce); const fees = transactionBaseFee.add(transactionByteFee.mul(txLength)); maxBalance = bnMax(freeBalance.sub(fees), ZERO); @@ -157,7 +156,6 @@ export default withMulti( withApi, withCalls( 'derive.balances.fees', - ['derive.balances.all', { paramName: 'stashId' }], - ['query.system.accountNonce', { paramName: 'stashId' }] + ['derive.balances.all', { paramName: 'stashId' }] ) ); diff --git a/packages/react-signer/src/Checks/constants.ts b/packages/react-signer/src/Checks/constants.ts index 52de4ba1a6..26a28e722a 100644 --- a/packages/react-signer/src/Checks/constants.ts +++ b/packages/react-signer/src/Checks/constants.ts @@ -11,8 +11,8 @@ const ZERO_BALANCE: DerivedBalances = { accountId: createType('AccountId'), accountNonce: createType('Index'), availableBalance: createType('Balance'), - lockedBalance: createType('Balance'), freeBalance: createType('Balance'), + lockedBalance: createType('Balance'), reservedBalance: createType('Balance'), vestedBalance: createType('Balance'), votingBalance: createType('Balance') diff --git a/packages/react-signer/src/Checks/index.tsx b/packages/react-signer/src/Checks/index.tsx index f0dc4ed95e..6471769409 100644 --- a/packages/react-signer/src/Checks/index.tsx +++ b/packages/react-signer/src/Checks/index.tsx @@ -42,7 +42,6 @@ interface Props extends I18nProps { extrinsic?: IExtrinsic | null; isSendable: boolean; onChange?: (hasAvailable: boolean) => void; - system_accountNonce?: BN; tip?: BN; } @@ -64,7 +63,7 @@ export const calcTxLength = (extrinsic?: IExtrinsic | null, nonce?: BN, tip?: BN ); }; -export function FeeDisplay ({ accountId, balances_all = ZERO_BALANCE, balances_fees = ZERO_FEES_BALANCES, className, contract_fees = ZERO_FEES_CONTRACT, extrinsic, isSendable, onChange, system_accountNonce = ZERO, t, tip }: Props): React.ReactElement | null { +export function FeeDisplay ({ accountId, balances_all = ZERO_BALANCE, balances_fees = ZERO_FEES_BALANCES, className, contract_fees = ZERO_FEES_CONTRACT, extrinsic, isSendable, onChange, t, tip }: Props): React.ReactElement | null { const { api } = useContext(ApiContext); const [state, setState] = useState({ allFees: ZERO, @@ -89,7 +88,7 @@ export function FeeDisplay ({ accountId, balances_all = ZERO_BALANCE, balances_f const fn = api.findCall(extrinsic.callIndex); const extMethod = fn.method; const extSection = fn.section; - const txLength = calcTxLength(extrinsic, system_accountNonce, tip); + const txLength = calcTxLength(extrinsic, balances_all.accountNonce, tip); const isSameExtrinsic = state.extMethod === extMethod && state.extSection === extSection; const extraAmount = isSameExtrinsic @@ -125,7 +124,7 @@ export function FeeDisplay ({ accountId, balances_all = ZERO_BALANCE, balances_f isReserved, overLimit }); - }, [accountId, balances_all, balances_fees, extra, extrinsic, system_accountNonce, tip]); + }, [accountId, balances_all, balances_fees, extra, extrinsic, balances_all.accountNonce, tip]); if (!accountId) { return null; @@ -249,7 +248,6 @@ export default translate( withCalls( 'derive.balances.fees', ['derive.balances.all', { paramName: 'accountId' }], - 'derive.contracts.fees', - ['query.system.accountNonce', { paramName: 'accountId' }] + 'derive.contracts.fees' )(FeeDisplay) );