1import { Nothing, Just } from 'folktale/maybe';
2import { Ok } from 'folktale/result';
3import React from 'react';
4import * as need from '../../lib/need';
6import { Code, H3 } from './Base';
7import { Button } from './Base';
8import { CheckboxButton, Input, InnerLabel, InnerLabelDropdown } from './Base';
9import { Warning } from './Base';
11import { GAS_LIMITS } from '../../lib/constants';
12import { BRIDGE_ERROR } from '../../lib/error';
13import { ROUTE_NAMES } from '../../lib/routeNames';
14import { compose } from '../../lib/lib';
15import * as tank from '../../lib/tank';
17 sendSignedTransaction,
23} from '../../lib/txn';
25import { withTxnConfirmations } from '../../store/txnConfirmations';
26import { withNetwork } from '../../store/network';
27import { withHistory } from '../../store/history';
28import { withWallet } from '../../store/wallet';
29import { withTxnCursor } from '../../store/txnCursor';
31const SUBMISSION_STATES = {
32 PROMPT: 'Send transaction',
33 PREPARING: 'Preparing...',
34 FUNDING: 'Finding transaction funding...',
35 SENDING: 'Sending transaction...',
38class StatelessTransaction extends React.Component {
44 gasLimit: GAS_LIMITS.DEFAULT.toString(),
45 showGasDetails: false,
52 txStatus: SUBMISSION_STATES.PROMPT,
56 this.createUnsignedTxn = this.createUnsignedTxn.bind(this);
57 this.setUserApproval = this.setUserApproval.bind(this);
58 this.setTxn = this.setTxn.bind(this);
59 this.setStx = this.setStx.bind(this);
60 this.sendTxn = this.sendTxn.bind(this);
61 this.setNonce = this.setNonce.bind(this);
62 this.setChainId = this.setChainId.bind(this);
63 this.setGasPrice = this.setGasPrice.bind(this);
64 this.setGasLimit = this.setGasLimit.bind(this);
65 this.rangeChange = this.rangeChange.bind(this);
66 this.toggleGasDetails = this.toggleGasDetails.bind(this);
67 this.handleChainUpdate = this.handleChainUpdate.bind(this);
71 const { props } = this;
73 const addr = need.addressFromWallet(props.wallet);
75 props.web3.matchWith({
78 const validWeb3 = w3.value;
80 const getTxMetadata = [
81 validWeb3.eth.getTransactionCount(addr),
82 validWeb3.eth.net.getId(),
83 validWeb3.eth.getGasPrice(),
86 Promise.all(getTxMetadata).then(r => {
90 gasPrice: fromWei(r[2], 'gwei'),
93 this.setState({ ...txMetadata });
99 handleChainUpdate(chainId) {
100 if (chainId === 'custom') {
115 const { state } = this;
116 this.setState({ userApproval: !state.userApproval });
121 showGasDetails: !this.state.showGasDetails,
133 this.setState({ txn });
136 createUnsignedTxn() {
137 const txn = this.props.createUnsignedTxn();
139 this.setState({ txn });
151 this.setState({ nonce });
155 setChainId(chainId) {
156 this.setState({ chainId });
160 setGasPrice(gasPrice) {
161 this.setState({ gasPrice });
166 this.setGasPrice(e.target.value);
169 setGasLimit(gasLimit) {
170 this.setState({ gasLimit });
182 const { props, state } = this;
183 const web3 = props.web3.value;
185 const stx = state.stx.matchWith({
186 Just: tx => tx.value,
188 throw BRIDGE_ERROR.MISSING_TXN;
192 if (props.beforeSend) {
194 this.setState({ txStatus: SUBMISSION_STATES.PREPARING });
195 const res = await props.beforeSend(stx);
196 if (res === false) throw new Error('beforeSend disallowed sending');
198 console.log('beforeSend error', e);
200 txStatus: SUBMISSION_STATES.PROMPT,
201 txError: Just('Something went wrong!'),
207 this.setState({ txStatus: SUBMISSION_STATES.FUNDING });
209 const rawTx = hexify(stx.serialize());
210 const cost = state.gasLimit * toWei(state.gasPrice, 'gwei');
212 const address = need.addressFromWallet(props.wallet);
213 let balance = await web3.eth.getBalance(address);
214 let hasBalance = balance >= cost;
216 let usedTank = false;
217 // if we need to, try and fund the transaction
219 usedTank = await this.ensureFundsFor(web3, address, cost, [rawTx]);
220 hasBalance = usedTank;
223 // if we still don't have sufficient balance, fail and tell the user
226 txStatus: SUBMISSION_STATES.PROMPT,
227 txError: Just(`Insufficient funds.
228 Address ${address} needs at least ${fromWei(cost.toString())} ETH,
229 currently has ${fromWei(balance.toString())} ETH.`),
232 // if we have the balance, proceed with submission
234 this.setState({ txStatus: SUBMISSION_STATES.SENDING });
236 sendSignedTransaction(
240 props.setTxnConfirmations
243 props.setTxnCursor(Just(Ok(txHash)));
246 if (props.newNetworkSeed) {
247 props.setNetworkSeed(Just(props.newNetworkSeed));
248 routeData.promptKeyfile = true;
251 if (props.newRevision) {
252 props.setNetworkRevision(Just(props.newRevision));
255 props.history.popAndPush(ROUTE_NAMES.SENT_TRANSACTION, routeData);
259 txStatus: SUBMISSION_STATES.PROMPT,
266 // uses the gas tank to ensure funds, but if we have to ask the user
267 // for funding, just resolve instead of waiting
268 // returns true if the gas tank was used, false otherwise
269 ensureFundsFor(web3, address, cost, signedTxs) {
270 return new Promise((resolve, reject) => {
272 .ensureFundsFor(web3, null, address, cost, signedTxs, () => {
281 getChainTitle(chainId) {
288 '1337': 'Geth private chains - 1337',
298 title: this.getChainTitle('1'),
302 title: this.getChainTitle('2'),
306 title: this.getChainTitle('3'),
310 title: this.getChainTitle('4'),
314 title: this.getChainTitle('42'),
318 title: this.getChainTitle('1337'),
322 title: this.getChainTitle('custom'),
329 const { web3, canGenerate } = this.props;
355 const { state } = this;
357 const canSign = Just.hasInstance(txn);
358 const canApprove = Just.hasInstance(stx);
359 const canSend = Just.hasInstance(stx) && userApproval === true;
361 const chainOptions = this.getChainOptions();
363 const generateButtonColor = Nothing.hasInstance(txn) ? 'blue' : 'green';
365 const signerButtonColor = Nothing.hasInstance(stx) ? 'blue' : 'green';
367 const generateTxnButton = (
370 disabled={!canGenerate}
371 prop-color={generateButtonColor}
372 prop-size={'lg wide'}
373 onClick={createUnsignedTxn}>
374 {'Generate Transaction'}
378 const unsignedTxnDisplay = txn.matchWith({
382 <H3 className={'mt-8'}>{'Unsigned Transaction'}</H3>
383 <Code>{JSON.stringify(tx.value, null, 2)}</Code>
388 const gasPriceRangeDialogue = (
390 <div className="mt-12 flex space-between align-baseline">
392 <span>Gas Price:</span>
393 <span className="ml-4 text-700 text-sm">{gasPrice} gwei</span>
395 <div className="text-sm">
396 <span>Max transaction fee: </span>
397 <span className="text-700">
398 {(gasPrice * gasLimit) / 1000000000} eth
410 onChange={this.rangeChange}
413 <div className="flex space-between text-sm mb-8">
420 const toggleGasDetailsDialogue = (
421 <span className="text-link" onClick={toggleGasDetails}>
426 const gasPriceDialogue = (
428 className={'mono mt-4'}
430 prop-format={'innerLabel'}
432 onChange={setGasPrice}>
433 <InnerLabel>{'Gas Price (gwei)'}</InnerLabel>
437 const gasLimitDialogue = (
439 className={'mono mt-4'}
441 prop-format={'innerLabel'}
443 onChange={setGasLimit}>
444 <InnerLabel>{'Gas Limit'}</InnerLabel>
448 const nonceDialogue = (
450 className={'mono mt-4 mb-4'}
452 prop-format={'innerLabel'}
455 <InnerLabel>{'Nonce'}</InnerLabel>
459 const chainDialogueTitle = customChain
461 : this.getChainTitle(chainId);
462 const chainDialogue = (
467 options={chainOptions}
468 handleUpdate={handleChainUpdate}
469 currentSelectionTitle={chainDialogueTitle}
473 const customChainDialogue = !customChain ? null : (
475 className={'mono mt-4 mb-8'}
477 prop-format={'innerLabel'}
479 onChange={setChainId}>
480 <InnerLabel>{'Chain ID'}</InnerLabel>
484 const onlineParamsDialogue = web3.matchWith({
490 {customChainDialogue}
495 const signTxnButton = (
499 prop-size={'lg wide'}
500 prop-color={signerButtonColor}
502 signTransaction({ ...this.props, ...this.state, setStx: this.setStx })
508 const signedTxnDisplay = stx.matchWith({
512 <H3 className={'mt-8'}>{'Signed Transaction'}</H3>
513 <Code>{JSON.stringify(renderSignedTx(tx.value), null, 2)}</Code>
518 const confirmButton = (
521 disabled={!canApprove}
522 onToggle={setUserApproval}
524 label="I approve this transaction and wish to send."
528 const sending = txStatus !== SUBMISSION_STATES.PROMPT;
529 const sendTxnSpinner = sending ? '' : 'hide';
530 const sendTxnButton = (
532 prop-size={'xl wide'}
534 disabled={!canSend || sending}
536 <span className="relative">
537 <span className={`btn-spinner ${sendTxnSpinner}`} />
543 const sendDialogue = web3.matchWith({
553 const txnErrorDialogue = Nothing.hasInstance(state.txError) ? (
556 <Warning className={'mt-8'}>
557 <H3 style={{ marginTop: 0, paddingTop: 0 }}>
558 {'There was an error sending your transaction.'}
560 {state.txError.value}
569 {gasPriceRangeDialogue}
570 {toggleGasDetailsDialogue}
578 {onlineParamsDialogue}
591export default compose(
593 withTxnConfirmations,
597)(StatelessTransaction);
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