1- import { hexToString } from "viem" ;
1+ import { Hex , hexToString } from "viem" ;
22import { inspectUrl } from "../../utils/rollups.inspect" ;
33import { InspectResponseBody , Report } from "../../utils/rollups.types" ;
44import { Jam , JamListFilter , JamLite , JamStats } from "./types" ;
55
6+ const safeHexToString = ( hex : Hex | undefined ) : string => {
7+ if ( ! hex ) return "Unknown error" ;
8+ try {
9+ return hexToString ( hex ) ;
10+ } catch {
11+ return "Failed to decode error message" ;
12+ }
13+ } ;
14+
615const parseReport = < T > ( report : Report , defaultValue : any ) : T => {
7- if ( report ) {
16+ if ( report && report . payload ) {
817 try {
918 return JSON . parse ( hexToString ( report . payload ) ) as T ;
1019 } catch ( error : any ) {
@@ -39,10 +48,26 @@ export const fetchJams = async (filter: JamListFilter = "all") => {
3948
4049 const data = ( await response . json ( ) ) as InspectResponseBody ;
4150
42- if ( data . status === "Exception" )
43- return createError ( hexToString ( data . exception_payload ) ) ;
51+ if ( data . status === "Exception" ) {
52+ let errorMsg = "Unknown exception" ;
53+ if ( data . exception_payload ) {
54+ errorMsg = safeHexToString ( data . exception_payload ) ;
55+ } else if ( data . reports && data . reports . length > 0 && data . reports [ 0 ] ?. payload ) {
56+ errorMsg = safeHexToString ( data . reports [ 0 ] . payload ) ;
57+ }
58+ return createError ( errorMsg ) ;
59+ }
60+
61+ if ( ! data . reports || data . reports . length === 0 ) {
62+ return createError ( "No reports returned from jams inspect" ) ;
63+ }
64+
65+ const report = data . reports [ 0 ] ;
66+ if ( ! report || ! report . payload ) {
67+ return createError ( "Invalid report structure from jams inspect" ) ;
68+ }
4469
45- const result = parseReport < JamLite [ ] > ( data . reports [ 0 ] , [ ] ) ;
70+ const result = parseReport < JamLite [ ] > ( report , [ ] ) ;
4671
4772 return result ;
4873} ;
@@ -65,31 +90,91 @@ export const fetchJamById = async (id: number) => {
6590
6691 const data = ( await response . json ( ) ) as InspectResponseBody ;
6792
68- if ( data . status === "Exception" )
69- return createError ( hexToString ( data . exception_payload ) ) ;
93+ if ( data . status === "Exception" ) {
94+ let errorMsg = "Unknown exception" ;
95+ if ( data . exception_payload ) {
96+ errorMsg = safeHexToString ( data . exception_payload ) ;
97+ } else if ( data . reports && data . reports . length > 0 && data . reports [ 0 ] ?. payload ) {
98+ errorMsg = safeHexToString ( data . reports [ 0 ] . payload ) ;
99+ }
100+ return createError ( errorMsg ) ;
101+ }
102+
103+ if ( ! data . reports || data . reports . length === 0 ) {
104+ return createError ( "No reports returned from jam detail inspect" ) ;
105+ }
70106
71- const result = parseReport < Jam > ( data . reports [ 0 ] , null ) ;
107+ const report = data . reports [ 0 ] ;
108+ if ( ! report || ! report . payload ) {
109+ return createError ( "Invalid report structure from jam detail inspect" ) ;
110+ }
111+
112+ const result = parseReport < Jam > ( report , null ) ;
72113 return result ;
73114} ;
74115
75116export const fetchJamsStats = async ( ) => {
76117 const payload = 'jamstats' ;
77118 const payloadBlob = new TextEncoder ( ) . encode ( payload ) ;
78- const response = await fetch ( inspectUrl , {
79- method : 'POST' ,
80- body : payloadBlob ,
81- } ) ;
119+
120+ console . log ( "Fetching jamstats from:" , inspectUrl ) ;
121+
122+ if ( ! inspectUrl || inspectUrl . includes ( "undefined" ) ) {
123+ return createError ( "Invalid inspect URL - check environment variables" ) ;
124+ }
125+
126+ let response : Response ;
127+ try {
128+ response = await fetch ( inspectUrl , {
129+ method : 'POST' ,
130+ body : payloadBlob ,
131+ } ) ;
132+ } catch ( error : any ) {
133+ console . error ( "Fetch error for jamstats:" , error ) ;
134+ return createError ( `Network error: ${ error . message || "Failed to connect" } ` ) ;
135+ }
82136
83137 if ( ! response . ok ) {
84138 return createError ( `Network response error - ${ response . status } ` ) ;
85139 }
86140
87- const data = ( await response . json ( ) ) as InspectResponseBody ;
141+ let data : InspectResponseBody ;
142+ try {
143+ data = ( await response . json ( ) ) as InspectResponseBody ;
144+ } catch ( error : any ) {
145+ console . error ( "JSON parse error for jamstats:" , error ) ;
146+ return createError ( `Failed to parse response: ${ error . message } ` ) ;
147+ }
148+
149+ console . log ( "jamstats response status:" , data . status , "reports count:" , data . reports ?. length ) ;
88150
89- if ( data . status === "Exception" )
90- return createError ( hexToString ( data . exception_payload ) ) ;
151+ // Try to parse report data first, even if status is Exception
152+ // (backend may return Exception status but still have valid data in reports)
153+ if ( data . reports && data . reports . length > 0 && data . reports [ 0 ] ?. payload ) {
154+ const report = data . reports [ 0 ] ;
155+ try {
156+ const result = parseReport < JamStats [ ] > ( report , [ ] ) ;
157+ // If we got a valid array, return it regardless of status
158+ if ( Array . isArray ( result ) ) {
159+ console . log ( "jamstats parsed successfully, count:" , result . length ) ;
160+ return result ;
161+ }
162+ } catch ( e ) {
163+ console . warn ( "Failed to parse jamstats report as data, treating as error" ) ;
164+ }
165+ }
91166
92- const result = parseReport < JamStats [ ] > ( data . reports [ 0 ] , [ ] ) ;
167+ // If we get here with Exception status, it's a real error
168+ if ( data . status === "Exception" ) {
169+ let errorMsg = "Unknown exception" ;
170+ if ( data . exception_payload ) {
171+ errorMsg = safeHexToString ( data . exception_payload ) ;
172+ } else if ( data . reports && data . reports . length > 0 && data . reports [ 0 ] ?. payload ) {
173+ errorMsg = safeHexToString ( data . reports [ 0 ] . payload ) ;
174+ }
175+ console . error ( "jamstats exception:" , errorMsg ) ;
176+ return createError ( errorMsg ) ;
177+ }
93178
94- return result ;
179+ return createError ( "No valid data returned from jamstats inspect" ) ;
95180} ;
0 commit comments