@@ -30,7 +30,16 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
30 const _web3 = need.web3(web3);
30 const _web3 = need.web3(web3);
31 const _wallet = need.wallet(wallet);
31 const _wallet = need.wallet(wallet);
33− const [error, setError] = useState();
33+ const [error, _setError] = useState();
34+ const setError = useCallback(
38+ console.error(error);
34 const [state, setState] = useState(STATE.NONE);
43 const [state, setState] = useState(STATE.NONE);
35 const [chainId, setChainId] = useState();
44 const [chainId, setChainId] = useState();
36 const [nonce, setNonce] = useState();
45 const [nonce, setNonce] = useState();
@@ -38,6 +47,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
38 const [gasLimit] = useState(initialGasLimit);
47 const [gasLimit] = useState(initialGasLimit);
39 const [unsignedTransaction, setUnsignedTransaction] = useState();
48 const [unsignedTransaction, setUnsignedTransaction] = useState();
40 const [signedTransaction, setSignedTransaction] = useState();
49 const [signedTransaction, setSignedTransaction] = useState();
50+ const [txHash, setTxHash] = useState();
42 const initializing = nonce === undefined || chainId === undefined;
52 const initializing = nonce === undefined || chainId === undefined;
43 const constructed = !!unsignedTransaction;
53 const constructed = !!unsignedTransaction;
@@ -55,6 +65,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
56 const generateAndSign = useCallback(async () => {
66 const generateAndSign = useCallback(async () => {
58 // TODO: set varaibles
69 // TODO: set varaibles
59 // txn.gas = toHex(gasLimit);
70 // txn.gas = toHex(gasLimit);
60 // txn.gasPrice = toHex(toWei(gasPrice, 'gwei'));
71 // txn.gasPrice = toHex(toWei(gasPrice, 'gwei'));
@@ -64,8 +75,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
64 walletType,
75 walletType,
65 walletHdPath,
76 walletHdPath,
66 networkType,
77 networkType,
67− txn: Just(unsignedTransaction),
78+ txn: unsignedTransaction,
68− setStx: () => {}, // TODO: remove
@@ -84,25 +94,15 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
85 networkType,
95 networkType,
87 unsignedTransaction,
98 unsignedTransaction,
88 walletHdPath,
99 walletHdPath,
89 walletType,
100 walletType,
92− const waitForConfirmation = useCallback(
95− await waitForTransactionConfirm(_web3, txHash);
96− setState(STATE.CONFIRMED);
104 const broadcast = useCallback(async () => {
103 const broadcast = useCallback(async () => {
105+ setError(undefined);
106 const txHash = await sendSignedTransaction(
106 const txHash = await sendSignedTransaction(
108 signedTransaction,
108 signedTransaction,
@@ -110,22 +110,27 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
112 setState(STATE.BROADCASTED);
112 setState(STATE.BROADCASTED);
113− waitForConfirmation(txHash);
113+ setTxHash(txHash);
115+ await waitForTransactionConfirm(_web3, txHash);
117+ setState(STATE.CONFIRMED);
114 } catch (error) {
118 } catch (error) {
115 setError(error);
119 setError(error);
117− }, [_web3, signedTransaction, waitForConfirmation]);
121+ }, [_web3, setError, signedTransaction]);
119 const reset = useCallback(() => {
123 const reset = useCallback(() => {
120 setState(STATE.NONE);
124 setState(STATE.NONE);
121 setError(undefined);
125 setError(undefined);
122 setGasPrice(0);
126 setGasPrice(0);
123 setSignedTransaction(undefined);
127 setSignedTransaction(undefined);
124− }, [setState]);
128+ }, [setError]);
126 useEffect(() => {
130 useEffect(() => {
127 (async () => {
131 (async () => {
133+ setError(undefined);
129 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
134 const [nonce, chainId, estimatedGasPrice] = await Promise.all([
130 _web3.eth.getTransactionCount(_wallet.address),
135 _web3.eth.getTransactionCount(_wallet.address),
131 _web3.eth.net.getId(),
136 _web3.eth.net.getId(),
@@ -139,7 +144,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
139 setError(error);
144 setError(error);
142− }, [_wallet.address, _web3.eth]);
147+ }, [_wallet.address, _web3.eth, setError]);
145 initializing,
150 initializing,
@@ -154,6 +159,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
156 inputsLocked,
161 inputsLocked,
158 constructed,
164 constructed,
@@ -165,6 +171,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
167 setGasPrice,
173 setGasPrice,