import React, { useMemo, useCallback } from 'react';
import cn from 'classnames';
import {
Grid,
ToggleInput,
ErrorText,
Flex,
LinkButton,
H5,
Text,
} from 'indigo-react';
import { useExploreTxUrl } from 'lib/explorer';
import { hexify } from 'lib/txn';
import { composeValidator, buildCheckboxValidator } from 'form/validators';
import BridgeForm from 'form/BridgeForm';
import Condition from 'form/Condition';
import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
import CopyButton from './CopyButton';
import ProgressButton from './ProgressButton';
import convertToInt from 'lib/convertToInt';
import NeedFundsNotice from './NeedFundsNotice';
export default function InlineEthereumTransaction({
// from useEthereumTransaction.bind
initializing,
canSign,
generateAndSign,
signed,
broadcast,
broadcasted,
confirmed,
completed,
reset,
error,
gasPrice,
setGasPrice,
resetGasPrice,
txHash,
nonce,
chainId,
needFunds,
signedTransaction,
confirmationProgress,
// additional from parent
label = 'Generate & Sign Transaction',
className,
onReturn,
}) {
// show receipt after successful broadcast
const showReceipt = broadcasted || confirmed || completed;
// 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 showSignedTx = signed;
const exploreTxUrl = useExploreTxUrl(txHash);
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 (
Transaction Complete
);
} else if (showBroadcastButton) {
return (
broadcast()}>
Send Transaction
);
} else if (showLoadingButton) {
return (
Sending Transaction
);
} else {
return (
{label}
);
}
};
const serializedTxHex = useMemo(
() => signedTransaction && hexify(signedTransaction.serialize()),
[signedTransaction]
);
return (
{() => (
<>
{renderPrimarySection()}
{error && (
{error.message}
)}
{needFunds && (
)}
{showConfigureInput && (
<>
Gas Price
{gasPrice} Gwei
{/* TODO(shrugs): move to indigo/RangeInput */}
setGasPrice(convertToInt(e.target.value, 10))
}
/>
Cheap
Fast
>
)}
{showSignedTx && (
<>
Nonce
{nonce}
Gas Price
{gasPrice.toFixed()} Gwei
Signed Transaction Hex
{serializedTxHex}
>
)}
{showReceipt && (
<>
Transaction Hash
Etherscan↗
{txHash}
>
)}
{completed && (
<>
Return
>
)}
>
)}
);
}