@@ -1,4 +1,5 @@
1−import { useCallback, useEffect, useState } from 'react';
1+import { useCallback, useState } from 'react';
2+import { fromWei } from 'web3-utils';
23
34import { useNetwork } from 'store/network';
45import { useWallet } from 'store/wallet';
@@ -9,7 +10,8 @@ import {
910 waitForTransactionConfirm,
1011} from './txn';
1112import * as need from './need';
12−import { fromWei } from 'web3-utils';
13+import { GAS_LIMITS } from './constants';
14+import useLifecycle from './useLifecycle';
1315
1416const STATE = {
1517 NONE: 'NONE',
@@ -22,7 +24,10 @@ const STATE = {
2224 * manage the state around sending and confirming an ethereum transaction
2325 * @param {number} initialGasLimit
2426 */
25−export default function useEthereumTransaction(initialGasLimit = 0) {
27+export default function useEthereumTransaction(
28+ initialGasLimit = GAS_LIMITS.DEFAULT,
29+ initialGasPrice = 20
30+) {
2631 const { wallet, walletType, walletHdPath } = useWallet();
2732 const { web3, networkType } = useNetwork();
2833 const _web3 = need.web3(web3);
@@ -41,7 +46,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
4146 const [state, setState] = useState(STATE.NONE);
4247 const [chainId, setChainId] = useState();
4348 const [nonce, setNonce] = useState();
44− const [gasPrice, setGasPrice] = useState(20); // gwei
49+ const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
4550 const [gasLimit] = useState(initialGasLimit);
4651 const [unsignedTransaction, setUnsignedTransaction] = useState();
4752 const [signedTransaction, setSignedTransaction] = useState();
@@ -53,8 +58,10 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
5358 const broadcasted = state === STATE.BROADCASTED;
5459 const confirmed = state === STATE.CONFIRMED;
5560
61+ // disable the inputs when:
5662 const inputsLocked = signed || broadcasted || confirmed;
5763
64+ // we can sign when:
5865 const canSign = !initializing && constructed;
5966
6067 const construct = useCallback(txn => setUnsignedTransaction(txn), [
@@ -116,13 +123,15 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
116123 }, [_web3, setError, signedTransaction]);
117124
118125 const reset = useCallback(() => {
126+ setUnsignedTransaction(undefined);
127+ setTxHash(undefined);
128+ setSignedTransaction(undefined);
129+ setGasPrice(initialGasPrice);
119130 setState(STATE.NONE);
120131 setError(undefined);
121− setGasPrice(0);
122− setSignedTransaction(undefined);
123− }, [setError]);
132+ }, [initialGasPrice, setError]);
124133
125− useEffect(() => {
134+ useLifecycle(() => {
126135 (async () => {
127136 try {
128137 setError(undefined);
@@ -139,9 +148,9 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
139148 setError(error);
140149 }
141150 })();
142− }, [_wallet.address, _web3.eth, setError]);
151+ });
143152
144− return {
153+ const values = {
145154 initializing,
146155 construct,
147156 constructed,
@@ -154,24 +163,17 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
154163 reset,
155164 error,
156165 inputsLocked,
166+ //
157167 txHash,
158168 signedTransaction,
159− bind: {
160− constructed,
161− canSign,
162− generateAndSign,
163− signed,
164− broadcast,
165− broadcasted,
166− confirmed,
167− reset,
168− error,
169− gasPrice,
170− setGasPrice,
171− txHash,
172− nonce,
173− chainId,
174− signedTransaction,
175− },
169+ gasPrice,
170+ setGasPrice,
171+ nonce,
172+ chainId,
173+ };
174+
175+ return {
176+ ...values,
177+ bind: values,
176178 };
177179}