signet_cold/cold_receipt.rs
1//! Unified receipt type for cold storage queries.
2
3use alloy::{
4 consensus::{Receipt as ConsensusReceipt, TxType},
5 primitives::{Address, B256, BlockNumber},
6};
7use signet_storage_types::{IndexedReceipt, SealedHeader};
8
9use crate::RpcLog;
10
11/// A receipt with enriched RPC log metadata and block context.
12///
13/// This is the unified return type for all cold storage receipt queries.
14/// It contains the consensus receipt with RPC-enriched logs (each log
15/// carries block and transaction metadata), plus per-transaction fields
16/// needed for RPC response assembly.
17#[derive(Debug, Clone)]
18pub struct ColdReceipt {
19 /// The consensus receipt with RPC-enriched logs.
20 pub receipt: ConsensusReceipt<RpcLog>,
21 /// Transaction type (legacy, EIP-2930, EIP-1559, EIP-4844).
22 pub tx_type: TxType,
23 /// Hash of the transaction that produced this receipt.
24 pub tx_hash: B256,
25 /// Gas used by this transaction alone.
26 pub gas_used: u64,
27 /// Block number containing this receipt.
28 pub block_number: BlockNumber,
29 /// Hash of the block containing this receipt.
30 pub block_hash: B256,
31 /// Block timestamp.
32 pub block_timestamp: u64,
33 /// Index of this transaction within the block.
34 pub transaction_index: u64,
35 /// Address of the transaction sender.
36 pub from: Address,
37}
38
39impl ColdReceipt {
40 /// Build a [`ColdReceipt`] from an [`IndexedReceipt`], its containing
41 /// [`SealedHeader`], and the transaction index within the block.
42 ///
43 /// Converts each consensus log into an [`RpcLog`] with full block and
44 /// transaction metadata attached.
45 pub fn new(ir: IndexedReceipt, header: &SealedHeader, transaction_index: u64) -> Self {
46 let block_number = header.number;
47 let block_hash = header.hash();
48 let block_timestamp = header.timestamp;
49
50 let rpc_logs: Vec<RpcLog> = ir
51 .receipt
52 .inner
53 .logs
54 .iter()
55 .enumerate()
56 .map(|(log_idx, log)| RpcLog {
57 inner: log.clone(),
58 block_hash: Some(block_hash),
59 block_number: Some(block_number),
60 block_timestamp: Some(block_timestamp),
61 transaction_hash: Some(ir.tx_hash),
62 transaction_index: Some(transaction_index),
63 log_index: Some(ir.first_log_index + log_idx as u64),
64 removed: false,
65 })
66 .collect();
67
68 let receipt = ConsensusReceipt {
69 status: ir.receipt.inner.status,
70 cumulative_gas_used: ir.receipt.inner.cumulative_gas_used,
71 logs: rpc_logs,
72 };
73
74 Self {
75 receipt,
76 tx_type: ir.receipt.tx_type,
77 tx_hash: ir.tx_hash,
78 gas_used: ir.gas_used,
79 block_number,
80 block_hash,
81 block_timestamp,
82 transaction_index,
83 from: ir.sender,
84 }
85 }
86}