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 './copiable/CopyButton';
26import ProgressButton from './ProgressButton';
27import { convertToInt } from 'lib/convertToInt';
28import NeedFundsNotice from './NeedFundsNotice';
29import NoticeBox from './NoticeBox';
31import './InlineEthereumTransaction.scss';
33export default function InlineEthereumTransaction({
34 // from useEthereumTransaction.bind
59 // additional from parent
60 label = 'Generate & Sign Transaction',
64 // show receipt after successful broadcast
65 const [showReceipt, setShowReceipt] = useState(false);
66 const toggleShowReceipt = useCallback(() => setShowReceipt(!showReceipt), [
70 // show configure controls pre-broadcast
71 const showConfigureInput = !(signed || broadcasted || confirmed || completed);
72 // show the send/loading button while signed, broadcasting, or confirme
73 const showBroadcastButton = signed;
74 const showLoadingButton = broadcasted || confirmed;
75 const canBroadcast = signed && !needFunds;
76 // show signed tx only when signing (for offline usage)
77 const showSignedTx = signed;
79 const [showGasDetails, _setShowGasDetails] = useState(false);
80 const toggleGasDetails = useCallback(
81 () => _setShowGasDetails(!showGasDetails),
82 [_setShowGasDetails, showGasDetails]
85 const validate = useMemo(
88 useAdvanced: buildCheckboxValidator(),
89 viewSigned: buildCheckboxValidator(),
94 const onValues = useCallback(
95 ({ valid, values, form }) => {
96 if (!values.useAdvanced) {
103 const renderPrimarySection = () => {
107 className="eth-tx-button"
111 onClick={() => reset()}>
115 } else if (completed) {
120 className="eth-tx-button"
130 finalCost={finalCost}
131 onClose={toggleShowReceipt}
137 className="underline mv2 pointer"
139 onClick={toggleShowReceipt}>
145 } else if (showBroadcastButton) {
148 className="eth-tx-button"
153 disabled={!canBroadcast}
154 onClick={() => broadcast()}>
158 } else if (showLoadingButton) {
161 className="eth-tx-button"
166 progress={confirmationProgress}>
173 className="eth-tx-button"
176 onClick={generateAndSign}
178 loading={!canSign && initializing}>
185 const serializedTxsHex = useMemo(
187 signedTransactions &&
188 signedTransactions.map(stx => hexify(stx.serialize())),
192 const numTxs = useMemo(() => (unsignedTransactions || []).length || 1, [
193 unsignedTransactions,
195 const maxCost = useMemo(
206 [gasLimit, gasPrice, numTxs]
209 const gasInfo = useMemo(() => {
210 const extra = numTxs === 1 ? '' : `* ${numTxs} txs`;
211 return showGasDetails
212 ? `${gasPrice} Gwei * ${gasLimit} ${extra} = ${maxCost} ETH`
214 }, [showGasDetails, maxCost, gasPrice, gasLimit, numTxs]);
217 <Grid className={cn(className, 'mt1')}>
218 <BridgeForm validate={validate} onValues={onValues}>
221 {renderPrimarySection()}
225 className="eth-tx-button" full as={ErrorText} className="mv1">
232 className="eth-tx-button"
240 {showConfigureInput && (
243 className="eth-tx-button"
248 inverseLabel="Back to Defaults"
250 disabled={!showConfigureInput || initializing}
253 <Condition when="useAdvanced" is={true}>
255 className="eth-tx-button" full className="mb2"></Grid.Item>
258 className="eth-tx-button"
264 <Flex.Item as={H5}>Transaction Fee</Flex.Item>
270 onClick={toggleGasDetails}
275 {/* TODO(shrugs): move to indigo/RangeInput */}
277 className="eth-tx-button"
285 setGasPrice(convertToInt(e.target.value, 10))
289 className="eth-tx-button"
295 <Flex.Item as={Text}>Cheap</Flex.Item>
296 <Flex.Item as={Text}>Fast</Flex.Item>
298 <Grid.Divider className="mt4" />
303 {showSignedTx && fakeSigned && (
306 className="eth-tx-button" full as={NoticeBox} className="mt2">
307 Your wallet will sign the transaction upon sending it.
312 {showSignedTx && !fakeSigned && (
315 className="eth-tx-button"
319 label="Signed Transaction"
321 disabled={!showSignedTx}
323 <Condition when="viewSigned" is={true}>
324 <SignedTransactionList
325 serializedTxsHex={serializedTxsHex}
339function SignedTransactionList({ serializedTxsHex, nonce, maxCost }) {
340 return serializedTxsHex.map((serializedTxHex, i) => (
341 <React.Fragment key={i}>
344 className="eth-tx-button" full as={Flex} justify="between" className="pv4 black f5">
345 <Flex.Item>Nonce</Flex.Item>
346 <Flex.Item>{nonce + i}</Flex.Item>
350 className="eth-tx-button" full as={Flex} justify="between" className="pv4 black f5">
351 <Flex.Item>Transaction Cost</Flex.Item>
352 <Flex.Item>{maxCost} ETH</Flex.Item>
356 className="eth-tx-button" full as={Flex} justify="between" className="mt3 mb2">
357 <Flex.Item as={H5}>Signed Transaction Hex</Flex.Item>
358 <Flex.Item as={CopyButton} text={serializedTxHex} />
361 className="eth-tx-button" full as="code" className="mb4 f6 mono gray4 wrap">
369function TransactionReceipt({ txHashes, finalCost, onClose }) {
370 const txUrls = useExploreTxUrls(txHashes);
374 className="eth-tx-button" full as={Flex} justify="between" className="pv2 black f5">
375 <Flex.Item>Receipt</Flex.Item>
376 <Flex.Item onClick={onClose} className="underline pointer">
381 className="eth-tx-button" full as={Flex} justify="between" className="pv2 black f5">
382 <Flex.Item>Transaction Cost</Flex.Item>
383 <Flex.Item className="mono">{finalCost} ETH</Flex.Item>
387 className="eth-tx-button" full as={Flex} col className="pv4">
388 <Flex.Item as={H5}>Receipt</Flex.Item>
391 txHashes.map((txHash, i) => (
392 <Flex.Item as={Flex} className="mv3">
398 className="f6 mono gray4 wrap ">
401 <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