1import { useCallback, useState } from 'react';
2import { fromWei } from 'web3-utils';
4import { useNetwork } from 'store/network';
5import { useWallet } from 'store/wallet';
10 waitForTransactionConfirm,
12import * as need from './need';
13import { GAS_LIMITS } from './constants';
14import useLifecycle from './useLifecycle';
19 BROADCASTED: 'BROADCASTED',
20 CONFIRMED: 'CONFIRMED',
24 * manage the state around sending and confirming an ethereum transaction
25 * @param {number} initialGasLimit
27export default function useEthereumTransaction(
28 initialGasLimit = GAS_LIMITS.DEFAULT,
31 const { wallet, walletType, walletHdPath } = useWallet();
32 const { web3, networkType } = useNetwork();
33 const _web3 = need.web3(web3);
34 const _wallet = need.wallet(wallet);
36 const [error, _setError] = useState();
37 const setError = useCallback(
46 const [state, setState] = useState(STATE.NONE);
47 const [chainId, setChainId] = useState();
48 const [nonce, setNonce] = useState();
49 const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
50 const [averageGasPrice, setAverageGasPrice] = useState(gasPrice); // gwei
51 const [gasLimit] = useState(initialGasLimit);
52 const [unsignedTransaction, setUnsignedTransaction] = useState();
53 const [signedTransaction, setSignedTransaction] = useState();
54 const [txHash, setTxHash] = useState();
56 const initializing = nonce === undefined || chainId === undefined;
57 const constructed = !!unsignedTransaction;
58 const isDefaultState = state === STATE.NONE;
59 const signed = state === STATE.SIGNED;
60 const broadcasted = state === STATE.BROADCASTED;
61 const confirmed = state === STATE.CONFIRMED;
63 // disable the inputs when:
64 const inputsLocked = signed || broadcasted || confirmed;
67 const canSign = !initializing && constructed;
69 const construct = useCallback(txn => setUnsignedTransaction(txn), [
70 setUnsignedTransaction,
73 const generateAndSign = useCallback(async () => {
77 const txn = await signTransaction({
82 txn: unsignedTransaction,
85 gasPrice: gasPrice.toFixed(0),
89 setSignedTransaction(txn);
90 setState(STATE.SIGNED);
107 const broadcast = useCallback(async () => {
110 const txHash = await sendSignedTransaction(
113 /* doubtNonceError */ false
116 setState(STATE.BROADCASTED);
119 await waitForTransactionConfirm(_web3, txHash);
121 setState(STATE.CONFIRMED);
125 }, [_web3, setError, signedTransaction]);
127 const reset = useCallback(() => {
128 setUnsignedTransaction(undefined);
129 setTxHash(undefined);
130 setSignedTransaction(undefined);
131 setGasPrice(averageGasPrice);
132 setState(STATE.NONE);
134 }, [averageGasPrice, setError]);
136 const resetGasPrice = useCallback(() => setGasPrice(averageGasPrice), [
145 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
146 _web3.eth.getTransactionCount(_wallet.address),
147 _web3.eth.net.getId(),
148 _web3.eth.getGasPrice(),
153 const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
154 setAverageGasPrice(gasPrice);
155 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