@@ -395,15 +395,7 @@ export async function getBlockReceipts(
395395 logsByHash . set ( log . transactionHash , existingLogs ) ;
396396 }
397397
398- // Ensure contract results are processed in transaction_index (block) order
399- const sortedContractResults = [ ...contractResults ] . sort ( ( a , b ) => {
400- const aIdx = a . transaction_index ?? Number . MAX_SAFE_INTEGER ;
401- const bIdx = b . transaction_index ?? Number . MAX_SAFE_INTEGER ;
402- return aIdx - bIdx ;
403- } ) ;
404-
405- let blockGasUsedBeforeTransaction = 0 ;
406- const receiptPromises = sortedContractResults . map ( async ( contractResult ) => {
398+ const receiptPromises = contractResults . map ( async ( contractResult ) => {
407399 if ( Utils . isRejectedDueToHederaSpecificValidation ( contractResult ) ) {
408400 logger . debug (
409401 `Transaction with hash %s is skipped due to hedera-specific validation failure (%s)` ,
@@ -425,11 +417,9 @@ export async function getBlockReceipts(
425417 logs : contractResult . logs ,
426418 receiptResponse : contractResult ,
427419 to,
428- blockGasUsedBeforeTransaction,
429420 } ;
430421
431422 const receipt = TransactionReceiptFactory . createRegularReceipt ( transactionReceiptParams ) as ITransactionReceipt ;
432- blockGasUsedBeforeTransaction += contractResult . gas_used ;
433423 return receipt ;
434424 } ) ;
435425
@@ -449,7 +439,17 @@ export async function getBlockReceipts(
449439 }
450440 }
451441
452- return receipts ;
442+ // after all the receipts are created, we need to sort them by transaction index and calculate the cumulative gas used
443+ const sortedReceipts = receipts . sort (
444+ ( a , b ) => parseInt ( a . transactionIndex ?? '0' , 16 ) - parseInt ( b . transactionIndex ?? '0' , 16 ) ,
445+ ) ;
446+ let blockGasUsedBeforeTransaction = 0 ;
447+ for ( const receipt of sortedReceipts ) {
448+ blockGasUsedBeforeTransaction += parseInt ( receipt . gasUsed , 16 ) ;
449+ receipt . cumulativeGasUsed = numberTo0x ( blockGasUsedBeforeTransaction ) ;
450+ }
451+
452+ return sortedReceipts as ITransactionReceipt [ ] ;
453453 } catch ( e : unknown ) {
454454 throw WorkersPool . wrapError ( e ) ;
455455 }
0 commit comments