@@ -281,14 +281,26 @@ function decodeLogs(logs: Log[]) {
281281}
282282
283283
284- function encodeReceipt ( receipt : Receipt ) : Buffer {
284+ export interface ReceiptEncodingOptions {
285+ /**
286+ * Use gasUsed instead of cumulativeGasUsed when encoding receipts.
287+ * This is needed for chains like Stable (chain ID 988) that deviate from
288+ * the Ethereum standard by using per-transaction gas instead of cumulative.
289+ */
290+ useGasUsed ?: boolean
291+ }
292+
293+
294+ function encodeReceipt ( receipt : Receipt , options ?: ReceiptEncodingOptions ) : Buffer {
285295 let type = receipt . type == '0x0' ? Buffer . alloc ( 0 ) : RLP . encode ( qty2Int ( receipt . type ) )
286296 let payload : Uint8Array
297+ // Use gasUsed instead of cumulativeGasUsed for certain chains (e.g., Stable)
298+ let gasField = options ?. useGasUsed ? receipt . gasUsed : receipt . cumulativeGasUsed
287299 if ( receipt . type == '0x7e' ) {
288300 // https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/deposits.md#deposit-receipt
289301 payload = RLP . encode ( [
290302 qty2Int ( receipt . status ) ,
291- BigInt ( receipt . cumulativeGasUsed ) ,
303+ BigInt ( gasField ) ,
292304 decodeHex ( receipt . logsBloom ) ,
293305 decodeLogs ( receipt . logs ) ,
294306 BigInt ( assertNotNull ( receipt . depositNonce , 'receipt.depositNonce is missing' ) ) ,
@@ -297,7 +309,7 @@ function encodeReceipt(receipt: Receipt): Buffer {
297309 } else {
298310 payload = RLP . encode ( [
299311 qty2Int ( receipt . status ) ,
300- BigInt ( receipt . cumulativeGasUsed ) ,
312+ BigInt ( gasField ) ,
301313 decodeHex ( receipt . logsBloom ) ,
302314 decodeLogs ( receipt . logs ) ,
303315 ] )
@@ -306,15 +318,15 @@ function encodeReceipt(receipt: Receipt): Buffer {
306318}
307319
308320
309- export async function receiptsRoot ( receipts : Receipt [ ] ) {
321+ export async function receiptsRoot ( receipts : Receipt [ ] , options ?: ReceiptEncodingOptions ) {
310322 let trie = await createMPT ( )
311323
312324 for ( let idx = 0 ; idx < receipts . length ; idx ++ ) {
313325 let receipt = receipts [ idx ]
314326 let key = RLP . encode ( idx )
315327 let value : Buffer
316328 try {
317- value = encodeReceipt ( receipt )
329+ value = encodeReceipt ( receipt , options )
318330 } catch ( err : any ) {
319331 throw addErrorContext ( err , {
320332 transactionIndex : qty2Int ( receipt . transactionIndex ) ,
0 commit comments