@@ -1,4 +1,5 @@
1−import { useCallback, useEffect, useState } from 'react';
1+import { useCallback, useState } from 'react';
2+import { fromWei } from 'web3-utils';
3import { useNetwork } from 'store/network';
4import { useNetwork } from 'store/network';
4import { useWallet } from 'store/wallet';
5import { useWallet } from 'store/wallet';
@@ -9,7 +10,8 @@ import {
9 waitForTransactionConfirm,
10 waitForTransactionConfirm,
10} from './txn';
11} from './txn';
11import * as need from './need';
12import * as need from './need';
12−import { fromWei } from 'web3-utils';
13+import { GAS_LIMITS } from './constants';
14+import useLifecycle from './useLifecycle';
14const STATE = {
16const STATE = {
15 NONE: 'NONE',
17 NONE: 'NONE',
@@ -22,7 +24,10 @@ const STATE = {
22 * manage the state around sending and confirming an ethereum transaction
24 * manage the state around sending and confirming an ethereum transaction
23 * @param {number} initialGasLimit
25 * @param {number} initialGasLimit
25−export default function useEthereumTransaction(initialGasLimit = 0) {
27+export default function useEthereumTransaction(
28+ initialGasLimit = GAS_LIMITS.DEFAULT,
26 const { wallet, walletType, walletHdPath } = useWallet();
31 const { wallet, walletType, walletHdPath } = useWallet();
27 const { web3, networkType } = useNetwork();
32 const { web3, networkType } = useNetwork();
28 const _web3 = need.web3(web3);
33 const _web3 = need.web3(web3);
@@ -41,7 +46,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
41 const [state, setState] = useState(STATE.NONE);
46 const [state, setState] = useState(STATE.NONE);
42 const [chainId, setChainId] = useState();
47 const [chainId, setChainId] = useState();
43 const [nonce, setNonce] = useState();
48 const [nonce, setNonce] = useState();
44− const [gasPrice, setGasPrice] = useState(20); // gwei
49+ const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
45 const [gasLimit] = useState(initialGasLimit);
50 const [gasLimit] = useState(initialGasLimit);
46 const [unsignedTransaction, setUnsignedTransaction] = useState();
51 const [unsignedTransaction, setUnsignedTransaction] = useState();
47 const [signedTransaction, setSignedTransaction] = useState();
52 const [signedTransaction, setSignedTransaction] = useState();
@@ -53,8 +58,10 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
53 const broadcasted = state === STATE.BROADCASTED;
58 const broadcasted = state === STATE.BROADCASTED;
54 const confirmed = state === STATE.CONFIRMED;
59 const confirmed = state === STATE.CONFIRMED;
61+ // disable the inputs when:
56 const inputsLocked = signed || broadcasted || confirmed;
62 const inputsLocked = signed || broadcasted || confirmed;
58 const canSign = !initializing && constructed;
65 const canSign = !initializing && constructed;
60 const construct = useCallback(txn => setUnsignedTransaction(txn), [
67 const construct = useCallback(txn => setUnsignedTransaction(txn), [
@@ -116,13 +123,15 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
116 }, [_web3, setError, signedTransaction]);
123 }, [_web3, setError, signedTransaction]);
118 const reset = useCallback(() => {
125 const reset = useCallback(() => {
126+ setUnsignedTransaction(undefined);
127+ setTxHash(undefined);
128+ setSignedTransaction(undefined);
129+ setGasPrice(initialGasPrice);
119 setState(STATE.NONE);
130 setState(STATE.NONE);
120 setError(undefined);
131 setError(undefined);
122− setSignedTransaction(undefined);
123− }, [setError]);
132+ }, [initialGasPrice, setError]);
125− useEffect(() => {
134+ useLifecycle(() => {
126 (async () => {
135 (async () => {
128 setError(undefined);
137 setError(undefined);
@@ -139,9 +148,9 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
139 setError(error);
148 setError(error);
142− }, [_wallet.address, _web3.eth, setError]);
151+ });
144− return {
153+ const values = {
145 initializing,
154 initializing,
146 construct,
155 construct,
147 constructed,
156 constructed,
@@ -154,24 +163,17 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
156 inputsLocked,
165 inputsLocked,
158 signedTransaction,
168 signedTransaction,
169− gasPrice,
169+ gasPrice,
170− setGasPrice,
170+ setGasPrice,
173− chainId,
172+ chainId,