1import { useCallback, useState } from 'react';
2import { Just } from 'folktale/maybe';
3import { fromWei, toWei } from 'web3-utils';
5import { useNetwork } from 'store/network';
6import { useWallet } from 'store/wallet';
7import { usePointCursor } from 'store/pointCursor';
9import { GAS_LIMITS } from './constants';
12 sendSignedTransaction,
13 waitForTransactionConfirm,
16import * as need from './need';
17import useLifecycle from './useLifecycle';
18import { ensureFundsFor } from './tank';
23 BROADCASTED: 'BROADCASTED',
24 CONFIRMED: 'CONFIRMED',
28 * manage the state around sending and confirming an ethereum transaction
29 * @param {number} initialGasLimit
31export default function useEthereumTransaction(
32 initialGasLimit = GAS_LIMITS.DEFAULT,
35 const { wallet, walletType, walletHdPath } = useWallet();
36 const { web3, networkType } = useNetwork();
37 const { pointCursor } = usePointCursor();
39 const _web3 = need.web3(web3);
40 const _wallet = need.wallet(wallet);
42 const [error, _setError] = useState();
43 const setError = useCallback(
52 const [state, setState] = useState(STATE.NONE);
53 const [chainId, setChainId] = useState();
54 const [nonce, setNonce] = useState();
55 const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
56 const [suggestedGasPrice, setSuggestedGasPrice] = useState(gasPrice); // gwei
57 const [gasLimit] = useState(initialGasLimit);
58 const [unsignedTransaction, setUnsignedTransaction] = useState();
59 const [signedTransaction, setSignedTransaction] = useState();
60 const [txHash, setTxHash] = useState();
61 const [needFunds, setNeedFunds] = useState();
63 const initializing = nonce === undefined || chainId === undefined;
64 const constructed = !!unsignedTransaction;
65 const isDefaultState = state === STATE.NONE;
66 const signed = state === STATE.SIGNED;
67 const broadcasted = state === STATE.BROADCASTED;
68 const confirmed = state === STATE.CONFIRMED;
70 // disable the inputs when:
71 const inputsLocked = signed || broadcasted || confirmed;
74 const canSign = !initializing && constructed;
76 const construct = useCallback(txn => setUnsignedTransaction(txn), [
77 setUnsignedTransaction,
80 const unconstruct = useCallback(() => setUnsignedTransaction(undefined), [
81 setUnsignedTransaction,
84 const generateAndSign = useCallback(async () => {
88 const txn = await signTransaction({
93 txn: unsignedTransaction,
96 gasPrice: gasPrice.toFixed(0),
100 setSignedTransaction(txn);
101 setState(STATE.SIGNED);
118 const broadcast = useCallback(async () => {
122 const rawTx = hexify(signedTransaction.serialize());
123 const cost = toWei(gasPrice.toFixed(0), 'gwei') * gasLimit;
125 let usedTank = false;
126 // if this ethereum transaction is being executed by a specific point
127 // see if we can use the tank
128 if (Just.hasInstance(pointCursor)) {
129 usedTank = await ensureFundsFor(
135 (address, minBalance, balance) =>
136 setNeedFunds({ address, minBalance, balance }),
137 () => setNeedFunds(undefined)
141 const txHash = await sendSignedTransaction(
144 /* doubtNonceError */ usedTank
147 setState(STATE.BROADCASTED);
150 await waitForTransactionConfirm(_web3, txHash);
152 setState(STATE.CONFIRMED);
166 const reset = useCallback(() => {
167 setUnsignedTransaction(undefined);
168 setTxHash(undefined);
169 setSignedTransaction(undefined);
170 setGasPrice(suggestedGasPrice);
171 setState(STATE.NONE);
173 setNeedFunds(undefined);
174 }, [suggestedGasPrice, setError]);
176 const resetGasPrice = useCallback(() => setGasPrice(suggestedGasPrice), [
185 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
186 _web3.eth.getTransactionCount(_wallet.address),
187 _web3.eth.net.getId(),
188 _web3.eth.getGasPrice(),
193 const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
194 setSuggestedGasPrice(gasPrice);
195 setGasPrice(gasPrice);
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