@@ -214,6 +214,23 @@ function getBlockTransactionCount(block: any): number {
214214 ) ;
215215}
216216
217+ function getTransactionTimestampMs ( tx : any ) : number {
218+ const value = tx ?. blockTime ?? tx ?. time ?? tx ?. timestamp ;
219+ if ( typeof value === 'number' ) {
220+ if ( value > 1e15 ) return Math . floor ( value / 1_000_000 ) ;
221+ if ( value > 1e12 ) return Math . floor ( value ) ;
222+ return Math . floor ( value * 1_000 ) ;
223+ }
224+ if ( typeof value === 'string' ) {
225+ if ( / ^ \d + $ / . test ( value ) ) {
226+ return getTransactionTimestampMs ( { time : Number ( value ) } ) ;
227+ }
228+ const parsed = Date . parse ( value ) ;
229+ return Number . isNaN ( parsed ) ? 0 : parsed ;
230+ }
231+ return 0 ;
232+ }
233+
217234function attachBlockMetadataToTransactions ( transactions : any [ ] , block : any ) : any [ ] {
218235 const blockHeight = getBlockHeightValue ( block ) ;
219236 const blockHash = getBlockHashValue ( block ) ;
@@ -256,6 +273,54 @@ async function fetchTransactionsForBlock(block: any): Promise<any[]> {
256273 }
257274}
258275
276+ export async function getRecentTransactionsPreview ( limit : number = 5 , cachedBlocks ?: any [ ] ) : Promise < any [ ] > {
277+ try {
278+ const fallbackBlocksResponse = Array . isArray ( cachedBlocks ) && cachedBlocks . length > 0
279+ ? null
280+ : await Blocks ( 1 , 25 ) ;
281+ const blocks = (
282+ Array . isArray ( cachedBlocks ) && cachedBlocks . length > 0
283+ ? cachedBlocks
284+ : (
285+ fallbackBlocksResponse ?. results ||
286+ fallbackBlocksResponse ?. blocks ||
287+ fallbackBlocksResponse ?. list ||
288+ [ ]
289+ )
290+ ) . filter ( ( block : any ) => getBlockTransactionCount ( block ) > 0 ) ;
291+
292+ if ( ! Array . isArray ( blocks ) || blocks . length === 0 ) return [ ] ;
293+
294+ const recentTransactions : any [ ] = [ ] ;
295+
296+ for ( const block of blocks ) {
297+ const blockTransactions = await fetchTransactionsForBlock ( block ) ;
298+ if ( blockTransactions . length > 0 ) {
299+ recentTransactions . push ( ...blockTransactions ) ;
300+ }
301+ if ( recentTransactions . length >= limit ) break ;
302+ }
303+
304+ recentTransactions . sort ( ( a , b ) => {
305+ const heightDelta = Number ( b ?. blockHeight ?? b ?. height ?? 0 ) - Number ( a ?. blockHeight ?? a ?. height ?? 0 ) ;
306+ if ( heightDelta !== 0 ) return heightDelta ;
307+
308+ const indexDelta = Number ( b ?. index ?? b ?. txIndex ?? b ?. transactionIndex ?? - 1 ) - Number ( a ?. index ?? a ?. txIndex ?? a ?. transactionIndex ?? - 1 ) ;
309+ if ( indexDelta !== 0 ) return indexDelta ;
310+
311+ const timeDelta = getTransactionTimestampMs ( b ) - getTransactionTimestampMs ( a ) ;
312+ if ( timeDelta !== 0 ) return timeDelta ;
313+
314+ return String ( b ?. hash ?? b ?. txHash ?? '' ) . localeCompare ( String ( a ?. hash ?? a ?. txHash ?? '' ) ) ;
315+ } ) ;
316+
317+ return recentTransactions . slice ( 0 , limit ) ;
318+ } catch ( error ) {
319+ console . error ( 'Error fetching recent transactions preview:' , error ) ;
320+ return [ ] ;
321+ }
322+ }
323+
259324// Optimized function to get transactions with real pagination
260325export async function getTransactionsWithRealPagination ( page : number , perPage : number = 10 , filters ?: {
261326 type ?: string ;
0 commit comments