on 363a711 help sign in
bridge/src/lib/useEthereumTransaction.js
190 lines 4.6 kB history raw
1import { useCallback, useState } from 'react';
2import { fromWei } from 'web3-utils';
4import { useNetwork } from 'store/network';
5import { useWallet } from 'store/wallet';
7import {
8 signTransaction,
9 sendSignedTransaction,
10 waitForTransactionConfirm,
11} from './txn';
12import * as need from './need';
13import { GAS_LIMITS } from './constants';
14import useLifecycle from './useLifecycle';
16const STATE = {
17 NONE: 'NONE',
18 SIGNED: 'SIGNED',
19 BROADCASTED: 'BROADCASTED',
20 CONFIRMED: 'CONFIRMED',
21};
23/**
24 * manage the state around sending and confirming an ethereum transaction
25 * @param {number} initialGasLimit
26 */
27export default function useEthereumTransaction(
28 initialGasLimit = GAS_LIMITS.DEFAULT,
29 initialGasPrice = 20
30) {
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(
38 error => {
39 _setError(error);
40 if (error) {
41 console.error(error);
42 }
43 },
44 [_setError]
45 );
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;
66 // we can sign when:
67 const canSign = !initializing && constructed;
69 const construct = useCallback(txn => setUnsignedTransaction(txn), [
70 setUnsignedTransaction,
71 ]);
73 const generateAndSign = useCallback(async () => {
74 try {
75 setError(undefined);
77 const txn = await signTransaction({
78 wallet: _wallet,
79 walletType,
80 walletHdPath,
81 networkType,
82 txn: unsignedTransaction,
83 nonce,
84 chainId,
85 gasPrice: gasPrice.toFixed(0),
86 gasLimit,
87 });
89 setSignedTransaction(txn);
90 setState(STATE.SIGNED);
91 } catch (error) {
92 setError(error);
93 }
94 }, [
95 _wallet,
96 chainId,
97 gasLimit,
98 gasPrice,
99 networkType,
100 nonce,
101 setError,
102 unsignedTransaction,
103 walletHdPath,
104 walletType,
105 ]);
107 const broadcast = useCallback(async () => {
108 try {
109 setError(undefined);
110 const txHash = await sendSignedTransaction(
111 _web3,
112 signedTransaction,
113 /* doubtNonceError */ false
114 );
116 setState(STATE.BROADCASTED);
117 setTxHash(txHash);
119 await waitForTransactionConfirm(_web3, txHash);
121 setState(STATE.CONFIRMED);
122 } catch (error) {
123 setError(error);
124 }
125 }, [_web3, setError, signedTransaction]);
127 const reset = useCallback(() => {
128 setUnsignedTransaction(undefined);
129 setTxHash(undefined);
130 setSignedTransaction(undefined);
131 setGasPrice(averageGasPrice);
132 setState(STATE.NONE);
133 setError(undefined);
134 }, [averageGasPrice, setError]);
136 const resetGasPrice = useCallback(() => setGasPrice(averageGasPrice), [
137 setGasPrice,
138 averageGasPrice,
139 ]);
141 useLifecycle(() => {
142 (async () => {
143 try {
144 setError(undefined);
145 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
146 _web3.eth.getTransactionCount(_wallet.address),
147 _web3.eth.net.getId(),
148 _web3.eth.getGasPrice(),
149 ]);
151 setNonce(nonce);
152 setChainId(chainId);
153 const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
154 setAverageGasPrice(gasPrice);
155 setGasPrice(gasPrice);
156 } catch (error) {
157 setError(error);
158 }
159 })();
160 });
162 const values = {
163 isDefaultState,
164 initializing,
165 construct,
166 constructed,
167 canSign,
168 generateAndSign,
169 signed,
170 broadcast,
171 broadcasted,
172 confirmed,
173 reset,
174 error,
175 inputsLocked,
176 //
177 txHash,
178 signedTransaction,
179 gasPrice,
180 setGasPrice,
181 resetGasPrice,
182 nonce,
183 chainId,
184 };
186 return {
187 ...values,
188 bind: values,
189 };