diff --git a/packages/app-accounts/src/modals/Transfer.tsx b/packages/app-accounts/src/modals/Transfer.tsx index adde0464f8..eac04f4d18 100644 --- a/packages/app-accounts/src/modals/Transfer.tsx +++ b/packages/app-accounts/src/modals/Transfer.tsx @@ -4,218 +4,157 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; -import { Index } from '@polkadot/types/interfaces'; -import { ApiProps } from '@polkadot/react-api/types'; import { DerivedFees } from '@polkadot/api-derive/types'; import { I18nProps } from '@polkadot/react-components/types'; import BN from 'bn.js'; -import React from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import styled from 'styled-components'; import { Button, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; import { Available } from '@polkadot/react-query'; -import Checks, { calcTxLength } from '@polkadot/react-signer/Checks'; -import { withApi, withCalls, withMulti } from '@polkadot/react-api'; -import { ZERO_FEES } from '@polkadot/react-signer/Checks/constants'; -import { bnMax } from '@polkadot/util'; +import Checks from '@polkadot/react-signer/Checks'; +import { ApiContext } from '@polkadot/react-api'; import translate from '../translate'; -interface Props extends ApiProps, I18nProps { +interface Props extends I18nProps { balances_fees?: DerivedFees; className?: string; onClose: () => void; recipientId?: string; senderId?: string; - system_accountNonce?: BN; -} - -interface State { - amount: BN; - extrinsic: SubmittableExtrinsic | null; - hasAvailable: boolean; - maxBalance?: BN; - recipientId?: string | null; - senderId?: string | null; } const ZERO = new BN(0); -class Transfer extends React.PureComponent { - public state: State; +// TODO Re-enable when we have proper fee calculation (incl. weights) +// async function calcMax (api: ApiPromise, balances_fees: DerivedFees | undefined, senderId: string, recipientId: string): Promise { +// let maxBalance = new BN(1); - public constructor (props: Props) { - super(props); +// if (!balances_fees) { +// return maxBalance; +// } - this.state = { - amount: ZERO, - extrinsic: null, - hasAvailable: true, - maxBalance: ZERO, - recipientId: props.recipientId || null, - senderId: props.senderId || null - }; - } +// const { transferFee, transactionBaseFee, transactionByteFee, creationFee } = balances_fees; - public componentDidUpdate (prevProps: Props, prevState: State): void { - const { balances_fees } = this.props; - const { extrinsic, recipientId, senderId } = this.state; - const hasLengthChanged = ((extrinsic && extrinsic.encodedLength) || 0) !== ((prevState.extrinsic && prevState.extrinsic.encodedLength) || 0); +// const [senderNonce, senderBalances, recipientBalances] = await Promise.all([ +// api.query.system.accountNonce(senderId), +// api.derive.balances.all(senderId), +// api.derive.balances.all(recipientId) +// ]); - if ((recipientId && prevState.recipientId !== recipientId) || - (balances_fees !== prevProps.balances_fees) || (prevState.senderId !== senderId) || - hasLengthChanged - ) { - this.setMaxBalance().catch((error: Error): void => console.error(error)); +// let prevMax = new BN(0); + +// // something goes screwy here when we move this out of the component :( +// let extrinsic: any; + +// while (!prevMax.eq(maxBalance)) { +// prevMax = maxBalance; +// extrinsic = api.tx.balances.transfer(senderNonce, prevMax); + +// const txLength = calcTxLength(extrinsic, senderNonce); +// const fees = transactionBaseFee +// .add(transactionByteFee.mul(txLength)) +// .add(transferFee) +// .add(recipientBalances.availableBalance.isZero() ? creationFee : ZERO); + +// maxBalance = bnMax(senderBalances.availableBalance.sub(fees), ZERO); +// } + +// return maxBalance; +// } + +function Transfer ({ className, onClose, recipientId: propRecipientId, senderId: propSenderId, t }: Props): React.ReactElement { + const { api } = useContext(ApiContext); + const [amount, setAmount] = useState(new BN(0)); + const [extrinsic, setExtrinsic] = useState(null); + const [hasAvailable, setHasAvailable] = useState(true); + const [maxBalance] = useState(new BN(0)); + const [recipientId, setRecipientId] = useState(propRecipientId || null); + const [senderId, setSenderId] = useState(propSenderId || null); + + useEffect((): void => { + if (senderId && recipientId) { + setExtrinsic(api.tx.balances.transfer(recipientId, amount || ZERO)); + + // We currently have not enabled the max functionality - we don't take care of weights + // calcMax(api, balances_fees, senderId, recipientId) + // .then(([maxBalance]): void => setMaxBalance(maxBalance)) + // .catch((error: Error): void => console.error(error)); } - } + }, [amount, recipientId, senderId]); - public render (): React.ReactNode { - const { className, onClose, recipientId: propRecipientId, senderId: propSenderId, t } = this.props; - const { extrinsic, hasAvailable, maxBalance, recipientId, senderId } = this.state; - const available = {t('available ')}; + const available = {t('available ')}; - return ( - - {t('Send funds')} - -
- } - onChange={this.onChangeFrom} - type='account' - /> - } - onChange={this.onChangeTo} - type='allPlus' - /> - - -
-
- - -