Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't crash transfer model if balances is not available #6787

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should be enough, I'm guessing it isn't so will take a look at why not after this is in. (e.g. the re-checks for the functions inside the Transfer file shouldn't be needed, it really should not try to execute anything on it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah good point, I made those changes first but you're probably right that this would be enough.

<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