|
1 | 1 | import { Web3Provider } from '@ethersproject/providers'; |
2 | | -import { useContext } from 'react'; |
3 | 2 | import { RouteResponse } from '@0xsquid/squid-types'; |
4 | 3 | import { Squid } from '@0xsquid/sdk'; |
5 | 4 | import { ethers } from 'ethers'; |
6 | | -import { Environment } from '@imtbl/config'; |
7 | 5 |
|
8 | 6 | import { StatusResponse } from '@0xsquid/sdk/dist/types'; |
9 | 7 | import { Flow } from '@imtbl/metrics'; |
10 | 8 | import { EIP6963ProviderInfo } from '@imtbl/checkout-sdk'; |
11 | 9 | import { isSquidNativeToken } from '../functions/isSquidNativeToken'; |
12 | | -import { useError } from './useError'; |
13 | | -import { AddTokensError, AddTokensErrorTypes } from '../../../widgets/add-tokens/types'; |
14 | | -import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext'; |
15 | | -import { sendAddTokensFailedEvent } from '../../../widgets/add-tokens/AddTokensWidgetEvents'; |
16 | 10 | import { retry } from '../../retry'; |
17 | 11 | import { withMetricsAsync } from '../../metrics'; |
18 | 12 | import { useAnalytics, UserJourney } from '../../../context/analytics-provider/SegmentAnalyticsProvider'; |
19 | 13 | import { isRejectedError } from '../../../functions/errorType'; |
20 | 14 |
|
21 | 15 | const TRANSACTION_NOT_COMPLETED = 'transaction not completed'; |
22 | 16 |
|
23 | | -export const useExecute = (contextId: string, environment: Environment) => { |
24 | | - const { showErrorHandover } = useError(environment); |
| 17 | +export const useExecute = ( |
| 18 | + userJourney: UserJourney, |
| 19 | + onTransactionError?: (err: unknown) => void, |
| 20 | +) => { |
25 | 21 | const { user } = useAnalytics(); |
26 | | - const { |
27 | | - eventTargetState: { eventTarget }, |
28 | | - } = useContext(EventTargetContext); |
29 | 22 |
|
30 | 23 | const waitForReceipt = async ( |
31 | 24 | provider: Web3Provider, |
@@ -59,83 +52,6 @@ export const useExecute = (contextId: string, environment: Environment) => { |
59 | 52 | return result; |
60 | 53 | }; |
61 | 54 |
|
62 | | - const handleTransactionError = (err: unknown) => { |
63 | | - const reason = `${ |
64 | | - (err as any)?.reason || (err as any)?.message || '' |
65 | | - }`.toLowerCase(); |
66 | | - |
67 | | - let errorType = AddTokensErrorTypes.WALLET_FAILED; |
68 | | - |
69 | | - if (reason.includes('failed') && reason.includes('open confirmation')) { |
70 | | - errorType = AddTokensErrorTypes.WALLET_POPUP_BLOCKED; |
71 | | - } |
72 | | - |
73 | | - if (reason.includes('rejected') && reason.includes('user')) { |
74 | | - errorType = AddTokensErrorTypes.WALLET_REJECTED; |
75 | | - } |
76 | | - |
77 | | - if ( |
78 | | - reason.includes('failed to submit') |
79 | | - && reason.includes('highest gas limit') |
80 | | - ) { |
81 | | - errorType = AddTokensErrorTypes.WALLET_REJECTED_NO_FUNDS; |
82 | | - } |
83 | | - |
84 | | - if ( |
85 | | - reason.includes('status failed') |
86 | | - || reason.includes('transaction failed') |
87 | | - ) { |
88 | | - errorType = AddTokensErrorTypes.TRANSACTION_FAILED; |
89 | | - sendAddTokensFailedEvent(eventTarget, errorType); |
90 | | - } |
91 | | - |
92 | | - if ( |
93 | | - reason.includes('unrecognized chain') |
94 | | - || reason.includes('unrecognized chain') |
95 | | - ) { |
96 | | - errorType = AddTokensErrorTypes.UNRECOGNISED_CHAIN; |
97 | | - } |
98 | | - |
99 | | - const error: AddTokensError = { |
100 | | - type: errorType, |
101 | | - data: { error: err }, |
102 | | - }; |
103 | | - |
104 | | - showErrorHandover(errorType, { contextId, error }); |
105 | | - }; |
106 | | - |
107 | | - // @TODO: Move to util function |
108 | | - const checkProviderChain = async ( |
109 | | - provider: Web3Provider, |
110 | | - chainId: string, |
111 | | - ): Promise<boolean> => { |
112 | | - if (!provider.provider.request) { |
113 | | - throw new Error('provider does not have request method'); |
114 | | - } |
115 | | - try { |
116 | | - const fromChainHex = `0x${parseInt(chainId, 10).toString(16)}`; |
117 | | - const providerChainId = await provider.provider.request({ |
118 | | - method: 'eth_chainId', |
119 | | - }); |
120 | | - |
121 | | - if (fromChainHex !== providerChainId) { |
122 | | - await provider.provider.request({ |
123 | | - method: 'wallet_switchEthereumChain', |
124 | | - params: [ |
125 | | - { |
126 | | - chainId: fromChainHex, |
127 | | - }, |
128 | | - ], |
129 | | - }); |
130 | | - return true; |
131 | | - } |
132 | | - return true; |
133 | | - } catch (error) { |
134 | | - handleTransactionError(error); |
135 | | - return false; |
136 | | - } |
137 | | - }; |
138 | | - |
139 | 55 | const getAllowance = async ( |
140 | 56 | provider: Web3Provider, |
141 | 57 | routeResponse: RouteResponse, |
@@ -164,7 +80,7 @@ export const useExecute = (contextId: string, environment: Environment) => { |
164 | 80 |
|
165 | 81 | return ethers.constants.MaxUint256; // no approval is needed for native tokens |
166 | 82 | } catch (error) { |
167 | | - showErrorHandover(AddTokensErrorTypes.DEFAULT, { contextId, error }); |
| 83 | + onTransactionError?.(error); |
168 | 84 | return undefined; |
169 | 85 | } |
170 | 86 | }; |
@@ -220,14 +136,14 @@ export const useExecute = (contextId: string, environment: Environment) => { |
220 | 136 | if (!isSquidNativeToken(routeResponse?.route?.params.fromToken)) { |
221 | 137 | return await withMetricsAsync( |
222 | 138 | (flow) => callApprove(flow, fromProviderInfo, provider, routeResponse), |
223 | | - `${UserJourney.ADD_TOKENS}_Approve`, |
| 139 | + `${userJourney}_Approve`, |
224 | 140 | await getAnonymousId(), |
225 | 141 | (error) => (isRejectedError(error) ? 'rejected' : ''), |
226 | 142 | ); |
227 | 143 | } |
228 | 144 | return undefined; |
229 | 145 | } catch (error) { |
230 | | - handleTransactionError(error); |
| 146 | + onTransactionError?.(error); |
231 | 147 | return undefined; |
232 | 148 | } |
233 | 149 | }; |
@@ -260,12 +176,12 @@ export const useExecute = (contextId: string, environment: Environment) => { |
260 | 176 | try { |
261 | 177 | return await withMetricsAsync( |
262 | 178 | (flow) => callExecute(flow, squid, fromProviderInfo, provider, routeResponse), |
263 | | - `${UserJourney.ADD_TOKENS}_Execute`, |
| 179 | + `${userJourney}_Execute`, |
264 | 180 | await getAnonymousId(), |
265 | 181 | (error) => (isRejectedError(error) ? 'rejected' : ''), |
266 | 182 | ); |
267 | 183 | } catch (error) { |
268 | | - handleTransactionError(error); |
| 184 | + onTransactionError?.(error); |
269 | 185 | return undefined; |
270 | 186 | } |
271 | 187 | }; |
@@ -307,13 +223,12 @@ export const useExecute = (contextId: string, environment: Environment) => { |
307 | 223 | }, |
308 | 224 | ); |
309 | 225 | } catch (error) { |
310 | | - handleTransactionError(error); |
| 226 | + onTransactionError?.(error); |
311 | 227 | return undefined; |
312 | 228 | } |
313 | 229 | }; |
314 | 230 |
|
315 | 231 | return { |
316 | | - checkProviderChain, |
317 | 232 | getAllowance, |
318 | 233 | approve, |
319 | 234 | execute, |
|
0 commit comments