@@ -47,21 +47,30 @@ pub struct Provider<P>(P, Option<Duration>);
47
47
#[ derive( Clone , Debug ) ]
48
48
pub struct EnsProvider < M > ( M , Option < Address > ) ;
49
49
50
+ #[ derive( Debug , Error ) ]
51
+ pub enum EnsError < M : Middleware > {
52
+ #[ error( "MiddlewareError: {0:?}" ) ]
53
+ MiddlewareError ( M :: Error ) ,
54
+ #[ error( transparent) ]
55
+ ProviderError ( #[ from] ProviderError ) ,
56
+ }
57
+
50
58
// The ENS Middleware overrides functions involving from/to and forwards everything
51
59
// else to the inner layer
52
60
#[ async_trait]
53
- impl < M : Middleware < Error = ProviderError > > Middleware for EnsProvider < M > {
54
- type Error = ProviderError ;
61
+ impl < M : Middleware > Middleware for EnsProvider < M > {
62
+ type Error = EnsError < M > ;
55
63
56
64
/// Returns the address that the `ens_name` resolves to (or None if not configured).
57
65
///
58
66
/// # Panics
59
67
///
60
68
/// If the bytes returned from the ENS registrar/resolver cannot be interpreted as
61
69
/// an address. This should theoretically never happen.
62
- async fn resolve_name ( & self , ens_name : & str ) -> Result < Address , ProviderError > {
63
- self . query_resolver ( ParamType :: Address , ens_name, ens:: ADDR_SELECTOR )
64
- . await
70
+ async fn resolve_name ( & self , ens_name : & str ) -> Result < Address , Self :: Error > {
71
+ Ok ( self
72
+ . query_resolver ( ParamType :: Address , ens_name, ens:: ADDR_SELECTOR )
73
+ . await ?)
65
74
}
66
75
67
76
////// Ethereum Naming Service
@@ -76,10 +85,11 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
76
85
///
77
86
/// If the bytes returned from the ENS registrar/resolver cannot be interpreted as
78
87
/// a string. This should theoretically never happen.
79
- async fn lookup_address ( & self , address : Address ) -> Result < String , ProviderError > {
88
+ async fn lookup_address ( & self , address : Address ) -> Result < String , Self :: Error > {
80
89
let ens_name = ens:: reverse_address ( address) ;
81
- self . query_resolver ( ParamType :: String , & ens_name, ens:: NAME_SELECTOR )
82
- . await
90
+ Ok ( self
91
+ . query_resolver ( ParamType :: String , & ens_name, ens:: NAME_SELECTOR )
92
+ . await ?)
83
93
}
84
94
85
95
/// Sends the transaction to the entire Ethereum network and returns the transaction's hash
@@ -88,7 +98,7 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
88
98
& self ,
89
99
mut tx : TransactionRequest ,
90
100
block : Option < BlockNumber > ,
91
- ) -> Result < TxHash , ProviderError > {
101
+ ) -> Result < TxHash , Self :: Error > {
92
102
if let Some ( ref to) = tx. to {
93
103
if let NameOrAddress :: Name ( ens_name) = to {
94
104
// resolve to an address
@@ -99,20 +109,26 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
99
109
}
100
110
}
101
111
102
- self . 0 . send_transaction ( tx, block) . await
112
+ self . 0
113
+ . send_transaction ( tx, block)
114
+ . await
115
+ . map_err ( EnsError :: MiddlewareError )
103
116
}
104
117
105
118
/// Returns the nonce of the address
106
119
async fn get_transaction_count < T : Into < NameOrAddress > + Send + Sync > (
107
120
& self ,
108
121
from : T ,
109
122
block : Option < BlockNumber > ,
110
- ) -> Result < U256 , ProviderError > {
123
+ ) -> Result < U256 , Self :: Error > {
111
124
let from = match from. into ( ) {
112
125
NameOrAddress :: Name ( ens_name) => self . resolve_name ( & ens_name) . await ?,
113
126
NameOrAddress :: Address ( addr) => addr,
114
127
} ;
115
- self . 0 . get_transaction_count ( from, block) . await
128
+ self . 0
129
+ . get_transaction_count ( from, block)
130
+ . await
131
+ . map_err ( EnsError :: MiddlewareError )
116
132
}
117
133
118
134
/// Get the storage of an address for a particular slot location
@@ -121,54 +137,72 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
121
137
from : T ,
122
138
location : H256 ,
123
139
block : Option < BlockNumber > ,
124
- ) -> Result < H256 , ProviderError > {
140
+ ) -> Result < H256 , Self :: Error > {
125
141
let from = match from. into ( ) {
126
142
NameOrAddress :: Name ( ens_name) => self . resolve_name ( & ens_name) . await ?,
127
143
NameOrAddress :: Address ( addr) => addr,
128
144
} ;
129
- self . 0 . get_storage_at ( from, location, block) . await
145
+ self . 0
146
+ . get_storage_at ( from, location, block)
147
+ . await
148
+ . map_err ( EnsError :: MiddlewareError )
130
149
}
131
150
132
151
/// Returns the deployed code at a given address
133
152
async fn get_code < T : Into < NameOrAddress > + Send + Sync > (
134
153
& self ,
135
154
at : T ,
136
155
block : Option < BlockNumber > ,
137
- ) -> Result < Bytes , ProviderError > {
156
+ ) -> Result < Bytes , Self :: Error > {
138
157
let at = match at. into ( ) {
139
158
NameOrAddress :: Name ( ens_name) => self . resolve_name ( & ens_name) . await ?,
140
159
NameOrAddress :: Address ( addr) => addr,
141
160
} ;
142
- self . 0 . get_code ( at, block) . await
161
+ self . 0
162
+ . get_code ( at, block)
163
+ . await
164
+ . map_err ( EnsError :: MiddlewareError )
143
165
}
144
166
145
167
/// Gets the latest block number via the `eth_BlockNumber` API
146
- async fn get_block_number ( & self ) -> Result < U64 , ProviderError > {
147
- self . 0 . get_block_number ( ) . await
168
+ async fn get_block_number ( & self ) -> Result < U64 , Self :: Error > {
169
+ self . 0
170
+ . get_block_number ( )
171
+ . await
172
+ . map_err ( EnsError :: MiddlewareError )
148
173
}
149
174
150
175
/// Gets the block at `block_hash_or_number` (transaction hashes only)
151
176
async fn get_block < T : Into < BlockId > + Send + Sync > (
152
177
& self ,
153
178
block_hash_or_number : T ,
154
- ) -> Result < Option < Block < TxHash > > , ProviderError > {
155
- self . 0 . get_block ( block_hash_or_number) . await
179
+ ) -> Result < Option < Block < TxHash > > , Self :: Error > {
180
+ self . 0
181
+ . get_block ( block_hash_or_number)
182
+ . await
183
+ . map_err ( EnsError :: MiddlewareError )
156
184
}
157
185
158
186
/// Gets the block at `block_hash_or_number` (full transactions included)
159
187
async fn get_block_with_txs < T : Into < BlockId > + Send + Sync > (
160
188
& self ,
161
189
block_hash_or_number : T ,
162
- ) -> Result < Option < Block < Transaction > > , ProviderError > {
163
- self . 0 . get_block_with_txs ( block_hash_or_number) . await
190
+ ) -> Result < Option < Block < Transaction > > , Self :: Error > {
191
+ self . 0
192
+ . get_block_with_txs ( block_hash_or_number)
193
+ . await
194
+ . map_err ( EnsError :: MiddlewareError )
164
195
}
165
196
166
197
async fn call (
167
198
& self ,
168
199
tx : & TransactionRequest ,
169
200
block : Option < BlockNumber > ,
170
- ) -> Result < Bytes , ProviderError > {
171
- self . 0 . call ( tx, block) . await
201
+ ) -> Result < Bytes , Self :: Error > {
202
+ self . 0
203
+ . call ( tx, block)
204
+ . await
205
+ . map_err ( EnsError :: MiddlewareError )
172
206
}
173
207
}
174
208
@@ -204,7 +238,7 @@ impl<M: Middleware> EnsProvider<M> {
204
238
param : ParamType ,
205
239
ens_name : & str ,
206
240
selector : Selector ,
207
- ) -> Result < T , ProviderError > {
241
+ ) -> Result < T , EnsError < M > > {
208
242
// Get the ENS address, prioritize the local override variable
209
243
let ens_addr = self . 1 . unwrap_or ( ens:: ENS_ADDRESS ) ;
210
244
@@ -213,18 +247,20 @@ impl<M: Middleware> EnsProvider<M> {
213
247
let data = self
214
248
. 0
215
249
. call ( & ens:: get_resolver ( ens_addr, ens_name) , None )
216
- . await ?;
250
+ . await
251
+ . map_err ( EnsError :: MiddlewareError ) ?;
217
252
218
253
let resolver_address: Address = decode_bytes ( ParamType :: Address , data) ;
219
254
if resolver_address == Address :: zero ( ) {
220
- return Err ( ProviderError :: EnsError ( ens_name. to_owned ( ) ) ) ;
255
+ return Err ( ProviderError :: EnsError ( ens_name. to_owned ( ) ) . into ( ) ) ;
221
256
}
222
257
223
258
// resolve
224
259
let data = self
225
260
. 0
226
261
. call ( & ens:: resolve ( resolver_address, selector, ens_name) , None )
227
- . await ?;
262
+ . await
263
+ . map_err ( EnsError :: MiddlewareError ) ?;
228
264
229
265
Ok ( decode_bytes ( param, data) )
230
266
}
@@ -239,9 +275,9 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
239
275
/// This will consume gas from the account that signed the transaction.
240
276
async fn send_transaction (
241
277
& self ,
242
- mut tx : TransactionRequest ,
243
- block : Option < BlockNumber > ,
244
- ) -> Result < TxHash , ProviderError > {
278
+ tx : TransactionRequest ,
279
+ _ : Option < BlockNumber > ,
280
+ ) -> Result < TxHash , Self :: Error > {
245
281
Ok ( self
246
282
. 0
247
283
. request ( "eth_sendTransaction" , [ tx] )
@@ -254,7 +290,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
254
290
// Functions for querying the state of the blockchain
255
291
256
292
/// Gets the latest block number via the `eth_BlockNumber` API
257
- async fn get_block_number ( & self ) -> Result < U64 , ProviderError > {
293
+ async fn get_block_number ( & self ) -> Result < U64 , Self :: Error > {
258
294
Ok ( self
259
295
. 0
260
296
. request ( "eth_blockNumber" , ( ) )
@@ -266,7 +302,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
266
302
async fn get_block < T : Into < BlockId > + Send + Sync > (
267
303
& self ,
268
304
block_hash_or_number : T ,
269
- ) -> Result < Option < Block < TxHash > > , ProviderError > {
305
+ ) -> Result < Option < Block < TxHash > > , Self :: Error > {
270
306
Ok ( self
271
307
. get_block_gen ( block_hash_or_number. into ( ) , false )
272
308
. await ?)
@@ -276,7 +312,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
276
312
async fn get_block_with_txs < T : Into < BlockId > + Send + Sync > (
277
313
& self ,
278
314
block_hash_or_number : T ,
279
- ) -> Result < Option < Block < Transaction > > , ProviderError > {
315
+ ) -> Result < Option < Block < Transaction > > , Self :: Error > {
280
316
Ok ( self
281
317
. get_block_gen ( block_hash_or_number. into ( ) , true )
282
318
. await ?)
@@ -287,7 +323,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
287
323
& self ,
288
324
from : T ,
289
325
block : Option < BlockNumber > ,
290
- ) -> Result < U256 , ProviderError > {
326
+ ) -> Result < U256 , Self :: Error > {
291
327
let from = utils:: serialize ( & from. into ( ) ) ;
292
328
let block = utils:: serialize ( & block. unwrap_or ( BlockNumber :: Latest ) ) ;
293
329
Ok ( self
@@ -303,7 +339,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
303
339
& self ,
304
340
tx : & TransactionRequest ,
305
341
block : Option < BlockNumber > ,
306
- ) -> Result < Bytes , ProviderError > {
342
+ ) -> Result < Bytes , Self :: Error > {
307
343
let tx = utils:: serialize ( tx) ;
308
344
let block = utils:: serialize ( & block. unwrap_or ( BlockNumber :: Latest ) ) ;
309
345
Ok ( self
@@ -319,7 +355,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
319
355
from : T ,
320
356
location : H256 ,
321
357
block : Option < BlockNumber > ,
322
- ) -> Result < H256 , ProviderError > {
358
+ ) -> Result < H256 , Self :: Error > {
323
359
let from = utils:: serialize ( & from. into ( ) ) ;
324
360
let location = utils:: serialize ( & location) ;
325
361
let block = utils:: serialize ( & block. unwrap_or ( BlockNumber :: Latest ) ) ;
@@ -335,7 +371,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
335
371
& self ,
336
372
at : T ,
337
373
block : Option < BlockNumber > ,
338
- ) -> Result < Bytes , ProviderError > {
374
+ ) -> Result < Bytes , Self :: Error > {
339
375
let at = utils:: serialize ( & at. into ( ) ) ;
340
376
let block = utils:: serialize ( & block. unwrap_or ( BlockNumber :: Latest ) ) ;
341
377
Ok ( self
@@ -345,11 +381,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
345
381
. map_err ( Into :: into) ?)
346
382
}
347
383
348
- async fn resolve_name ( & self , ens_name : & str ) -> Result < Address , Self :: Error > {
384
+ async fn resolve_name ( & self , _ : & str ) -> Result < Address , Self :: Error > {
349
385
unimplemented ! ( "No ENS at the inner layer of the onion" ) ;
350
386
}
351
387
352
- async fn lookup_address ( & self , address : Address ) -> Result < String , Self :: Error > {
388
+ async fn lookup_address ( & self , _ : Address ) -> Result < String , Self :: Error > {
353
389
unimplemented ! ( "No ENS at the inner layer of the onion" ) ;
354
390
}
355
391
}
0 commit comments