Skip to content

Commit

Permalink
don't crash transfer model if balances is not available (#6787)
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill authored Jan 5, 2022
1 parent 95bfe56 commit 5a1d71a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 11 additions & 7 deletions packages/page-accounts/src/Sidebar/AccountMenuButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import styled from 'styled-components';
import Transfer from '@polkadot/app-accounts/modals/Transfer';
import { useTranslation } from '@polkadot/app-accounts/translate';
import { Button } from '@polkadot/react-components';
import { useToggle } from '@polkadot/react-hooks';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { AddressFlags } from '@polkadot/react-hooks/types';
import { isFunction } from '@polkadot/util';

interface Props {
className?: string;
Expand All @@ -28,6 +29,7 @@ interface Props {
function AccountMenuButtons ({ className = '', flags, isEditing, isEditingName, onCancel, onForgetAddress, onSaveName, onSaveTags, onUpdateName, recipientId, toggleIsEditingName, toggleIsEditingTags }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [isTransferOpen, toggleIsTransferOpen] = useToggle();
const api = useApi();

const _onForgetAddress = useCallback(
(): void => {
Expand Down Expand Up @@ -85,12 +87,14 @@ function AccountMenuButtons ({ className = '', flags, isEditing, isEditingName,
)
: (
<Button.Group>
<Button
icon='paper-plane'
isDisabled={isEditing}
label={t<string>('Send')}
onClick={toggleIsTransferOpen}
/>
{isFunction(api.api.tx.balances?.transfer) && (
<Button
icon='paper-plane'
isDisabled={isEditing}
label={t<string>('Send')}
onClick={toggleIsTransferOpen}
/>
)}
{!flags.isOwned && !flags.isInContacts && (
<Button
icon='plus'
Expand Down
22 changes: 11 additions & 11 deletions packages/page-accounts/src/modals/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
const fromId = propSenderId || senderId as string;
const toId = propRecipientId || recipientId as string;

if (balances && balances.accountId.eq(fromId) && fromId && toId && isFunction(api.rpc.payment?.queryInfo)) {
if (balances && balances.accountId?.eq(fromId) && fromId && toId && isFunction(api.rpc.payment?.queryInfo)) {
setTimeout((): void => {
try {
api.tx.balances
.transfer(toId, balances.availableBalance)
?.transfer(toId, balances.availableBalance)
.paymentInfo(fromId)
.then(({ partialFee }): void => {
const adjFee = partialFee.muln(110).div(BN_HUNDRED);
const maxTransfer = balances.availableBalance.sub(adjFee);

setMaxTransfer(
maxTransfer.gt(api.consts.balances.existentialDeposit)
maxTransfer.gt(api.consts.balances?.existentialDeposit)
? [maxTransfer, false]
: [null, true]
);
Expand All @@ -95,7 +95,7 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
? accountInfo.refcount.isZero()
: accountInfo.consumers.isZero()
: true;
const canToggleAll = !isProtected && balances && balances.accountId.eq(propSenderId || senderId) && maxTransfer && noReference;
const canToggleAll = !isProtected && balances && balances.accountId?.eq(propSenderId || senderId) && maxTransfer && noReference;

return (
<Modal
Expand Down Expand Up @@ -165,7 +165,7 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
onChange={setAmount}
/>
<InputBalance
defaultValue={api.consts.balances.existentialDeposit}
defaultValue={api.consts.balances?.existentialDeposit}
help={t<string>('The minimum amount that an account should have to be deemed active')}
isDisabled
label={t<string>('existential deposit')}
Expand All @@ -175,7 +175,7 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
}
</Modal.Columns>
<Modal.Columns hint={t('With the keep-alive option set, the account is protected against removal due to low balances.')}>
{isFunction(api.tx.balances.transferKeepAlive) && (
{isFunction(api.tx.balances?.transferKeepAlive) && (
<Toggle
className='typeToggle'
label={
Expand Down Expand Up @@ -213,17 +213,17 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
onStart={onClose}
params={
canToggleAll && isAll
? isFunction(api.tx.balances.transferAll)
? isFunction(api.tx.balances?.transferAll)
? [propRecipientId || recipientId, false]
: [propRecipientId || recipientId, maxTransfer]
: [propRecipientId || recipientId, amount]
}
tx={
canToggleAll && isAll && isFunction(api.tx.balances.transferAll)
? api.tx.balances.transferAll
canToggleAll && isAll && isFunction(api.tx.balances?.transferAll)
? api.tx.balances?.transferAll
: isProtected
? api.tx.balances.transferKeepAlive
: api.tx.balances.transfer
? api.tx.balances?.transferKeepAlive
: api.tx.balances?.transfer
}
/>
</Modal.Actions>
Expand Down

0 comments on commit 5a1d71a

Please sign in to comment.