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 [suggestedGasPrice, setSuggestedGasPrice] = 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 unconstruct = useCallback(() => setUnsignedTransaction(undefined), [
74 setUnsignedTransaction,
77 const generateAndSign = useCallback(async () => {
81 const txn = await signTransaction({
86 txn: unsignedTransaction,
89 gasPrice: gasPrice.toFixed(0),
93 setSignedTransaction(txn);
94 setState(STATE.SIGNED);
111 const broadcast = useCallback(async () => {
114 const txHash = await sendSignedTransaction(
117 /* doubtNonceError */ false
120 setState(STATE.BROADCASTED);
123 await waitForTransactionConfirm(_web3, txHash);
125 setState(STATE.CONFIRMED);
129 }, [_web3, setError, signedTransaction]);
131 const reset = useCallback(() => {
132 setUnsignedTransaction(undefined);
133 setTxHash(undefined);
134 setSignedTransaction(undefined);
135 setGasPrice(suggestedGasPrice);
136 setState(STATE.NONE);
138 }, [suggestedGasPrice, setError]);
140 const resetGasPrice = useCallback(() => setGasPrice(suggestedGasPrice), [
149 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
150 _web3.eth.getTransactionCount(_wallet.address),
151 _web3.eth.net.getId(),
152 _web3.eth.getGasPrice(),
157 const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
158 setSuggestedGasPrice(gasPrice);
159 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