@@ -3,18 +3,10 @@ import { type Block, createPublicClient, type Hash, http } from "viem";
33import { mainnet } from "viem/chains" ;
44import {
55 getAuditEventsByTransactionHash ,
6- isAuditEventBackend ,
76 meterBundleResponseFromAuditEvent ,
87 transactionMetadataFromAuditEvents ,
98} from "@/lib/audit-events" ;
10- import {
11- type BlockData ,
12- type BlockTransaction ,
13- cacheBlockData ,
14- getBlockFromCache ,
15- getBundleHistory ,
16- getTransactionMetadataByHash ,
17- } from "@/lib/s3" ;
9+ import type { BlockData , BlockTransaction } from "@/lib/transaction-data" ;
1810
1911function serializeBlockData ( block : BlockData ) {
2012 return {
@@ -88,53 +80,26 @@ async function enrichTransactionWithBundleData(txHash: string): Promise<{
8880 bundleId : string | null ;
8981 meterBundleResponse : Record < string , unknown > | null ;
9082} > {
91- if ( isAuditEventBackend ( ) ) {
92- const events = await getAuditEventsByTransactionHash ( txHash ) ;
93- const metadata = transactionMetadataFromAuditEvents ( events ) ;
94- const accepted = events . find (
95- ( event ) => event . event_type === "SIMULATION_ACCEPTED" ,
96- ) ;
97- return {
98- bundleId : metadata ?. bundle_ids [ 0 ] ?? null ,
99- meterBundleResponse : accepted
100- ? ( meterBundleResponseFromAuditEvent ( accepted ) as unknown as Record <
101- string ,
102- unknown
103- > )
104- : null ,
105- } ;
106- }
107-
108- const metadata = await getTransactionMetadataByHash ( txHash ) ;
109- if ( ! metadata || metadata . bundle_ids . length === 0 ) {
110- return { bundleId : null , meterBundleResponse : null } ;
111- }
112-
113- const bundleId = metadata . bundle_ids [ 0 ] ;
114- const bundleHistory = await getBundleHistory ( bundleId ) ;
115- if ( ! bundleHistory ) {
116- return { bundleId, meterBundleResponse : null } ;
117- }
118-
119- const receivedEvent = bundleHistory . history . find (
120- ( e ) => e . event === "Received" ,
83+ const events = await getAuditEventsByTransactionHash ( txHash ) ;
84+ const metadata = transactionMetadataFromAuditEvents ( events ) ;
85+ const accepted = events . find (
86+ ( event ) => event . event_type === "SIMULATION_SUCCEEDED" ,
12187 ) ;
122- if ( ! receivedEvent ?. data ?. bundle ?. meter_bundle_response ) {
123- return { bundleId, meterBundleResponse : null } ;
124- }
125-
12688 return {
127- bundleId,
128- meterBundleResponse : receivedEvent . data . bundle
129- . meter_bundle_response as unknown as Record < string , unknown > ,
89+ bundleId : metadata ?. bundle_ids [ 0 ] ?? null ,
90+ meterBundleResponse : accepted
91+ ? ( meterBundleResponseFromAuditEvent ( accepted ) as unknown as Record <
92+ string ,
93+ unknown
94+ > )
95+ : null ,
13096 } ;
13197}
13298
13399async function buildAndCacheBlockData (
134100 rpcBlock : Block < bigint , true > ,
135101 hash : Hash ,
136102 number : bigint ,
137- shouldCache = true ,
138103) : Promise < BlockData > {
139104 const transactions : BlockTransaction [ ] = await Promise . all (
140105 rpcBlock . transactions . map ( async ( tx , index ) => {
@@ -161,69 +126,15 @@ async function buildAndCacheBlockData(
161126 cachedAt : Date . now ( ) ,
162127 } ;
163128
164- if ( shouldCache ) {
165- await cacheBlockData ( blockData ) ;
166- }
167-
168129 return blockData ;
169130}
170131
171- // On OP Stack, the first transaction (index 0) is the L1 attributes deposit transaction.
172- // This is not a perfect check (ideally we'd check tx.type === 'deposit' or type 0x7e),
173- // but sufficient for filtering out system transactions that don't need simulation data.
174- function isSystemTransaction ( tx : BlockTransaction ) : boolean {
175- return tx . index === 0 ;
176- }
177-
178- async function refetchMissingTransactionSimulations (
179- block : BlockData ,
180- ) : Promise < { updatedBlock : BlockData ; hasUpdates : boolean } > {
181- const transactionsToRefetch = block . transactions . filter (
182- ( tx ) => tx . bundleId === null && ! isSystemTransaction ( tx ) ,
183- ) ;
184-
185- if ( transactionsToRefetch . length === 0 ) {
186- return { updatedBlock : block , hasUpdates : false } ;
187- }
188-
189- const refetchResults = await Promise . all (
190- transactionsToRefetch . map ( async ( tx ) => {
191- const enriched = await enrichTransactionWithBundleData ( tx . hash ) ;
192- return { hash : tx . hash , ...enriched } ;
193- } ) ,
194- ) ;
195-
196- let hasUpdates = false ;
197- const updatedTransactions = block . transactions . map ( ( tx ) => {
198- const refetchResult = refetchResults . find ( ( r ) => r . hash === tx . hash ) ;
199- if ( refetchResult && refetchResult . bundleId !== null ) {
200- hasUpdates = true ;
201- return {
202- ...tx ,
203- bundleId : refetchResult . bundleId ,
204- meterBundleResponse : refetchResult . meterBundleResponse ,
205- } ;
206- }
207- return tx ;
208- } ) ;
209-
210- return {
211- updatedBlock : {
212- ...block ,
213- transactions : updatedTransactions ,
214- cachedAt : hasUpdates ? Date . now ( ) : block . cachedAt ,
215- } ,
216- hasUpdates,
217- } ;
218- }
219-
220132export async function GET (
221133 _request : NextRequest ,
222134 { params } : { params : Promise < { hash : string } > } ,
223135) {
224136 try {
225137 const { hash : identifier } = await params ;
226- const auditBackend = isAuditEventBackend ( ) ;
227138
228139 // If the identifier is a block number, resolve it to a hash first
229140 if ( isBlockNumber ( identifier ) ) {
@@ -232,42 +143,14 @@ export async function GET(
232143 return NextResponse . json ( { error : "Block not found" } , { status : 404 } ) ;
233144 }
234145
235- // Check cache by resolved hash
236- const cachedBlock = auditBackend
237- ? null
238- : await getBlockFromCache ( rpcBlock . hash ) ;
239- if ( cachedBlock ) {
240- const { updatedBlock, hasUpdates } =
241- await refetchMissingTransactionSimulations ( cachedBlock ) ;
242- if ( hasUpdates ) {
243- await cacheBlockData ( updatedBlock ) ;
244- }
245- return NextResponse . json ( serializeBlockData ( updatedBlock ) ) ;
246- }
247-
248146 const blockData = await buildAndCacheBlockData (
249147 rpcBlock ,
250148 rpcBlock . hash ,
251149 rpcBlock . number ,
252- ! auditBackend ,
253150 ) ;
254151 return NextResponse . json ( serializeBlockData ( blockData ) ) ;
255152 }
256153
257- const cachedBlock = auditBackend
258- ? null
259- : await getBlockFromCache ( identifier ) ;
260- if ( cachedBlock ) {
261- const { updatedBlock, hasUpdates } =
262- await refetchMissingTransactionSimulations ( cachedBlock ) ;
263-
264- if ( hasUpdates ) {
265- await cacheBlockData ( updatedBlock ) ;
266- }
267-
268- return NextResponse . json ( serializeBlockData ( updatedBlock ) ) ;
269- }
270-
271154 const rpcBlock = await fetchBlockFromRpc ( identifier ) ;
272155 if ( ! rpcBlock || ! rpcBlock . hash || ! rpcBlock . number ) {
273156 return NextResponse . json ( { error : "Block not found" } , { status : 404 } ) ;
@@ -277,7 +160,6 @@ export async function GET(
277160 rpcBlock ,
278161 rpcBlock . hash ,
279162 rpcBlock . number ,
280- ! auditBackend ,
281163 ) ;
282164 return NextResponse . json ( serializeBlockData ( blockData ) ) ;
283165 } catch ( error ) {
0 commit comments