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 { withNetwork } from '../../store/network';
26import { withHistory } from '../../store/history';
27import { withWallet } from '../../store/wallet';
28import { withTxnCursor } from '../../store/txnCursor';
30const SUBMISSION_STATES = {
31 PROMPT: 'Send transaction',
32 PREPARING: 'Preparing...',
33 FUNDING: 'Finding transaction funding...',
34 SENDING: 'Sending transaction...',
37class StatelessTransaction extends React.Component {
43 gasLimit: GAS_LIMITS.DEFAULT.toString(),
44 showGasDetails: false,
51 txStatus: SUBMISSION_STATES.PROMPT,
55 this.createUnsignedTxn = this.createUnsignedTxn.bind(this);
56 this.setUserApproval = this.setUserApproval.bind(this);
57 this.setTxn = this.setTxn.bind(this);
58 this.setStx = this.setStx.bind(this);
59 this.sendTxn = this.sendTxn.bind(this);
60 this.setNonce = this.setNonce.bind(this);
61 this.setChainId = this.setChainId.bind(this);
62 this.setGasPrice = this.setGasPrice.bind(this);
63 this.setGasLimit = this.setGasLimit.bind(this);
64 this.rangeChange = this.rangeChange.bind(this);
65 this.toggleGasDetails = this.toggleGasDetails.bind(this);
66 this.handleChainUpdate = this.handleChainUpdate.bind(this);
70 const { props } = this;
72 const addr = need.addressFromWallet(props.wallet);
74 props.web3.matchWith({
77 const validWeb3 = w3.value;
79 const getTxMetadata = [
80 validWeb3.eth.getTransactionCount(addr),
81 validWeb3.eth.net.getId(),
82 validWeb3.eth.getGasPrice(),
85 Promise.all(getTxMetadata).then(r => {
89 gasPrice: fromWei(r[2], 'gwei'),
92 this.setState({ ...txMetadata });
98 handleChainUpdate(chainId) {
99 if (chainId === 'custom') {
114 const { state } = this;
115 this.setState({ userApproval: !state.userApproval });
120 showGasDetails: !this.state.showGasDetails,
132 this.setState({ txn });
135 createUnsignedTxn() {
136 const txn = this.props.createUnsignedTxn();
138 this.setState({ txn });
150 this.setState({ nonce });
154 setChainId(chainId) {
155 this.setState({ chainId });
159 setGasPrice(gasPrice) {
160 this.setState({ gasPrice });
165 this.setGasPrice(e.target.value);
168 setGasLimit(gasLimit) {
169 this.setState({ gasLimit });
181 const { props, state } = this;
182 const web3 = props.web3.value;
184 const stx = state.stx.matchWith({
185 Just: tx => tx.value,
187 throw new Error(BRIDGE_ERROR.MISSING_TXN);
191 if (props.beforeSend) {
193 this.setState({ txStatus: SUBMISSION_STATES.PREPARING });
194 const res = await props.beforeSend(stx);
195 if (res === false) throw new Error('beforeSend disallowed sending');
197 console.log('beforeSend error', e);
199 txStatus: SUBMISSION_STATES.PROMPT,
200 txError: Just('Something went wrong!'),
206 this.setState({ txStatus: SUBMISSION_STATES.FUNDING });
208 const rawTx = hexify(stx.serialize());
209 const cost = state.gasLimit * toWei(state.gasPrice, 'gwei');
211 const address = need.addressFromWallet(props.wallet);
212 let balance = await web3.eth.getBalance(address);
213 let hasBalance = balance >= cost;
215 let usedTank = false;
216 // if we need to, try and fund the transaction
218 usedTank = await this.ensureFundsFor(web3, address, cost, [rawTx]);
219 hasBalance = usedTank;
222 // if we still don't have sufficient balance, fail and tell the user
225 txStatus: SUBMISSION_STATES.PROMPT,
226 txError: Just(`Insufficient funds.
227 Address ${address} needs at least ${fromWei(cost.toString())} ETH,
228 currently has ${fromWei(balance.toString())} ETH.`),
231 // if we have the balance, proceed with submission
233 this.setState({ txStatus: SUBMISSION_STATES.SENDING });
235 sendSignedTransaction(web3, stx, usedTank)
237 props.setTxnCursor(Just(Ok(txHash)));
240 if (props.newNetworkSeed) {
241 props.setNetworkSeed(Just(props.newNetworkSeed));
242 routeData.promptKeyfile = true;
245 if (props.newRevision) {
246 props.setNetworkRevision(Just(props.newRevision));
249 props.history.popAndPush(ROUTE_NAMES.SENT_TRANSACTION, routeData);
253 txStatus: SUBMISSION_STATES.PROMPT,
260 // uses the gas tank to ensure funds, but if we have to ask the user
261 // for funding, just resolve instead of waiting
262 // returns true if the gas tank was used, false otherwise
263 ensureFundsFor(web3, address, cost, signedTxs) {
264 return new Promise((resolve, reject) => {
266 .ensureFundsFor(web3, null, address, cost, signedTxs, () => {
275 getChainTitle(chainId) {
282 '1337': 'Geth private chains - 1337',
292 title: this.getChainTitle('1'),
296 title: this.getChainTitle('2'),
300 title: this.getChainTitle('3'),
304 title: this.getChainTitle('4'),
308 title: this.getChainTitle('42'),
312 title: this.getChainTitle('1337'),
316 title: this.getChainTitle('custom'),
323 const { web3, canGenerate } = this.props;
349 const { state } = this;
351 const canSign = Just.hasInstance(txn);
352 const canApprove = Just.hasInstance(stx);
353 const canSend = Just.hasInstance(stx) && userApproval === true;
355 const chainOptions = this.getChainOptions();
357 const generateButtonColor = Nothing.hasInstance(txn) ? 'blue' : 'green';
359 const signerButtonColor = Nothing.hasInstance(stx) ? 'blue' : 'green';
361 const generateTxnButton = (
364 disabled={!canGenerate}
365 prop-color={generateButtonColor}
366 prop-size={'lg wide'}
367 onClick={createUnsignedTxn}>
368 {'Generate Transaction'}
372 const unsignedTxnDisplay = txn.matchWith({
376 <H3 className={'mt-8'}>{'Unsigned Transaction'}</H3>
377 <Code>{JSON.stringify(tx.value, null, 2)}</Code>
382 const gasPriceRangeDialogue = (
384 <div className="mt-12 flex space-between align-baseline">
386 <span>Gas Price:</span>
387 <span className="ml-4 text-700 text-sm">{gasPrice} gwei</span>
389 <div className="text-sm">
390 <span>Max transaction fee: </span>
391 <span className="text-700">
392 {(gasPrice * gasLimit) / 1000000000} eth
404 onChange={this.rangeChange}
407 <div className="flex space-between text-sm mb-8">
414 const toggleGasDetailsDialogue = (
415 <span className="text-link" onClick={toggleGasDetails}>
420 const gasPriceDialogue = (
422 className={'mono mt-4'}
424 prop-format={'innerLabel'}
426 onChange={setGasPrice}>
427 <InnerLabel>{'Gas Price (gwei)'}</InnerLabel>
431 const gasLimitDialogue = (
433 className={'mono mt-4'}
435 prop-format={'innerLabel'}
437 onChange={setGasLimit}>
438 <InnerLabel>{'Gas Limit'}</InnerLabel>
442 const nonceDialogue = (
444 className={'mono mt-4 mb-4'}
446 prop-format={'innerLabel'}
449 <InnerLabel>{'Nonce'}</InnerLabel>
453 const chainDialogueTitle = customChain
455 : this.getChainTitle(chainId);
456 const chainDialogue = (
461 options={chainOptions}
462 handleUpdate={handleChainUpdate}
463 currentSelectionTitle={chainDialogueTitle}
467 const customChainDialogue = !customChain ? null : (
469 className={'mono mt-4 mb-8'}
471 prop-format={'innerLabel'}
473 onChange={setChainId}>
474 <InnerLabel>{'Chain ID'}</InnerLabel>
478 const onlineParamsDialogue = web3.matchWith({
484 {customChainDialogue}
489 const signTxnButton = (
493 prop-size={'lg wide'}
494 prop-color={signerButtonColor}
498 await signTransaction({
501 wallet: this.props.wallet.value,
511 const signedTxnDisplay = stx.matchWith({
515 <H3 className={'mt-8'}>{'Signed Transaction'}</H3>
516 <Code>{JSON.stringify(renderSignedTx(tx.value), null, 2)}</Code>
521 const confirmButton = (
524 disabled={!canApprove}
525 onToggle={setUserApproval}
527 label="I approve this transaction and wish to send."
531 const sending = txStatus !== SUBMISSION_STATES.PROMPT;
532 const sendTxnSpinner = sending ? '' : 'hide';
533 const sendTxnButton = (
535 prop-size={'xl wide'}
537 disabled={!canSend || sending}
539 <span className="relative">
540 <span className={`btn-spinner ${sendTxnSpinner}`} />
546 const sendDialogue = web3.matchWith({
556 const txnErrorDialogue = Nothing.hasInstance(state.txError) ? (
559 <Warning className={'mt-8'}>
560 <H3 style={{ marginTop: 0, paddingTop: 0 }}>
561 {'There was an error sending your transaction.'}
563 {state.txError.value}
572 {gasPriceRangeDialogue}
573 {toggleGasDetailsDialogue}
581 {onlineParamsDialogue}
594export default compose(
599)(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