import React, { useEffect } from 'react';
import cn from 'classnames';
import {
Grid,
ToggleInput,
ErrorText,
Flex,
LinkButton,
H5,
Text,
HelpText,
} from 'indigo-react';
import { fromWei } from 'web3-utils';
import { useCheckboxInput } from 'lib/useInputs';
import { useExploreTxUrl } from 'lib/explorer';
import { hexify } from 'lib/txn';
import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
import WarningBox from './WarningBox';
export default function InlineEthereumTransaction({
// from useEthereumTransaction.bind
initializing,
canSign,
generateAndSign,
signed,
broadcast,
broadcasted,
confirmed,
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;
// show configure controls pre-broadcast
const showConfigureInput = !(signed || broadcasted || confirmed);
// show signed tx only when signing (for offline usage)
const showSignedTx = signed;
const exploreTxUrl = useExploreTxUrl(txHash);
const [advancedInput, { data: advancedOpen }] = useCheckboxInput({
name: 'advanced',
label: 'Advanced Configuration',
inverseLabel: 'Cancel Advanced Configuration',
disabled: !showConfigureInput,
});
const [
viewSignedTransaction,
{ data: signedTransactionOpen },
] = useCheckboxInput({
name: 'viewsigned',
label: 'View Signed Transaction',
inverseLabel: 'Hide Signed Transaction',
disabled: !showSignedTx,
});
// reset gas price when closing advanced configuration
useEffect(() => {
if (!advancedOpen) {
resetGasPrice();
}
}, [advancedOpen, resetGasPrice]);
const renderPrimaryButton = () => {
if (error) {
return (
reset()}>
Reset Transaction
);
} else if (confirmed) {
return (
<>
Return
>
);
} else if (signed || broadcasted) {
return (
broadcast()}>
Send Transaction
);
} else {
return (
{label}
);
}
};
return (
{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 && (
<>
{advancedOpen && (
<>
Gas Price
{gasPrice} Gwei
{/* TODO(shrugs): move to indigo/RangeInput */}
setGasPrice(parseInt(e.target.value, 10))}
/>
Cheap
Fast
Nonce: {nonce}
Chain ID: {chainId}
>
)}
>
)}
{showSignedTx && (
<>
{signedTransactionOpen && (
{hexify(signedTransaction.serialize())}
)}
>
)}
{showReceipt && (
<>
Transaction Hash
Etherscan↗
{txHash}
>
)}
);
}