import React, { useMemo, useCallback } from 'react';
import cn from 'classnames';
import {
Grid,
ToggleInput,
ErrorText,
Flex,
LinkButton,
H5,
Text,
} from 'indigo-react';
import { useExploreTxUrl, useExploreTxUrls } from 'lib/explorer';
import { hexify } from 'lib/txn';
import pluralize from 'lib/pluralize';
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,
txHashes,
nonce,
chainId,
needFunds,
signedTransactions,
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 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 serializedTxsHex = useMemo(
() =>
signedTransactions &&
signedTransactions.map(stx => hexify(stx.serialize())),
[signedTransactions]
);
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 && (
<>
>
)}
{showReceipt && (
<>
>
)}
{completed && (
<>
Return
>
)}
>
)}
);
}
function SignedTransactionList({ serializedTxsHex, nonce, gasPrice }) {
return serializedTxsHex.map((serializedTxHex, i) => (
Nonce
{nonce + i}
Gas Price
{gasPrice.toFixed()} Gwei
Signed Transaction Hex
{serializedTxHex}
));
}
function HashReceiptList({ txHashes }) {
const txUrls = useExploreTxUrls(txHashes);
const header = pluralize(txHashes.length, 'Hash', 'Hashes');
return (
<>
Transaction {header}
{txHashes &&
txHashes.map((txHash, i) => (
<>
{txHash}
Etherscan↗
>
))}
>
);
}