1import React, { useMemo, useCallback, useState } from 'react';
2import cn from 'classnames';
12import { toBN } from 'web3-utils';
14import { ReactComponent as InfoIcon } from 'assets/info.svg';
16import { useExploreTxUrls } from 'lib/explorer';
17import { hexify } from 'lib/txn';
18import { safeFromWei, safeToWei } from 'lib/lib';
20import { composeValidator, buildCheckboxValidator } from 'form/validators';
21import BridgeForm from 'form/BridgeForm';
22import Condition from 'form/Condition';
24import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
25import CopyButton from './CopyButton';
26import ProgressButton from './ProgressButton';
27import convertToInt from 'lib/convertToInt';
28import NeedFundsNotice from './NeedFundsNotice';
30export default function InlineEthereumTransaction({
31 // from useEthereumTransaction.bind
54 // additional from parent
55 label = 'Generate & Sign Transaction',
59 // show receipt after successful broadcast
60 const [showReceipt, setShowReceipt] = useState(false);
61 const toggleShowReceipt = useCallback(() => setShowReceipt(!showReceipt), [
65 // show configure controls pre-broadcast
66 const showConfigureInput = !(signed || broadcasted || confirmed || completed);
67 // show the send/loading button while signed, broadcasting, or confirme
68 const showBroadcastButton = signed;
69 const showLoadingButton = broadcasted || confirmed;
70 const canBroadcast = signed && !needFunds;
71 // show signed tx only when signing (for offline usage)
72 const showSignedTx = signed;
74 const [showGasDetails, _setShowGasDetails] = useState(false);
75 const toggleGasDetails = useCallback(
76 () => _setShowGasDetails(!showGasDetails),
77 [_setShowGasDetails, showGasDetails]
80 const validate = useMemo(
83 useAdvanced: buildCheckboxValidator(),
84 viewSigned: buildCheckboxValidator(),
89 const onValues = useCallback(
90 ({ valid, values, form }) => {
91 if (!values.useAdvanced) {
98 const renderPrimarySection = () => {
101 <Grid.Item full as={RestartButton} solid onClick={() => reset()}>
105 } else if (completed) {
109 <Grid.Item full as={RestartButton} onClick={onReturn}>
117 onClose={toggleShowReceipt}
123 className="underline mv2 pointer"
125 onClick={toggleShowReceipt}>
131 } else if (showBroadcastButton) {
138 disabled={!canBroadcast}
139 onClick={() => broadcast()}>
143 } else if (showLoadingButton) {
150 progress={confirmationProgress}>
159 onClick={generateAndSign}
161 loading={!canSign && initializing}>
168 const serializedTxsHex = useMemo(
170 signedTransactions &&
171 signedTransactions.map(stx => hexify(stx.serialize())),
175 const numTxs = useMemo(() => (unsignedTransactions || []).length || 1, [
176 unsignedTransactions,
178 const maxCost = useMemo(
189 [gasLimit, gasPrice, numTxs]
192 const gasInfo = useMemo(() => {
193 const extra = numTxs === 1 ? '' : `* ${numTxs} TXN`;
194 return showGasDetails
195 ? `${gasPrice} Gwei * ${gasLimit} ${extra} = ${maxCost} ETH`
197 }, [showGasDetails, maxCost, gasPrice, gasLimit, numTxs]);
200 <Grid className={cn(className, 'mt1')}>
201 <BridgeForm validate={validate} onValues={onValues}>
204 {renderPrimarySection()}
207 <Grid.Item full as={ErrorText} className="mv1">
221 {showConfigureInput && (
228 inverseLabel="Back to Defaults"
230 disabled={!showConfigureInput || initializing}
233 <Condition when="useAdvanced" is={true}>
234 <Grid.Item full className="mb2"></Grid.Item>
242 <Flex.Item as={H5}>Transaction Fee</Flex.Item>
248 onClick={toggleGasDetails}
253 {/* TODO(shrugs): move to indigo/RangeInput */}
262 setGasPrice(convertToInt(e.target.value, 10))
271 <Flex.Item as={Text}>Cheap</Flex.Item>
272 <Flex.Item as={Text}>Fast</Flex.Item>
274 <Grid.Divider className="mt4" />
285 label="Signed Transaction"
287 disabled={!showSignedTx}
289 <Condition when="viewSigned" is={true}>
290 <SignedTransactionList
291 serializedTxsHex={serializedTxsHex}
305function SignedTransactionList({ serializedTxsHex, nonce, maxCost }) {
306 return serializedTxsHex.map((serializedTxHex, i) => (
307 <React.Fragment key={i}>
309 <Grid.Item full as={Flex} justify="between" className="pv4 black f5">
310 <Flex.Item>Nonce</Flex.Item>
311 <Flex.Item>{nonce + i}</Flex.Item>
314 <Grid.Item full as={Flex} justify="between" className="pv4 black f5">
315 <Flex.Item>Transaction Cost</Flex.Item>
316 <Flex.Item>{maxCost} ETH</Flex.Item>
319 <Grid.Item full as={Flex} justify="between" className="mt3 mb2">
320 <Flex.Item as={H5}>Signed Transaction Hex</Flex.Item>
321 <Flex.Item as={CopyButton} text={serializedTxHex} />
323 <Grid.Item full as="code" className="mb4 f6 mono gray4 wrap">
331function TransactionReceipt({ txHashes, maxCost, onClose }) {
332 const txUrls = useExploreTxUrls(txHashes);
335 <Grid.Item full as={Flex} justify="between" className="pv2 black f5">
336 <Flex.Item>Receipt</Flex.Item>
337 <Flex.Item onClick={onClose} className="underline pointer">
341 <Grid.Item full as={Flex} justify="between" className="pv2 black f5">
342 <Flex.Item>Transaction Cost</Flex.Item>
343 <Flex.Item className="mono">{maxCost} ETH</Flex.Item>
346 <Grid.Item full as={Flex} col className="pv4">
347 <Flex.Item as={H5}>Receipt</Flex.Item>
350 txHashes.map((txHash, i) => (
351 <Flex.Item as={Flex} className="mv3">
357 className="f6 mono gray4 wrap ">
360 <Flex.Item className="ml8" as={LinkButton} href={txUrls[i]}>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list