/* eslint-disable @typescript-eslint/camelcase */ // Copyright 2017-2019 @polkadot/app-accounts authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; import { DerivedFees } from '@polkadot/api-derive/types'; import { I18nProps } from '@polkadot/react-components/types'; import BN from 'bn.js'; 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 from '@polkadot/react-signer/Checks'; import { ApiContext } from '@polkadot/react-api'; import translate from '../translate'; interface Props extends I18nProps { balances_fees?: DerivedFees; className?: string; onClose: () => void; recipientId?: string; senderId?: string; } const ZERO = new BN(0); // 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); // if (!balances_fees) { // return maxBalance; // } // const { transferFee, transactionBaseFee, transactionByteFee, creationFee } = balances_fees; // const [senderNonce, senderBalances, recipientBalances] = await Promise.all([ // api.query.system.accountNonce(senderId), // api.derive.balances.all(senderId), // api.derive.balances.all(recipientId) // ]); // 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]); const transferrable = {t('transferrable ')}; return ( {t('Send funds')}
} onChange={setSenderId} type='account' /> } onChange={setRecipientId} type='allPlus' />