import React, { useMemo, useCallback, useState } from 'react';
import cn from 'classnames';
import { Grid, ErrorText, Flex, LinkButton, H5 } from 'indigo-react';
import { toBN } from 'web3-utils';
import { useExploreTxUrls } from 'lib/explorer';
import { safeFromWei, safeToWei } from 'lib/lib';
import { composeValidator, buildCheckboxValidator } from 'form/validators';
import BridgeForm from 'form/BridgeForm';
import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
import ProgressButton from './ProgressButton';
import NeedFundsNotice from './NeedFundsNotice';
import './InlineEthereumTransaction.scss';
import FeeDropdown from './L2/Dropdowns/FeeDropdown';
export default function InlineEthereumTransaction({
// from useEthereumTransaction.bind
initializing,
canSign,
generateAndSign,
signed,
fakeSigned,
broadcast,
broadcasted,
confirmed,
completed,
reset,
error,
gasPrice,
setGasPrice,
resetGasPrice,
txHashes,
needFunds,
confirmationProgress,
gasLimit,
unsignedTransactions,
finalCost,
// additional from parent
label = 'Generate & Sign Transaction',
className,
onReturn,
}) {
// show receipt after successful broadcast
const [showReceipt, setShowReceipt] = useState(false);
const toggleShowReceipt = useCallback(() => setShowReceipt(!showReceipt), [
setShowReceipt,
showReceipt,
]);
// show configure controls pre-broadcast
const showConfigureInput = !(signed || broadcasted || confirmed || completed);
// show the send/loading button while signed, broadcasting, or confirme
const showBroadcastButton = signed;
const showLoadingButton = broadcasted || confirmed;
const canBroadcast = signed && !needFunds;
// show signed tx only when signing (for offline usage)
const validate = useMemo(
() =>
composeValidator({
useAdvanced: buildCheckboxValidator(),
viewSigned: buildCheckboxValidator(),
}),
[]
);
const onValues = useCallback(
({ valid, values, form }) => {
if (!values.useAdvanced) {
resetGasPrice();
}
},
[resetGasPrice]
);
const renderPrimarySection = () => {
if (error) {
return (
reset()}>
Reset Transaction
);
} else if (completed) {
return (
<>
Return
{showReceipt && (
)}
{!showReceipt && (
View Receipt
)}
>
);
} else if (showBroadcastButton) {
return (
broadcast()}>
Send Transaction
);
} else if (showLoadingButton) {
return (
Sending Transaction
);
} else {
return (
{label}
);
}
};
const numTxs = useMemo(() => (unsignedTransactions || []).length || 1, [
unsignedTransactions,
]);
const maxCost = useMemo(
() =>
safeFromWei(
safeToWei(
toBN(gasLimit)
.mul(toBN(gasPrice))
.mul(toBN(numTxs)),
'gwei'
),
'ether'
),
[gasLimit, gasPrice, numTxs]
);
return (
{() => (
<>
{showConfigureInput && (
<>
Gas Fee
Max Transaction Cost
{maxCost} ETH
>
)}
{renderPrimarySection()}
{error && (
{error.message}
)}
{needFunds && (
)}
>
)}
);
}
function TransactionReceipt({ txHashes, finalCost, onClose }) {
const txUrls = useExploreTxUrls(txHashes);
return (
<>
Receipt
Close
Transaction Cost
{finalCost} ETH
Receipt
{txHashes &&
txHashes.map((txHash, i) => (
<>
{txHash}
Etherscan↗
>
))}
>
);
}