Add check for reward destination existence (#5180)

* Add check for reward destination existence

* Align checks
This commit is contained in:
Jaco 2021-05-01 07:46:52 +03:00 committed by GitHub
parent 3a36f54342
commit 3f34af239b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -1,13 +1,14 @@
// Copyright 2017-2021 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveBalancesAll } from '@polkadot/api-derive/types';
import type { RewardDestination } from '@polkadot/types/interfaces';
import type { DestinationType } from '../types';
import React, { useMemo, useState } from 'react';
import { Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { Dropdown, InputAddress, MarkError, Modal, TxButton } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { useTranslation } from '../../translate';
import { createDestCurr } from '../destOptions';
@ -24,6 +25,7 @@ function SetRewardDestination ({ controllerId, defaultDestination, onClose, stas
const { api } = useApi();
const [destination, setDestination] = useState<DestinationType>(() => ((defaultDestination?.isAccount ? 'Account' : defaultDestination?.toString()) || 'Staked') as 'Staked');
const [destAccount, setDestAccount] = useState<string | null>(() => defaultDestination?.isAccount ? defaultDestination.asAccount.toString() : null);
const destBalance = useCall<DeriveBalancesAll>(api.derive.balances.all, [destAccount]);
const options = useMemo(
() => createDestCurr(t),
@ -31,6 +33,7 @@ function SetRewardDestination ({ controllerId, defaultDestination, onClose, stas
);
const isAccount = destination === 'Account';
const isDestError = isAccount && destBalance && destBalance.accountId.eq(destAccount) && destBalance.freeBalance.isZero();
return (
<Modal
@ -69,13 +72,16 @@ function SetRewardDestination ({ controllerId, defaultDestination, onClose, stas
value={destAccount}
/>
)}
{isDestError && (
<MarkError content={t<string>('The selected destination account does not exist and cannot be used to receive rewards')} />
)}
</Modal.Columns>
</Modal.Content>
<Modal.Actions onCancel={onClose}>
<TxButton
accountId={controllerId}
icon='sign-in-alt'
isDisabled={!controllerId || (isAccount && !destAccount)}
isDisabled={!controllerId || (isAccount && (!destAccount || isDestError))}
label={t<string>('Set reward destination')}
onStart={onClose}
params={[

View File

@ -8,7 +8,7 @@ import type { BondInfo } from './types';
import BN from 'bn.js';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Dropdown, InputAddress, InputBalance, Modal, Static } from '@polkadot/react-components';
import { Dropdown, InputAddress, InputBalance, MarkError, Modal, Static } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { BalanceFree, BlockToTime } from '@polkadot/react-query';
import { BN_ZERO } from '@polkadot/util';
@ -46,6 +46,7 @@ function Bond ({ className = '', isNominating, minNomination, onChange }: Props)
const [stashId, setStashId] = useState<string | null>(null);
const [startBalance, setStartBalance] = useState<BN | null>(null);
const stashBalance = useCall<DeriveBalancesAll>(api.derive.balances.all, [stashId]);
const destBalance = useCall<DeriveBalancesAll>(api.derive.balances.all, [destAccount]);
const bondedBlocks = useUnbondDuration();
const options = useMemo(
@ -90,6 +91,7 @@ function Bond ({ className = '', isNominating, minNomination, onChange }: Props)
const hasValue = !!amount?.gtn(0);
const isAccount = destination === 'Account';
const isDestError = isAccount && destBalance && destBalance.accountId.eq(destAccount) && destBalance.freeBalance.isZero();
return (
<div className={className}>
@ -175,6 +177,9 @@ function Bond ({ className = '', isNominating, minNomination, onChange }: Props)
value={destAccount}
/>
)}
{isDestError && (
<MarkError content={t<string>('The selected destination account does not exist and cannot be used to receive rewards')} />
)}
</Modal.Columns>
</div>
);