@@ -30,7 +30,16 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
3030 const _web3 = need.web3(web3);
3131 const _wallet = need.wallet(wallet);
3232
33− const [error, setError] = useState();
33+ const [error, _setError] = useState();
34+ const setError = useCallback(
35+ error => {
36+ _setError(error);
37+ if (error) {
38+ console.error(error);
39+ }
40+ },
41+ [_setError]
42+ );
3443 const [state, setState] = useState(STATE.NONE);
3544 const [chainId, setChainId] = useState();
3645 const [nonce, setNonce] = useState();
@@ -38,6 +47,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
3847 const [gasLimit] = useState(initialGasLimit);
3948 const [unsignedTransaction, setUnsignedTransaction] = useState();
4049 const [signedTransaction, setSignedTransaction] = useState();
50+ const [txHash, setTxHash] = useState();
4151
4252 const initializing = nonce === undefined || chainId === undefined;
4353 const constructed = !!unsignedTransaction;
@@ -55,6 +65,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
5565
5666 const generateAndSign = useCallback(async () => {
5767 try {
68+ setError(undefined);
5869 // TODO: set varaibles
5970 // txn.gas = toHex(gasLimit);
6071 // txn.gasPrice = toHex(toWei(gasPrice, 'gwei'));
@@ -64,8 +75,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
6475 walletType,
6576 walletHdPath,
6677 networkType,
67− txn: Just(unsignedTransaction),
68− setStx: () => {}, // TODO: remove
78+ txn: unsignedTransaction,
6979 nonce,
7080 chainId,
7181 gasPrice,
@@ -84,25 +94,15 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
8494 gasPrice,
8595 networkType,
8696 nonce,
97+ setError,
8798 unsignedTransaction,
8899 walletHdPath,
89100 walletType,
90101 ]);
91102
92− const waitForConfirmation = useCallback(
93− async txHash => {
94− try {
95− await waitForTransactionConfirm(_web3, txHash);
96− setState(STATE.CONFIRMED);
97− } catch (error) {
98− setError(error);
99− }
100− },
101− [_web3]
102− );
103−
104103 const broadcast = useCallback(async () => {
105104 try {
105+ setError(undefined);
106106 const txHash = await sendSignedTransaction(
107107 _web3,
108108 signedTransaction,
@@ -110,22 +110,27 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
110110 );
111111
112112 setState(STATE.BROADCASTED);
113− waitForConfirmation(txHash);
113+ setTxHash(txHash);
114+
115+ await waitForTransactionConfirm(_web3, txHash);
116+
117+ setState(STATE.CONFIRMED);
114118 } catch (error) {
115119 setError(error);
116120 }
117− }, [_web3, signedTransaction, waitForConfirmation]);
121+ }, [_web3, setError, signedTransaction]);
118122
119123 const reset = useCallback(() => {
120124 setState(STATE.NONE);
121125 setError(undefined);
122126 setGasPrice(0);
123127 setSignedTransaction(undefined);
124− }, [setState]);
128+ }, [setError]);
125129
126130 useEffect(() => {
127131 (async () => {
128132 try {
133+ setError(undefined);
129134 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
130135 _web3.eth.getTransactionCount(_wallet.address),
131136 _web3.eth.net.getId(),
@@ -139,7 +144,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
139144 setError(error);
140145 }
141146 })();
142− }, [_wallet.address, _web3.eth]);
147+ }, [_wallet.address, _web3.eth, setError]);
143148
144149 return {
145150 initializing,
@@ -154,6 +159,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
154159 reset,
155160 error,
156161 inputsLocked,
162+ txHash,
157163 bind: {
158164 constructed,
159165 canSign,
@@ -165,6 +171,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
165171 reset,
166172 error,
167173 setGasPrice,
174+ txHash,
168175 },
169176 };
170177}