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 [gasLimit] = useState(initialGasLimit);
51 const [unsignedTransaction, setUnsignedTransaction] = useState();
52 const [signedTransaction, setSignedTransaction] = useState();
53 const [txHash, setTxHash] = useState();
55 const initializing = nonce === undefined || chainId === undefined;
56 const constructed = !!unsignedTransaction;
57 const signed = state === STATE.SIGNED;
58 const broadcasted = state === STATE.BROADCASTED;
59 const confirmed = state === STATE.CONFIRMED;
61 // disable the inputs when:
62 const inputsLocked = signed || broadcasted || confirmed;
65 const canSign = !initializing && constructed;
67 const construct = useCallback(txn => setUnsignedTransaction(txn), [
68 setUnsignedTransaction,
71 const generateAndSign = useCallback(async () => {
75 const txn = await signTransaction({
80 txn: unsignedTransaction,
83 gasPrice: gasPrice.toFixed(0),
87 setSignedTransaction(txn);
88 setState(STATE.SIGNED);
105 const broadcast = useCallback(async () => {
108 const txHash = await sendSignedTransaction(
111 /* doubtNonceError */ false
114 setState(STATE.BROADCASTED);
117 await waitForTransactionConfirm(_web3, txHash);
119 setState(STATE.CONFIRMED);
123 }, [_web3, setError, signedTransaction]);
125 const reset = useCallback(() => {
126 setUnsignedTransaction(undefined);
127 setTxHash(undefined);
128 setSignedTransaction(undefined);
129 setGasPrice(initialGasPrice);
130 setState(STATE.NONE);
132 }, [initialGasPrice, setError]);
138 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
139 _web3.eth.getTransactionCount(_wallet.address),
140 _web3.eth.net.getId(),
141 _web3.eth.getGasPrice(),
146 setGasPrice(parseInt(fromWei(estimatedGasPrice, 'gwei'), 10));
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