@@ -27,86 +27,108 @@ export function wiresaw<const wiresawTransport extends Transport>(
27
27
...rest ,
28
28
// TODO: type `request` so we don't have to cast
29
29
async request ( req ) : ReturnType < EIP1193RequestFn > {
30
- if ( req . method === "eth_chainId" ) {
31
- if ( chainId != null ) return chainId ;
32
- if ( transport . fallbackEth ) {
33
- const { request : fallbackRequest } = transport . fallbackEth ( opts ) ;
34
- return ( chainId = await fallbackRequest ( req ) ) ;
30
+ try {
31
+ if ( req . method === "eth_chainId" ) {
32
+ if ( chainId != null ) return chainId ;
33
+ if ( transport . fallbackEth ) {
34
+ const { request : fallbackRequest } = transport . fallbackEth ( opts ) ;
35
+ return ( chainId = await fallbackRequest ( req ) ) ;
36
+ }
37
+ return ( chainId = await originalRequest ( req ) ) ;
35
38
}
36
- return ( chainId = await originalRequest ( req ) ) ;
37
- }
38
39
39
- if ( req . method === "eth_estimateGas" ) {
40
- return originalRequest ( { ...req , method : "wiresaw_estimateGas" } ) ;
41
- }
40
+ if ( req . method === "eth_estimateGas" ) {
41
+ return await originalRequest ( { ...req , method : "wiresaw_estimateGas" } ) ;
42
+ }
42
43
43
- if ( req . method === "eth_call" ) {
44
- return originalRequest ( { ...req , method : "wiresaw_call" } ) ;
45
- }
44
+ if ( req . method === "eth_call" ) {
45
+ return await originalRequest ( { ...req , method : "wiresaw_call" } ) ;
46
+ }
46
47
47
- if ( req . method === "eth_getTransactionCount" ) {
48
- return originalRequest ( { ...req , method : "wiresaw_getTransactionCount" } ) ;
49
- }
48
+ if ( req . method === "eth_getTransactionCount" ) {
49
+ return await originalRequest ( { ...req , method : "wiresaw_getTransactionCount" } ) ;
50
+ }
50
51
51
- if ( req . method === "eth_getTransactionReceipt" ) {
52
- return getTransactionReceipt ( ( req . params as [ Hex ] ) [ 0 ] ) ;
53
- }
52
+ if ( req . method === "eth_getTransactionReceipt" ) {
53
+ return await getTransactionReceipt ( ( req . params as [ Hex ] ) [ 0 ] ) ;
54
+ }
54
55
55
- if ( req . method === "eth_sendUserOperation" ) {
56
- const { userOpHash, txHash } = ( await originalRequest ( {
57
- ...req ,
58
- method : "wiresaw_sendUserOperation" ,
59
- } ) ) as WiresawSendUserOperationResult ;
60
- transactionHashes [ userOpHash ] = txHash ;
61
- return userOpHash ;
62
- }
56
+ if ( req . method === "eth_sendUserOperation" ) {
57
+ const { userOpHash, txHash } = ( await originalRequest ( {
58
+ ...req ,
59
+ method : "wiresaw_sendUserOperation" ,
60
+ } ) ) as WiresawSendUserOperationResult ;
61
+ transactionHashes [ userOpHash ] = txHash ;
62
+ return userOpHash ;
63
+ }
63
64
64
- if ( req . method === "eth_getUserOperationReceipt" ) {
65
- const userOpHash = ( req . params as [ Hex ] ) [ 0 ] ;
66
- const knownTransactionHash = transactionHashes [ userOpHash ] ;
67
- if ( knownTransactionHash ) {
68
- const transactionReceipt = await getTransactionReceipt ( knownTransactionHash ) ;
69
- if ( transactionReceipt ) {
70
- return getUserOperationReceipt ( userOpHash , transactionReceipt ) ;
65
+ if ( req . method === "eth_getUserOperationReceipt" ) {
66
+ const userOpHash = ( req . params as [ Hex ] ) [ 0 ] ;
67
+ const knownTransactionHash = transactionHashes [ userOpHash ] ;
68
+ if ( knownTransactionHash ) {
69
+ const transactionReceipt = await getTransactionReceipt ( knownTransactionHash ) ;
70
+ if ( transactionReceipt ) {
71
+ return getUserOperationReceipt ( userOpHash , transactionReceipt ) ;
72
+ }
73
+ }
74
+ if ( transport . fallbackBundler ) {
75
+ const { request : fallbackRequest } = transport . fallbackBundler ( opts ) ;
76
+ return await fallbackRequest ( req ) ;
71
77
}
72
78
}
73
- if ( transport . fallbackBundler ) {
74
- const { request : fallbackRequest } = transport . fallbackBundler ( opts ) ;
75
- return fallbackRequest ( req ) ;
76
- }
77
- }
78
79
79
- if ( req . method === "eth_estimateUserOperationGas" ) {
80
- try {
81
- return await estimateUserOperationGas ( {
82
- request : originalRequest ,
83
- params : req . params as never ,
84
- } ) ;
85
- } catch ( e ) {
86
- console . warn ( "[wiresaw] estimating user operation gas failed, falling back to bundler" , e ) ;
80
+ if ( req . method === "eth_estimateUserOperationGas" ) {
81
+ try {
82
+ return await estimateUserOperationGas ( {
83
+ request : originalRequest ,
84
+ params : req . params as never ,
85
+ } ) ;
86
+ } catch ( e ) {
87
+ console . warn ( "[wiresaw] estimating user operation gas failed, falling back to bundler" , e ) ;
88
+ }
89
+
90
+ if ( transport . fallbackBundler ) {
91
+ const { request : fallbackRequest } = transport . fallbackBundler ( opts ) ;
92
+ return await fallbackRequest ( req ) ;
93
+ }
87
94
}
88
95
89
- if ( transport . fallbackBundler ) {
90
- const { request : fallbackRequest } = transport . fallbackBundler ( opts ) ;
91
- return fallbackRequest ( req ) ;
96
+ // Fallback to regular RPC for methods that don't require wiresaw
97
+ if (
98
+ req . method === "eth_blockNumber" ||
99
+ req . method === "eth_getBlockByNumber" ||
100
+ req . method === "eth_maxPriorityFeePerGas"
101
+ ) {
102
+ if ( transport . fallbackEth ) {
103
+ const { request : fallbackRequest } = transport . fallbackEth ( opts ) ;
104
+ return await fallbackRequest ( req ) ;
105
+ }
106
+ return await originalRequest ( req ) ;
92
107
}
93
- }
94
108
95
- // Fallback to regular RPC for methods that don't require wiresaw
96
- if (
97
- req . method === "eth_blockNumber" ||
98
- req . method === "eth_getBlockByNumber" ||
99
- req . method === "eth_maxPriorityFeePerGas"
100
- ) {
109
+ return await originalRequest ( req ) ;
110
+ } catch ( e ) {
111
+ console . warn ( "[wiresaw] request error" , e ) ;
112
+ const bundlerMethods = [
113
+ "eth_estimateUserOperationGas" ,
114
+ "eth_sendUserOperation" ,
115
+ "eth_getUserOperationReceipt" ,
116
+ ] ;
117
+ if ( bundlerMethods . includes ( req . method ) ) {
118
+ if ( transport . fallbackBundler ) {
119
+ const { request : fallbackRequest } = transport . fallbackBundler ( opts ) ;
120
+ console . warn ( "[wiresaw] falling back to bundler rpc" , req ) ;
121
+ return fallbackRequest ( req ) ;
122
+ }
123
+ }
101
124
if ( transport . fallbackEth ) {
102
125
const { request : fallbackRequest } = transport . fallbackEth ( opts ) ;
126
+ console . warn ( "[wiresaw] falling back to eth rpc" , req ) ;
103
127
return fallbackRequest ( req ) ;
104
128
}
105
- return originalRequest ( req ) ;
129
+ throw e ;
106
130
}
107
131
108
- return originalRequest ( req ) ;
109
-
110
132
async function getTransactionReceipt ( hash : Hex ) : Promise < RpcTransactionReceipt | undefined > {
111
133
// Return cached receipt if available
112
134
if ( transactionReceipts [ hash ] ) return transactionReceipts [ hash ] ;
0 commit comments