@@ -19,7 +19,28 @@ type BlockfrostProposalListItem = {
1919 ratified_epoch : number | null ;
2020} ;
2121
22+ type BlockfrostProposalDetailsItem = {
23+ proposed_epoch ?: number | null ;
24+ activation_epoch ?: number | null ;
25+ expiration ?: number | null ;
26+ deposit ?: string | null ;
27+ return_address ?: string | null ;
28+ parameters ?: unknown ;
29+ ratified_epoch ?: number | null ;
30+ enacted_epoch ?: number | null ;
31+ dropped_epoch ?: number | null ;
32+ expired_epoch ?: number | null ;
33+ } ;
34+
2235const getErrorStatus = ( error : unknown ) : number | undefined => {
36+ if ( typeof error === "string" ) {
37+ try {
38+ const parsed = JSON . parse ( error ) as unknown ;
39+ return getErrorStatus ( parsed ) ;
40+ } catch {
41+ return undefined ;
42+ }
43+ }
2344 if (
2445 error &&
2546 typeof error === "object" &&
@@ -120,52 +141,71 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
120141 try {
121142 const provider = getProvider ( Number ( network ) ) ;
122143 const list = ( await provider . get (
123- `/ governance/proposals?count=${ count } &page=${ page } &order=${ order } ` ,
144+ `governance/proposals?count=${ count } &page=${ page } &order=${ order } ` ,
124145 ) ) as BlockfrostProposalListItem [ ] ;
125146
126- const active = Array . isArray ( list )
127- ? list . filter ( ( item ) => {
128- const status = getProposalStatus ( {
129- id : "" ,
130- tx_hash : item . tx_hash ,
131- cert_index : Number ( item . cert_index ) ,
132- governance_type : item . governance_type ,
133- deposit : "" ,
134- return_address : "" ,
135- governance_description : { tag : "" } ,
136- ratified_epoch : item . ratified_epoch ,
137- enacted_epoch : item . enacted_epoch ,
138- dropped_epoch : item . dropped_epoch ,
139- expired_epoch : item . expired_epoch ,
140- expiration : null ,
141- } ) ;
142- return status === "active" ;
143- } )
144- : [ ] ;
147+ const statusResolved = await Promise . all (
148+ ( Array . isArray ( list ) ? list : [ ] ) . map ( async ( item ) => {
149+ const txHash = item . tx_hash ;
150+ const certIndex = Number ( item . cert_index ) ;
151+ let detailsForStatus : BlockfrostProposalDetailsItem | null = null ;
152+
153+ try {
154+ detailsForStatus = ( await provider . get (
155+ `governance/proposals/${ txHash } /${ certIndex } ` ,
156+ ) ) as BlockfrostProposalDetailsItem ;
157+ } catch ( error ) {
158+ const status = getErrorStatus ( error ) ;
159+ if ( status && status !== 404 ) throw error ;
160+ }
161+
162+ const status = getProposalStatus ( {
163+ id : "" ,
164+ tx_hash : txHash ,
165+ cert_index : certIndex ,
166+ governance_type : item . governance_type ,
167+ deposit :
168+ typeof detailsForStatus ?. deposit === "string"
169+ ? detailsForStatus . deposit
170+ : "" ,
171+ return_address :
172+ typeof detailsForStatus ?. return_address === "string"
173+ ? detailsForStatus . return_address
174+ : "" ,
175+ governance_description : { tag : "" } ,
176+ ratified_epoch :
177+ detailsForStatus ?. ratified_epoch ?? item . ratified_epoch ?? null ,
178+ enacted_epoch :
179+ detailsForStatus ?. enacted_epoch ?? item . enacted_epoch ?? null ,
180+ dropped_epoch :
181+ detailsForStatus ?. dropped_epoch ?? item . dropped_epoch ?? null ,
182+ expired_epoch :
183+ detailsForStatus ?. expired_epoch ?? item . expired_epoch ?? null ,
184+ expiration :
185+ typeof detailsForStatus ?. expiration === "number"
186+ ? detailsForStatus . expiration
187+ : null ,
188+ } ) ;
189+
190+ return { item, detailsForStatus, status } ;
191+ } ) ,
192+ ) ;
193+
194+ const active = statusResolved . filter ( ( entry ) => entry . status === "active" ) ;
145195
146196 const proposals = await Promise . all (
147- active . map ( async ( item ) => {
197+ active . map ( async ( { item, detailsForStatus } ) => {
148198 const txHash = item . tx_hash ;
149199 const certIndex = Number ( item . cert_index ) ;
150200 let metadata : any = null ;
151- let details : any = null ;
152201
153202 try {
154- metadata = await provider . get ( `/ governance/proposals/${ txHash } /${ certIndex } /metadata` ) ;
203+ metadata = await provider . get ( `governance/proposals/${ txHash } /${ certIndex } /metadata` ) ;
155204 } catch ( error ) {
156205 const status = getErrorStatus ( error ) ;
157206 if ( status !== 404 ) throw error ;
158207 }
159208
160- if ( includeDetails ) {
161- try {
162- details = await provider . get ( `/governance/proposals/${ txHash } /${ certIndex } ` ) ;
163- } catch ( error ) {
164- const status = getErrorStatus ( error ) ;
165- if ( status && status !== 404 ) throw error ;
166- }
167- }
168-
169209 const body = metadata ?. json_metadata ?. body ?? { } ;
170210 const authors = Array . isArray ( metadata ?. json_metadata ?. authors )
171211 ? metadata . json_metadata . authors
@@ -187,16 +227,30 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
187227 details : includeDetails
188228 ? {
189229 proposedEpoch :
190- typeof details ?. proposed_epoch === "number" ? details . proposed_epoch : null ,
230+ typeof detailsForStatus ?. proposed_epoch === "number"
231+ ? detailsForStatus . proposed_epoch
232+ : null ,
191233 activationEpoch :
192- typeof details ?. activation_epoch === "number" ? details . activation_epoch : null ,
193- expiration : typeof details ?. expiration === "number" ? details . expiration : null ,
194- deposit : typeof details ?. deposit === "string" ? details . deposit : null ,
234+ typeof detailsForStatus ?. activation_epoch === "number"
235+ ? detailsForStatus . activation_epoch
236+ : null ,
237+ expiration :
238+ typeof detailsForStatus ?. expiration === "number"
239+ ? detailsForStatus . expiration
240+ : null ,
241+ deposit :
242+ typeof detailsForStatus ?. deposit === "string"
243+ ? detailsForStatus . deposit
244+ : null ,
195245 returnAddress :
196- typeof details ?. return_address === "string" ? details . return_address : null ,
246+ typeof detailsForStatus ?. return_address === "string"
247+ ? detailsForStatus . return_address
248+ : null ,
197249 parameters :
198- details && typeof details === "object" && "parameters" in details
199- ? details . parameters ?? null
250+ detailsForStatus &&
251+ typeof detailsForStatus === "object" &&
252+ "parameters" in detailsForStatus
253+ ? detailsForStatus . parameters ?? null
200254 : null ,
201255 }
202256 : undefined ,
0 commit comments