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) {
107 } else if (showBroadcastButton) {
114 disabled={!canBroadcast}
115 onClick={() => broadcast()}>
119 } else if (showLoadingButton) {
126 progress={confirmationProgress}>
135 onClick={generateAndSign}
137 loading={!canSign && initializing}>
144 const serializedTxsHex = useMemo(
146 signedTransactions &&
147 signedTransactions.map(stx => hexify(stx.serialize())),
151 const numTxs = useMemo(() => (unsignedTransactions || []).length || 1, [
152 unsignedTransactions,
154 const maxCost = useMemo(
164 [gasLimit, gasPrice, numTxs]
167 const gasInfo = useMemo(() => {
168 const extra = numTxs === 1 ? '' : `* ${numTxs} TXN`;
169 return showGasDetails
170 ? `${gasPrice} Gwei * ${gasLimit} ${extra} = ${maxCost} ETH`
172 }, [showGasDetails, maxCost, gasPrice, gasLimit, numTxs]);
175 <Grid className={cn(className, 'mt1')}>
176 <BridgeForm validate={validate} onValues={onValues}>
179 {renderPrimarySection()}
182 <Grid.Item full as={ErrorText} className="mv1">
196 {showConfigureInput && (
203 inverseLabel="Back to Defaults"
205 disabled={!showConfigureInput || initializing}
208 <Condition when="useAdvanced" is={true}>
209 <Grid.Item full className="mb2"></Grid.Item>
217 <Flex.Item as={H5}>Gas Price</Flex.Item>
223 onClick={toggleGasDetails}
228 {/* TODO(shrugs): move to indigo/RangeInput */}
237 setGasPrice(convertToInt(e.target.value, 10))
246 <Flex.Item as={Text}>Cheap</Flex.Item>
247 <Flex.Item as={Text}>Fast</Flex.Item>
249 <Grid.Divider className="mt4" />
260 label="Signed Transaction"
262 disabled={!showSignedTx}
264 <Condition when="viewSigned" is={true}>
265 <SignedTransactionList
266 serializedTxsHex={serializedTxsHex}
277 <Grid.Item full as={RestartButton} onClick={onReturn}>
285 onClose={toggleShowReceipt}
291 className="underline mv2 pointer"
293 onClick={toggleShowReceipt}>
306function SignedTransactionList({ serializedTxsHex, nonce, gasPrice }) {
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>Gas Price</Flex.Item>
317 <Flex.Item>{gasPrice.toFixed()} Gwei</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, maxCost, 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>Gas Price</Flex.Item>
344 <Flex.Item className="mono">{maxCost} 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