import React, { useMemo, useCallback } from 'react';
import cn from 'classnames';
import {
Grid,
ToggleInput,
ErrorText,
Flex,
LinkButton,
H5,
Text,
HelpText,
} from 'indigo-react';
import { fromWei } from 'web3-utils';
import { useExploreTxUrl } from 'lib/explorer';
import { hexify } from 'lib/txn';
import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
import WarningBox from './WarningBox';
import { composeValidator, buildCheckboxValidator } from 'form/validators';
import BridgeForm from 'form/BridgeForm';
import Condition from 'form/Condition';
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,
// 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 || broadcasted || confirmed;
const canBroadcast = signed && !needFunds;
const isLoading = broadcasted || confirmed;
// 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 renderPrimaryButton = () => {
if (error) {
return (
reset()}>
Reset Transaction
);
} else if (completed) {
return (
<>
Return
>
);
} else if (showBroadcastButton) {
return (
broadcast()}>
Send Transaction
);
} else {
return (
{label}
);
}
};
return (
{({ handleSubmit }) => (
<>
{renderPrimaryButton()}
{error && (
{error.message}
)}
{needFunds && (
The address {needFunds.address} needs at least{' '}
{fromWei(needFunds.minBalance)} ETH and currently has{' '}
{fromWei(needFunds.balance)} ETH. Waiting until the account has
enough funds.
)}
{showConfigureInput && (
<>
Gas Price
{gasPrice} Gwei
{/* TODO(shrugs): move to indigo/RangeInput */}
setGasPrice(parseInt(e.target.value, 10))}
/>
Cheap
Fast
Nonce: {nonce}
Chain ID: {chainId}
>
)}
{showSignedTx && (
<>
{hexify(signedTransaction.serialize())}
>
)}
{showReceipt && (
<>
Transaction Hash
Etherscan↗
{txHash}
>
)}
>
)}
);
}