@@ -97,15 +97,15 @@ pub fn fetch_pending_events(
9797
9898 // process individual transactions in the block.
9999 // the iterator will start with txn index == cursor.txn.idx
100- for ( tx_idx, ( tx_hash, events ) ) in pending_block
100+ for ( tx_idx, ( tx_hash, tx_events ) ) in pending_block
101101 . transactions
102102 . iter ( )
103103 . map ( |receipt| ( receipt. receipt . transaction_hash , receipt. receipt . receipt . events ( ) ) )
104104 . enumerate ( )
105105 . skip ( cursor. txn . idx )
106106 {
107107 if tx_idx == cursor. txn . idx {
108- match events . len ( ) . cmp ( & cursor. txn . event ) {
108+ match tx_events . len ( ) . cmp ( & cursor. txn . event ) {
109109 Ordering :: Equal | Ordering :: Greater => { }
110110 Ordering :: Less => continue ,
111111 }
@@ -119,7 +119,7 @@ pub fn fetch_pending_events(
119119 None ,
120120 tx_idx,
121121 tx_hash,
122- events ,
122+ tx_events ,
123123 filter,
124124 chunk_size as usize ,
125125 buffer,
@@ -222,24 +222,24 @@ pub fn fetch_events_at_blocks(
222222 Ok ( None )
223223}
224224
225- /// An iterator that yields events that match the given filters.
225+ /// An iterator that yields events (with their original indices) that match the given filters.
226226#[ derive( Debug ) ]
227- struct FilteredEvents < ' a , I : Iterator < Item = & ' a Event > > {
227+ struct FilteredEvents < ' a , I : Iterator < Item = ( usize , & ' a Event ) > > {
228228 iter : I ,
229229 filter : & ' a Filter ,
230230}
231231
232- impl < ' a , I : Iterator < Item = & ' a Event > > FilteredEvents < ' a , I > {
232+ impl < ' a , I : Iterator < Item = ( usize , & ' a Event ) > > FilteredEvents < ' a , I > {
233233 fn new ( iter : I , filter : & ' a Filter ) -> Self {
234234 Self { iter, filter }
235235 }
236236}
237237
238- impl < ' a , I : Iterator < Item = & ' a Event > > Iterator for FilteredEvents < ' a , I > {
239- type Item = & ' a Event ;
238+ impl < ' a , I : Iterator < Item = ( usize , & ' a Event ) > > Iterator for FilteredEvents < ' a , I > {
239+ type Item = ( usize , & ' a Event ) ;
240240
241241 fn next ( & mut self ) -> Option < Self :: Item > {
242- for event in self . iter . by_ref ( ) {
242+ for ( idx , event) in self . iter . by_ref ( ) {
243243 // Skip this event if there is an address filter but doesn't match the address of the
244244 // event.
245245 if self . filter . address . is_some_and ( |addr| addr != event. from_address ) {
@@ -271,7 +271,7 @@ impl<'a, I: Iterator<Item = &'a Event>> Iterator for FilteredEvents<'a, I> {
271271 } ;
272272
273273 if is_matched {
274- return Some ( event) ;
274+ return Some ( ( idx , event) ) ;
275275 }
276276 }
277277
@@ -303,7 +303,7 @@ fn fetch_tx_events(
303303 block_hash : Option < BlockHash > ,
304304 tx_idx : usize ,
305305 tx_hash : TxHash ,
306- events : & [ Event ] ,
306+ tx_events : & [ Event ] ,
307307 filter : & Filter ,
308308 chunk_size : usize ,
309309 buffer : & mut Vec < EmittedEvent > ,
@@ -312,18 +312,24 @@ fn fetch_tx_events(
312312 // number of events we have taken.
313313 let total_can_take = chunk_size. saturating_sub ( buffer. len ( ) ) ;
314314
315+ // Enumerate events first to preserve original indices, then filter.
315316 // skip events according to the continuation token.
316- let filtered = FilteredEvents :: new ( events. iter ( ) , filter)
317- . map ( |e| EmittedEvent {
318- block_hash,
319- block_number,
320- keys : e. keys . clone ( ) ,
321- data : e. data . clone ( ) ,
322- transaction_hash : tx_hash,
323- from_address : e. from_address ,
317+ let filtered = FilteredEvents :: new ( tx_events. iter ( ) . enumerate ( ) , filter)
318+ . map ( |( event_idx, e) | {
319+ (
320+ event_idx,
321+ EmittedEvent {
322+ block_hash,
323+ block_number,
324+ keys : e. keys . clone ( ) ,
325+ data : e. data . clone ( ) ,
326+ transaction_hash : tx_hash,
327+ from_address : e. from_address ,
328+ transaction_index : tx_idx as u64 ,
329+ event_index : event_idx as u64 ,
330+ } ,
331+ )
324332 } )
325- // enumerate so that we can keep track of the event's index in the transaction
326- . enumerate ( )
327333 . skip ( next_event_idx)
328334 . take ( total_can_take)
329335 . collect :: < Vec < _ > > ( ) ;
@@ -349,7 +355,7 @@ fn fetch_tx_events(
349355 } ;
350356
351357 // if there are still more events that we haven't fetched yet for this tx.
352- if new_last_event < events . len ( ) {
358+ if new_last_event < tx_events . len ( ) {
353359 return Ok ( Some ( PartialCursor { idx : tx_idx, event : new_last_event } ) ) ;
354360 }
355361 }
0 commit comments