Check for funded controllers (#2987)

This commit is contained in:
Jaco Greeff 2020-06-19 13:28:20 +02:00 committed by GitHub
parent 7ae0c855ff
commit e6ca9829a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveBalancesAll } from '@polkadot/api-derive/types';
import { AccountId, StakingLedger } from '@polkadot/types/interfaces';
import React, { useEffect, useState } from 'react';
@ -26,7 +27,7 @@ interface ErrorState {
function ValidateController ({ accountId, controllerId, defaultController, onError }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const bondedId = useCall<string | null>(api.query.staking.bonded, [controllerId], {
const bondedId = useCall<string | null>(controllerId ? api.query.staking.bonded : null, [controllerId], {
transform: (value: Option<AccountId>): string | null =>
value.isSome
? value.unwrap().toString()
@ -38,6 +39,7 @@ function ValidateController ({ accountId, controllerId, defaultController, onErr
? value.unwrap().stash.toString()
: null
});
const allBalances = useCall<DeriveBalancesAll>(controllerId ? api.derive.balances.all : null, [controllerId]);
const [{ error, isFatal }, setError] = useState<ErrorState>({ error: null, isFatal: false });
useEffect((): void => {
@ -49,18 +51,21 @@ function ValidateController ({ accountId, controllerId, defaultController, onErr
if (bondedId) {
isFatal = true;
newError = t<string>('A controller account should not map to another stash. This selected controller is a stash, controlled by {{bondedId}}', { replace: { bondedId } });
newError = t('A controller account should not map to another stash. This selected controller is a stash, controlled by {{bondedId}}', { replace: { bondedId } });
} else if (stashId) {
isFatal = true;
newError = t<string>('A controller account should not be set to manages multiple stashes. The selected controller is already controlling {{stashId}}', { replace: { stashId } });
newError = t('A controller account should not be set to manage multiple stashes. The selected controller is already controlling {{stashId}}', { replace: { stashId } });
} else if (allBalances?.freeBalance.isZero()) {
isFatal = true;
newError = t('The controller does no have sufficient funds available to cover transaction fees. Ensure that a funded controller is used.');
} else if (controllerId === accountId) {
newError = t<string>('Distinct stash and controller accounts are recommended to ensure fund security. You will be allowed to make the transaction, but take care to not tie up all funds, only use a portion of the available funds during this period.');
newError = t('Distinct stash and controller accounts are recommended to ensure fund security. You will be allowed to make the transaction, but take care to not tie up all funds, only use a portion of the available funds during this period.');
}
onError(newError, isFatal);
setError((state) => state.error !== newError ? { error: newError, isFatal } : state);
}
}, [accountId, bondedId, controllerId, defaultController, onError, stashId, t]);
}, [accountId, allBalances, bondedId, controllerId, defaultController, onError, stashId, t]);
if (!error || !accountId) {
return null;