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';
19import useDeepEqualReference from './useDeepEqualReference';
24 BROADCASTED: 'BROADCASTED',
25 CONFIRMED: 'CONFIRMED',
29 * manage the state around sending and confirming an ethereum transaction
30 * @param {number} initialGasLimit
32export default function useEthereumTransaction(
33 initialGasLimit = GAS_LIMITS.DEFAULT,
36 const { wallet, walletType, walletHdPath } = useWallet();
37 const { web3, networkType } = useNetwork();
38 const { pointCursor } = usePointCursor();
40 const _web3 = need.web3(web3);
41 const _wallet = need.wallet(wallet);
43 const [error, _setError] = useState();
44 const setError = useCallback(
53 const [state, setState] = useState(STATE.NONE);
54 const [chainId, setChainId] = useState();
55 const [nonce, setNonce] = useState();
56 const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
57 const [suggestedGasPrice, setSuggestedGasPrice] = useState(gasPrice); // gwei
58 const [gasLimit] = useState(initialGasLimit);
59 const [unsignedTransaction, setUnsignedTransaction] = useState();
60 const [signedTransaction, setSignedTransaction] = useState();
61 const [txHash, setTxHash] = useState();
62 const [needFunds, setNeedFunds] = useState();
64 const initializing = nonce === undefined || chainId === undefined;
65 const constructed = !!unsignedTransaction;
66 const isDefaultState = state === STATE.NONE;
67 const signed = state === STATE.SIGNED;
68 const broadcasted = state === STATE.BROADCASTED;
69 const confirmed = state === STATE.CONFIRMED;
71 // disable the inputs when:
72 const inputsLocked = signed || broadcasted || confirmed;
75 const canSign = !initializing && constructed;
77 const construct = useCallback(txn => setUnsignedTransaction(txn), [
78 setUnsignedTransaction,
81 const unconstruct = useCallback(() => setUnsignedTransaction(undefined), [
82 setUnsignedTransaction,
85 const generateAndSign = useCallback(async () => {
89 const txn = await signTransaction({
94 txn: unsignedTransaction,
97 gasPrice: gasPrice.toFixed(0),
101 setSignedTransaction(txn);
102 setState(STATE.SIGNED);
119 const broadcast = useCallback(async () => {
123 const rawTx = hexify(signedTransaction.serialize());
124 const cost = toWei(gasPrice.toFixed(0), 'gwei') * gasLimit;
126 let usedTank = false;
127 // if this ethereum transaction is being executed by a specific point
128 // see if we can use the tank
129 if (Just.hasInstance(pointCursor)) {
130 usedTank = await ensureFundsFor(
136 (address, minBalance, balance) =>
137 setNeedFunds({ address, minBalance, balance }),
138 () => setNeedFunds(undefined)
142 const txHash = await sendSignedTransaction(
145 /* doubtNonceError */ usedTank
148 setState(STATE.BROADCASTED);
151 await waitForTransactionConfirm(_web3, txHash);
153 setState(STATE.CONFIRMED);
167 const reset = useCallback(() => {
168 setUnsignedTransaction(undefined);
169 setTxHash(undefined);
170 setSignedTransaction(undefined);
171 setGasPrice(suggestedGasPrice);
172 setState(STATE.NONE);
174 setNeedFunds(undefined);
175 }, [suggestedGasPrice, setError]);
177 const resetGasPrice = useCallback(() => setGasPrice(suggestedGasPrice), [
186 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
187 _web3.eth.getTransactionCount(_wallet.address),
188 _web3.eth.net.getId(),
189 _web3.eth.getGasPrice(),
194 const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
195 setSuggestedGasPrice(gasPrice);
196 setGasPrice(gasPrice);
203 const values = useDeepEqualReference({
229 return { ...values, bind: values };
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