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
55 // additional from parent
56 label = 'Generate & Sign Transaction',
60 // show receipt after successful broadcast
61 const [showReceipt, setShowReceipt] = useState(false);
62 const toggleShowReceipt = useCallback(() => setShowReceipt(!showReceipt), [
66 // show configure controls pre-broadcast
67 const showConfigureInput = !(signed || broadcasted || confirmed || completed);
68 // show the send/loading button while signed, broadcasting, or confirme
69 const showBroadcastButton = signed;
70 const showLoadingButton = broadcasted || confirmed;
71 const canBroadcast = signed && !needFunds;
72 // show signed tx only when signing (for offline usage)
73 const showSignedTx = signed;
75 const [showGasDetails, _setShowGasDetails] = useState(false);
76 const toggleGasDetails = useCallback(
77 () => _setShowGasDetails(!showGasDetails),
78 [_setShowGasDetails, showGasDetails]
81 const validate = useMemo(
84 useAdvanced: buildCheckboxValidator(),
85 viewSigned: buildCheckboxValidator(),
90 const onValues = useCallback(
91 ({ valid, values, form }) => {
92 if (!values.useAdvanced) {
99 const renderPrimarySection = () => {
102 <Grid.Item full as={RestartButton} solid onClick={() => reset()}>
106 } else if (completed) {
110 <Grid.Item full as={RestartButton} onClick={onReturn}>
117 finalCost={finalCost}
118 onClose={toggleShowReceipt}
124 className="underline mv2 pointer"
126 onClick={toggleShowReceipt}>
132 } else if (showBroadcastButton) {
139 disabled={!canBroadcast}
140 onClick={() => broadcast()}>
144 } else if (showLoadingButton) {
151 progress={confirmationProgress}>
160 onClick={generateAndSign}
162 loading={!canSign && initializing}>
169 const serializedTxsHex = useMemo(
171 signedTransactions &&
172 signedTransactions.map(stx => hexify(stx.serialize())),
176 const numTxs = useMemo(() => (unsignedTransactions || []).length || 1, [
177 unsignedTransactions,
179 const maxCost = useMemo(
190 [gasLimit, gasPrice, numTxs]
193 const gasInfo = useMemo(() => {
194 const extra = numTxs === 1 ? '' : `* ${numTxs} txs`;
195 return showGasDetails
196 ? `${gasPrice} Gwei * ${gasLimit} ${extra} = ${maxCost} ETH`
198 }, [showGasDetails, maxCost, gasPrice, gasLimit, numTxs]);
201 <Grid className={cn(className, 'mt1')}>
202 <BridgeForm validate={validate} onValues={onValues}>
205 {renderPrimarySection()}
208 <Grid.Item full as={ErrorText} className="mv1">
222 {showConfigureInput && (
229 inverseLabel="Back to Defaults"
231 disabled={!showConfigureInput || initializing}
234 <Condition when="useAdvanced" is={true}>
235 <Grid.Item full className="mb2"></Grid.Item>
243 <Flex.Item as={H5}>Transaction Fee</Flex.Item>
249 onClick={toggleGasDetails}
254 {/* TODO(shrugs): move to indigo/RangeInput */}
263 setGasPrice(convertToInt(e.target.value, 10))
272 <Flex.Item as={Text}>Cheap</Flex.Item>
273 <Flex.Item as={Text}>Fast</Flex.Item>
275 <Grid.Divider className="mt4" />
286 label="Signed Transaction"
288 disabled={!showSignedTx}
290 <Condition when="viewSigned" is={true}>
291 <SignedTransactionList
292 serializedTxsHex={serializedTxsHex}
306function SignedTransactionList({ serializedTxsHex, nonce, maxCost }) {
307 return serializedTxsHex.map((serializedTxHex, i) => (
308 <React.Fragment key={i}>
310 <Grid.Item full as={Flex} justify="between" className="pv4 black f5">
311 <Flex.Item>Nonce</Flex.Item>
312 <Flex.Item>{nonce + i}</Flex.Item>
315 <Grid.Item full as={Flex} justify="between" className="pv4 black f5">
316 <Flex.Item>Transaction Cost</Flex.Item>
317 <Flex.Item>{maxCost} ETH</Flex.Item>
320 <Grid.Item full as={Flex} justify="between" className="mt3 mb2">
321 <Flex.Item as={H5}>Signed Transaction Hex</Flex.Item>
322 <Flex.Item as={CopyButton} text={serializedTxHex} />
324 <Grid.Item full as="code" className="mb4 f6 mono gray4 wrap">
332function TransactionReceipt({ txHashes, finalCost, onClose }) {
333 const txUrls = useExploreTxUrls(txHashes);
336 <Grid.Item full as={Flex} justify="between" className="pv2 black f5">
337 <Flex.Item>Receipt</Flex.Item>
338 <Flex.Item onClick={onClose} className="underline pointer">
342 <Grid.Item full as={Flex} justify="between" className="pv2 black f5">
343 <Flex.Item>Transaction Cost</Flex.Item>
344 <Flex.Item className="mono">{finalCost} ETH</Flex.Item>
347 <Grid.Item full as={Flex} col className="pv4">
348 <Flex.Item as={H5}>Receipt</Flex.Item>
351 txHashes.map((txHash, i) => (
352 <Flex.Item as={Flex} className="mv3">
358 className="f6 mono gray4 wrap ">
361 <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