reth_rpc_eth_api/helpers/
receipt.rs

1//! Loads a receipt from database. Helper trait for `eth_` block and transaction RPC methods, that
2//! loads receipt data w.r.t. network.
3
4use crate::{EthApiTypes, RpcNodeCoreExt, RpcReceipt};
5use alloy_consensus::{transaction::TransactionMeta, TxReceipt};
6use futures::Future;
7use reth_primitives_traits::SignerRecoverable;
8use reth_rpc_convert::{transaction::ConvertReceiptInput, RpcConvert};
9use reth_rpc_eth_types::{
10    error::FromEthApiError, utils::calculate_gas_used_and_next_log_index, EthApiError,
11};
12use reth_storage_api::{ProviderReceipt, ProviderTx};
13
14/// Assembles transaction receipt data w.r.t to network.
15///
16/// Behaviour shared by several `eth_` RPC methods, not exclusive to `eth_` receipts RPC methods.
17pub trait LoadReceipt:
18    EthApiTypes<RpcConvert: RpcConvert<Primitives = Self::Primitives>> + RpcNodeCoreExt + Send + Sync
19{
20    /// Helper method for `eth_getBlockReceipts` and `eth_getTransactionReceipt`.
21    fn build_transaction_receipt(
22        &self,
23        tx: ProviderTx<Self::Provider>,
24        meta: TransactionMeta,
25        receipt: ProviderReceipt<Self::Provider>,
26    ) -> impl Future<Output = Result<RpcReceipt<Self::NetworkTypes>, Self::Error>> + Send {
27        async move {
28            let hash = meta.block_hash;
29            // get all receipts for the block
30            let all_receipts = self
31                .cache()
32                .get_receipts(hash)
33                .await
34                .map_err(Self::Error::from_eth_err)?
35                .ok_or(EthApiError::HeaderNotFound(hash.into()))?;
36
37            let (gas_used, next_log_index) =
38                calculate_gas_used_and_next_log_index(meta.index, &all_receipts);
39
40            Ok(self
41                .converter()
42                .convert_receipts(vec![ConvertReceiptInput {
43                    tx: tx
44                        .try_into_recovered_unchecked()
45                        .map_err(Self::Error::from_eth_err)?
46                        .as_recovered_ref(),
47                    gas_used: receipt.cumulative_gas_used() - gas_used,
48                    receipt,
49                    next_log_index,
50                    meta,
51                }])?
52                .pop()
53                .unwrap())
54        }
55    }
56}