1import { useCallback, useEffect, useState } from 'react';
2import { Just } from 'folktale/maybe';
4import { useNetwork } from 'store/network';
5import { useWallet } from 'store/wallet';
10 waitForTransactionConfirm,
13import * as need from './need';
14import { toHex, toWei, fromWei } from 'web3-utils';
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(initialGasLimit = 0) {
28 const { wallet, walletType, walletHdPath } = useWallet();
29 const { web3, networkType } = useNetwork();
30 const _web3 = need.web3(web3);
31 const _wallet = need.wallet(wallet);
33 const [error, setError] = useState();
34 const [state, setState] = useState(STATE.NONE);
35 const [chainId, setChainId] = useState();
36 const [nonce, setNonce] = useState();
37 const [gasPrice, setGasPrice] = useState(20); // gwei
38 const [gasLimit] = useState(initialGasLimit);
39 const [unsignedTransaction, setUnsignedTransaction] = useState();
40 const [signedTransaction, setSignedTransaction] = useState();
42 const initializing = nonce === undefined || chainId === undefined;
43 const constructed = !!unsignedTransaction;
44 const signed = state === STATE.SIGNED;
45 const broadcasted = state === STATE.BROADCASTED;
46 const confirmed = state === STATE.CONFIRMED;
48 const inputsLocked = signed || broadcasted || confirmed;
50 const canSign = !initializing && constructed;
52 const construct = useCallback(txn => setUnsignedTransaction(txn), [
53 setUnsignedTransaction,
56 const generateAndSign = useCallback(async () => {
58 // TODO: set varaibles
59 // txn.gas = toHex(gasLimit);
60 // txn.gasPrice = toHex(toWei(gasPrice, 'gwei'));
62 const txn = await signTransaction({
63 wallet, // wallet: _wallet,
67 txn: Just(unsignedTransaction),
68 setStx: () => {}, // TODO: remove
75 setSignedTransaction(txn);
76 setState(STATE.SIGNED);
92 const waitForConfirmation = useCallback(
95 await waitForTransactionConfirm(_web3, txHash);
96 setState(STATE.CONFIRMED);
104 const broadcast = useCallback(async () => {
106 const txHash = await sendSignedTransaction(
108 Just(signedTransaction),
109 /* doubtNonceError */ false
112 setState(STATE.BROADCASTED);
113 waitForConfirmation(txHash);
117 }, [_web3, signedTransaction, waitForConfirmation]);
119 const reset = useCallback(() => {
120 setState(STATE.NONE);
123 setSignedTransaction(undefined);
129 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
130 _web3.eth.getTransactionCount(_wallet.address),
131 _web3.eth.net.getId(),
132 _web3.eth.getGasPrice(),
137 setGasPrice(fromWei(estimatedGasPrice, 'gwei'));
142 }, [_wallet.address, _web3.eth]);
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