@@ -5,11 +5,20 @@ import {
55 bundleHistoryFromAuditEvents ,
66 getAuditEventsByBlockNumber ,
77 getAuditEventsByTransactionHash ,
8+ isAuditConfigured ,
89 meterBundleResponseFromAuditEvent ,
910 transactionMetadataFromAuditEvents ,
1011} from "@/lib/audit-events" ;
11- import type { BlockData , BlockTransaction } from "@/lib/transaction-data" ;
12- import { getBlockFromCache } from "@/lib/s3" ;
12+ import {
13+ getBlockFromCache ,
14+ getBundleHistory ,
15+ getTransactionMetadataByHash ,
16+ } from "@/lib/s3" ;
17+ import type {
18+ BlockData ,
19+ BlockTransaction ,
20+ BundleEvent ,
21+ } from "@/lib/transaction-data" ;
1322
1423const BLOCK_EVENT_TYPES = new Set ( [
1524 "BUILDER_FLASHBLOCK_STARTED" ,
@@ -87,26 +96,91 @@ async function fetchBlockFromRpcByNumber(
8796 }
8897}
8998
90- async function enrichTransactionWithBundleData ( txHash : string ) : Promise < {
99+ async function enrichTransactionFromS3 ( txHash : string ) : Promise < {
91100 bundleId : string | null ;
92101 meterBundleResponse : Record < string , unknown > | null ;
93102} > {
94- const events = await getAuditEventsByTransactionHash ( txHash ) ;
95- const metadata = transactionMetadataFromAuditEvents ( events ) ;
96- const accepted = events . find (
97- ( event ) => event . event_type === "SIMULATION_SUCCEEDED" ,
103+ const metadata = await getTransactionMetadataByHash ( txHash ) ;
104+ if ( ! metadata || metadata . bundle_ids . length === 0 ) {
105+ return { bundleId : null , meterBundleResponse : null } ;
106+ }
107+
108+ const bundleId = metadata . bundle_ids [ 0 ] ;
109+ const bundleHistory = await getBundleHistory ( bundleId ) ;
110+ if ( ! bundleHistory ) {
111+ return { bundleId, meterBundleResponse : null } ;
112+ }
113+
114+ const receivedEvent = bundleHistory . history . find (
115+ ( event ) => event . event === "Received" ,
98116 ) ;
117+ if ( ! receivedEvent ?. data ?. bundle ?. meter_bundle_response ) {
118+ return { bundleId, meterBundleResponse : null } ;
119+ }
120+
99121 return {
100- bundleId : metadata ?. bundle_ids [ 0 ] ?? null ,
101- meterBundleResponse : accepted
102- ? ( meterBundleResponseFromAuditEvent ( accepted ) as unknown as Record <
103- string ,
104- unknown
105- > )
106- : null ,
122+ bundleId,
123+ meterBundleResponse : receivedEvent . data . bundle
124+ . meter_bundle_response as unknown as Record < string , unknown > ,
107125 } ;
108126}
109127
128+ async function enrichTransactionWithBundleData ( txHash : string ) : Promise < {
129+ bundleId : string | null ;
130+ meterBundleResponse : Record < string , unknown > | null ;
131+ } > {
132+ if ( ! isAuditConfigured ( ) ) {
133+ return enrichTransactionFromS3 ( txHash ) ;
134+ }
135+
136+ try {
137+ const events = await getAuditEventsByTransactionHash ( txHash ) ;
138+ const metadata = transactionMetadataFromAuditEvents ( events ) ;
139+ const bundleId = metadata ?. bundle_ids [ 0 ] ?? null ;
140+ if ( bundleId !== null ) {
141+ const accepted = events . find (
142+ ( event ) => event . event_type === "SIMULATION_SUCCEEDED" ,
143+ ) ;
144+ return {
145+ bundleId,
146+ meterBundleResponse : accepted
147+ ? ( meterBundleResponseFromAuditEvent ( accepted ) as unknown as Record <
148+ string ,
149+ unknown
150+ > )
151+ : null ,
152+ } ;
153+ }
154+ } catch {
155+ // Audit is an optional read path; retain the S3-backed behavior on errors.
156+ }
157+
158+ return enrichTransactionFromS3 ( txHash ) ;
159+ }
160+
161+ async function getBlockEventHistory (
162+ hash : Hash ,
163+ number : bigint ,
164+ ) : Promise < BundleEvent [ ] > {
165+ if ( ! isAuditConfigured ( ) ) {
166+ return [ ] ;
167+ }
168+
169+ try {
170+ return (
171+ bundleHistoryFromAuditEvents (
172+ hash ,
173+ ( await getAuditEventsByBlockNumber ( Number ( number ) ) ) . filter ( ( event ) =>
174+ BLOCK_EVENT_TYPES . has ( event . event_type ) ,
175+ ) ,
176+ ) ?. history ?? [ ]
177+ ) ;
178+ } catch ( error ) {
179+ console . error ( "Failed to fetch block event history from audit RPC:" , error ) ;
180+ return [ ] ;
181+ }
182+ }
183+
110184async function buildAndCacheBlockData (
111185 rpcBlock : Block < bigint , true > ,
112186 hash : Hash ,
@@ -132,13 +206,7 @@ async function buildAndCacheBlockData(
132206 number,
133207 timestamp : rpcBlock . timestamp ,
134208 transactions,
135- eventHistory :
136- bundleHistoryFromAuditEvents (
137- hash ,
138- ( await getAuditEventsByBlockNumber ( Number ( number ) ) ) . filter ( ( event ) =>
139- BLOCK_EVENT_TYPES . has ( event . event_type ) ,
140- ) ,
141- ) ?. history ?? [ ] ,
209+ eventHistory : await getBlockEventHistory ( hash , number ) ,
142210 gasUsed : rpcBlock . gasUsed ,
143211 gasLimit : rpcBlock . gasLimit ,
144212 cachedAt : Date . now ( ) ,
0 commit comments