diff --git a/packages/page-accounts/src/modals/Transfer.tsx b/packages/page-accounts/src/modals/Transfer.tsx index 4ba2152ac7..7b2375bb6e 100644 --- a/packages/page-accounts/src/modals/Transfer.tsx +++ b/packages/page-accounts/src/modals/Transfer.tsx @@ -2,13 +2,15 @@ // 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 BN from 'bn.js'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import { InputAddress, InputBalance, Modal, Toggle, TxButton } from '@polkadot/react-components'; -import { useApi } from '@polkadot/react-hooks'; +import { useApi, useCall } from '@polkadot/react-hooks'; import { Available } from '@polkadot/react-query'; -import { BN_ZERO } from '@polkadot/util'; +import { BN_ZERO, isFunction } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -25,11 +27,40 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send const [amount, setAmount] = useState(BN_ZERO); const [hasAvailable] = useState(true); const [isProtected, setIsProtected] = useState(true); - const [maxBalance] = useState(BN_ZERO); + const [isAll, setIsAll] = useState(false); + const [maxTransfer, setMaxTransfer] = useState(null); const [recipientId, setRecipientId] = useState(propRecipientId || null); const [senderId, setSenderId] = useState(propSenderId || null); + const balances = useCall(api.derive.balances.all, [senderId]); + + useEffect((): void => { + if (balances && balances.accountId.eq(senderId) && recipientId && senderId && isFunction(api.rpc.payment?.queryInfo)) { + setTimeout((): void => { + try { + api.tx.balances + .transfer(recipientId, balances.availableBalance) + .paymentInfo(senderId) + .then(({ partialFee }): void => { + const maxTransfer = balances.availableBalance.sub(partialFee); + + setMaxTransfer( + maxTransfer.gt(api.consts.balances.existentialDeposit) + ? maxTransfer + : null + ); + }) + .catch(console.error); + } catch (error) { + console.error((error as Error).message); + } + }, 0); + } else { + setMaxTransfer(null); + } + }, [api, balances, recipientId, senderId]); const transferrable = {t('transferrable')}; + const canToggleAll = !isProtected && balances && balances.accountId.eq(senderId) && maxTransfer; return ( - ('Type the amount you want to transfer. Note that you can select the unit on the right e.g sending 1 milli is equivalent to sending 0.001.')} - isError={!hasAvailable} - isZeroable - label={t('amount')} - maxValue={maxBalance} - onChange={setAmount} - withMax - /> - ('The minimum amount that an account should have to be deemed active')} - isDisabled - label={t('existential deposit')} - /> + {canToggleAll && isAll + ? ( + ('The full account balance to be transferred, minus the transaction fees')} + isDisabled + key={maxTransfer?.toString()} + label={t('transferrable minus fees')} + /> + ) + : ( + <> + ('Type the amount you want to transfer. Note that you can select the unit on the right e.g sending 1 milli is equivalent to sending 0.001.')} + isError={!hasAvailable} + isZeroable + label={t('amount')} + onChange={setAmount} + /> + ('The minimum amount that an account should have to be deemed active')} + isDisabled + label={t('existential deposit')} + /> + + ) + } {api.tx.balances.transferKeepAlive && ( ('Transfer with account keep-alive checks') @@ -111,6 +156,14 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send value={isProtected} /> )} + {canToggleAll && ( + ('Transfer the full account balance, reap the sender')} + onChange={setIsAll} + value={isAll} + /> + )}

{t('If the recipient account is new, the balance needs to be more than the existential deposit. Likewise if the sending account balance drops below the same value, the account will be removed from the state.')}

@@ -126,7 +179,11 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send isDisabled={!hasAvailable || !recipientId || !amount} label={t('Make Transfer')} onStart={onClose} - params={[recipientId, amount]} + params={ + canToggleAll && isAll + ? [recipientId, maxTransfer] + : [recipientId, amount] + } tx={ isProtected && api.tx.balances.transferKeepAlive ? 'balances.transferKeepAlive' @@ -153,7 +210,11 @@ export default React.memo(styled(Transfer)` flex-basis: 10rem; } - .aliveToggle { + .typeToggle { text-align: right; } + + .typeToggle+.typeToggle { + margin-top: 0.375rem; + } `);